Skip to content

Commit

Permalink
fix(schematics): default to workspace npm scope as the default prefix…
Browse files Browse the repository at this point in the history
… when creating apps and libs
  • Loading branch information
vsavkin committed May 15, 2018
1 parent 2a774cc commit 375496a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 17 deletions.
29 changes: 29 additions & 0 deletions packages/schematics/src/collection/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ describe('app', () => {
);
expect(tsconfigE2E.extends).toEqual('../../tsconfig.json');
});

it('should default the prefix to npmScope', () => {
const noPrefix = schematicRunner.runSchematic(
'app',
{ name: 'myApp' },
appTree
);
expect(
JSON.parse(noPrefix.read('angular.json').toString()).projects['my-app']
.prefix
).toEqual('proj');
expect(
noPrefix.read('apps/my-app-e2e/src/app.e2e-spec.ts').toString()
).toContain('Welcome to proj!');

const withPrefix = schematicRunner.runSchematic(
'app',
{ name: 'myApp', prefix: 'custom' },
appTree
);
expect(
JSON.parse(withPrefix.read('angular.json').toString()).projects[
'my-app'
].prefix
).toEqual('custom');
expect(
withPrefix.read('apps/my-app-e2e/src/app.e2e-spec.ts').toString()
).toContain('Welcome to custom!');
});
});

