Skip to content

refactor: use Angular CLI utility functions for parsing the project #122

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

Merged
merged 4 commits into from
Sep 26, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/add-ns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export default function (options: MigrationOptions): Rule {
validateOptions(options),

getProjectSettings(options.project),
validateProjectSettings,

addNativeScriptSchematics,

Expand Down Expand Up @@ -70,17 +69,6 @@ const validateOptions = (options: MigrationOptions) => () => {
}
};

/**
* This schematic should only be used with ng/cli v6+
*/
const validateProjectSettings = (_tree: Tree) => {
const cliVer = projectSettings.ngCliSemVer;

if (cliVer.major < 6) {
throw new SchematicsException(`@angular/cli ${cliVer.toString()} version detected. Upgrade to 6.0 or newer.`);
}
}

const getProjectSettings = (projectName: string) => (tree: Tree, context: SchematicContext) => {
context.logger.info('Reading Project Settings');
projectSettings = getAngularProjectSettings(tree, projectName);
Expand Down
1 change: 1 addition & 0 deletions src/add-ns/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Schema {
webExtension: string;
/**
* The name of the project to be converted to a code sharing structure
* The default option is the smart default 'projectName' provided by the Angular CLI.
*/
project: string;
/**
Expand Down
101 changes: 38 additions & 63 deletions src/angular-project-parser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import * as ts from 'typescript';
import { join, dirname, basename } from 'path';
import { Tree, SchematicsException } from '@angular-devkit/schematics';
// import { getWorkspace, WorkspaceProject } from '@schematics/angular/utility/config';
import { getWorkspace } from '@schematics/angular/utility/config';
import { getProjectTargets } from '@schematics/angular/utility/project-targets';

import { getSourceFile, getAngularJson, safeGet } from './utils';
import { getSourceFile, safeGet } from './utils';
import { findNode, getFunctionParams, findImportPath } from './ast-utils';
import { SemVer, getAngularSemver, getAngularCLISemver } from './node-utils';

export interface AngularProjectSettings {
/** ng cli Npm Version */
ngCliSemVer: SemVer;
/** ng Npm Version */
ngSemVer: SemVer;

/** default: '' */
root: string;

Expand Down Expand Up @@ -57,9 +52,6 @@ export interface CoreProjectSettings {
mainPath: string;
prefix: string;
tsConfig: string;

ngCliSemVer: SemVer;
ngSemVer: SemVer;
}

/**
Expand Down Expand Up @@ -90,7 +82,7 @@ export interface ClassMetadata {
path: string
}

export function getAngularProjectSettings(tree: Tree, projectName: string = ''): AngularProjectSettings {
export function getAngularProjectSettings(tree: Tree, projectName: string): AngularProjectSettings {
const projectSettings = getCoreProjectSettings(tree, projectName);
const entryModule = getEntryModuleMetadata(tree, projectSettings.mainPath);

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

// Step 1 - get appRoot => open .angular-cli.json -> get apps.root
export function getCoreProjectSettings(tree: Tree, projectName: string): CoreProjectSettings {
const ngCliSemVer = getAngularCLISemver(tree);
const ngSemVer = getAngularSemver(tree);

if (ngCliSemVer.major >= 6) {
const project = getProjectObject(tree, projectName);

const root = project.root || '';
const sourceRoot: string = project.sourceRoot || 'src';
const mainPath: string =
safeGet(project, 'targets', 'build', 'options', 'main') || // Angular CLI 6.2
safeGet(project, 'architect', 'build', 'options', 'main') || // Angular CLI 6.1
'src/main.ts';
const mainName: string = basename(mainPath).replace('.ts', '');
const prefix: string = project.prefix || 'app';
const tsConfig: string =
safeGet(project, 'targets', 'build', 'options', 'tsConfig') || // Angular CLI 6.2
safeGet(project, 'architect', 'build', 'options', 'tsConfig') || // Angular CLI 6.1
'src/tsconfig.app.json';

return {
ngCliSemVer,
ngSemVer,

root,
sourceRoot,
mainName,
mainPath,
prefix,
tsConfig,
};
} else {
throw new SchematicsException(`This schematic is not compatible with @angular/cli 1.x, use 6.x or newer`);
const project = getProjectObject(tree, projectName);
const targets = getProjectTargets(project);
if (!targets) {
throw new SchematicsException(
`Failed to find build targets for project ${projectName}!`
);
}
}

export function getProjectObject(tree: Tree, projectName: string) {
const angularJson = getAngularJson(tree);

// return the requested project object
if (projectName) {
const project = angularJson.projects[projectName];
if (!project) {
throw new SchematicsException(`Couldn't find --projectName "${projectName}" in angular.json`);
}

return project;
const buildTarget = targets.build;
if (!buildTarget) {
throw new SchematicsException(
`Failed to find build target for project ${projectName}!`
);
}

// or return the default project
if (angularJson.defaultProject) {
return angularJson.projects[angularJson.defaultProject];
const root = project.root;
const sourceRoot = project.sourceRoot || 'src';
const mainPath = safeGet(buildTarget, 'options', 'main');
const mainName = basename(mainPath).replace(/\.ts$/, '');
const prefix = project.prefix;
const tsConfig = safeGet(buildTarget, 'options', 'tsConfig');

return {
root,
sourceRoot,
mainName,
mainPath,
prefix,
tsConfig,
};
}

function getProjectObject(tree: Tree, projectName: string) {
const workspace = getWorkspace(tree);
const project = workspace.projects[projectName];
if (!project) {
throw new SchematicsException(`Couldn't find project "${projectName}" in the workspace!`);
}

// or return the first project on the list
// this is the same behaviour as in ng cli
return Object.values(angularJson.projects)[0];
return project;
}

// Step 2 - get entryModule and entryModulePath => open ${sourceRoot}/${main}.ts
Expand Down
1 change: 1 addition & 0 deletions src/generate/component/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Schema {
path?: string;
/**
* The name of the project.
* The default option is the smart default 'projectName' provided by the Angular CLI.
*/
project: string;
/**
Expand Down
1 change: 1 addition & 0 deletions src/generate/module/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Schema {
path?: string;
/**
* The name of the project.
* The default option is the smart default 'projectName' provided by the Angular CLI.
*/
project: string;
/**
Expand Down
5 changes: 3 additions & 2 deletions src/migrate-component/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export interface Schema {
nsext?: string;
webext?: string;
/**
* Allows specification of the project to be updated
* Allows specification of the project to be updated.
* The default option is the smart default 'projectName' provided by the Angular CLI.
*/
project?: string
project: string
}
1 change: 1 addition & 0 deletions src/migrate-module/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Schema {
nsext?: string;
/**
* Allows specification of the project to be updated
* The default option is the smart default 'projectName' provided by the Angular CLI.
*/
project: string
}
66 changes: 0 additions & 66 deletions src/node-utils.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ export const getNsConfig = (tree: Tree): NsConfig => {
return getJsonFile<NsConfig>(tree, '/nsconfig.json');
}

export const getAngularJson = (tree: Tree): any => {
return getJsonFile<any>(tree, '/angular.json');
}

export const getFileContents = (tree: Tree, filePath: string): string => {
const buffer = tree.read(filePath) || '';
return buffer.toString();
Expand Down