Skip to content

Commit

Permalink
feat(core): update create-nx-plugin to generate cli library (nrwl#15994)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Apr 20, 2023
1 parent 16e4061 commit 8400484
Show file tree
Hide file tree
Showing 50 changed files with 947 additions and 104 deletions.
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -5579,6 +5579,14 @@
"isExternal": false,
"disableCollapsible": false
},
{
"id": "create-package",
"path": "/packages/nx-plugin/generators/create-package",
"name": "create-package",
"children": [],
"isExternal": false,
"disableCollapsible": false
},
{
"id": "e2e-project",
"path": "/packages/nx-plugin/generators/e2e-project",
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/manifests/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,15 @@
"path": "/packages/nx-plugin/generators/plugin",
"type": "generator"
},
"/packages/nx-plugin/generators/create-package": {
"description": "Create a package which can be used by npx to create a new workspace",
"file": "generated/packages/nx-plugin/generators/create-package.json",
"hidden": false,
"name": "create-package",
"originalFilePath": "/packages/nx-plugin/src/generators/create-package/schema.json",
"path": "/packages/nx-plugin/generators/create-package",
"type": "generator"
},
"/packages/nx-plugin/generators/e2e-project": {
"description": "Create a E2E application for a Nx Plugin.",
"file": "generated/packages/nx-plugin/generators/e2e-project.json",
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,15 @@
"path": "nx-plugin/generators/plugin",
"type": "generator"
},
{
"description": "Create a package which can be used by npx to create a new workspace",
"file": "generated/packages/nx-plugin/generators/create-package.json",
"hidden": false,
"name": "create-package",
"originalFilePath": "/packages/nx-plugin/src/generators/create-package/schema.json",
"path": "nx-plugin/generators/create-package",
"type": "generator"
},
{
"description": "Create a E2E application for a Nx Plugin.",
"file": "generated/packages/nx-plugin/generators/e2e-project.json",
Expand Down
3 changes: 2 additions & 1 deletion docs/generated/packages/nx-plugin/executors/e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
"runInBand": {
"alias": "i",
"description": "Run all tests serially in the current process (rather than creating a worker pool of child processes that run tests). This is sometimes useful for debugging, but such use cases are pretty rare. Useful for CI. (https://jestjs.io/docs/cli#--runinband)",
"type": "boolean"
"type": "boolean",
"default": true
},
"showConfig": {
"description": "Print your Jest config and then exits. (https://jestjs.io/docs/en/cli#--showconfig)",
Expand Down
75 changes: 75 additions & 0 deletions docs/generated/packages/nx-plugin/generators/create-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "create-package",
"factory": "./src/generators/create-package/create-package",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginCreatePackage",
"title": "Create a framework package",
"description": "Create a framework package that uses Nx CLI.",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The package name of cli, e.g. `create-framework-package`. Note this must be a valid NPM name to be published.",
"$default": { "$source": "argv", "index": 0 },
"x-priority": "important"
},
"project": {
"type": "string",
"description": "The name of the generator project.",
"alias": "p",
"$default": { "$source": "projectName" },
"x-prompt": "What is the name of the project for the generator?",
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
"directory": {
"type": "string",
"description": "A directory where the app is placed."
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
},
"tags": {
"type": "string",
"description": "Add tags to the library (used for linting).",
"alias": "t"
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"compiler": {
"type": "string",
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"e2eTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end to end (E2E) tests.",
"default": "jest"
}
},
"required": ["name", "project"],
"presets": []
},
"description": "Create a package which can be used by npx to create a new workspace",
"implementation": "/packages/nx-plugin/src/generators/create-package/create-package.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/create-package/schema.json",
"type": "generator"
}
6 changes: 6 additions & 0 deletions docs/generated/packages/nx-plugin/generators/executor.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
"type": "boolean",
"default": false,
"description": "Do not add an eslint configuration for plugin json files."
},
"skipFormat": {
"type": "boolean",
"description": "Skip formatting files.",
"default": false,
"x-priority": "internal"
}
},
"required": ["project", "name"],
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/packages/nx-plugin/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"type": "string",
"description": "Plugin name",
"aliases": ["name"]
},
"createPackageName": {
"type": "string",
"description": "Name of package which creates a workspace"
}
},
"required": ["pluginName"],
Expand Down
1 change: 1 addition & 0 deletions docs/generated/packages/workspace/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"enum": ["cypress", "jest", "detox", "none"]
}
},
"required": ["preset", "name"],
"presets": []
},
"description": "Create application in an empty workspace.",
Expand Down
28 changes: 27 additions & 1 deletion e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
createFile,
expectTestsPass,
getPackageManagerCommand,
killPorts,
newProject,
readJson,
readProjectConfig,
Expand Down Expand Up @@ -410,4 +409,31 @@ describe('Nx Plugin', () => {
expect(pluginProject.tags).toEqual(['e2etag', 'e2ePackage']);
}, 90000);
});

