在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):gulpjs/undertaker-registry开源软件地址(OpenSource Url):https://github.com/gulpjs/undertaker-registry开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):undertaker-registryDefault registry in gulp 4. Usagevar gulp = require('gulp');
var UndertakerRegistry = require('undertaker-registry');
var registry = new UndertakerRegistry();
gulp.registry(registry); APInew UndertakerRegistry([options])Constructor for the default registry. Inherit from this constructor to build custom registries. init(taker)No-op method that receives the undertaker instance. Useful to set pre-defined tasks using the
get(taskName) => FunctionReturns the task with that name or undefined if no task is registered with that name. Useful for custom task storage. Custom registries can override this method when inheriting from this default registry. set(taskName, fn) => [Function]Adds a task to the registry. If tasks() => ObjectReturns an object listing all tasks in the registry. Necessary to override if the Custom RegistriesCustom registries are constructor functions allowing you to pre-define/share tasks or add custom functionality to your registries. A registry's prototype should define:
You should not call these functions yourself; leave that to Undertaker, so it can keep its metadata consistent. The easiest way to create a custom registry is to inherit from undertaker-registry: var util = require('util');
var DefaultRegistry = require('undertaker-registry');
function MyRegistry() {
DefaultRegistry.call(this);
}
util.inherits(MyRegistry, DefaultRegistry);
module.exports = MyRegistry; Sharing tasksTo share common tasks with all your projects, you can expose an For example you might want to share a var fs = require('fs');
var util = require('util');
var DefaultRegistry = require('undertaker-registry');
var del = require('del');
function CommonRegistry(opts) {
DefaultRegistry.call(this);
opts = opts || {};
this.buildDir = opts.buildDir || './build';
}
util.inherits(CommonRegistry, DefaultRegistry);
CommonRegistry.prototype.init = function (takerInst) {
var buildDir = this.buildDir;
var exists = fs.existsSync(buildDir);
if (exists) {
throw new Error(
'Cannot initialize common tasks. ' + buildDir + ' directory exists.'
);
}
takerInst.task('clean', function () {
return del([buildDir]);
});
};
module.exports = CommonRegistry; Then to use it in a project: var Undertaker = require('undertaker');
var CommonRegistry = require('myorg-common-tasks');
var taker = new Undertaker(CommonRegistry({ buildDir: '/dist' }));
taker.task(
'build',
taker.series('clean', function build(cb) {
// do things
cb();
})
); Sharing FunctionalitiesBy controlling how tasks are added to the registry, you can decorate them. For example if you wanted all tasks to share some data, you can use a custom registry to bind them to that data. Be sure to return the altered task, as per the description of registry methods above: var util = require('util');
var Undertaker = require('undertaker');
var DefaultRegistry = require('undertaker-registry');
// Some task defined somewhere else
var BuildRegistry = require('./build.js');
var ServeRegistry = require('./serve.js');
function ConfigRegistry(config) {
DefaultRegistry.call(this);
this.config = config;
}
util.inherits(ConfigRegistry, DefaultRegistry);
ConfigRegistry.prototype.set = function set(name, fn) {
// The `DefaultRegistry` uses `this._tasks` for storage.
var task = (this._tasks[name] = fn.bind(this.config));
return task;
};
var taker = new Undertaker();
taker.registry(new BuildRegistry());
taker.registry(new ServeRegistry());
// `taker.registry` will reset each task in the registry with
// `ConfigRegistry.prototype.set` which will bind them to the config object.
taker.registry(
new ConfigRegistry({
src: './src',
build: './build',
bindTo: '0.0.0.0:8888',
})
);
taker.task(
'default',
taker.series('clean', 'build', 'serve', function (cb) {
console.log('Server bind to ' + this.bindTo);
console.log('Serving' + this.build);
cb();
})
); LicenseMIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论