Skip to content

Commit

Permalink
fix(nx-plugin): require importPath
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr committed Jul 3, 2020
1 parent bc1b441 commit 428ac8c
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 24 deletions.
8 changes: 7 additions & 1 deletion docs/angular/api-nx-plugin/schematics/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ nx g plugin ... --dry-run
Generate libs/plugins/my-plugin:

```bash
nx g plugin my-plugin --directory=plugins
nx g plugin my-plugin --directory=plugins --importPath=@myorg/my-plugin
```

## Options
Expand All @@ -40,6 +40,12 @@ Type: `string`

A directory where the plugin is placed

### importPath

Type: `string`

How the plugin will be published, like @myorg/my-awesome-plugin. Note this must be a valid npm name

### linter

Default: `tslint`
Expand Down
8 changes: 7 additions & 1 deletion docs/react/api-nx-plugin/schematics/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ nx g plugin ... --dry-run
Generate libs/plugins/my-plugin:

```bash
nx g plugin my-plugin --directory=plugins
nx g plugin my-plugin --directory=plugins --importPath=@myorg/my-plugin
```

## Options
Expand All @@ -40,6 +40,12 @@ Type: `string`

A directory where the plugin is placed

### importPath

Type: `string`

How the plugin will be published, like @myorg/my-awesome-plugin. Note this must be a valid npm name

### linter

Default: `tslint`
Expand Down
24 changes: 17 additions & 7 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ forEachCli((currentCLIName) => {
ensureProject();
const plugin = uniq('plugin');

runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter}`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --importPath=@proj/${plugin}`
);
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');

