@eljs/create
Version:
Create a project from a remote template.
178 lines (176 loc) • 6.38 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/core/create.ts
var create_exports = {};
__export(create_exports, {
Create: () => Create
});
module.exports = __toCommonJS(create_exports);
var import_utils = require("../utils");
var import_utils2 = require("@eljs/utils");
var import_node_path = __toESM(require("node:path"));
var import_download = require("./download");
var import_runner = require("./runner");
var debug = (0, import_utils2.createDebugger)("create:class");
var Create = class {
/**
* 构造函数选项
*/
constructorOptions;
/**
* 当前工作目录
*/
cwd;
/**
* 模版
*/
template;
/**
* 模版根路径
*/
_templateRootPath;
/**
* 是否为本地模版
*/
_isLocal = false;
constructor(options) {
const { cwd = process.cwd(), template } = options;
this.constructorOptions = options;
this.cwd = cwd;
this.template = template;
}
/**
* 运行创建流程
* @param projectName 项目名称
*/
async run(projectName) {
try {
const targetDir = import_node_path.default.resolve(this.cwd, projectName);
debug == null ? void 0 : debug(`targetDir:`, targetDir);
debug == null ? void 0 : debug(`projectName:`, projectName);
if (await (0, import_utils2.isPathExists)(targetDir) && !this.constructorOptions.merge) {
if (this.constructorOptions.force) {
await (0, import_utils2.remove)(targetDir);
} else {
import_utils2.logger.clear();
const { action } = await (0, import_utils2.prompts)([
{
name: "action",
type: "select",
message: `Target directory ${import_utils2.chalk.cyan(targetDir)} already exists, pick an action:`,
choices: [
{ title: "Overwrite", value: "overwrite" },
{ title: "Merge", value: "merge" },
{ title: "Cancel", value: false }
]
}
]);
if (!action) {
return;
} else if (action === "overwrite") {
import_utils2.logger.event(`Removing ${import_utils2.chalk.cyan(targetDir)} ...`);
await (0, import_utils2.remove)(targetDir);
}
}
}
await (0, import_utils2.mkdir)(targetDir);
await this._resolveTemplate();
debug == null ? void 0 : debug(`templateRootPath`, this._templateRootPath);
const configFile = await (0, import_utils2.tryPaths)([
(0, import_node_path.join)(this._templateRootPath, "create.config.ts"),
(0, import_node_path.join)(this._templateRootPath, "create.config.js")
]);
const generatorFile = await (0, import_utils2.tryPaths)([
(0, import_node_path.join)(this._templateRootPath, "generators/index.ts"),
(0, import_node_path.join)(this._templateRootPath, "generators/index.js")
]);
if (!generatorFile && !configFile) {
throw new import_utils.AppError(
`Invalid template ${import_utils2.chalk.cyan(this._templateRootPath)}, missing \`create.config.ts\` or \`generators/index.ts\`.`
);
}
const runner = new import_runner.Runner({
cwd: this._templateRootPath,
plugins: generatorFile ? [generatorFile] : []
});
await runner.run(targetDir, projectName);
} finally {
if (!this._isLocal && await (0, import_utils2.isPathExists)(this._templateRootPath)) {
await (0, import_utils2.remove)(this._templateRootPath);
}
}
}
/**
* 解析模版
*/
async _resolveTemplate() {
if ((0, import_utils2.isString)(this.template)) {
if (this.template.startsWith(".") || this.template.startsWith("/")) {
const templateRootPath = (0, import_node_path.join)(this.cwd, this.template);
if (!await (0, import_utils2.isDirectory)(templateRootPath)) {
throw new import_utils.AppError(
`Invalid local template ${import_utils2.chalk.cyan(this.template)}.`
);
}
this._templateRootPath = templateRootPath;
this._isLocal = true;
return;
}
try {
const cwd = import_utils2.resolve.sync(this.template, {
basedir: this.cwd
});
this._templateRootPath = await (0, import_utils2.findUp)(
async (directory) => {
const exist = await (0, import_utils2.isPathExists)(
import_node_path.default.join(directory, "package.json")
);
if (exist) {
return directory;
}
return;
},
{ cwd, type: "directory" }
);
this._isLocal = true;
return;
} catch (_) {
this.template = {
type: "npm",
value: this.template
};
}
}
const download = new import_download.Download(this.template);
this._templateRootPath = await download.download();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Create
});