@ky-cli/create
Version: 
ky-cli 创建项目包
105 lines (98 loc) • 3.04 kB
JavaScript
const path = require("path");
const fsExtra = require("fs-extra");
const deepmerge = require("deepmerge");
const log = require("@ky-cli/log");
const basicConfig = {
	type: "simple",
	"main-project-name": "main",
	"vice-router-path": "packages/main/src/router/blocks/vice.js",
	"vice-config-path": "packages/main",
	entryDir: "packages",
	"main-home-path": "../../views/home"
};
class SetSubProject {
	constructor(project) {
		const configPath = path.resolve(process.cwd(), "ky.config.json");
		fsExtra.ensureFileSync(configPath);
		try {
			this.kyConfig = deepmerge(
				basicConfig,
				fsExtra.readJsonSync(configPath) || {}
			);
		} catch (e) {
			this.kyConfig = deepmerge(basicConfig, {});
		}
		this.childProjectList = fs
			.readdirSync(path.resolve(process.cwd(), this.kyConfig.entryDir))
			.filter(e => e !== this.kyConfig["main-project-name"]);
	}
	getChildMess() {
		const result = [];
		this.childProjectList.forEach(item => {
			const filePath = path.resolve(
				path.resolve(
					process.cwd(),
					this.kyConfig.entryDir,
					item,
					"vue.config.js"
				)
			);
			if (!fsExtra.pathExistsSync(filePath)) return false;
			const fileContent = fs.readFileSync(filePath, "utf8");
			const idx = fileContent.lastIndexOf('port: ');
			if (idx === -1) return false;
			result.push({
				name: item,
				port: fileContent.slice(idx + 7, idx + 11)
			});
		});
		return result;
	}
	setRouteContent() {
		const routerFile = path.resolve(
			process.cwd(),
			this.kyConfig["vice-router-path"]
		);
		if (!fsExtra.pathExistsSync(routerFile)) return;
		if (
			!fsExtra.pathExistsSync(
				path.resolve(routerFile, "../" + this.kyConfig["main-home-path"])
			)
		) {
			log.error("main-home-path路径错误", "配置的路径在项目中不存在!");
			Promise.reject(new Error("配置的路径在项目中不存在!"));
		}
		const messList = this.getChildMess();
		const routerContent = messList.reduce((total, item, index) => {
			const template = `{
    path: '/vice${item.name}/*',
    name: 'vice${item.name}',
    component: () => import(/* webpackChunkName: "home" */ '${this.kyConfig["main-home-path"]}')
}`;
			return (
				total +
				template +
				(index === this.childProjectList.length - 1 ? "" : ",")
			);
		}, "export default [");
		fsExtra.outputFileSync(routerFile, routerContent + "];");
	}
	setAppsContent() {
		const configFile = path.resolve(
			process.cwd(),
			this.kyConfig["vice-config-path"]
		);
		if (!fsExtra.pathExistsSync(configFile)) return;
		const messList = this.getChildMess();
		const routerContent = messList.reduce(
			(total, item, index) => {
				const template = `{ name: 'vice${item.name}', entry: isProduction ? '/vice${item.name}/' : '//localhost:${item.port}', activeRule: '/vice${item.name}/' }`;
				return total + template + (index === messList.length - 1 ? "" : ",");
			},
			`const isProduction = process.env.NODE_ENV === 'production';
export default [`
		);
		fsExtra.outputFileSync(configFile, routerContent + "];");
	}
}
module.exports = SetSubProject;