Skip to content

Commit

Permalink
fix(Schematics): Upgrade schematics to new CLI structure
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Minimum dependency for @ngrx/schematics has changed:

@angular-devkit/core: ^0.5.0
@angular-devkit/schematics: ^0.5.0
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Apr 10, 2018
1 parent ca5df12 commit b99d9ff
Show file tree
Hide file tree
Showing 41 changed files with 1,214 additions and 479 deletions.
4 changes: 3 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"$schema":
"./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"example-app": {
"root": "",
"root": "example-app",
"projectType": "application",
"architect": {
"build": {
Expand Down
32 changes: 8 additions & 24 deletions example-app/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2017",
"dom"
],
"lib": ["es2017", "dom"],
"outDir": "../out-tsc/app",
"target": "es5",
"module": "es2015",
"types": [],
"baseUrl": ".",
"rootDir": "../",
"paths": {
"@ngrx/effects": [
"../modules/effects"
],
"@ngrx/store": [
"../modules/store"
],
"@ngrx/store-devtools": [
"../modules/store-devtools"
],
"@ngrx/router-store": [
"../modules/router-store"
],
"@ngrx/entity": [
"../modules/entity"
]
"@ngrx/effects": ["../modules/effects"],
"@ngrx/store": ["../modules/store"],
"@ngrx/store-devtools": ["../modules/store-devtools"],
"@ngrx/router-store": ["../modules/router-store"],
"@ngrx/entity": ["../modules/entity"],
"@ngrx/schematics": ["../modules/schematics"]
}
},
"exclude": [
"../node_modules",
"test.ts",
"**/*.spec.ts"
]
"exclude": ["../node_modules", "test.ts", "**/*.spec.ts"]
}
4 changes: 2 additions & 2 deletions modules/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"homepage": "https://github.com/ngrx/platform#readme",
"schematics": "./collection.json",
"peerDependencies": {
"@angular-devkit/core": "^0.4.0",
"@angular-devkit/schematics": "^0.4.0"
"@angular-devkit/core": "^0.5.0",
"@angular-devkit/schematics": "^0.5.0"
}
}
72 changes: 50 additions & 22 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as ActionOptions } from './schema';
import {
getProjectPath,
createWorkspace,
} from '../utility/test/create-workspace';

