Skip to content

Commit

Permalink
fix(schematics): exclude environment imports for libraries
Browse files Browse the repository at this point in the history
Closes #1205, #1197
  • Loading branch information
brandonroberts committed Jul 29, 2018
1 parent 22284ab commit ca72f8f
Show file tree
Hide file tree
Showing 25 changed files with 176 additions and 38 deletions.
2 changes: 1 addition & 1 deletion modules/effects/schematics-core/index.ts
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions modules/effects/schematics-core/utility/ast-utils.ts
Expand Up @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata(
return [];
}

node = node[node.length - 1];

const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
Expand Down Expand Up @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata(
}
}

node = node[node.length - 1];

let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
Expand Down
20 changes: 18 additions & 2 deletions modules/effects/schematics-core/utility/project.ts
@@ -1,7 +1,7 @@
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
) {
Expand All @@ -11,7 +11,14 @@ export function getProjectPath(
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);
Expand All @@ -26,3 +33,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';
}
2 changes: 1 addition & 1 deletion modules/entity/schematics-core/index.ts
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions modules/entity/schematics-core/utility/ast-utils.ts
Expand Up @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata(
return [];
}

node = node[node.length - 1];

const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
Expand Down Expand Up @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata(
}
}

node = node[node.length - 1];

let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
Expand Down
20 changes: 18 additions & 2 deletions modules/entity/schematics-core/utility/project.ts
@@ -1,7 +1,7 @@
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
) {
Expand All @@ -11,7 +11,14 @@ export function getProjectPath(
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);
Expand All @@ -26,3 +33,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';
}
2 changes: 1 addition & 1 deletion modules/router-store/schematics-core/index.ts
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions modules/router-store/schematics-core/utility/ast-utils.ts
Expand Up @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata(
return [];
}

node = node[node.length - 1];

const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
Expand Down Expand Up @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata(
}
}

node = node[node.length - 1];

let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
Expand Down
20 changes: 18 additions & 2 deletions modules/router-store/schematics-core/utility/project.ts
@@ -1,7 +1,7 @@
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
) {
Expand All @@ -11,7 +11,14 @@ export function getProjectPath(
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);
Expand All @@ -26,3 +33,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';
}
2 changes: 1 addition & 1 deletion modules/schematics-core/index.ts
Expand Up @@ -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 = {
Expand Down
13 changes: 12 additions & 1 deletion modules/schematics-core/testing/create-workspace.ts
Expand Up @@ -26,6 +26,10 @@ const defaultModuleOptions = {
flat: false,
};

const defaultLibOptions = {
name: 'baz',
};

export function getTestProjectPath(
workspaceOptions: any = defaultWorkspaceOptions,
appOptions: any = defaultAppOptions
Expand All @@ -37,7 +41,8 @@ export function createWorkspace(
schematicRunner: SchematicTestRunner,
appTree: UnitTestTree,
workspaceOptions = defaultWorkspaceOptions,
appOptions = defaultAppOptions
appOptions = defaultAppOptions,
libOptions = defaultLibOptions
) {
appTree = schematicRunner.runExternalSchematic(
'@schematics/angular',
Expand All @@ -50,6 +55,12 @@ export function createWorkspace(
appOptions,
appTree
);
appTree = schematicRunner.runExternalSchematic(
'@schematics/angular',
'library',
libOptions,
appTree
);

return appTree;
}
4 changes: 2 additions & 2 deletions modules/schematics-core/utility/ast-utils.ts
Expand Up @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata(
return [];
}

node = node[node.length - 1];

const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
Expand Down Expand Up @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata(
}
}

node = node[node.length - 1];

let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
Expand Down
20 changes: 18 additions & 2 deletions modules/schematics-core/utility/project.ts
@@ -1,7 +1,7 @@
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
) {
Expand All @@ -11,7 +11,14 @@ export function getProjectPath(
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);
Expand All @@ -26,3 +33,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';
}
2 changes: 1 addition & 1 deletion modules/schematics/schematics-core/index.ts
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions modules/schematics/schematics-core/utility/ast-utils.ts
Expand Up @@ -352,6 +352,8 @@ function _addSymbolToNgModuleMetadata(
return [];
}

node = node[node.length - 1];

const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
Expand Down Expand Up @@ -398,8 +400,6 @@ function _addSymbolToNgModuleMetadata(
}
}

node = node[node.length - 1];

let toInsert: string;
let position = node.getEnd();
if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
Expand Down
20 changes: 18 additions & 2 deletions modules/schematics/schematics-core/utility/project.ts
@@ -1,7 +1,7 @@
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
) {
Expand All @@ -11,7 +11,14 @@ export function getProjectPath(
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);
Expand All @@ -26,3 +33,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';
}
4 changes: 2 additions & 2 deletions modules/schematics/src/store/files/__statePath__/index.ts
Expand Up @@ -5,7 +5,7 @@ import {
createSelector,
MetaReducer
} from '@ngrx/store';
import { environment } from '<%= environmentsPath %>';
<% if (!isLib) { %>import { environment } from '<%= environmentsPath %>';<% } %>

export interface <%= classify(stateInterface) %> {

Expand All @@ -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 ? [] : <% } %>[];

0 comments on commit ca72f8f

Please sign in to comment.