Skip to content

Commit

Permalink
feat(core): update create-nx-plugin to generate cli library (+1 squas…
Browse files Browse the repository at this point in the history
…hed commit)

Squashed commits:
[d7782395d] feat(nx-plugin): add create-package plugin
  • Loading branch information
xiongemi committed Apr 3, 2023
1 parent db0fd2f commit f996a7a
Show file tree
Hide file tree
Showing 39 changed files with 841 additions and 35 deletions.
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -5473,6 +5473,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 @@ -1920,6 +1920,15 @@
"path": "/packages/nx-plugin/generators/plugin",
"type": "generator"
},
"/packages/nx-plugin/generators/create-package": {
"description": "Create a framework package that uses Nx CLI",
"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 @@ -1895,6 +1895,15 @@
"path": "nx-plugin/generators/plugin",
"type": "generator"
},
{
"description": "Create a framework package that uses Nx CLI",
"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
84 changes: 84 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,84 @@
{
"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, like `create-framework-app`. 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"
},
"skipTsConfig": {
"type": "boolean",
"default": false,
"description": "Do not update tsconfig.json for development experience.",
"x-priority": "internal"
},
"setParserOptionsProject": {
"type": "boolean",
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"compiler": {
"type": "string",
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"importPath": {
"type": "string",
"description": "How the plugin will be published, like `create-framework-app`. Note this must be a valid NPM name. Will use name if not provided."
}
},
"required": ["name", "project"],
"presets": []
},
"description": "Create a framework package that uses Nx CLI",
"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"
}
5 changes: 5 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,11 @@
"type": "string",
"description": "Plugin name",
"aliases": ["name"]
},
"cliName": {
"type": "string",
"description": "Name of cli command to create workspace with plugin",
"aliases": ["name"]
}
},
"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 @@ -86,6 +86,7 @@
"default": false
}
},
"required": ["preset", "name"],
"presets": []
},
"description": "Create application in an empty workspace.",
Expand Down
30 changes: 27 additions & 3 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ProjectConfiguration } from '@nrwl/devkit';
import {
checkFilesExist,
expectTestsPass,
isNotWindows,
killPorts,
newProject,
readJson,
Expand All @@ -12,8 +11,6 @@ import {
uniq,
updateFile,
createFile,
readFile,
removeFile,
cleanupProject,
runCommand,
getPackageManagerCommand,
Expand Down Expand Up @@ -411,4 +408,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();
});
});
2 changes: 2 additions & 0 deletions e2e/workspace-create/src/create-nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ describe('create-nx-plugin', () => {
);

expect(() => runCLI(`e2e e2e`)).not.toThrow();
expect(() => runCLI(`build create-${pluginName}-package`)).not.toThrow();
expect(() => runCLI(`e2e ${pluginName}`)).not.toThrow();
});
});
16 changes: 11 additions & 5 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function determinePluginName(parsedArgs: CreateNxPluginArguments) {

interface CreateNxPluginArguments {
pluginName: string;
cliName?: string;
packageManager: PackageManager;
ci: CI;
allPrompts: boolean;
Expand All @@ -83,11 +84,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('cliName', {
describe: 'Name of the CLI package to create workspace with plugin',
type: 'string',
}),
withNxCloud,
withCI,
withAllPrompts,
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 @@ -188,6 +188,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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { PackageManager } from './utils/package-manager';
import { CI } from './utils/ci/ci-list';

export interface CreateWorkspaceOptions {
name: string; // Workspace name (e.g. org name)
packageManager: PackageManager; // Package manager to use
preset?: string; // Preset to use
nxCloud: boolean; // Enable Nx Cloud
/**
* @description Enable interactive mode with presets
Expand Down
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/create-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
nxCloud && nxCloudInstallRes?.code === 0
);
}
if (!skipGit) {
if (!skipGit && commit) {
try {
await initializeGitRepo(directory, { defaultBase, commit });
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import { isKnownPreset } from './preset';
/**
* This function is used to check if a preset is a third party preset.
* @param preset
* @returns null if the preset is a known Nx preset or preset does not exist, the normalized preset otherwise.
* @returns null if the preset is a known Nx preset or preset does not exist, the package name of preset otherwise.
*/
export async function getThirdPartyPreset(
preset?: string
): Promise<string | null> {
if (preset && !isKnownPreset(preset)) {
// extract the package name from the preset
const packageName = preset.match(/.+@/)
? preset[0] + preset.substring(1).split('@')[0]
: preset;
const validateResult = validateNpmPackage(packageName);
if (validateResult.validForNewPackages) {
return Promise.resolve(preset);
return Promise.resolve(packageName);
} else {
//! Error here
output.error({
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"schema": "./src/generators/plugin/schema.json",
"description": "Create a Nx Plugin."
},
"create-package": {
"factory": "./src/generators/create-package/create-package",
"schema": "./src/generators/create-package/schema.json",
"description": "Create a framework package that uses Nx CLI"
},
"e2e-project": {
"factory": "./src/generators/e2e-project/e2e",
"schema": "./src/generators/e2e-project/schema.json",
Expand Down Expand Up @@ -48,6 +53,11 @@
"schema": "./src/generators/plugin/schema.json",
"description": "Create a Nx Plugin."
},
"create-package": {
"factory": "./src/generators/create-package/create-package#createPackageSchematic",
"schema": "./src/generators/create-package/schema.json",
"description": "Create a framework package that uses Nx CLI"
},
"e2e-project": {
"factory": "./src/generators/e2e-project/e2e#e2eProjectSchematic",
"schema": "./src/generators/e2e-project/schema.json",
Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/generators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './src/generators/create-package/create-package';
export * from './src/generators/e2e-project/e2e';
export * from './src/generators/executor/executor';
export * from './src/generators/generator/generator';
Expand Down
Empty file removed packages/nx-plugin/index.ts
Empty file.
Loading

0 comments on commit f996a7a

Please sign in to comment.