我使用类使我的代码更易于管理,但是我收到一条错误消息:
TypeError:无法读取未定义的属性“设置”
当我来执行一系列任务时,就像这样:
exports.push = series(git.remote, git.add, git.commit, git.push);
在我的 gulpfile.js 中,我创建了该类的一个实例,并传入了其他两个包含相关数据的类的实例。
gulpfile.js
/**
* Clear Console
*/
process.stdout.write('\033c');
/**
* Require plugins
*/
const chalk = require('chalk');
const log = require('fancy-log');
const { series } = require('gulp');
/**
* Require includes
*/
const settings = require('./includes/settings.js');
const theme = require('./includes/theme.js');
const git = require('./includes/git.js')(theme, settings);
/**
* Main tasks
*/
exports.default = (cb) => {
log(chalk.bgRed('Please run a task, a list has been provided below.'));
// dump out tasks here
cb();
};
exports.push = series(git.remote, git.add, git.commit, git.push);
exports.get = git.get;
/**
* Task descriptions
* @type {string}
*/
exports.push.description = 'Push the modified theme files to the repository, or create a repository if it doesn\'t exist.';
exports.get.description = 'Pull down a theme\'s repository, ensure to add the --theme flag and the theme name.';
下面是引起问题的代码部分,我猜对了gulp是否没有使用Git
类的实例而是创建了一个新实例,是否正确?如果可以,我该如何解决?
git.js
class Git
{
constructor(theme, settings) {
this.theme = theme;
this.settings = settings;
this.init();
}
remote(done) {
git.addRemote('origin', this.settings.get('git_url') + this.theme.folder + '.git', function (err) {
if (err) {
if (err.message.indexOf('remote origin already exists' > -1)) {
log(chalk.bgYellow.black('Remote already exists, skipping...'));
} else {
console.log(err);
}
}
});
done();
}
在进一步检查中,当通过gulp系列运行此方法时,似乎没有任何访问构造函数属性的权限。
或者,如果任务在 gulpfile.js 顶部的初始设置之前运行?这似乎不太可能,因为节点将首先需要文件。