Skip to content

refactor(@angular/cli): update generate command module file resolution #5467

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 1 commit into from
Mar 30, 2017
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
21 changes: 8 additions & 13 deletions packages/@angular/cli/blueprints/component/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as chalk from 'chalk';
import * as fs from 'fs';
import * as path from 'path';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { getAppFromConfig } from '../../utilities/app-utils';
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
import { resolveModulePath } from '../../utilities/resolve-module-file';

import * as fs from 'fs';
import * as path from 'path';
import * as chalk from 'chalk';
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const findParentModule = require('../../utilities/find-parent-module').default;
const getFiles = Blueprint.prototype.files;
Expand Down Expand Up @@ -107,14 +108,8 @@ export default Blueprint.extend({
beforeInstall: function (options: any) {
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
} else {
try {
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
Expand All @@ -135,7 +130,7 @@ export default Blueprint.extend({
const defaultPrefix = (appConfig && appConfig.prefix) || '';

let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
? '' : (this.options.prefix || defaultPrefix);
? '' : (this.options.prefix || defaultPrefix);
prefix = prefix && `${prefix}-`;

this.selector = stringUtils.dasherize(prefix + parsedPath.name);
Expand Down
40 changes: 17 additions & 23 deletions packages/@angular/cli/blueprints/directive/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NodeHost} from '../../lib/ast-tools';
import {CliConfig} from '../../models/config';
import {getAppFromConfig} from '../../utilities/app-utils';
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
import * as chalk from 'chalk';
import * as path from 'path';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { getAppFromConfig } from '../../utilities/app-utils';
import { resolveModulePath } from '../../utilities/resolve-module-file';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';

const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const findParentModule = require('../../utilities/find-parent-module').default;
Expand Down Expand Up @@ -58,17 +58,11 @@ export default Blueprint.extend({
}
],

beforeInstall: function(options: any) {
beforeInstall: function (options: any) {
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw ' ';
}
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
} else {
try {
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
Expand All @@ -89,7 +83,7 @@ export default Blueprint.extend({
const defaultPrefix = (appConfig && appConfig.prefix) || '';

let prefix = (this.options.prefix === 'false' || this.options.prefix === '')
? '' : (this.options.prefix || defaultPrefix);
? '' : (this.options.prefix || defaultPrefix);
prefix = prefix && `${prefix}-`;


Expand All @@ -111,7 +105,7 @@ export default Blueprint.extend({
};
},

files: function() {
files: function () {
let fileList = getFiles.call(this) as Array<string>;

if (this.options && !this.options.spec) {
Expand All @@ -135,7 +129,7 @@ export default Blueprint.extend({
};
},

afterInstall: function(options: any) {
afterInstall: function (options: any) {
const returns: Array<any> = [];
const className = stringUtils.classify(`${options.entity.name}Directive`);
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);
Expand All @@ -161,9 +155,9 @@ export default Blueprint.extend({
}
return result;
}));
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
}

return Promise.all(returns);
Expand Down
28 changes: 11 additions & 17 deletions packages/@angular/cli/blueprints/guard/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as chalk from 'chalk';
import * as path from 'path';
import { oneLine } from 'common-tags';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
import { getAppFromConfig } from '../../utilities/app-utils';
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
import { resolveModulePath } from '../../utilities/resolve-module-file';

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
Expand Down Expand Up @@ -35,17 +35,11 @@ export default Blueprint.extend({
}
],

beforeInstall: function(options: any) {
const appConfig = getAppFromConfig(this.options.app);
beforeInstall: function (options: any) {
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
const appConfig = getAppFromConfig(this.options.app);
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
}
},

Expand All @@ -70,7 +64,7 @@ export default Blueprint.extend({
};
},

files: function() {
files: function () {
let fileList = getFiles.call(this) as Array<string>;

if (this.options && !this.options.spec) {
Expand Down Expand Up @@ -114,8 +108,8 @@ export default Blueprint.extend({
astUtils.addProviderToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost)));
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
'update',
path.relative(this.project.root, this.pathToModule));
}

return Promise.all(returns);
Expand Down
26 changes: 10 additions & 16 deletions packages/@angular/cli/blueprints/pipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NodeHost} from '../../lib/ast-tools';
import {CliConfig} from '../../models/config';
import {getAppFromConfig} from '../../utilities/app-utils';
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
import * as chalk from 'chalk';
import * as path from 'path';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
import { getAppFromConfig } from '../../utilities/app-utils';
import { resolveModulePath } from '../../utilities/resolve-module-file';

const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const findParentModule = require('../../utilities/find-parent-module').default;
Expand Down Expand Up @@ -56,14 +56,8 @@ export default Blueprint.extend({
beforeInstall: function(options: any) {
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
} else {
try {
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
Expand Down
32 changes: 13 additions & 19 deletions packages/@angular/cli/blueprints/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {NodeHost} from '../../lib/ast-tools';
import {CliConfig} from '../../models/config';
import {getAppFromConfig} from '../../utilities/app-utils';
import {dynamicPathParser} from '../../utilities/dynamic-path-parser';
import * as chalk from 'chalk';
import * as path from 'path';
import { oneLine } from 'common-tags';
import { NodeHost } from '../../lib/ast-tools';
import { CliConfig } from '../../models/config';
import { dynamicPathParser } from '../../utilities/dynamic-path-parser';
import { getAppFromConfig } from '../../utilities/app-utils';
import { resolveModulePath } from '../../utilities/resolve-module-file';

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
Expand Down Expand Up @@ -40,17 +40,11 @@ export default Blueprint.extend({
}
],

beforeInstall: function(options: any) {
beforeInstall: function (options: any) {
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

if (!fs.existsSync(this.pathToModule)) {
throw 'Module specified does not exist';
}
this.pathToModule =
resolveModulePath(options.module, this.project, this.project.root, appConfig);
}
},

Expand All @@ -75,7 +69,7 @@ export default Blueprint.extend({
};
},

files: function() {
files: function () {
let fileList = getFiles.call(this) as Array<string>;

if (this.options && !this.options.spec) {
Expand Down Expand Up @@ -119,8 +113,8 @@ export default Blueprint.extend({
astUtils.addProviderToModule(this.pathToModule, className, importPath)
.then((change: any) => change.apply(NodeHost)));
this._writeStatusToUI(chalk.yellow,
'update',
path.relative(this.project.root, this.pathToModule));
'update',
path.relative(this.project.root, this.pathToModule));
}

return Promise.all(returns);
Expand Down
46 changes: 46 additions & 0 deletions packages/@angular/cli/utilities/resolve-module-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as path from 'path';
import * as fs from 'fs';
import { dynamicPathParser } from './dynamic-path-parser';

export function resolveModulePath(
moduleNameFromFlag: string, project: any, projectRoot: any, appConfig: any): string {
let baseModuleName = moduleNameFromFlag;
let parentFolders = '';

if (baseModuleName.includes(path.sep)) {
const splitPath = baseModuleName.split(path.sep);
baseModuleName = splitPath.pop();
parentFolders = splitPath.join(path.sep);
}

if (baseModuleName.includes('.')) {
baseModuleName = baseModuleName
.split('.')
.filter(part => part !== 'module' && part !== 'ts')
.join('.');
}

const baseModuleWithFileSuffix = `${baseModuleName}.module.ts`;

const moduleRelativePath = path.join(parentFolders, baseModuleWithFileSuffix);
let fullModulePath = buildFullPath(project, moduleRelativePath, appConfig, projectRoot);

if (!fs.existsSync(fullModulePath)) {
const moduleWithFolderPrefix =
path.join(parentFolders, baseModuleName, baseModuleWithFileSuffix);
fullModulePath = buildFullPath(project, moduleWithFolderPrefix, appConfig, projectRoot);
}

if (!fs.existsSync(fullModulePath)) {
throw 'Specified module does not exist';
}

return fullModulePath;
}

function buildFullPath(project: any, relativeModulePath: string, appConfig: any, projectRoot: any) {
const parsedPath = dynamicPathParser(project, relativeModulePath, appConfig);
const fullModulePath = path.join(projectRoot, parsedPath.dir, parsedPath.base);

return fullModulePath;
}
Loading