Skip to content

Commit

Permalink
Merge pull request #1130 from nxext/cap-plugin
Browse files Browse the repository at this point in the history
Feature: Add Capacitor Crystal Plugin
  • Loading branch information
edbzn authored Dec 9, 2024
2 parents 32a4fc9 + a22caa8 commit b245d3c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 121 deletions.
26 changes: 25 additions & 1 deletion e2e/capacitor-e2e/tests/capacitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('capacitor-project e2e', () => {
`generate @nx/web:application ${appDir} --style=css --bundler=vite --e2eTestRunner=none --linter=none --skipFormat=true`
);
await runNxCommandAsync(
`generate @nxext/capacitor:configuration --project=${app} --appName=test --appId=test --skipFormat=true`
`generate @nxext/capacitor:configuration --project=${app} --appName=test --appId=test.example.app --skipFormat=true`
);
});

Expand Down Expand Up @@ -55,4 +55,28 @@ describe('capacitor-project e2e', () => {
);
expect(capHelpResults.stdout).toContain('Usage: cap');
});

it('should add android platform', async () => {
const capResults = await runNxCommandAsync(`run ${app}:add:android`);
expect(stripAnsi(capResults.stdout)).toContain(
'[success] android platform added!'
);
});

it('should sync android platform', async () => {
const capResults = await runNxCommandAsync(`run ${app}:sync:android`);
expect(stripAnsi(capResults.stdout)).toContain('✔ update android');
});

it('should add ios platform', async () => {
const capResults = await runNxCommandAsync(`run ${app}:add:ios`);
expect(stripAnsi(capResults.stdout)).toContain(
'[success] ios platform added!'
);
});

it('should sync ios platform', async () => {
const capResults = await runNxCommandAsync(`run ${app}:sync:ios`);
expect(stripAnsi(capResults.stdout)).toContain('✔ update ios');
});
});
2 changes: 1 addition & 1 deletion e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function createTestProject() {
});

