Skip to content

Commit b91eb0f

Browse files
authored
refactor: use Angular CLI utility functions for parsing the project (#122)
1 parent 758184b commit b91eb0f

File tree

9 files changed

+45
-147
lines changed

9 files changed

+45
-147
lines changed

src/add-ns/index.ts

-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export default function (options: MigrationOptions): Rule {
3535
validateOptions(options),
3636

3737
getProjectSettings(options.project),
38-
validateProjectSettings,
3938

4039
addNativeScriptSchematics,
4140

@@ -70,17 +69,6 @@ const validateOptions = (options: MigrationOptions) => () => {
7069
}
7170
};
7271

73-
/**
74-
* This schematic should only be used with ng/cli v6+
75-
*/
76-
const validateProjectSettings = (_tree: Tree) => {
77-
const cliVer = projectSettings.ngCliSemVer;
78-
79-
if (cliVer.major < 6) {
80-
throw new SchematicsException(`@angular/cli ${cliVer.toString()} version detected. Upgrade to 6.0 or newer.`);
81-
}
82-
}
83-
8472
const getProjectSettings = (projectName: string) => (tree: Tree, context: SchematicContext) => {
8573
context.logger.info('Reading Project Settings');
8674
projectSettings = getAngularProjectSettings(tree, projectName);

src/add-ns/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Schema {
99
webExtension: string;
1010
/**
1111
* The name of the project to be converted to a code sharing structure
12+
* The default option is the smart default 'projectName' provided by the Angular CLI.
1213
*/
1314
project: string;
1415
/**

src/angular-project-parser.ts

+38-63
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import * as ts from 'typescript';
22
import { join, dirname, basename } from 'path';
33
import { Tree, SchematicsException } from '@angular-devkit/schematics';
4-
// import { getWorkspace, WorkspaceProject } from '@schematics/angular/utility/config';
4+
import { getWorkspace } from '@schematics/angular/utility/config';
5+
import { getProjectTargets } from '@schematics/angular/utility/project-targets';
56

6-
import { getSourceFile, getAngularJson, safeGet } from './utils';
7+
import { getSourceFile, safeGet } from './utils';
78
import { findNode, getFunctionParams, findImportPath } from './ast-utils';
8-
import { SemVer, getAngularSemver, getAngularCLISemver } from './node-utils';
99

1010
export interface AngularProjectSettings {
11-
/** ng cli Npm Version */
12-
ngCliSemVer: SemVer;
13-
/** ng Npm Version */
14-
ngSemVer: SemVer;
15-
1611
/** default: '' */
1712
root: string;
1813

@@ -57,9 +52,6 @@ export interface CoreProjectSettings {
5752
mainPath: string;
5853
prefix: string;
5954
tsConfig: string;
60-
61-
ngCliSemVer: SemVer;
62-
ngSemVer: SemVer;
6355
}
6456

6557
/**
@@ -90,7 +82,7 @@ export interface ClassMetadata {
9082
path: string
9183
}
9284

93-
export function getAngularProjectSettings(tree: Tree, projectName: string = ''): AngularProjectSettings {
85+
export function getAngularProjectSettings(tree: Tree, projectName: string): AngularProjectSettings {
9486
const projectSettings = getCoreProjectSettings(tree, projectName);
9587
const entryModule = getEntryModuleMetadata(tree, projectSettings.mainPath);
9688

@@ -114,64 +106,47 @@ export function getAngularProjectSettings(tree: Tree, projectName: string = ''):
114106
};
115107
}
116108

117-
// Step 1 - get appRoot => open .angular-cli.json -> get apps.root
118109
export function getCoreProjectSettings(tree: Tree, projectName: string): CoreProjectSettings {
119-
const ngCliSemVer = getAngularCLISemver(tree);
120-
const ngSemVer = getAngularSemver(tree);
121-
122-
if (ngCliSemVer.major >= 6) {
123-
const project = getProjectObject(tree, projectName);
124-
125-
const root = project.root || '';
126-
const sourceRoot: string = project.sourceRoot || 'src';
127-
const mainPath: string =
128-
safeGet(project, 'targets', 'build', 'options', 'main') || // Angular CLI 6.2
129-
safeGet(project, 'architect', 'build', 'options', 'main') || // Angular CLI 6.1
130-
'src/main.ts';
131-
const mainName: string = basename(mainPath).replace('.ts', '');
132-
const prefix: string = project.prefix || 'app';
133-
const tsConfig: string =
134-
safeGet(project, 'targets', 'build', 'options', 'tsConfig') || // Angular CLI 6.2
135-
safeGet(project, 'architect', 'build', 'options', 'tsConfig') || // Angular CLI 6.1
136-
'src/tsconfig.app.json';
137-
138-
return {
139-
ngCliSemVer,
140-
ngSemVer,
141-
142-
root,
143-
sourceRoot,
144-
mainName,
145-
mainPath,
146-
prefix,
147-
tsConfig,
148-
};
149-
} else {
150-
throw new SchematicsException(`This schematic is not compatible with @angular/cli 1.x, use 6.x or newer`);
110+
const project = getProjectObject(tree, projectName);
111+
const targets = getProjectTargets(project);
112+
if (!targets) {
113+
throw new SchematicsException(
114+
`Failed to find build targets for project ${projectName}!`
115+
);
151116
}
152-
}
153117

154-
export function getProjectObject(tree: Tree, projectName: string) {
155-
const angularJson = getAngularJson(tree);
156-
157-
// return the requested project object
158-
if (projectName) {
159-
const project = angularJson.projects[projectName];
160-
if (!project) {
161-
throw new SchematicsException(`Couldn't find --projectName "${projectName}" in angular.json`);
162-
}
163-
164-
return project;
118+
const buildTarget = targets.build;
119+
if (!buildTarget) {
120+
throw new SchematicsException(
121+
`Failed to find build target for project ${projectName}!`
122+
);
165123
}
166124

167-
// or return the default project
168-
if (angularJson.defaultProject) {
169-
return angularJson.projects[angularJson.defaultProject];
125+
const root = project.root;
126+
const sourceRoot = project.sourceRoot || 'src';
127+
const mainPath = safeGet(buildTarget, 'options', 'main');
128+
const mainName = basename(mainPath).replace(/\.ts$/, '');
129+
const prefix = project.prefix;
130+
const tsConfig = safeGet(buildTarget, 'options', 'tsConfig');
131+
132+
return {
133+
root,
134+
sourceRoot,
135+
mainName,
136+
mainPath,
137+
prefix,
138+
tsConfig,
139+
};
140+
}
141+
142+
function getProjectObject(tree: Tree, projectName: string) {
143+
const workspace = getWorkspace(tree);
144+
const project = workspace.projects[projectName];
145+
if (!project) {
146+
throw new SchematicsException(`Couldn't find project "${projectName}" in the workspace!`);
170147
}
171148

172-
// or return the first project on the list
173-
// this is the same behaviour as in ng cli
174-
return Object.values(angularJson.projects)[0];
149+
return project;
175150
}
176151

177152
// Step 2 - get entryModule and entryModulePath => open ${sourceRoot}/${main}.ts

src/generate/component/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface Schema {
3030
path?: string;
3131
/**
3232
* The name of the project.
33+
* The default option is the smart default 'projectName' provided by the Angular CLI.
3334
*/
3435
project: string;
3536
/**

src/generate/module/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface Schema {
3434
path?: string;
3535
/**
3636
* The name of the project.
37+
* The default option is the smart default 'projectName' provided by the Angular CLI.
3738
*/
3839
project: string;
3940
/**

src/migrate-component/schema.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export interface Schema {
2323
nsext?: string;
2424
webext?: string;
2525
/**
26-
* Allows specification of the project to be updated
26+
* Allows specification of the project to be updated.
27+
* The default option is the smart default 'projectName' provided by the Angular CLI.
2728
*/
28-
project?: string
29+
project: string
2930
}

src/migrate-module/schema.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface Schema {
1010
nsext?: string;
1111
/**
1212
* Allows specification of the project to be updated
13+
* The default option is the smart default 'projectName' provided by the Angular CLI.
1314
*/
1415
project: string
1516
}

src/node-utils.ts

-66
This file was deleted.

src/utils.ts

-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ export const getNsConfig = (tree: Tree): NsConfig => {
135135
return getJsonFile<NsConfig>(tree, '/nsconfig.json');
136136
}
137137

138-
export const getAngularJson = (tree: Tree): any => {
139-
return getJsonFile<any>(tree, '/angular.json');
140-
}
141-
142138
export const getFileContents = (tree: Tree, filePath: string): string => {
143139
const buffer = tree.read(filePath) || '';
144140
return buffer.toString();

0 commit comments

Comments
 (0)