在 yeoman 生成器中获取项目基本路径
Getting the project base path in a yeoman generator
我已将 yeoman 生成器依赖项从 0.18.10 更新到 0.20.3。
我已将已弃用的 this.dest
更新为 this.destinationRoot()
我现在在获取项目的基本路径时遇到生成器问题,因此我可以将文件从一个位置复制到另一个位置。
我创建了一个将路径放在一起的函数,然后将其传递给另一个函数,该函数排除了一些文件被复制。
这是我遇到错误的函数
// Copy Bower files to another directory
var copyBowerFiles = function (component, to, exclude) {
var base = this.destinationRoot(),
publicDir = base + '/' + this.publicDir,
publicAssetsDir = publicDir + '/assets',
bowerComponentsDir = publicAssetsDir + '/bower_components',
bower,
from;
to = (base + '/' + to || publicAssetsDir);
from = bowerComponentsDir + '/' + component;
//this.dest.copy(from, to);
this.bulkDirectory(from, copyDestPathPartial.call(this, to, exclude));
};
这在结束函数中被调用:
end: function () {
this.installDependencies({
callback: function () {
copyBowerFiles.call('jam', this.publicDir, excludeJamFiles);
}.bind(this)
});
}
我收到错误消息:
var base = this.destinationRoot(),
^
TypeError: undefined is not a function
我也试过 sourceRoot()
我想更新我的生成器以使用最新版本的生成器。任何帮助使这项工作的帮助都会很棒。
还有调用函数的时候第一个参数还要传this
吗?
编辑:
这是 copyDestPathPartial 函数
// Copy destination path partial
var copyDestPathPartial = function (to, exclude) {
exclude = exclude || [];
return function (abs, root, sub, file) {
if (!_.contains(exclude, file) && ! _.contains(exclude, sub)) {
this.copy(abs, to + '/' + (sub || '') + '/' + file);
}
}.bind(this.destinationRoot());
};
当我在 copyBowerFiles 函数中使用 this
时,我收到另一条错误消息,指出当我调用此函数时:
throw new TypeError('Arguments to path.resolve must be strings');
copyDestPathPartial
函数不是输出字符串吗?
这只是一个JavaScript错误,copyBowerFiles
里面的this
并不是你想的那样。
根据您编写的代码,this
等于 jam
。
所以您需要:copyBowerFiles.call(this, 'jam', this.publicDir, excludeJamFiles);
。作为调用的第一个参数是 this
值。请参阅文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
话虽这么说,分配随机 this
值非常脏,而且很难维护。为什么不使 copyBowerFiles
成为原型方法?
我已将 yeoman 生成器依赖项从 0.18.10 更新到 0.20.3。
我已将已弃用的 this.dest
更新为 this.destinationRoot()
我现在在获取项目的基本路径时遇到生成器问题,因此我可以将文件从一个位置复制到另一个位置。 我创建了一个将路径放在一起的函数,然后将其传递给另一个函数,该函数排除了一些文件被复制。
这是我遇到错误的函数
// Copy Bower files to another directory
var copyBowerFiles = function (component, to, exclude) {
var base = this.destinationRoot(),
publicDir = base + '/' + this.publicDir,
publicAssetsDir = publicDir + '/assets',
bowerComponentsDir = publicAssetsDir + '/bower_components',
bower,
from;
to = (base + '/' + to || publicAssetsDir);
from = bowerComponentsDir + '/' + component;
//this.dest.copy(from, to);
this.bulkDirectory(from, copyDestPathPartial.call(this, to, exclude));
};
这在结束函数中被调用:
end: function () {
this.installDependencies({
callback: function () {
copyBowerFiles.call('jam', this.publicDir, excludeJamFiles);
}.bind(this)
});
}
我收到错误消息:
var base = this.destinationRoot(),
^
TypeError: undefined is not a function
我也试过 sourceRoot()
我想更新我的生成器以使用最新版本的生成器。任何帮助使这项工作的帮助都会很棒。
还有调用函数的时候第一个参数还要传this
吗?
编辑: 这是 copyDestPathPartial 函数
// Copy destination path partial
var copyDestPathPartial = function (to, exclude) {
exclude = exclude || [];
return function (abs, root, sub, file) {
if (!_.contains(exclude, file) && ! _.contains(exclude, sub)) {
this.copy(abs, to + '/' + (sub || '') + '/' + file);
}
}.bind(this.destinationRoot());
};
当我在 copyBowerFiles 函数中使用 this
时,我收到另一条错误消息,指出当我调用此函数时:
throw new TypeError('Arguments to path.resolve must be strings');
copyDestPathPartial
函数不是输出字符串吗?
这只是一个JavaScript错误,copyBowerFiles
里面的this
并不是你想的那样。
根据您编写的代码,this
等于 jam
。
所以您需要:copyBowerFiles.call(this, 'jam', this.publicDir, excludeJamFiles);
。作为调用的第一个参数是 this
值。请参阅文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
话虽这么说,分配随机 this
值非常脏,而且很难维护。为什么不使 copyBowerFiles
成为原型方法?