2022-10-14 17:07:31 +08:00

65 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/**
* @author zhaoyingbo
* @desc 初始化 canary 项目的文件以及配置
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.initProject = void 0;
const path = require("path");
const fs = require("fs");
const const_1 = require("./const");
/**
* 创建配置文件
* @param existDir 配置文件夹地址
* @returns
*/
function createConfigFile(existDir) {
// 在根目录创建配置文件
const configDir = path.resolve(process.cwd(), `./${const_1.CANARY_CONFIG_FILE}`);
// 如果已经有配置文件就不要动了
if (fs.existsSync(configDir))
return;
const config = Object.assign({}, const_1.PROJECT_CONFIG.defaultConfig);
// 有配置文件夹
if (existDir && fs.existsSync(existDir)) {
config.canaryDir = existDir;
}
// 创建新的配置文件
fs.writeFile(configDir, JSON.stringify(config, null, 2), err => err && console.log(err));
}
/**
* 创建中文语言示例文件
*/
function createCnFile() {
// 中文语言文件夹地址
const cnDir = `${const_1.PROJECT_CONFIG.dir}/zh-CN`;
// 没有则创建
if (!fs.existsSync(cnDir)) {
fs.mkdirSync(cnDir);
fs.writeFile(`${cnDir}/index.ts`, const_1.PROJECT_CONFIG.zhIndexFile, err => err && console.log(err));
fs.writeFile(`${cnDir}/common.ts`, const_1.PROJECT_CONFIG.zhTestFile, err => err && console.log(err));
}
}
/**
* 初始化国际化项目
* @param existDir 多语言文件夹地址
*/
function initProject(existDir) {
// 有用户输入的文件夹,不存在默认位置创建
if (existDir && !fs.existsSync(existDir)) {
console.log('\n输入的目录不存在已为你生成默认文件夹');
fs.mkdirSync(const_1.PROJECT_CONFIG.dir);
}
// 没有输入,默认位置创建
else if (!existDir && !fs.existsSync(const_1.PROJECT_CONFIG.dir)) {
fs.mkdirSync(const_1.PROJECT_CONFIG.dir);
}
// 创建配置文件
createConfigFile(existDir);
// 没有已经存在的多语言文件夹,创建默认的中文语言示例文件
if (!(existDir && fs.existsSync(existDir))) {
createCnFile();
}
}
exports.initProject = initProject;
//# sourceMappingURL=init.js.map