@eljs/utils
Version:
Collection of nodejs utility.
180 lines (178 loc) • 5.49 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/generator/base-generator.ts
var base_generator_exports = {};
__export(base_generator_exports, {
BaseGenerator: () => BaseGenerator
});
module.exports = __toCommonJS(base_generator_exports);
var import_cli = require("../cli");
var import_file = require("../file");
var import_logger = require("../logger");
var import_type = require("../type");
var import_chalk = __toESM(require("chalk"));
var import_node_fs = require("node:fs");
var import_node_os = require("node:os");
var import_prompts = __toESM(require("prompts"));
var TARGET_DIR_WHITE_LIST = [".git", "LICENSE"];
var BaseGenerator = class {
/**
* 目标文件基准路径
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
basedir;
/**
* 问询结果
*/
prompts;
/**
* 模版渲染选项
*/
renderTemplateOptions;
_basedir = "";
constructor(basedir, renderTemplateOptions) {
this.basedir = basedir;
this.prompts = {};
this.renderTemplateOptions = renderTemplateOptions;
}
async run() {
const questions = this.prompting();
this.prompts = await (0, import_prompts.default)(questions);
if ((0, import_type.isFunction)(this.basedir)) {
this._basedir = this.basedir(this.prompts);
} else {
this._basedir = this.basedir;
}
await this.writing();
}
prompting() {
return [];
}
async writing() {
}
/**
* 拷贝文件
* @param from 源文件路径
* @param to 目标文件路径
* @param options 选项
*/
copyFileSync(from, to, options = {}) {
(0, import_file.copyFileSync)(from, to, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 拷贝文件
* @param from 源文件路径
* @param to 目标文件路径
* @param options 选项
*/
async copyFile(from, to, options = {}) {
await (0, import_file.copyFile)(from, to, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 拷贝模版
* @param from 源文件路径
* @param to 目标文件路径
* @param data 模版数据
* @param options 选项
*/
copyTplSync(from, to, data, options = {}) {
(0, import_file.copyTplSync)(from, to, data, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 拷贝模版
* @param from 源文件路径
* @param to 目标文件路径
* @param data 模版数据
* @param options 选项
*/
async copyTpl(from, to, data, options = {}) {
await (0, import_file.copyTpl)(from, to, data, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 拷贝文件夹
* @param from 源文件路径
* @param to 目标文件路径
* @param options 选项
*/
copyDirectorySync(from, to, data, options = {}) {
(0, import_file.copyDirectorySync)(from, to, data, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 拷贝文件夹
* @param from 源文件路径
* @param to 目标文件路径
* @param options 选项
*/
async copyDirectory(from, to, data, options = {}) {
await (0, import_file.copyDirectory)(from, to, data, {
...options,
renderOptions: this.renderTemplateOptions,
basedir: this._basedir
});
}
/**
* 检查文件夹
* @param targetDir 目标路径
*/
checkDir(targetDir) {
const files = (0, import_node_fs.readdirSync)(targetDir).filter(
(file) => !TARGET_DIR_WHITE_LIST.includes(file)
);
if (files.length) {
import_logger.logger.warn(`当前文件夹 ${import_chalk.default.cyan(targetDir)} 存在如下文件:${import_node_os.EOL}`);
files.forEach((file) => console.log(" - " + file));
console.log();
return (0, import_cli.confirm)(`确定要覆盖当前文件夹吗?`, true);
}
return true;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BaseGenerator
});