it('should be able to generate a create-package plugin ', async () => {
const plugin = uniq('plugin');
const createAppName = `create-${plugin}-app`;
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin}`);
runCLI(
`generate @nrwl/nx-plugin:create-package ${createAppName} --project=${plugin}`
);

const buildResults = runCLI(`build ${createAppName}`);
expect(buildResults).toContain('Done compiling TypeScript files');

checkFilesExist(
`libs/${plugin}/src/generators/preset`,
`libs/${createAppName}`,
`dist/libs/${createAppName}/bin/index.js`
);
});

it('should throw an error when run create-package for an invalid plugin ', async () => {
const plugin = uniq('plugin');
expect(() =>
runCLI(
`generate @nrwl/nx-plugin:create-package ${plugin} --project=invalid-plugin`
)
).toThrow();
});
});
21 changes: 20 additions & 1 deletion e2e/workspace-create/src/create-nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('create-nx-plugin', () => {

runCreatePlugin(pluginName, {
packageManager,
extraArgs: `--createPackageName='false'`,
});

checkFilesExist(
Expand All @@ -31,7 +32,7 @@ describe('create-nx-plugin', () => {

runCLI(`build ${pluginName}`);

checkFilesExist(`dist/package.json`);
checkFilesExist(`dist/package.json`, `dist/src/index.js`);

runCLI(
`generate @nrwl/nx-plugin:generator ${generatorName} --project=${pluginName}`
Expand All @@ -48,4 +49,22 @@ describe('create-nx-plugin', () => {
`dist/executors.json`
);
});

it('should be able to create a repo with create workspace cli', () => {
const pluginName = uniq('plugin');

runCreatePlugin(pluginName, {
packageManager,
extraArgs: `--createPackageName=create-${pluginName}-package`,
});

runCLI(`build ${pluginName}`);
checkFilesExist(`dist/package.json`, `dist/generators.json`);

runCLI(`build create-${pluginName}-package`);
checkFilesExist(`dist/create-${pluginName}-package/bin/index.js`);

expect(() => runCLI(`e2e e2e`)).not.toThrow();
expect(() => runCLI(`e2e create-${pluginName}-package-e2e`)).not.toThrow();
});
});
75 changes: 46 additions & 29 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,44 @@ export const yargsDecorator = {

const nxVersion = require('../package.json').version;

function determinePluginName(parsedArgs: CreateNxPluginArguments) {
async function determinePluginName(
parsedArgs: CreateNxPluginArguments
): Promise<string> {
if (parsedArgs.pluginName) {
return Promise.resolve(parsedArgs.pluginName);
return parsedArgs.pluginName;
}

return enquirer
.prompt([
{
name: 'pluginName',
message: `Plugin name `,
type: 'input',
validate: (s) => (s.length ? true : 'Name cannot be empty'),
},
])
.then((a: { pluginName: string }) => {
if (!a.pluginName) {
output.error({
title: 'Invalid name',
bodyLines: [`Name cannot be empty`],
});
process.exit(1);
}
return a.pluginName;
});
const results = await enquirer.prompt<{ pluginName: string }>([
{
name: 'pluginName',
message: `Plugin name `,
type: 'input',
validate: (s_1) => (s_1.length ? true : 'Plugin name cannot be empty'),
},
]);
return results.pluginName;
}

async function determineCreatePackageName(
parsedArgs: CreateNxPluginArguments
): Promise<string> {
if (parsedArgs.createPackageName) {
return parsedArgs.createPackageName;
}

const results = await enquirer.prompt<{ createPackageName: string }>([
{
name: 'createPackageName',
message: `Create a package which can be used by npx to create a new workspace (Leave blank to not create this package)`,
type: 'input',
},
]);
return results.createPackageName;
}

interface CreateNxPluginArguments {
pluginName: string;
createPackageName?: string;
packageManager: PackageManager;
ci: CI;
allPrompts: boolean;
Expand All @@ -89,11 +99,16 @@ export const commandsObject: yargs.Argv<CreateNxPluginArguments> = yargs
'Create a new Nx plugin workspace',
(yargs) =>
withOptions(
yargs.positional('pluginName', {
describe: chalk.dim`Plugin name`,
type: 'string',
alias: ['name'],
}),
yargs
.positional('pluginName', {
describe: chalk.dim`Plugin name`,
type: 'string',
alias: ['name'],
})
.option('createPackageName', {
describe: 'Name of the CLI package to create workspace with plugin',
type: 'string',
}),
withNxCloud,
withCI,
withAllPrompts,
Expand Down Expand Up @@ -164,19 +179,21 @@ async function normalizeArgsMiddleware(
argv: yargs.Arguments<CreateNxPluginArguments>
): Promise<void> {
try {
const name = await determinePluginName(argv);
const pluginName = await determinePluginName(argv);
const createPackageName = await determineCreatePackageName(argv);
const packageManager = await determinePackageManager(argv);
const defaultBase = await determineDefaultBase(argv);
const nxCloud = await determineNxCloud(argv);
const ci = await determineCI(argv, nxCloud);

Object.assign(argv, {
name,
pluginName,
createPackageName,
nxCloud,
packageManager,
defaultBase,
ci,
});
} as Partial<CreateNxPluginArguments & CreateWorkspaceOptions>);
} catch (e) {
console.error(e);
process.exit(1);
Expand Down
1 change: 1 addition & 0 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ async function normalizeArgsMiddleware(
} else if (monorepoStyle === 'node-standalone') {
preset = Preset.NodeStandalone;
} else {
// when choose integrated monorepo, further prompt for preset
preset = await determinePreset(argv);
}
} else if (argv.preset === 'react') {
Expand Down
2 changes: 0 additions & 2 deletions packages/create-nx-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
"bugs": {
"url": "https://github.com/nrwl/nx/issues"
},
"main": "./index.js",
"typings": "./index.d.ts",
"homepage": "https://nx.dev",
"dependencies": {
"chalk": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build-base": {
"executor": "@nrwl/js:tsc",
"options": {
"main": "packages/create-nx-workspace/bin/create-nx-workspace.ts",
"main": "packages/create-nx-workspace/index.ts",
"assets": [
{
"input": "packages/create-nx-workspace",
Expand Down
4 changes: 3 additions & 1 deletion packages/create-nx-workspace/src/create-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export async function createPreset<T extends CreateWorkspaceOptions>(
}
}

if (process.env.NX_VERBOSE_LOGGING !== 'true') {
if (
!(process.env.NX_VERBOSE_LOGGING === 'true' || args.includes('--verbose'))
) {
args = '--quiet ' + args;
}
const command = `g ${preset}:preset ${args}`;
Expand Down
Loading

0 comments on commit 8400484

Please sign in to comment.