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 4a0b580 commit 58cb872
Show file tree
Hide file tree
Showing 25 changed files with 218 additions and 45 deletions.
2 changes: 1 addition & 1 deletion modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions modules/effects/schematics-core/utility/project.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 === 'library';
}
2 changes: 1 addition & 1 deletion modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions modules/entity/schematics-core/utility/project.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 === 'library';
}
2 changes: 1 addition & 1 deletion modules/router-store/schematics-core/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions modules/router-store/schematics-core/utility/project.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 === 'library';
}
2 changes: 1 addition & 1 deletion modules/schematics-core/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions modules/schematics-core/utility/project.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 === 'library';
}
2 changes: 1 addition & 1 deletion modules/schematics/schematics-core/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions modules/schematics/schematics-core/utility/project.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 === 'library';
}
Loading

0 comments on commit 58cb872

Please sign in to comment.