describe('Action Schematic', () => {
const schematicRunner = new SchematicTestRunner(
Expand All @@ -9,49 +16,70 @@ describe('Action Schematic', () => {
);
const defaultOptions: ActionOptions = {
name: 'foo',
path: 'app',
sourceDir: 'src',
// path: 'app',
project: 'bar',
spec: false,
group: false,
flat: true,
};

const projectPath = getProjectPath();

let appTree: UnitTestTree;

beforeEach(() => {
appTree = createWorkspace(schematicRunner, appTree);
});

it('should create one file', () => {
const tree = schematicRunner.runSchematic('action', defaultOptions);
expect(tree.files.length).toEqual(1);
expect(tree.files[0]).toEqual('/src/app/foo.actions.ts');
const tree = schematicRunner.runSchematic(
'action',
defaultOptions,
appTree
);
expect(
tree.files.indexOf(`${projectPath}/src/app/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create two files if spec is true', () => {
const options = {
...defaultOptions,
spec: true,
};
const tree = schematicRunner.runSchematic('action', options);
expect(tree.files.length).toEqual(2);
const tree = schematicRunner.runSchematic('action', options, appTree);
expect(
tree.files.indexOf('/src/app/foo.actions.spec.ts')
tree.files.indexOf(`${projectPath}/src/app/foo.actions.spec.ts`)
).toBeGreaterThanOrEqual(0);
expect(
tree.files.indexOf('/src/app/foo.actions.ts')
tree.files.indexOf(`${projectPath}/src/app/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create an enum named "Foo"', () => {
const tree = schematicRunner.runSchematic('action', defaultOptions);
const fileEntry = tree.get(tree.files[0]);
if (fileEntry) {
const fileContent = fileEntry.content.toString();
expect(fileContent).toMatch(/export enum FooActionTypes/);
}
const tree = schematicRunner.runSchematic(
'action',
defaultOptions,
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(/export enum FooActionTypes/);
});

it('should group within an "actions" folder if group is set', () => {
const tree = schematicRunner.runSchematic('action', {
...defaultOptions,
group: true,
});
expect(tree.files.length).toEqual(1);
expect(tree.files[0]).toEqual('/src/app/actions/foo.actions.ts');
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
group: true,
},
appTree
);
expect(
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});
});
43 changes: 23 additions & 20 deletions modules/schematics/src/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ import {
noop,
template,
url,
Tree,
SchematicContext,
} from '@angular-devkit/schematics';
import * as stringUtils from '../strings';
import { Schema as ActionOptions } from './schema';
import { getProjectPath } from '../utility/project';

export default function(options: ActionOptions): Rule {
options.path = options.path ? normalize(options.path) : options.path;
const sourceDir = options.sourceDir;
if (!sourceDir) {
throw new SchematicsException(`sourceDir option is required.`);
}
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);

const templateSource = apply(url('./files'), [
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
template({
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'actions' : ''
),
...stringUtils,
...(options as object),
dot: () => '.',
} as any),
move(sourceDir),
]);
const templateSource = apply(url('./files'), [
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
template({
'if-flat': (s: string) =>
stringUtils.group(
options.flat ? '' : s,
options.group ? 'actions' : ''
),
...stringUtils,
...(options as object),
dot: () => '.',
} as any),
]);

return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
return chain([branchAndMerge(chain([mergeWith(templateSource)]))])(
host,
context
);
};
}
22 changes: 20 additions & 2 deletions modules/schematics/src/action/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
export interface Schema {
/**
* The name of the component.
*/

name: string;
appRoot?: string;
/**
* The path to create the component.
*/

path?: string;
sourceDir?: string;
/**
* The name of the project.
*/
project?: string;
/**
* Specifies if a spec file is generated.
*/
spec?: boolean;
/**
* Flag to indicate if a dir is created.
*/

flat?: boolean;
/**
* Group actions file within 'actions' folder
*/

group?: boolean;
}
16 changes: 5 additions & 11 deletions modules/schematics/src/action/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
"type": "object",
"properties": {
"name": {
"type": "string"
},
"appRoot": {
"description": "The name of the action.",
"type": "string"
},
"path": {
"type": "string",
"default": "app"
},
"sourceDir": {
"type": "string",
"default": "src"
"format": "path",
"description": "The path to create the component.",
"visible": false
},
"spec": {
"type": "boolean",
Expand All @@ -35,7 +31,5 @@
"aliases": ["g"]
}
},
"required": [
"name"
]
"required": ["name"]
}
39 changes: 10 additions & 29 deletions modules/schematics/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,35 @@ import {
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { createAppModule, getFileContent } from './utility/test';
import {
createWorkspace,
getProjectPath,
} from './utility/test/create-workspace';

describe('CLI Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/schematics',
path.join(__dirname, '../collection.json')
);

const workspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};

const appOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
viewEncapsulation: 'Emulated',
routing: false,
style: 'css',
skipTests: false,
};

const defaultOptions = {
name: 'foo',
project: 'bar',
};

const projectPath = getProjectPath();

let appTree: UnitTestTree;

beforeEach(() => {
appTree = schematicRunner.runExternalSchematic(
'@schematics/angular',
'workspace',
workspaceOptions
);
appTree = schematicRunner.runExternalSchematic(
'@schematics/angular',
'application',
appOptions,
appTree
);
appTree = createWorkspace(schematicRunner, appTree);
});

it('should create a class by the angular/cli', () => {
const options = { ...defaultOptions, state: undefined };
const tree = schematicRunner.runSchematic('class', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo.ts');
const content = tree.readContent(`${projectPath}/src/app/foo.ts`);

expect(content).toMatch(/export class Foo/);
});
});
Loading

0 comments on commit b99d9ff

Please sign in to comment.