diff --git a/modules/effects/schematics-core/index.ts b/modules/effects/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/effects/schematics-core/index.ts +++ b/modules/effects/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/effects/schematics-core/utility/ast-utils.ts b/modules/effects/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/effects/schematics-core/utility/ast-utils.ts +++ b/modules/effects/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/effects/schematics-core/utility/project.ts b/modules/effects/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/effects/schematics-core/utility/project.ts +++ b/modules/effects/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/entity/schematics-core/index.ts b/modules/entity/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/entity/schematics-core/index.ts +++ b/modules/entity/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/entity/schematics-core/utility/ast-utils.ts b/modules/entity/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/entity/schematics-core/utility/ast-utils.ts +++ b/modules/entity/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/entity/schematics-core/utility/project.ts b/modules/entity/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/entity/schematics-core/utility/project.ts +++ b/modules/entity/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/router-store/schematics-core/index.ts b/modules/router-store/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/router-store/schematics-core/index.ts +++ b/modules/router-store/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/router-store/schematics-core/utility/ast-utils.ts b/modules/router-store/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/router-store/schematics-core/utility/ast-utils.ts +++ b/modules/router-store/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/router-store/schematics-core/utility/project.ts b/modules/router-store/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/router-store/schematics-core/utility/project.ts +++ b/modules/router-store/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/schematics-core/index.ts b/modules/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/schematics-core/index.ts +++ b/modules/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/schematics-core/testing/create-workspace.ts b/modules/schematics-core/testing/create-workspace.ts index cc6e357255..ec34f25f14 100644 --- a/modules/schematics-core/testing/create-workspace.ts +++ b/modules/schematics-core/testing/create-workspace.ts @@ -26,6 +26,10 @@ const defaultModuleOptions = { flat: false, }; +const defaultLibOptions = { + name: 'baz', +}; + export function getTestProjectPath( workspaceOptions: any = defaultWorkspaceOptions, appOptions: any = defaultAppOptions @@ -37,7 +41,8 @@ export function createWorkspace( schematicRunner: SchematicTestRunner, appTree: UnitTestTree, workspaceOptions = defaultWorkspaceOptions, - appOptions = defaultAppOptions + appOptions = defaultAppOptions, + libOptions = defaultLibOptions ) { appTree = schematicRunner.runExternalSchematic( '@schematics/angular', @@ -50,6 +55,12 @@ export function createWorkspace( appOptions, appTree ); + appTree = schematicRunner.runExternalSchematic( + '@schematics/angular', + 'library', + libOptions, + appTree + ); return appTree; } diff --git a/modules/schematics-core/utility/ast-utils.ts b/modules/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/schematics-core/utility/ast-utils.ts +++ b/modules/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/schematics-core/utility/project.ts b/modules/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/schematics-core/utility/project.ts +++ b/modules/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/schematics/schematics-core/index.ts b/modules/schematics/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/schematics/schematics-core/index.ts +++ b/modules/schematics/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/schematics/schematics-core/utility/ast-utils.ts b/modules/schematics/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/schematics/schematics-core/utility/ast-utils.ts +++ b/modules/schematics/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/schematics/schematics-core/utility/project.ts b/modules/schematics/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/schematics/schematics-core/utility/project.ts +++ b/modules/schematics/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/schematics/src/store/files/__statePath__/index.ts b/modules/schematics/src/store/files/__statePath__/index.ts index 3051836e0a..0d86d8d1db 100644 --- a/modules/schematics/src/store/files/__statePath__/index.ts +++ b/modules/schematics/src/store/files/__statePath__/index.ts @@ -5,7 +5,7 @@ import { createSelector, MetaReducer } from '@ngrx/store'; -import { environment } from '<%= environmentsPath %>'; +<% if (!isLib) { %>import { environment } from '<%= environmentsPath %>';<% } %> export interface <%= classify(stateInterface) %> { @@ -16,4 +16,4 @@ export const reducers: ActionReducerMap<<%= classify(stateInterface) %>> = { }; -export const metaReducers: MetaReducer<<%= classify(stateInterface) %>>[] = !environment.production ? [] : []; +export const metaReducers: MetaReducer<<%= classify(stateInterface) %>>[] = <% if (!isLib) { %>!environment.production ? [] : <% } %>[]; diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index 8fd32e478e..28f4daaeb0 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -9,6 +9,7 @@ import { getTestProjectPath, createWorkspace, } from '../../../schematics-core/testing'; +import { getProject } from '../../../schematics-core'; describe('Store Schematic', () => { const schematicRunner = new SchematicTestRunner( @@ -84,6 +85,18 @@ describe('Store Schematic', () => { ); }); + it('should not import the environments in the reducers for a library', () => { + const options = { + ...defaultOptions, + project: 'baz', + module: 'baz.module.ts', + }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = tree.readContent(`/projects/baz/src/lib/reducers/index.ts`); + expect(content).not.toMatch(/import { environment }/); + }); + it('should fail if specified module does not exist', () => { const options = { ...defaultOptions, module: '/src/app/app.moduleXXX.ts' }; let thrownError: Error | null = null; diff --git a/modules/schematics/src/store/index.ts b/modules/schematics/src/store/index.ts index 61eb968ae4..0821d76d58 100644 --- a/modules/schematics/src/store/index.ts +++ b/modules/schematics/src/store/index.ts @@ -20,6 +20,7 @@ import { Change, InsertChange, getProjectPath, + isLib, findModuleFromOptions, addImportToModule, parseName, @@ -158,6 +159,7 @@ export default function(options: StoreOptions): Rule { template({ ...stringUtils, ...(options as object), + isLib: isLib(host, options), environmentsPath, } as any), move(parsedPath.path), diff --git a/modules/store-devtools/schematics-core/index.ts b/modules/store-devtools/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/store-devtools/schematics-core/index.ts +++ b/modules/store-devtools/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/store-devtools/schematics-core/utility/ast-utils.ts b/modules/store-devtools/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/store-devtools/schematics-core/utility/ast-utils.ts +++ b/modules/store-devtools/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/store-devtools/schematics-core/utility/project.ts b/modules/store-devtools/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/store-devtools/schematics-core/utility/project.ts +++ b/modules/store-devtools/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +} diff --git a/modules/store/schematics-core/index.ts b/modules/store/schematics-core/index.ts index 5444469384..c41e1f8ec1 100644 --- a/modules/store/schematics-core/index.ts +++ b/modules/store/schematics-core/index.ts @@ -48,7 +48,7 @@ export { omit, } from './utility/ngrx-utils'; -export { getProjectPath } from './utility/project'; +export { getProjectPath, getProject, isLib } from './utility/project'; export { insertImport } from './utility/route-utils'; export const stringUtils = { diff --git a/modules/store/schematics-core/utility/ast-utils.ts b/modules/store/schematics-core/utility/ast-utils.ts index 1bd4484651..b1f263974e 100644 --- a/modules/store/schematics-core/utility/ast-utils.ts +++ b/modules/store/schematics-core/utility/ast-utils.ts @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata( return []; } + node = node[node.length - 1]; + const effectsModule = nodeArray.find( node => (node.getText().includes('EffectsModule.forRoot') && @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata( } } - node = node[node.length - 1]; - let toInsert: string; let position = node.getEnd(); if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) { diff --git a/modules/store/schematics-core/utility/project.ts b/modules/store/schematics-core/utility/project.ts index c9aeda34a8..4d397a6589 100644 --- a/modules/store/schematics-core/utility/project.ts +++ b/modules/store/schematics-core/utility/project.ts @@ -1,17 +1,29 @@ import { getWorkspace } from './config'; import { Tree } from '@angular-devkit/schematics'; -export function getProjectPath( +export interface WorkspaceProject { + root: string; + projectType: string; +} + +export function getProject( host: Tree, options: { project?: string | undefined; path?: string | undefined } -) { +): WorkspaceProject { const workspace = getWorkspace(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } - const project = workspace.projects[options.project]; + return workspace.projects[options.project]; +} + +export function getProjectPath( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); if (project.root.substr(-1) === '/') { project.root = project.root.substr(0, project.root.length - 1); @@ -26,3 +38,12 @@ export function getProjectPath( return options.path; } + +export function isLib( + host: Tree, + options: { project?: string | undefined; path?: string | undefined } +) { + const project = getProject(host, options); + + return project.projectType !== 'application'; +}