@falconix/ymcli
Version:
A CLI tool for creating and building multi-platform Vue projects
103 lines (89 loc) • 2.84 kB
JavaScript
const path = require("path");
const inquirer = require("inquirer").default;
const fs = require("fs-extra");
const {
pathExists,
createDirectory,
createGitignore,
createRootVscodeConfig,
createVueElementProject,
createVueVantProject,
} = require("../utils");
/**
* 创建多平台项目
* @param {string} projectName 项目名称
*/
async function createProject(projectName) {
const projectPath = path.resolve(process.cwd(), projectName);
if (await pathExists(projectPath)) {
console.error(`Error: Project "${projectName}" already exists.`);
process.exit(1);
}
try {
const answers = await inquirer.prompt([
{
type: "input",
name: "appId",
message: "请输入app唯一标识 (appId):",
validate: (value) => {
if (value.trim()) return true;
return "appId不能为空";
},
},
{
type: "input",
name: "name",
message: "请输入app名称:",
validate: (value) => {
if (value.trim()) return true;
return "app名称不能为空";
},
},
]);
console.log(`Creating project directory: ${projectPath}`);
await createDirectory(projectPath);
const appJsonContent = {
appId: answers.appId,
name: answers.name,
icon: "",
permissions: [],
webEntrys: [],
pcEntrys: [],
mobileEntrys: [],
};
await fs.writeFile(
path.join(projectPath, "app.json"),
JSON.stringify(appJsonContent, null, 2),
"utf8"
);
console.log("Created app.json configuration file");
await createRootVscodeConfig(projectPath);
const platforms = ["web", "mobile"];
for (const platform of platforms) {
const platformPath = path.join(projectPath, platform);
await createDirectory(platformPath);
}
// 创建 .gitignore 文件
await createGitignore(projectPath);
const gitignorePath = path.join(projectPath, ".gitignore");
let gitignoreContent = await fs.readFile(gitignorePath, "utf8");
if (!gitignoreContent.includes("dist/")) {
gitignoreContent += "\ndist/";
await fs.writeFile(gitignorePath, gitignoreContent, "utf8");
}
// 创建 Web 项目
console.log("Creating Web project (Vue + Element Plus)...");
await createVueElementProject(path.join(projectPath, "web"));
// 创建 Mobile 项目
console.log("Creating Mobile project (Vue + Vant)...");
await createVueVantProject(path.join(projectPath, "mobile"));
console.log(`Successfully created project "${projectName}"`);
console.log("To get started:");
console.log(` cd ${projectName}`);
console.log(" ymcli build [platform]");
} catch (error) {
console.error("Error creating project:", error);
process.exit(1);
}
}
module.exports = createProject;