这是indexloc提供的服务,不要输入任何密码
Skip to content
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
41 changes: 21 additions & 20 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ function jsonEscape(str) {
return jsesc(str, {quotes: 'double'});
}

function generateName(str) {
str = _.chain(str)
.deburr()
.trim()
.replace(/\s/g, '-')
.value();
return str.toLowerCase();
}

module.exports = generators.Base.extend({
initializing: function() {
this.pkg = require('../package.json');

this.dirname = path.basename(this.destinationRoot());
this.dirnameNoJs = path.basename(this.dirname, '.js');
this.dirnameWithJs = this.dirnameNoJs + '.js';
},

prompting: function() {
Expand Down Expand Up @@ -58,7 +53,10 @@ module.exports = generators.Base.extend({
type: 'input',
name: 'repo',
message: 'What is the repository/project name?',
default: generateName(this.appname)
// This default works when the directory was generated from a cloned
// repository, or when the user intends to make a repo with the same
// name as the directory. We assume this is the most common situation.
default: this.dirname
}, {
type: 'input',
name: 'description',
Expand All @@ -73,7 +71,11 @@ module.exports = generators.Base.extend({
type: 'input',
name: 'variable',
message: 'If there is one, what is the name of this project\'s main variable?',
default: _.camelCase(this.appname)
// The directory name – without `.js` – is likely to be the name of the
// main variable. For instance:
// `hello-there.js` => `helloThere`
// `hello-there` => `helloThere`
default: _.camelCase(this.dirnameNoJs)
}];

var self = this;
Expand All @@ -82,16 +84,15 @@ module.exports = generators.Base.extend({
self.user = jsonEscape(props.user);
self.repo = jsonEscape(props.repo);

// Remove `.js` from the npm module name, per the "tips" section of the
// npm documentation: https://docs.npmjs.com/files/package.json#name
if (_.endsWith(self.repo, '.js')) {
self.moduleName = self.repo.slice(0, -3);
} else {
self.moduleName = self.repo;
}
// A good candidate for the module name is the directory, which we assume
// to be derived from the repository name, stripped of any `.js` extension.
// The extension is stripped per the "tips" section of the npm docs:
// https://docs.npmjs.com/files/package.json#name
self.moduleName = jsonEscape(self.dirnameNoJs.toLowerCase());

// The mainFile, on the other hand, must always have an extension
self.entryFileName = self.moduleName + '.js';
// The mainFile, on the other hand, must always have an extension. Once
// again we derive this from the name of the directory.
self.fileName = jsonEscape(self.dirnameWithJs.toLowerCase());

self.description = jsonEscape(props.description);
self.author = jsonEscape(props.author);
Expand Down
4 changes: 2 additions & 2 deletions app/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "<%= moduleName %>",
"version": "0.0.1",
"description": "<%- description %>",
"main": "dist/<%= entryFileName %>",
"main": "dist/<%= fileName %>",
"scripts": {
"test": "gulp",
"lint": "gulp lint",
Expand Down Expand Up @@ -55,7 +55,7 @@
"webpack-stream": "^3.1.0"
},
"babelBoilerplateOptions": {
"entryFileName": "<%= entryFileName %>",
"entryFileName": "<%= fileName %>",
"mainVarName": "<%= variable %>"
}
}