@sanpjs/core
Version:
@sanpjs/core
78 lines • 2.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const createContext_1 = __importDefault(require("./createContext"));
class Sanp {
_BuilderClass;
_BundlerClass;
_runArgs;
builder;
bundler;
context;
running;
callback;
constructor({ Builder, Bundler }) {
this._BuilderClass = Builder || require('@sanpjs/builder');
this._BundlerClass = Bundler || require('@sanpjs/bundler-webpack');
this.running = false;
this._runArgs = null;
this.context = {};
}
applyPlugin() {
const plugins = this.context.plugins;
if (Array.isArray(plugins)) {
plugins.forEach(plugin => {
if (typeof plugin === 'function') {
plugin.call(this.context, this.context);
}
else {
plugin.apply(this.context);
}
});
}
}
async run(args, callback) {
this.callback = callback;
if (this.running) {
return callback && callback('Sanp is running');
}
this._runArgs = args;
this.running = true;
await this._createContext(args);
let firstRun = true;
this.builder?.run(args, async (err) => {
if (err) {
return callback && callback(err);
}
if (firstRun) {
firstRun = false;
this.bundler?.run(args, callback);
}
});
}
async _createContext(args) {
// 加载配置
this.context = await (0, createContext_1.default)(args);
this.context.sanp = this;
this.applyPlugin();
this.builder = new this._BuilderClass(this.context);
this.bundler = new this._BundlerClass(this.context);
}
// 重启
async reboot() {
if (this.running === true) {
await this.close();
}
return await this.run(this._runArgs, this.callback);
}
async close() {
this.running = false;
this.builder && (await this.builder.close());
this.bundler && (await this.bundler.close());
}
}
exports.default = Sanp;
;
//# sourceMappingURL=Sanp.js.map