describe('nested', () => {
Expand Down
35 changes: 24 additions & 11 deletions packages/schematics/src/collection/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
move,
noop,
Rule,
Tree
Tree,
SchematicContext
} from '@angular-devkit/schematics';
import { Schema } from './schema';
import * as ts from 'typescript';
Expand All @@ -19,9 +20,11 @@ import { wrapIntoFormat } from '../../utils/tasks';
import { toFileName } from '../../utils/name-utils';
import { offsetFromRoot } from '@nrwl/schematics/src/utils/common';
import {
getNpmScope,
getWorkspacePath,
replaceAppNameWithPath
} from '@nrwl/schematics/src/utils/cli-config-utils';
import { updateFile } from '../../../../../e2e/utils';

interface NormalizedSchema extends Schema {
appProjectRoot: string;
Expand Down Expand Up @@ -94,9 +97,11 @@ function addRouterRootConfiguration(options: NormalizedSchema): Rule {
};
}

const staticComponentContent = `
function updateComponentTemplate(options: NormalizedSchema): Rule {
return (host: Tree) => {
const baseContent = `
<div style="text-align:center">
<h1>Welcome to app!</h1>
<h1>Welcome to ${getNpmScope(host)}!</h1>
<img width="300" src="https://raw.githubusercontent.com/nrwl/nx/master/nx-logo.png">
</div>
Expand All @@ -109,12 +114,9 @@ Nx is designed to help you create and build enterprise grade Angular application
<h2>Quick Start & Documentation</h2>
<a href="https://nrwl.io/nx">Watch a 5-minute video on how to get started with Nx.</a>`;

function updateComponentTemplate(options: NormalizedSchema): Rule {
return (host: Tree) => {
const content = options.routing
? `${staticComponentContent}\n<router-outlet></router-outlet>`
: staticComponentContent;
? `${baseContent}\n<router-outlet></router-outlet>`
: baseContent;
host.overwrite(
`${options.appProjectRoot}/src/app/app.component.html`,
content
Expand Down Expand Up @@ -182,6 +184,15 @@ function updateProject(options: NormalizedSchema): Rule {

function updateE2eProject(options: NormalizedSchema): Rule {
return (host: Tree) => {
// patching the spec file because of a bug in the CLI application schematic
// it hardcodes "app" in the e2e tests
const spec = `${options.e2eProjectRoot}/src/app.e2e-spec.ts`;
const content = host.read(spec).toString();
host.overwrite(
spec,
content.replace('Welcome to app!', `Welcome to ${options.prefix}!`)
);

return chain([
updateJsonInTree(getWorkspacePath(host), json => {
const project = json.projects[options.e2eProjectName];
Expand Down Expand Up @@ -210,8 +221,8 @@ function updateE2eProject(options: NormalizedSchema): Rule {
}

export default function(schema: Schema): Rule {
return wrapIntoFormat(() => {
const options = normalizeOptions(schema);
return wrapIntoFormat((host: Tree) => {
const options = normalizeOptions(host, schema);
return chain([
externalSchematic('@schematics/angular', 'application', {
...options,
Expand All @@ -231,7 +242,7 @@ export default function(schema: Schema): Rule {
});
}

function normalizeOptions(options: Schema): NormalizedSchema {
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const appDirectory = options.directory
? `${toFileName(options.directory)}/${toFileName(options.name)}`
: toFileName(options.name);
Expand All @@ -246,8 +257,10 @@ function normalizeOptions(options: Schema): NormalizedSchema {
? options.tags.split(',').map(s => s.trim())
: [];

const defaultPrefix = getNpmScope(host);
return {
...options,
prefix: options.prefix ? options.prefix : defaultPrefix,
name: appProjectName,
appProjectRoot,
e2eProjectRoot,
Expand Down
1 change: 0 additions & 1 deletion packages/schematics/src/collection/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"type": "string",
"format": "html-selector",
"description": "The prefix to apply to generated selectors.",
"default": "app",
"alias": "p"
},
"style": {
Expand Down
9 changes: 6 additions & 3 deletions packages/schematics/src/collection/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ function updateTsConfig(options: NormalizedSchema): Rule {
}

export default function(schema: Schema): Rule {
return wrapIntoFormat(() => {
const options = normalizeOptions(schema);
return wrapIntoFormat((host: Tree) => {
const options = normalizeOptions(host, schema);
if (!options.routing && options.lazy) {
throw new Error(`routing must be set`);
}
Expand Down Expand Up @@ -389,7 +389,7 @@ export default function(schema: Schema): Rule {
});
}

function normalizeOptions(options: Schema): NormalizedSchema {
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const projectDirectory = options.directory
? `${toFileName(options.directory)}/${toFileName(options.name)}`
: toFileName(options.name);
Expand All @@ -401,8 +401,11 @@ function normalizeOptions(options: Schema): NormalizedSchema {
? options.tags.split(',').map(s => s.trim())
: [];
const modulePath = `${projectRoot}/src/lib/${projectName}.module.ts`;
const defaultPrefix = getNpmScope(host);

return {
...options,
prefix: options.prefix ? options.prefix : defaultPrefix,
name: projectName,
projectRoot,
entryFile: 'index',
Expand Down
23 changes: 23 additions & 0 deletions packages/schematics/src/collection/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ describe('lib', () => {
expect(tree.exists('libs/my-lib/src/index.ts')).toBeTruthy();
expect(tree.exists('libs/my-lib/src/lib/my-lib.module.ts')).toBeTruthy();
});

it('should default the prefix to npmScope', () => {
const noPrefix = schematicRunner.runSchematic(
'lib',
{ name: 'myLib' },
appTree
);
expect(
JSON.parse(noPrefix.read('angular.json').toString()).projects['my-lib']
.prefix
).toEqual('proj');

const withPrefix = schematicRunner.runSchematic(
'app',
{ name: 'myLib', prefix: 'custom' },
appTree
);
expect(
JSON.parse(withPrefix.read('angular.json').toString()).projects[
'my-lib'
].prefix
).toEqual('custom');
});
});

describe('nested', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/src/collection/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Schema {
flat?: boolean;
commonModule?: boolean;

prefix?: string;
routing?: boolean;
lazy?: boolean;
parentModule?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/schematics/src/collection/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"type": "string",
"format": "html-selector",
"description": "The prefix to apply to generated selectors.",
"default": "lib",
"alias": "p"
},
"skipPackageJson": {
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/src/utils/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class FormatFiles implements TaskConfigurationGenerator<any> {
export function wrapIntoFormat(fn: Function): any {
return (host: Tree, context: SchematicContext) => {
context.addTask(new FormatFiles());
return fn(context)(host, context);
return fn(host, context)(host, context);
};
}

0 comments on commit 375496a

Please sign in to comment.