feat(schematics): add all libs as apps to angularcli.json to support code generation

This commit is contained in:
vsavkin 2017-09-22 15:25:21 -04:00
parent 9935beef5c
commit e2c8ef7a36
12 changed files with 98 additions and 56 deletions

View File

@ -1,11 +1,11 @@
import {checkFilesExists, cleanup, copyMissingPackages, newBazelApp, readFile, runCLI, runSchematic} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, ngNewBazel, readFile, runCLI, runSchematic} from '../utils';
describe('application', () => {
beforeEach(cleanup);
describe('generate', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel --skip-install');
ngNewBazel('--collection=@nrwl/bazel --skip-install');
runSchematic('@nrwl/bazel:app --name=myApp');
checkFilesExists(
@ -35,7 +35,7 @@ describe('application', () => {
describe('build', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel');
ngNewBazel('--collection=@nrwl/bazel');
copyMissingPackages();
runSchematic('@nrwl/bazel:app --name=myApp');
expect(runCLI('build')).toContain('main.bundle.js');
@ -44,7 +44,7 @@ describe('application', () => {
describe('test', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel');
ngNewBazel('--collection=@nrwl/bazel');
copyMissingPackages();
runSchematic('@nrwl/bazel:app --name=myApp');
expect(runCLI('test --single-run')).toContain('Executed 1 of 1 SUCCESS');

View File

@ -1,11 +1,11 @@
import {checkFilesExists, cleanup, copyMissingPackages, newBazelApp, readFile, runCLI, runSchematic} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, ngNewBazel, readFile, runCLI, runSchematic} from '../utils';
describe('library', () => {
beforeEach(cleanup);
describe('generate', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel --skip-install');
ngNewBazel('--collection=@nrwl/bazel --skip-install');
runSchematic('@nrwl/bazel:lib --name=myLib');
checkFilesExists(
@ -20,7 +20,7 @@ describe('library', () => {
describe('build', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel');
ngNewBazel('--collection=@nrwl/bazel');
copyMissingPackages();
runSchematic('@nrwl/bazel:lib --name=lib');

View File

@ -1,11 +1,11 @@
import {checkFilesExists, cleanup, copyMissingPackages, newApp, newBazelApp, readFile, runCLI, runCommand, runSchematic, updateFile} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, ngNew, ngNewBazel, readFile, runCLI, runCommand, runSchematic, updateFile} from '../utils';
describe('angular library', () => {
beforeEach(cleanup);
describe('generate', () => {
it('creates a new angularlibrary in a workspace', () => {
newBazelApp('--collection=@nrwl/bazel --skip-install');
ngNewBazel('--collection=@nrwl/bazel --skip-install');
runSchematic('@nrwl/bazel:nglib --name=myLib');
checkFilesExists(
@ -20,7 +20,7 @@ describe('angular library', () => {
describe('build', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel');
ngNewBazel('--collection=@nrwl/bazel');
copyMissingPackages();
runSchematic('@nrwl/bazel:nglib --name=myLib');
@ -30,7 +30,7 @@ describe('angular library', () => {
describe('test', () => {
it('should work', () => {
newBazelApp('--collection=@nrwl/bazel');
ngNewBazel('--collection=@nrwl/bazel');
copyMissingPackages();
runSchematic('@nrwl/bazel:nglib --name=myLib');
expect(runCLI('test --single-run')).toContain('Executed 0 of 0');

View File

@ -1,10 +1,10 @@
import {checkFilesExists, cleanup, copyMissingPackages, newApp, newBazelApp, readFile, runCLI, runCommand, runSchematic, updateFile} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, ngNew, ngNewBazel, readFile, runCLI, runCommand, runSchematic, updateFile} from '../utils';
describe('workspace', () => {
beforeEach(cleanup);
it('creates a new workspace for developing angular applications', () => {
newBazelApp('--collection=@nrwl/bazel --skip-install');
ngNewBazel('--collection=@nrwl/bazel --skip-install');
checkFilesExists(`tsconfig.json`, `WORKSPACE`, `BUILD.bazel`);
});
});

View File

@ -1,10 +1,10 @@
import {checkFilesExists, cleanup, newApp, readFile, runCLI, runSchematic, updateFile} from '../utils';
import {checkFilesExists, cleanup, ngNew, readFile, runCLI, runSchematic, updateFile} from '../utils';
describe('Nrwl Convert to Nx Workspace', () => {
beforeEach(cleanup);
it('should generate a workspace', () => {
newApp('--skip-install --npmScope=nrwl');
ngNew('--skip-install --npmScope=nrwl');
// update package.json
const packageJson = JSON.parse(readFile('package.json'));
@ -53,7 +53,7 @@ describe('Nrwl Convert to Nx Workspace', () => {
});
it('should build and test', () => {
newApp();
ngNew();
runSchematic('@nrwl/schematics:convert-to-workspace --npmScope=nrwl');
runSchematic('@nrwl/schematics:lib --name=mylib --ngmodule');

View File

@ -1,11 +1,11 @@
import {checkFilesExists, cleanup, copyMissingPackages, newApp, readFile, runCLI} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, ngNew, readFile, runCLI} from '../utils';
describe('ngrx', () => {
beforeEach(cleanup);
describe('root', () => {
it('should generate', () => {
newApp('--skip-install');
ngNew('--skip-install');
runCLI('generate ngrx app --module=src/app/app.module.ts --root --collection=@nrwl/schematics');
checkFilesExists(
@ -19,7 +19,7 @@ describe('ngrx', () => {
});
it('should build', () => {
newApp();
ngNew();
copyMissingPackages();
runCLI('generate ngrx app --module=src/app/app.module.ts --root --collection=@nrwl/schematics');
@ -28,7 +28,7 @@ describe('ngrx', () => {
}, 50000);
it('should add empty root configuration', () => {
newApp();
ngNew();
copyMissingPackages();
runCLI('generate ngrx app --module=src/app/app.module.ts --onlyEmptyRoot --collection=@nrwl/schematics');
@ -42,7 +42,7 @@ describe('ngrx', () => {
describe('feature', () => {
it('should generate', () => {
newApp('--skip-install');
ngNew('--skip-install');
runCLI('generate ngrx app --module=src/app/app.module.ts --collection=@nrwl/schematics');
checkFilesExists(
@ -57,7 +57,7 @@ describe('ngrx', () => {
});
it('should generate files without importing them', () => {
newApp('--skip-install');
ngNew('--skip-install');
runCLI('generate ngrx app --module=src/app/app.module.ts --onlyAddFiles --collection=@nrwl/schematics');
checkFilesExists(
@ -71,7 +71,7 @@ describe('ngrx', () => {
});
it('should update package.json', () => {
newApp('--skip-install');
ngNew('--skip-install');
runCLI('generate ngrx app --module=src/app/app.module.ts --collection=@nrwl/schematics');
const contents = JSON.parse(readFile('package.json'));

View File

@ -1,10 +1,10 @@
import {cleanup, copyMissingPackages, newApp, readFile, runCLI, runSchematic, updateFile} from '../utils';
import {cleanup, copyMissingPackages, ngNew, readFile, runCLI, runSchematic, updateFile} from '../utils';
describe('Upgrade', () => {
beforeEach(cleanup);
it('should generate an upgrade shell', () => {
newApp();
ngNew();
copyMissingPackages();
updateFile('src/legacy.js', `
@ -29,7 +29,7 @@ describe('Upgrade', () => {
}, 50000);
it('should update package.json', () => {
newApp('--skip-install');
ngNew('--skip-install');
runCLI(
'generate upgrade-shell legacy --module=src/app/app.module.ts --angularJsImport=../legacy ' +
'--angularJsCmpSelector=rootLegacyCmp --collection=@nrwl/schematics');
@ -40,7 +40,7 @@ describe('Upgrade', () => {
});
it('should not update package.json when --skipPackageJson', () => {
newApp('--skipInstall');
ngNew('--skipInstall');
runCLI(
'generate upgrade-shell legacy --module=src/app/app.module.ts --angularJsImport=../legacy ' +
'--angularJsCmpSelector=rootLegacyCmp --skipPackageJson --collection=@nrwl/schematics');

View File

@ -1,10 +1,10 @@
import {checkFilesExists, cleanup, copyMissingPackages, newApp, readFile, runCLI, updateFile} from '../utils';
import {checkFilesExists, cleanup, copyMissingPackages, newApp, newLib, ngNew, readFile, runCLI, updateFile} from '../utils';
describe('Nrwl Workspace', () => {
beforeEach(cleanup);
it('should generate an empty workspace', () => {
newApp('--collection=@nrwl/schematics --skip-install');
ngNew('--collection=@nrwl/schematics --skip-install');
const angularCliJson = JSON.parse(readFile('.angular-cli.json'));
expect(angularCliJson.apps).toEqual([]);
@ -17,8 +17,8 @@ describe('Nrwl Workspace', () => {
describe('app', () => {
it('should generate an app', () => {
newApp('--collection=@nrwl/schematics --skip-install');
runCLI('generate app myapp --collection=@nrwl/schematics');
ngNew('--collection=@nrwl/schematics --skip-install');
newApp('myapp');
const angularCliJson = JSON.parse(readFile('.angular-cli.json'));
expect(angularCliJson.apps[0].name).toEqual('myapp');
@ -29,9 +29,9 @@ describe('Nrwl Workspace', () => {
});
it('should build app', () => {
newApp('--collection=@nrwl/schematics');
ngNew('--collection=@nrwl/schematics');
copyMissingPackages();
runCLI('generate app myapp --collection=@nrwl/schematics');
newApp('myapp');
runCLI('build --aot');
checkFilesExists('dist/apps/myapp/main.bundle.js');
expect(runCLI('test --single-run')).toContain('Executed 1 of 1 SUCCESS');
@ -40,17 +40,20 @@ describe('Nrwl Workspace', () => {
describe('lib', () => {
it('should generate a lib', () => {
newApp('--collection=@nrwl/schematics --skip-install');
runCLI('generate lib mylib --collection=@nrwl/schematics');
ngNew('--collection=@nrwl/schematics --skip-install');
newLib('mylib');
const angularCliJson = JSON.parse(readFile('.angular-cli.json'));
expect(angularCliJson.apps[0].name).toEqual('mylib');
checkFilesExists('libs/mylib/src/mylib.ts', 'libs/mylib/src/mylib.spec.ts', 'libs/mylib/index.ts');
});
it('should test a lib', () => {
newApp('--collection=@nrwl/schematics');
ngNew('--collection=@nrwl/schematics');
copyMissingPackages();
runCLI('generate app myapp --collection=@nrwl/schematics');
runCLI('generate lib mylib --collection=@nrwl/schematics');
newApp('myapp');
newLib('generate lib mylib');
expect(runCLI('test --single-run')).toContain('Executed 2 of 2 SUCCESS');
});
@ -58,26 +61,26 @@ describe('Nrwl Workspace', () => {
describe('nglib', () => {
it('should generate an ng lib', () => {
newApp('--collection=@nrwl/schematics --skip-install');
runCLI('generate lib mylib --collection=@nrwl/schematics --ngmodule');
ngNew('--collection=@nrwl/schematics --skip-install');
newLib('mylib --ngmodule');
checkFilesExists('libs/mylib/src/mylib.module.ts', 'libs/mylib/src/mylib.module.spec.ts', 'libs/mylib/index.ts');
});
it('should test an ng lib', () => {
newApp('--collection=@nrwl/schematics');
ngNew('--collection=@nrwl/schematics');
copyMissingPackages();
runCLI('generate app myapp --collection=@nrwl/schematics');
runCLI('generate lib mylib --collection=@nrwl/schematics --ngmodule');
newApp('myapp');
newLib('mylib --ngmodule');
expect(runCLI('test --single-run')).toContain('Executed 2 of 2 SUCCESS');
});
it('should resolve dependencies on the lib', () => {
newApp('--collection=@nrwl/schematics --npmScope=nrwl');
ngNew('--collection=@nrwl/schematics --npmScope=nrwl');
copyMissingPackages();
runCLI('generate app myapp --collection=@nrwl/schematics');
runCLI('generate lib mylib --collection=@nrwl/schematics --ngmodule');
newApp('myapp');
newLib('mylib --ngmodule');
updateFile('apps/myapp/src/app/app.module.ts', `
import { NgModule } from '@angular/core';

View File

@ -4,12 +4,12 @@ import * as path from 'path';
const projectName: string = 'proj';
export function newApp(command?: string): string {
export function ngNew(command?: string): string {
return execSync(`../node_modules/.bin/ng new proj ${command}`, {cwd: `./tmp`}).toString();
}
export function newBazelApp(command?: string): string {
const res = newApp(command);
export function ngNewBazel(command?: string): string {
const res = ngNew(command);
const cliPath = path.join('tmp', projectName, 'node_modules', '@angular', 'cli');
execSync(`rm -rf ${cliPath}`);
execSync(`cp -r node_modules/clis/bazel ${cliPath}`);
@ -20,6 +20,20 @@ export function runCLI(command?: string): string {
return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${projectName}`}).toString();
}
// switch to ng generate, once CLI is fixed
export function newApp(name: string): string {
return runCLI(`generate app ${name}`)
// return execSync(`../../node_modules/.bin/schematics @nrwl/schematics:app --name=${name}
// --collection=@nrwl/schematics`, { cwd: `./tmp/${projectName}` }).toString();
}
// switch to ng generate, once CLI is fixed
export function newLib(name: string): string {
return runCLI(`generate lib ${name}`)
// return execSync(`../../node_modules/.bin/schematics @nrwl/schematics:lib --name=${name}
// --collection=@nrwl/schematics`, { cwd: `./tmp/${projectName}` }).toString();
}
export function runSchematic(command: string): string {
return execSync(`../../node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${projectName}`}).toString();
}

View File

@ -85,7 +85,7 @@ It's similar to the standard CLI projects with a few changes:
#### Create an App
Run `ng generate app myapp --collection=@nrwl/schematics`, and you will see the following files created:
Run `ng generate app myapp`, and you will see the following files created:
```
apps/myapp/src/main.ts
@ -104,7 +104,7 @@ As with the standard CLI, running `ng build` without specifying an app will buil
#### Create a Lib
Run `ng generate lib mylib --collection=@nrwl/schematics`, and you will see the following files created:
Run `ng generate lib mylib`, and you will see the following files created:
```
libs/mylib/src/mylib.ts
@ -156,7 +156,7 @@ schematics @nrwl/schematics:convert-to-workspace
#### Root
Run `ng generate ngrx app --module=src/app/app.module.ts --root --collection=@nrwl/schematics`, and you will see the following files created:
Run `ng generate ngrx app --module=src/app/app.module.ts --root`, and you will see the following files created:
```
/src/app/+state/app.actions.ts
@ -172,11 +172,11 @@ Also, `app.module.ts` will have `StoreModule.forRoot` and `EffectsModule.forRoot
#### onlyEmptyRoot
Run `ng generate ngrx app --module=src/app/app.module.ts --onlyEmptyRoot --collection=@nrwl/schematics` to only add the `StoreModule.forRoot` and `EffectsModule.forRoot` calls without generating any new files.
Run `ng generate ngrx app --module=src/app/app.module.ts --onlyEmptyRoot` to only add the `StoreModule.forRoot` and `EffectsModule.forRoot` calls without generating any new files.
#### Feature
Run `ng generate ngrx app --module=src/app/mymodule/mymodule.module.ts --collection=@nrwl/schematics`, and you will see the following files created:
Run `ng generate ngrx app --module=src/app/mymodule/mymodule.module.ts`, and you will see the following files created:
```
/src/app/mymodule/+state/app.actions.ts
@ -198,7 +198,7 @@ Add `--onlyAddFiles` to generate files without adding imports to the module.
### upgrade-shell
Run `ng generate upgrade-shell legacy --module=src/app/app.module.ts --angularJsCmpSelector=rootLegacyCmp --collection=@nrwl/schematics` and you will see the following files created:
Run `ng generate upgrade-shell legacy --module=src/app/app.module.ts --angularJsCmpSelector=rootLegacyCmp` and you will see the following files created:
```
/src/app/legacy-setup.ts

View File

@ -3,6 +3,30 @@ import {Schema} from './schema';
import {names, toFileName} from '@nrwl/schematics';
import * as path from 'path';
function addLibToAngularCliJson(options: Schema): Rule {
return (host: Tree) => {
const appConfig = {
'name': options.name,
'root': path.join('libs', options.name, options.sourceDir),
'test': '../../../test.js'
};
if (!host.exists('.angular-cli.json')) {
throw new Error('Missing .angular-cli.json');
}
const sourceText = host.read('.angular-cli.json')!.toString('utf-8');
const json = JSON.parse(sourceText);
if (!json['apps']) {
json['apps'] = [];
}
json['apps'].push(appConfig);
host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
return host;
};
}
export default function(options: Schema): Rule {
const fullPath = path.join('libs', toFileName(options.name), options.sourceDir);
@ -10,5 +34,5 @@ export default function(options: Schema): Rule {
url(options.ngmodule ? './ngfiles' : './files'),
[template({...names(options.name), dot: '.', tmpl: '', ...options as object})]);
return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
return chain([branchAndMerge(chain([mergeWith(templateSource)])), addLibToAngularCliJson(options)]);
}

View File

@ -27,6 +27,7 @@
},
"defaults": {
"schematics": {
"collection": "@nrwl/schematics",
"newProject": ["app", "lib"]
},
"styleExt": "<%= style %>",<% if (!minimal) { %>