UNPKG

noshift.js

Version:
182 lines (149 loc) โ€ข 4.85 kB
import { execSync } from "child_process"; import fs from "fs/promises"; import path from "path"; import { fileURLToPath } from "url"; import inquirer from "inquirer"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const DEFAULT_BUILD_DIR = "build"; export default async function create(projectNameArg) { const cwd = process.cwd(); // ๐ŸŒ ่จ€่ชž้ธๆŠž const { lang } = await inquirer.prompt([ { type: "list", name: "lang", message: "Select language / ่จ€่ชžใ‚’้ธใ‚“ใงใใ ใ•ใ„", choices: [ { name: "English", value: "en" }, { name: "ๆ—ฅๆœฌ่ชž", value: "ja" }, ], }, ]); const t = (key) => { const messages = { projectName: { en: "Enter project name:", ja: "ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆๅใ‚’ๅ…ฅๅŠ›ใ—ใฆใใ ใ•ใ„:", }, creatingDir: { en: "๐Ÿ“ Creating project directory:", ja: "๐Ÿ“ ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ไฝœๆˆ:", }, initializingNpm: { en: "๐Ÿ“ฆ Initializing npm...", ja: "๐Ÿ“ฆ npm ๅˆๆœŸๅŒ–ไธญ...", }, creatingConfig: { en: "โš™๏ธ Creating nsjs.config.js", ja: "โš™๏ธ nsjs.config.js ใ‚’ไฝœๆˆ", }, usePrettier: { en: "Do you want to format compiled code with Prettier?", ja: "ใ‚ณใƒณใƒ‘ใ‚คใƒซๅพŒใฎใ‚ณใƒผใƒ‰ใ‚’ Prettier ใงๆ•ดๅฝขใ—ใพใ™ใ‹๏ผŸ", }, installingPrettier: { en: "๐Ÿ“ฆ Installing Prettier...", ja: "๐Ÿ“ฆ Prettier ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซไธญ...", }, installingNoshift: { en: "๐Ÿ“ฆ Installing noshift.js...", ja: "๐Ÿ“ฆ noshift.js ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซไธญ...", }, success: { en: "๐ŸŽ‰ Project created successfully!", ja: "๐ŸŽ‰ ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆไฝœๆˆใŒๅฎŒไบ†ใ—ใพใ—ใŸ!", }, nextSteps: { en: "๐Ÿ‘‰ cd {name} && npm run dev", ja: "๐Ÿ‘‰ cd {name} && npm run dev", }, }; return messages[key][lang]; }; // ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆๅๅ–ๅพ— let projectName = projectNameArg; if (!projectName) { const answer = await inquirer.prompt([ { type: "input", name: "projectName", message: t("projectName"), default: "my-noshift-app", }, ]); projectName = answer.projectName; } const projectPath = path.join(cwd, projectName); await fs.mkdir(projectPath, { recursive: true }); console.log(`\n${t("creatingDir")} ${projectPath}`); process.chdir(projectPath); console.log(t("initializingNpm")); execSync("npm init -y", { stdio: "inherit" }); // package.json ใซ scripts.dev / scripts.build ใ‚’่ฟฝๅŠ  const pkgPath = path.join(projectPath, "package.json"); const pkgRaw = await fs.readFile(pkgPath, "utf-8"); const pkg = JSON.parse(pkgRaw); pkg.scripts.dev = "node ./node_modules/noshift.js/commands/dev.js"; pkg.scripts.build = "node ./node_modules/noshift.js/commands/build.js"; await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2)); // nsjs.config.js ใ‚’ไฝœๆˆ const configContent = `export default { build: "", // ็ฉบใชใ‚‰ ./build ใซใƒ“ใƒซใƒ‰ใ•ใ‚Œใพใ™ }; `; await fs.writeFile("nsjs.config.js", configContent); console.log(t("creatingConfig")); // Prettier ใฎไฝฟ็”จ const { usePrettier } = await inquirer.prompt([ { type: "confirm", name: "usePrettier", message: t("usePrettier"), default: true, }, ]); if (usePrettier) { console.log(t("installingPrettier")); execSync("npm install --save-dev prettier", { stdio: "inherit" }); const prettierIgnore = `build/\nnode_modules/\n`; await fs.writeFile(".prettierignore", prettierIgnore); const prettierConfig = `{ "semi": true, "singleQuote": false, "trailingComma": "es5" }`; await fs.writeFile(".prettierrc", prettierConfig); } console.log(t("installingNoshift")); execSync("npm install noshift.js", { stdio: "inherit" }); await fs.mkdir("src", { recursive: true }); await fs.writeFile("src/index.nsjs", "console.log^8^2Hello World!^2^9;"); // README.md const readme = lang === "ja" ? `# ${projectName} ใ“ใ‚Œใฏ [noshift.js](https://github.com/otoneko1102/NoShift.js) ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ ## ้–‹็™บ \`\`\`bash npm run dev \`\`\` ## ใƒ“ใƒซใƒ‰ \`\`\`bash npm run build \`\`\` ` : `# ${projectName} This is a [NoShift.js](https://github.com/otoneko1102/NoShift.js) project. ## Development \`\`\`bash npm run dev \`\`\` ## Build \`\`\`bash npm run build \`\`\` `; await fs.writeFile("README.md", readme); console.log(`\n${t("success")}`); console.log("\n" + t("nextSteps").replace("{name}", projectName)); }