execSync(
`npx --yes create-nx-workspace@19 ${projectName} --preset apps --nxCloud skip --interactive false --packageManager pnpm`,
`npx --yes create-nx-workspace@latest ${projectName} --preset apps --nxCloud skip --interactive false --packageManager pnpm`,
{
cwd: dirname(projectDirectory),
stdio: 'inherit',
Expand Down
9 changes: 7 additions & 2 deletions packages/capacitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"directory": "packages/capacitor"
},
"license": "MIT",
"main": "src/index.js",
"exports": {
".": "./src/index.js",
"./package.json": "./package.json",
"./plugin": "./src/plugins/plugin.js"
},
"generators": "./generators.json",
"executors": "./executors.json",
"nx-migrations": {
Expand All @@ -19,7 +23,8 @@
"builders": "./executors.json",
"dependencies": {
"ignore": "^5.3.1",
"tslib": "^2.3.0"
"tslib": "^2.3.0",
"@nxext/common": "20.0.1"
},
"peerDependencies": {
"@nx/devkit": "^20.0.0",
Expand Down
81 changes: 4 additions & 77 deletions packages/capacitor/src/generators/configuration/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addProjectConfiguration,
normalizePath,
readJson,
readNxJson,
readProjectConfiguration,
Tree,
writeJson,
Expand Down Expand Up @@ -90,84 +91,10 @@ describe('capacitor-project', () => {
);
});

it('should update workspace.json', async () => {
it('should update nx.json', async () => {
await generator(appTree, options);
const projectConfiguration = readProjectConfiguration(
appTree,
options.project
);

expect(projectConfiguration.targets.cap.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.cap.options).toEqual({
cmd: '--help',
});

expect(projectConfiguration.targets.add.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.add.options).toEqual({
cmd: 'add',
});
expect(
projectConfiguration.targets.add.configurations['ios'].cmd
).toEqual('add ios');
expect(
projectConfiguration.targets.add.configurations['android'].cmd
).toEqual('add android');

expect(projectConfiguration.targets.copy.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.copy.options).toEqual({
cmd: 'copy',
});
expect(
projectConfiguration.targets.copy.configurations['ios'].cmd
).toEqual('copy ios');
expect(
projectConfiguration.targets.copy.configurations['android'].cmd
).toEqual('copy android');

expect(projectConfiguration.targets.open.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.open.options).toEqual({
cmd: 'open',
});
expect(
projectConfiguration.targets.open.configurations['ios'].cmd
).toEqual('open ios');
expect(
projectConfiguration.targets.open.configurations['android'].cmd
).toEqual('open android');

expect(projectConfiguration.targets.sync.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.sync.options).toEqual({
cmd: 'sync',
});
expect(
projectConfiguration.targets.sync.configurations['ios'].cmd
).toEqual('sync ios');
expect(
projectConfiguration.targets.sync.configurations['android'].cmd
).toEqual('sync android');

expect(projectConfiguration.targets.update.executor).toEqual(
'@nxext/capacitor:cap'
);
expect(projectConfiguration.targets.update.options).toEqual({
cmd: 'update',
});
expect(
projectConfiguration.targets.update.configurations['ios'].cmd
).toEqual('update ios');
expect(
projectConfiguration.targets.update.configurations['android'].cmd
).toEqual('update android');
const nxJson = readNxJson(appTree);
expect(nxJson.plugins).toContain('@nxext/capacitor/plugin');
});

it('should not remove existing target configurations', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/capacitor/src/generators/configuration/generator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { convertNxGenerator, formatFiles, Tree } from '@nx/devkit';
import { addCapacitorConfig } from './lib/add-capacitor-config';
import { addDependencies } from './lib/add-dependencies';
import { addProject } from './lib/add-project';
import { normalizeOptions } from './lib/normalize-options';
import { updateProjectGitignore } from './lib/update-project-gitignore';
import { updateProjectPackageJson } from './lib/update-project-package-json';
import { CapacitorConfigurationSchema } from './schema';
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { addPluginToNxJson } from '@nxext/common';

export async function capacitorConfigurationGenerator(
host: Tree,
Expand All @@ -18,8 +18,8 @@ export async function capacitorConfigurationGenerator(
const installTask = addDependencies(host);
addCapacitorConfig(host, normalizedOptions);
updateProjectGitignore(host, normalizedOptions);
addProject(host, normalizedOptions);
updateProjectPackageJson(host, normalizedOptions);
addPluginToNxJson('@nxext/capacitor/plugin', host);

if (!options.skipFormat) {
await formatFiles(host);
Expand Down
38 changes: 0 additions & 38 deletions packages/capacitor/src/generators/configuration/lib/add-project.ts

This file was deleted.

99 changes: 99 additions & 0 deletions packages/capacitor/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
CreateNodesV2,
ProjectConfiguration,
TargetConfiguration,
} from '@nx/devkit';
import { existsSync } from 'node:fs';
import { dirname, join } from 'node:path';

export interface CapacitorPluginOptions {}

export type CapCommand = 'add' | 'copy' | 'open' | 'run' | 'sync' | 'update';
export type CapPlatform = 'ios' | 'android';
export type CapCommandFormat =
| `cap ${CapCommand}`
| `cap ${CapCommand} ${CapPlatform}`;

export const createNodesV2: CreateNodesV2<CapacitorPluginOptions> = [
'**/capacitor.config.ts',
(configFiles) => {
return configFiles.map((configFile) => {
const projectRoot = dirname(configFile);

const isProject =
existsSync(join(projectRoot, 'project.json')) ||
existsSync(join(projectRoot, 'package.json'));
if (!isProject) {
return null;
}

return [
projectRoot,
{
projects: {
[projectRoot]: {
projectType: 'application',
targets: buildTargets(projectRoot),
},
},
},
];
});
},
];

function buildTargets(projectRoot: string): ProjectConfiguration['targets'] {
const commands: CapCommand[] = [
'add',
'copy',
'open',
'run',
'sync',
'update',
];
const platforms: CapPlatform[] = ['ios', 'android'];

const baseTargetOptions: TargetConfiguration<any> = {
options: {
cwd: projectRoot,
},
cache: false,
metadata: {
technologies: ['capacitor'],
},
};

const targets: ProjectConfiguration['targets'] = {
cap: {
command: 'cap --help',
...baseTargetOptions,
},
};

const formatCapCommand = (
command: CapCommand,
platform?: CapPlatform
): CapCommandFormat => {
if (platform) {
return `cap ${command} ${platform}`;
}

return `cap ${command}`;
};

for (const command of commands) {
targets[command] = {
command: formatCapCommand(command),
...baseTargetOptions,
};

for (const platform of platforms) {
targets[command].configurations ??= {};
targets[command].configurations[platform] = {
command: formatCapCommand(command, platform),
};
}
}

return targets;
}

0 comments on commit b245d3c

Please sign in to comment.