Skip to content
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

refactor: convert blueprints to typescript #4306

Merged
merged 1 commit into from Jan 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

module.exports = {
export default Blueprint.extend({
description: '',

availableOptions: [
{ name: 'spec', type: Boolean }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName.split('.')[0]);
normalizeEntityName: function (entityName: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shorten it even more by writing methods directly in the object

export default Blueprint.extend({
  normalizeEntityName(entityName: string) {
    ...
  }
})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be a second pass to add more type information and cleanup the code.
The goal here was to convert with minimal changes to reduce the chance of conflicts with other active PRs.

const parsedPath = dynamicPathParser(this.project, entityName.split('.')[0]);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options) {
const rawName = options.args[1];
locals: function (options: any) {
const rawName = options.args[1] as string;
const nameParts = rawName.split('.')
.filter(part => part.length !== 0);

Expand All @@ -40,7 +40,7 @@ module.exports = {
},

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

if (this.options && !this.options.spec) {
fileList = fileList.filter(p => p.indexOf('__name__.spec.ts') < 0);
Expand All @@ -61,4 +61,4 @@ module.exports = {
}
};
}
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const NodeHost = require('@angular-cli/ast-tools').NodeHost;

module.exports = {
export default Blueprint.extend({
description: '',

availableOptions: [
Expand All @@ -25,7 +25,7 @@ module.exports = {
{ name: 'export', type: Boolean, default: false }
],

beforeInstall: function(options) {
beforeInstall: function(options: any) {
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -38,27 +38,28 @@ module.exports = {
} else {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
} catch (e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
normalizeEntityName: function (entityName: string) {
const parsedPath = dynamicPathParser(this.project, entityName);

this.dynamicPath = parsedPath;

var defaultPrefix = '';
let defaultPrefix = '';
if (this.project.ngConfig &&
this.project.ngConfig.apps[0] &&
this.project.ngConfig.apps[0].prefix) {
defaultPrefix = this.project.ngConfig.apps[0].prefix;
}

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

this.selector = stringUtils.dasherize(prefix + parsedPath.name);
Expand All @@ -70,7 +71,7 @@ module.exports = {
return parsedPath.name;
},

locals: function (options) {
locals: function (options: any) {
this.styleExt = 'css';
if (this.project.ngConfig &&
this.project.ngConfig.defaults &&
Expand Down Expand Up @@ -114,7 +115,7 @@ module.exports = {
},

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

if (this.options && this.options.inlineTemplate) {
fileList = fileList.filter(p => p.indexOf('.html') < 0);
Expand All @@ -129,15 +130,15 @@ module.exports = {
return fileList;
},

fileMapTokens: function (options) {
fileMapTokens: function (options: any) {
// Return custom template variables here.
return {
__path__: () => {
var dir = this.dynamicPath.dir;
let dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
var srcDir = this.project.ngConfig.apps[0].root;
const srcDir = this.project.ngConfig.apps[0].root;
this.appDir = dir.substr(dir.indexOf(srcDir) + srcDir.length);
this.generatePath = dir;
return dir;
Expand All @@ -148,12 +149,12 @@ module.exports = {
};
},

afterInstall: function(options) {
afterInstall: function(options: any) {
if (options.dryRun) {
return;
}

const returns = [];
const returns: Array<any> = [];
const className = stringUtils.classify(`${options.entity.name}Component`);
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);
const componentDir = path.relative(path.dirname(this.pathToModule), this.generatePath);
Expand All @@ -162,17 +163,19 @@ module.exports = {
if (!options.skipImport) {
returns.push(
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
.then(change => change.apply(NodeHost))
.then((result) => {
.then((change: any) => change.apply(NodeHost))
.then((result: any) => {
if (options.export) {
return astUtils.addExportToModule(this.pathToModule, className, importPath)
.then(change => change.apply(NodeHost));
.then((change: any) => change.apply(NodeHost));
}
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);
}
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const NodeHost = require('@angular-cli/ast-tools').NodeHost;
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;

module.exports = {
export default Blueprint.extend({
description: '',

availableOptions: [
Expand All @@ -21,7 +21,7 @@ module.exports = {
{ name: 'export', type: Boolean, default: false }
],

beforeInstall: function(options) {
beforeInstall: function(options: any) {
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -34,35 +34,36 @@ module.exports = {
} else {
try {
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
} catch(e) {
} catch (e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
}
}
}
},

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
normalizeEntityName: function (entityName: string) {
const parsedPath = dynamicPathParser(this.project, entityName);

this.dynamicPath = parsedPath;

var defaultPrefix = '';
let defaultPrefix = '';
if (this.project.ngConfig &&
this.project.ngConfig.apps[0] &&
this.project.ngConfig.apps[0].prefix) {
defaultPrefix = this.project.ngConfig.apps[0].prefix;
}

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


this.selector = stringUtils.camelize(prefix + parsedPath.name);
return parsedPath.name;
},

locals: function (options) {
locals: function (options: any) {
options.spec = options.spec !== undefined ?
options.spec :
this.project.ngConfigObj.get('defaults.spec.directive');
Expand All @@ -75,7 +76,7 @@ module.exports = {
},

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

if (this.options && !this.options.spec) {
fileList = fileList.filter(p => p.indexOf('__name__.directive.spec.ts') < 0);
Expand All @@ -84,11 +85,11 @@ module.exports = {
return fileList;
},

fileMapTokens: function (options) {
fileMapTokens: function (options: any) {
// Return custom template variables here.
return {
__path__: () => {
var dir = this.dynamicPath.dir;
let dir = this.dynamicPath.dir;
if (!options.locals.flat) {
dir += path.sep + options.dasherizedModuleName;
}
Expand All @@ -98,12 +99,12 @@ module.exports = {
};
},

afterInstall: function(options) {
afterInstall: function(options: any) {
if (options.dryRun) {
return;
}

const returns = [];
const returns: Array<any> = [];
const className = stringUtils.classify(`${options.entity.name}Directive`);
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);
const fullGeneratePath = path.join(this.project.root, this.generatePath);
Expand All @@ -114,17 +115,19 @@ module.exports = {
if (!options.skipImport) {
returns.push(
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
.then(change => change.apply(NodeHost))
.then((result) => {
.then((change: any) => change.apply(NodeHost))
.then((result: any) => {
if (options.export) {
return astUtils.addExportToModule(this.pathToModule, className, importPath)
.then(change => change.apply(NodeHost));
.then((change: any) => change.apply(NodeHost));
}
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);
}
};
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const stringUtils = require('ember-cli-string-utils');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const Blueprint = require('../../ember-cli/lib/models/blueprint');

module.exports = {
export default Blueprint.extend({
description: '',

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);
normalizeEntityName: function (entityName: string) {
const parsedPath = dynamicPathParser(this.project, entityName);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options) {
locals: function (options: any) {
this.fileName = stringUtils.dasherize(options.entity.name);

return {
Expand All @@ -33,4 +34,4 @@ module.exports = {
}
};
}
};
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
const stringUtils = require('ember-cli-string-utils');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
const dynamicPathParser = require('../../utilities/dynamic-path-parser');
const Blueprint = require('../../ember-cli/lib/models/blueprint');

module.exports = {
export default Blueprint.extend({
description: '',

anonymousOptions: [
'<interface-type>'
],
normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);

normalizeEntityName: function (entityName: string) {
const parsedPath = dynamicPathParser(this.project, entityName);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options) {
var interfaceType = options.args [2]
locals: function (options: any) {
const interfaceType = options.args[2];
this.fileName = stringUtils.dasherize(options.entity.name);
if (interfaceType) {
this.fileName += '.' + interfaceType;
this.fileName += '.' + interfaceType;
}
var prefix = '';
let prefix = '';
if (this.project.ngConfig &&
this.project.ngConfig.defaults &&
this.project.ngConfig.defaults.prefixInterfaces) {
prefix = 'I';
}
return {
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat,
fileName: this.fileName,
Expand All @@ -47,4 +48,4 @@ module.exports = {
}
};
}
};
});