Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schematics): add project flag support to specify apps or libs #1477

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/schematics-core/testing/create-workspace.ts
Expand Up @@ -3,13 +3,13 @@ import {
SchematicTestRunner,
} from '@angular-devkit/schematics/testing';

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

const defaultAppOptions = {
export const defaultAppOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
Expand Down
21 changes: 20 additions & 1 deletion modules/schematics/src/action/index.spec.ts
Expand Up @@ -7,6 +7,8 @@ import { Schema as ActionOptions } from './schema';
import {
getTestProjectPath,
createWorkspace,
defaultWorkspaceOptions,
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Action Schematic', () => {
Expand All @@ -16,7 +18,6 @@ describe('Action Schematic', () => {
);
const defaultOptions: ActionOptions = {
name: 'foo',
// path: 'app',
project: 'bar',
spec: false,
group: false,
Expand All @@ -31,6 +32,24 @@ describe('Action Schematic', () => {
appTree = createWorkspace(schematicRunner, appTree);
});

it('should create an action to specified project if provided', () => {
const options = {
...defaultOptions,
project: 'baz',
};

const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
...defaultAppOptions,
name: 'baz',
});

const tree = schematicRunner.runSchematic('action', options, appTree);
const files = tree.files;
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create one file', () => {
const tree = schematicRunner.runSchematic(
'action',
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/action/schema.json
Expand Up @@ -18,6 +18,12 @@
"description": "The path to create the component.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false,
"aliases": ["p"]
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
Expand Down
2 changes: 0 additions & 2 deletions modules/schematics/src/container/index.spec.ts
Expand Up @@ -4,7 +4,6 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as ContainerOptions } from './schema';
import {} from '../../schematics-core';
import {
getTestProjectPath,
createWorkspace,
Expand All @@ -19,7 +18,6 @@ describe('Container Schematic', () => {
const defaultOptions: ContainerOptions = {
name: 'foo',
project: 'bar',
// path: 'src/app',
inlineStyle: false,
inlineTemplate: false,
changeDetection: 'Default',
Expand Down
3 changes: 2 additions & 1 deletion modules/schematics/src/container/schema.json
Expand Up @@ -13,7 +13,8 @@
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false
"visible": false,
"aliases": ["p"]
},
"name": {
"type": "string",
Expand Down
25 changes: 23 additions & 2 deletions modules/schematics/src/effect/index.spec.ts
Expand Up @@ -3,12 +3,13 @@ import {
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import {} from '../../schematics-core';
import { Schema as EffectOptions } from './schema';
import {
getTestProjectPath,
createWorkspace,
createAppModuleWithEffects,
defaultWorkspaceOptions,
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Effect Schematic', () => {
Expand All @@ -19,7 +20,6 @@ describe('Effect Schematic', () => {

const defaultOptions: EffectOptions = {
name: 'foo',
// path: 'app',
project: 'bar',
spec: true,
module: undefined,
Expand All @@ -37,6 +37,27 @@ describe('Effect Schematic', () => {
appTree = createWorkspace(schematicRunner, appTree);
});

it('should create an effect to specified project if provided', () => {
const options = {
...defaultOptions,
project: 'baz',
};

const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
...defaultAppOptions,
name: 'baz',
});

const tree = schematicRunner.runSchematic('effect', options, appTree);
const files = tree.files;
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.spec.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create an effect with a spec file', () => {
const options = { ...defaultOptions };

Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/effect/schema.json
Expand Up @@ -18,6 +18,12 @@
"description": "The path to create the component.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false,
"aliases": ["p"]
},
"flat": {
"type": "boolean",
"default": true,
Expand Down
28 changes: 26 additions & 2 deletions modules/schematics/src/entity/index.spec.ts
Expand Up @@ -4,10 +4,11 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as EntityOptions } from './schema';
import {} from '../../schematics-core';
import {
getTestProjectPath,
createWorkspace,
defaultWorkspaceOptions,
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Entity Schematic', () => {
Expand All @@ -18,7 +19,6 @@ describe('Entity Schematic', () => {
const defaultOptions: EntityOptions = {
name: 'foo',
project: 'bar',
// path: 'app',
spec: false,
};

Expand Down Expand Up @@ -48,6 +48,30 @@ describe('Entity Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

it('should create 3 files of an entity to specified project if provided', () => {
const options = {
...defaultOptions,
project: 'baz',
};

const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
...defaultAppOptions,
name: 'baz',
});

const tree = schematicRunner.runSchematic('entity', options, appTree);
const files = tree.files;
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.model.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create a folder if flat is false', () => {
const tree = schematicRunner.runSchematic(
'entity',
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/entity/schema.json
Expand Up @@ -18,6 +18,12 @@
"description": "The path to create the component.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false,
"aliases": ["p"]
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
Expand Down
40 changes: 32 additions & 8 deletions modules/schematics/src/feature/index.spec.ts
Expand Up @@ -4,10 +4,11 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as FeatureOptions } from './schema';
import {} from '../../schematics-core';
import {
getTestProjectPath,
createWorkspace,
defaultWorkspaceOptions,
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Feature Schematic', () => {
Expand All @@ -18,7 +19,6 @@ describe('Feature Schematic', () => {
const defaultOptions: FeatureOptions = {
name: 'foo',
project: 'bar',
// path: 'app',
module: '',
spec: true,
group: false,
Expand Down Expand Up @@ -54,6 +54,36 @@ describe('Feature Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

it('should create all files of a feature to specified project if provided', () => {
const options = {
...defaultOptions,
project: 'baz',
};

const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
...defaultAppOptions,
name: 'baz',
});

const tree = schematicRunner.runSchematic('feature', options, appTree);
const files = tree.files;
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.spec.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.ts`)
).toBeGreaterThanOrEqual(0);
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.spec.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create all files of a feature within grouped folders if group is set', () => {
const options = { ...defaultOptions, group: true };

Expand Down Expand Up @@ -85,12 +115,6 @@ describe('Feature Schematic', () => {
};

const tree = schematicRunner.runSchematic('feature', options, appTree);
const effectsFileContent = tree.readContent(
`${projectPath}/src/app/foo/effects/foo.effects.ts`
);
const reducerFileContent = tree.readContent(
`${projectPath}/src/app/foo/reducers/foo.reducer.ts`
);
const moduleFileContent = tree.readContent(
`${projectPath}/src/app/app.module.ts`
);
Expand Down
8 changes: 7 additions & 1 deletion modules/schematics/src/feature/schema.json
Expand Up @@ -7,9 +7,15 @@
"path": {
"type": "string",
"format": "path",
"description": "The path to create the component.",
"description": "The path to create the feature.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false,
"aliases": ["p"]
},
"name": {
"description": "The name of the feature.",
"type": "string",
Expand Down
22 changes: 20 additions & 2 deletions modules/schematics/src/reducer/index.spec.ts
Expand Up @@ -4,11 +4,12 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as ReducerOptions } from './schema';
import {} from '../../schematics-core';
import {
getTestProjectPath,
createReducers,
createWorkspace,
defaultWorkspaceOptions,
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Reducer Schematic', () => {
Expand All @@ -19,7 +20,6 @@ describe('Reducer Schematic', () => {
const defaultOptions: ReducerOptions = {
name: 'foo',
project: 'bar',
// path: 'app',
spec: false,
};

Expand All @@ -43,6 +43,24 @@ describe('Reducer Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

it('should create a reducer to specified project if provided', () => {
const options = {
...defaultOptions,
project: 'baz',
};

const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
...defaultAppOptions,
name: 'baz',
});

const tree = schematicRunner.runSchematic('reducer', options, appTree);
const files = tree.files;
expect(
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create two files if spec is true', () => {
const options = {
...defaultOptions,
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/reducer/schema.json
Expand Up @@ -18,6 +18,12 @@
"description": "The path to create the component.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"visible": false,
"aliases": ["p"]
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
Expand Down