[英]my yeoman generator won't copy files
I am making a web app using yeoman and I'm stuck in creating a generator. 我正在使用yeoman制作一个Web应用程序,但我一直无法创建发电机。 The problem is that it won't copy files to the output folder. 问题在于它不会将文件复制到输出文件夹。
Here's my code: 这是我的代码:
'use strict';
var fs = require('fs');
var path = require('path');
var yeoman = require('yeoman-generator');
var yosay = require('yosay');
var chalk = require('chalk');
var wiredep = require('wiredep');
module.exports=yeoman.extend({
scaffoldFolders: function(){
this.mkdir("app");
this.mkdir("app/css");
this.mkdir("app/sections");
this.mkdir("build");
},
initializing: function(){
this.pkg=require('../../package.json');
},
prompting: function() {
var done = this.async();
this.log(yosay(
'Welcome to the marvelous ' + chalk.red('generator-palmyra') + ' generator!'
));
var prompts = [{
type: 'checkbox',
name: 'mainframeworks',
message:'Would you like AngularJS or JQuery ?',
choices: [{
name: 'Angular',
value: 'includeAngular',
checked: true
}, {
name: 'JQuery',
value: 'includeJQuery',
checked: true
}]
},
{
type: 'checkbox',
name: 'features',
message:'What more front-end frameworks would you like ?',
choices: [{
name: 'Sass',
value: 'includeSass',
checked: true
}, {
name: 'Bootstrap',
value: 'includeBootstrap',
checked: true
}, {
name: 'Modernizr',
value: 'includeModernizr',
checked: true
}]
}
];
this.prompt(prompts, function (answers) {
var features = answers.features;
var mainframeworks = answers.mainframeworks;
var hasFeature = function (feat) {
return features.indexOf(feat) !== -1;
};
var hasMainframeworks = function (mainframework) {
return mainframeworks.indexOf(mainframework) !== -1;
};
// manually deal with the response, get back and store the results.
this.includeSass = hasFeature('includeSass');
this.includeBootstrap = hasFeature('includeBootstrap');
this.includeModernizr = hasFeature('includeModernizr');
this.includeAngular = hasMainframeworks('includeAngular');
this.includeJQuery = hasMainframeworks('includeJQuery');
done();
}.bind(this));
},
writing() {
gulpfile= function(){
this.fs.copy(
this.templatePath('gulpfile.js'),
this.destinationPath('gulpfile.js')
);
},
packageJSON= function () {
this.fs.copy(
this.templatePath('_package.json'),
this.destinationPath('package.json')
);
},
git= function () {
this.fs.copy(
this.templatePath('gitignore'),
this.destinationPath('.gitignore')
);
this.fs.copy(
this.templatePath('gitattributes'),
this.destinationPath('.gitattributes')
);
},
bower= function () {
var bower = {
name: this._.slugify(this.appname),
private: true,
dependencies: {}
};
if (this.includeBootstrap) {
var bs = 'bootstrap' + (this.includeSass ? '-sass' : '');
bower.dependencies[bs] = '~3.3.1';
}
if (this.includeModernizr) {
bower.dependencies.modernizr = '~2.8.1';
}
if (this.includeAngular) {
bower.dependencies.angular = '~1.3.15';
}
if (this.includeJQuery) {
bower.dependencies.jquery = '~2.1.1';
}
this.fs.copy(
this.templatePath('bowerrc'),
this.destinationPath('.bowerrc')
);
this.write('bower.json', JSON.stringify(bower, null, 2));
},
jshint= function () {
this.fs.copy(
this.templatePath('jshintrc'),
this.destinationPath('.jshintrc')
);
},
mainStylesheet= function () {
var css = 'main';
if (this.includeSass) {
css += '.scss';
} else {
css += '.css';
}
this.copy(css, 'app/styles/' + css);
},
writeIndex= function () {
this.indexFile = this.src.read('index.html');
this.indexFile = this.engine(this.indexFile, this);
// wire Bootstrap plugins
if (this.includeBootstrap) {
var bs = '/bower_components/';
if (this.includeSass) {
bs += 'bootstrap-sass/assets/javascripts/bootstrap/';
} else {
bs += 'bootstrap/js/';
}
this.indexFile = this.appendScripts(this.indexFile, 'scripts/plugins.js', [
bs + 'affix.js',
bs + 'alert.js',
bs + 'dropdown.js',
bs + 'tooltip.js',
bs + 'modal.js',
bs + 'transition.js',
bs + 'button.js',
bs + 'popover.js',
bs + 'carousel.js',
bs + 'scrollspy.js',
bs + 'collapse.js',
bs + 'tab.js'
]);
}
this.indexFile = this.appendFiles({
html: this.indexFile,
fileType: 'js',
optimizedPath: 'scripts/main.js',
sourceFileList: ['scripts/main.js']
});
this.write('app/index.html', this.indexFile);
},
app= function () {
this.copy('main.js', 'app/scripts/main.js');
}
},
install: function () {
var howToInstall =
'\nAfter running ' +
chalk.yellow.bold('npm install & bower install') +
', inject your' +
'\nfront end dependencies by running ' +
chalk.yellow.bold('gulp wiredep') +
'.';
if (this.options['skip-install']) {
this.log(howToInstall);
return;
}
this.installDependencies();
this.on('end', function () {
var bowerJson = this.dest.readJSON('bower.json');
// wire Bower packages to .html
wiredep({
bowerJson: bowerJson,
directory: 'bower_components',
exclude: ['bootstrap-sass', 'bootstrap.js'],
ignorePath: /^(\.\.\/)*\.\./,
src: 'app/index.html'
});
if (this.includeSass) {
// wire Bower packages to .scss
wiredep({
bowerJson: bowerJson,
directory: 'bower_components',
ignorePath: /^(\.\.\/)+/,
src: 'app/styles/*.scss'
});
}
}.bind(this));
}
});
I think the problem is in the writing method. 我认为问题出在写法上。 I also wanted to ask where to go next? 我还想问下下一步去哪里? Or did I pass a fundamental step toward learning web dev 还是我迈出了学习网络开发人员的第一步
if you format your code, you'll see the writing
function isn't doing anything. 如果格式化代码,您会发现writing
功能没有做任何事情。 It declares a bunch of sub-functions, but doesn't run anyone. 它声明了很多子功能,但没有运行任何人。
I think the issue is you want an object but instead wrote a function: writing() {}
instead of writing: {}
. 我认为问题是您想要一个对象,而是编写了一个函数: writing() {}
而不是writing: {}
。
I did a quick fix of the code, but didn't test it out. 我对代码做了快速修复,但没有对其进行测试。 So there might be further syntax issue, but it should roughly look like this: 因此可能存在进一步的语法问题,但大致应如下所示:
'use strict'; var fs = require('fs'); var path = require('path'); var yeoman = require('yeoman-generator'); var yosay = require('yosay'); var chalk = require('chalk'); var wiredep = require('wiredep'); module.exports = yeoman.extend({ scaffoldFolders: function() { this.mkdir('app'); this.mkdir('app/css'); this.mkdir('app/sections'); this.mkdir('build'); }, initializing: function() { this.pkg = require('../../package.json'); }, prompting: function() { var done = this.async(); this.log( yosay( 'Welcome to the marvelous ' + chalk.red('generator-palmyra') + ' generator!' ) ); var prompts = [ { type: 'checkbox', name: 'mainframeworks', message: 'Would you like AngularJS or JQuery ?', choices: [ { name: 'Angular', value: 'includeAngular', checked: true, }, { name: 'JQuery', value: 'includeJQuery', checked: true, }, ], }, { type: 'checkbox', name: 'features', message: 'What more front-end frameworks would you like ?', choices: [ { name: 'Sass', value: 'includeSass', checked: true, }, { name: 'Bootstrap', value: 'includeBootstrap', checked: true, }, { name: 'Modernizr', value: 'includeModernizr', checked: true, }, ], }, ]; this.prompt( prompts, function(answers) { var features = answers.features; var mainframeworks = answers.mainframeworks; var hasFeature = function(feat) { return features.indexOf(feat) !== -1; }; var hasMainframeworks = function(mainframework) { return mainframeworks.indexOf(mainframework) !== -1; }; // manually deal with the response, get back and store the results. this.includeSass = hasFeature('includeSass'); this.includeBootstrap = hasFeature('includeBootstrap'); this.includeModernizr = hasFeature('includeModernizr'); this.includeAngular = hasMainframeworks('includeAngular'); this.includeJQuery = hasMainframeworks('includeJQuery'); done(); }.bind(this) ); }, writing: { gulpfile: function() { this.fs.copy( this.templatePath('gulpfile.js'), this.destinationPath('gulpfile.js') ); }, packageJSON: function() { this.fs.copy( this.templatePath('_package.json'), this.destinationPath('package.json') ); }, git: function() { this.fs.copy( this.templatePath('gitignore'), this.destinationPath('.gitignore') ); this.fs.copy( this.templatePath('gitattributes'), this.destinationPath('.gitattributes') ); }, bower: function() { var bower = { name: this._.slugify(this.appname), private: true, dependencies: {}, }; if (this.includeBootstrap) { var bs = 'bootstrap' + (this.includeSass ? '-sass' : ''); bower.dependencies[bs] = '~3.3.1'; } if (this.includeModernizr) { bower.dependencies.modernizr = '~2.8.1'; } if (this.includeAngular) { bower.dependencies.angular = '~1.3.15'; } if (this.includeJQuery) { bower.dependencies.jquery = '~2.1.1'; } this.fs.copy(this.templatePath('bowerrc'), this.destinationPath('.bowerrc')); this.write('bower.json', JSON.stringify(bower, null, 2)); }, jshint: function() { this.fs.copy( this.templatePath('jshintrc'), this.destinationPath('.jshintrc') ); }, mainStylesheet: function() { var css = 'main'; if (this.includeSass) { css += '.scss'; } else { css += '.css'; } this.copy(css, 'app/styles/' + css); }, writeIndex: function() { this.indexFile = this.src.read('index.html'); this.indexFile = this.engine(this.indexFile, this); // wire Bootstrap plugins if (this.includeBootstrap) { var bs = '/bower_components/'; if (this.includeSass) { bs += 'bootstrap-sass/assets/javascripts/bootstrap/'; } else { bs += 'bootstrap/js/'; } this.indexFile = this.appendScripts( this.indexFile, 'scripts/plugins.js', [ bs + 'affix.js', bs + 'alert.js', bs + 'dropdown.js', bs + 'tooltip.js', bs + 'modal.js', bs + 'transition.js', bs + 'button.js', bs + 'popover.js', bs + 'carousel.js', bs + 'scrollspy.js', bs + 'collapse.js', bs + 'tab.js', ] ); } this.indexFile = this.appendFiles({ html: this.indexFile, fileType: 'js', optimizedPath: 'scripts/main.js', sourceFileList: ['scripts/main.js'], }); this.write('app/index.html', this.indexFile); }, app: function() { this.copy('main.js', 'app/scripts/main.js'); }, }, install: function() { var howToInstall = '\\nAfter running ' + chalk.yellow.bold('npm install & bower install') + ', inject your' + '\\nfront end dependencies by running ' + chalk.yellow.bold('gulp wiredep') + '.'; if (this.options['skip-install']) { this.log(howToInstall); return; } this.installDependencies(); this.on( 'end', function() { var bowerJson = this.dest.readJSON('bower.json'); // wire Bower packages to .html wiredep({ bowerJson: bowerJson, directory: 'bower_components', exclude: ['bootstrap-sass', 'bootstrap.js'], ignorePath: /^(\\.\\.\\/)*\\.\\./, src: 'app/index.html', }); if (this.includeSass) { // wire Bower packages to .scss wiredep({ bowerJson: bowerJson, directory: 'bower_components', ignorePath: /^(\\.\\.\\/)+/, src: 'app/styles/*.scss', }); } }.bind(this) ); }, });
我删除了var done = async()并将“ this.prompt”替换为“ return this.prompt”,大多数文件已被复制...但是仍然存在更多无效代码::
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.