@eljs/utils
Version:
Collection of nodejs utility.
126 lines (124 loc) • 3.32 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/generator/generator.ts
var generator_exports = {};
__export(generator_exports, {
Generator: () => Generator
});
module.exports = __toCommonJS(generator_exports);
var import_file = require("../file");
var import_type = require("../type");
var import_base_generator = require("./base-generator");
var Generator = class extends import_base_generator.BaseGenerator {
/**
* 模版文件夹路径
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
src;
/**
* 目标文件夹路径
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dest;
/**
* 问询列表
*/
questions;
/**
* 模版渲染数据
*/
data;
/**
* 模版写入完成回调函数
*/
onGeneratorDone;
_dest = "";
_src = "";
_data = {};
constructor({
src,
dest,
basedir,
questions,
data,
renderTemplateOptions,
onGeneratorDone
}) {
super(basedir || dest, renderTemplateOptions);
this.src = src;
this.dest = dest;
this.data = data || {};
this.questions = questions || [];
this.onGeneratorDone = onGeneratorDone;
}
async run() {
await super.run();
if (this.onGeneratorDone) {
this.onGeneratorDone({
src: this._src,
dest: this._dest,
data: this._data
});
}
}
prompting() {
return this.questions || [];
}
async writing() {
if ((0, import_type.isFunction)(this.dest)) {
this._dest = this.dest(this.prompts);
} else {
this._dest = this.dest;
}
if (!(0, import_file.isPathExistsSync)(this._dest)) {
(0, import_file.mkdirSync)(this._dest);
} else {
const overwrite = await this.checkDir(this._dest);
if (!overwrite) {
return;
}
}
if ((0, import_type.isFunction)(this.src)) {
this._src = this.src(this.prompts);
} else {
this._src = this.src;
}
if ((0, import_type.isFunction)(this.data)) {
this._data = this.data(this.prompts);
} else {
this._data = this.data;
}
const data = {
...this.prompts,
...this._data
};
if ((0, import_file.isDirectorySync)(this._src)) {
await this.copyDirectory(this._src, this._dest, data);
} else {
if (this._src.endsWith(".tpl")) {
await this.copyTpl(this._src, this._dest, data);
} else {
await this.copyFile(this._src, this._dest, data);
}
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Generator
});