@sanpjs/cmd-init
Version:
Sanp init command
66 lines • 2.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const utils_1 = require("@sanpjs/utils");
const prompts_1 = __importDefault(require("prompts"));
exports.default = (template, dest, options) => {
return async (ctx, task) => {
task.info('Start checking target directory status');
if (fs_extra_1.default.existsSync(dest)) {
// 如果强制带--force,那就删了这个目录
if (options.force) {
task.info('--force delete target directory');
fs_extra_1.default.remove(dest);
// 如果是当前目录下建
}
else if (options.isCurrentDir) {
task.info(); // 添加这一句下面才能显示 prompt
// 来一个疑问句,问是否确定在当前目录创建?
const { ok } = await (0, prompts_1.default)([
{
name: 'ok',
type: 'confirm',
message: 'Are you sure to create in the current directory?'
}
]);
// 如果不愿意,那么此流程停止
if (!ok) {
return;
}
}
else {
task.info();
// 取一个相对目录
const shortDest = path_1.default.relative(process.cwd(), dest);
// 处理对于已经存在的目录
const { action } = await (0, prompts_1.default)([
{
name: 'action',
type: 'select',
message: `The directory ${utils_1.color.cyan(shortDest)} already exists. Please select an operation:`,
choices: [
{ title: 'overwrite', value: 'overwrite' },
{ title: 'merge', value: 'merge' },
{ title: 'cancel', value: '' }
]
}
]);
// 选了取消
if (!action) {
return task.error(`Cancel overwrite ${shortDest} directory`);
// 选了覆盖
}
else if (action === 'overwrite') {
task.info(`Overwrite selected, first delete ${shortDest}...`);
await fs_extra_1.default.remove(dest);
}
}
}
task.complete();
};
};
//# sourceMappingURL=check.js.map