Expand Down Expand Up @@ -62,7 +64,9 @@ forEachCli((currentCLIName) => {
it(`should run the plugin's e2e tests`, async (done) => {
ensureProject();
const plugin = uniq('plugin');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter}`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --importPath=@proj/${plugin}`
);
const results = await runCLIAsync(`e2e ${plugin}-e2e`);
expect(results.stdout).toContain('Compiling TypeScript files');
expectTestsPass(results);
Expand All @@ -75,7 +79,9 @@ forEachCli((currentCLIName) => {
const plugin = uniq('plugin');
const version = '1.0.0';

runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter}`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --importPath=@proj/${plugin}`
);
runCLI(
`generate @nrwl/nx-plugin:migration --project=${plugin} --version=${version} --packageJsonUpdates=false`
);
Expand Down Expand Up @@ -112,7 +118,9 @@ forEachCli((currentCLIName) => {
const plugin = uniq('plugin');
const schematic = uniq('schematic');

runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter}`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --importPath=@proj/${plugin}`
);
runCLI(
`generate @nrwl/nx-plugin:schematic ${schematic} --project=${plugin}`
);
Expand Down Expand Up @@ -152,7 +160,9 @@ forEachCli((currentCLIName) => {
const plugin = uniq('plugin');
const builder = uniq('builder');

runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter}`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --importPath=@proj/${plugin}`
);
runCLI(`generate @nrwl/nx-plugin:builder ${builder} --project=${plugin}`);

const lintResults = runCLI(`lint ${plugin}`);
Expand Down Expand Up @@ -190,7 +200,7 @@ forEachCli((currentCLIName) => {
ensureProject();
const plugin = uniq('plugin');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --directory subdir`
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --directory subdir --importPath=@proj/${plugin}`
);
checkFilesExist(`libs/subdir/${plugin}/package.json`);
const workspace = readJson(workspaceConfigName());
Expand All @@ -206,7 +216,7 @@ forEachCli((currentCLIName) => {
ensureProject();
const plugin = uniq('plugin');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --tags=e2etag,e2ePackage`
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=${linter} --tags=e2etag,e2ePackage --importPath=@proj/${plugin}`
);
const nxJson = readJson('nx.json');
expect(nxJson.projects[plugin].tags).toEqual(['e2etag', 'e2ePackage']);
Expand Down
6 changes: 4 additions & 2 deletions packages/create-nx-plugin/bin/create-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ function createWorkspace(
}

function createNxPlugin(workspaceName, pluginName) {
console.log(`nx generate @nrwl/nx-plugin:plugin ${pluginName}`);
console.log(
`nx generate @nrwl/nx-plugin:plugin ${pluginName} --importPath=${workspaceName}/${pluginName}`
);
execSync(
`node ./node_modules/@nrwl/cli/bin/nx.js generate @nrwl/nx-plugin:plugin ${pluginName}`,
`node ./node_modules/@nrwl/cli/bin/nx.js generate @nrwl/nx-plugin:plugin ${pluginName} --importPath=${workspaceName}/${pluginName}`,
{
cwd: workspaceName,
stdio: [0, 1, 2],
Expand Down
6 changes: 5 additions & 1 deletion packages/nx-plugin/src/schematics/builder/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ describe('NxPlugin builder', () => {
beforeEach(async () => {
projectName = 'my-plugin';
appTree = createEmptyWorkspace(ngSchematics.Tree.empty());
appTree = await runSchematic('plugin', { name: projectName }, appTree);
appTree = await runSchematic(
'plugin',
{ name: projectName, importPath: '@proj/my-plugin' },
appTree
);
});

it('should generate files', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ describe('NxPlugin migration', () => {
beforeEach(async () => {
projectName = 'my-plugin';
appTree = createEmptyWorkspace(ngSchematics.Tree.empty());
appTree = await runSchematic('plugin', { name: projectName }, appTree);
appTree = await runSchematic(
'plugin',
{ name: projectName, importPath: '@proj/my-plugin' },
appTree
);
});

it('should update the workspace.json file', async () => {
Expand Down
48 changes: 40 additions & 8 deletions packages/nx-plugin/src/schematics/plugin/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ describe('NxPlugin plugin', () => {
});

it('should update the workspace.json file', async () => {
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
const workspace = await readWorkspace(tree);
const project = workspace.projects['my-plugin'];
expect(project.root).toEqual('libs/my-plugin');
Expand Down Expand Up @@ -62,7 +66,11 @@ describe('NxPlugin plugin', () => {
});

it('should update the tsconfig.lib.json file', async () => {
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
const tsLibConfig = readJsonInTree(
tree,
'libs/my-plugin/tsconfig.lib.json'
Expand All @@ -71,7 +79,11 @@ describe('NxPlugin plugin', () => {
});

it('should create schematic and builder files', async () => {
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
expect(tree.exists('libs/my-plugin/collection.json')).toBeTruthy();
expect(tree.exists('libs/my-plugin/builders.json')).toBeTruthy();
expect(
Expand Down Expand Up @@ -115,7 +127,11 @@ describe('NxPlugin plugin', () => {

it('should call the @nrwl/node:lib schematic', async () => {
const externalSchematicSpy = jest.spyOn(ngSchematics, 'externalSchematic');
await runSchematic('plugin', { name: 'myPlugin' }, appTree);
await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
expect(externalSchematicSpy).toBeCalledWith(
'@nrwl/node',
'lib',
Expand All @@ -127,7 +143,11 @@ describe('NxPlugin plugin', () => {

it('should call the @nrwl/nx-plugin:e2e schematic', async () => {
const schematicSpy = jest.spyOn(ngSchematics, 'schematic');
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
expect(schematicSpy).toBeCalledWith(
'e2e-project',
expect.objectContaining({
Expand All @@ -140,7 +160,11 @@ describe('NxPlugin plugin', () => {

it('should call the @nrwl/nx-plugin:schematic schematic', async () => {
const schematicSpy = jest.spyOn(ngSchematics, 'schematic');
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
expect(schematicSpy).toBeCalledWith(
'schematic',
expect.objectContaining({
Expand All @@ -152,7 +176,11 @@ describe('NxPlugin plugin', () => {

it('should call the @nrwl/nx-plugin:builder schematic', async () => {
const schematicSpy = jest.spyOn(ngSchematics, 'schematic');
const tree = await runSchematic('plugin', { name: 'myPlugin' }, appTree);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', importPath: '@proj/my-plugin' },
appTree
);
expect(schematicSpy).toBeCalledWith(
'builder',
expect.objectContaining({
Expand All @@ -171,7 +199,11 @@ describe('NxPlugin plugin', () => {
);
const tree = await runSchematic(
'plugin',
{ name: 'myPlugin', unitTestRunner: 'none' },
{
name: 'myPlugin',
importPath: '@proj/my-plugin',
unitTestRunner: 'none',
},
appTree
);

Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/src/schematics/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function (schema: NormalizedSchema): Rule {
externalSchematic('@nrwl/node', 'lib', {
...schema,
publishable: true,
importPath: schema.importPath,
unitTestRunner: options.unitTestRunner,
}),
addFiles(options),
Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/src/schematics/plugin/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Linter } from '@nrwl/workspace';
export interface Schema {
name: string;
directory?: string;
importPath: string;
skipTsConfig: boolean;
skipFormat: boolean;
tags?: string;
Expand Down
8 changes: 6 additions & 2 deletions packages/nx-plugin/src/schematics/plugin/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "object",
"examples": [
{
"command": "g plugin my-plugin --directory=plugins",
"command": "g plugin my-plugin --directory=plugins --importPath=@myorg/my-plugin",
"description": "Generate libs/plugins/my-plugin"
}
],
Expand All @@ -24,6 +24,10 @@
"description": "A directory where the plugin is placed",
"alias": "d"
},
"importPath": {
"type": "string",
"description": "How the plugin will be published, like @myorg/my-awesome-plugin. Note this must be a valid npm name"
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
Expand Down Expand Up @@ -52,5 +56,5 @@
"description": "Do not update tsconfig.json for development experience."
}
},
"required": ["name"]
"required": ["name", "importPath"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ describe('NxPlugin schematic', () => {
beforeEach(async () => {
projectName = 'my-plugin';
appTree = createEmptyWorkspace(ngSchematics.Tree.empty());
appTree = await runSchematic('plugin', { name: projectName }, appTree);
appTree = await runSchematic(
'plugin',
{ name: projectName, importPath: '@proj/my-plugin' },
appTree
);
});

it('should generate files', async () => {
Expand Down

0 comments on commit 428ac8c

Please sign in to comment.