UNPKG

create-scripts

Version:

A multi-tool script management utility that supports package.json scripts management for Git, SVN, and NPM.

212 lines (211 loc) 10.2 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const readline = __importStar(require("readline")); const child_process_1 = require("child_process"); // 创建命令行交互接口 const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); // 问题询问函数 const askQuestion = (question, defaultValue) => { const fullQuestion = defaultValue ? `${question} (default: ${defaultValue}): ` : `${question}: `; return new Promise((resolve) => { rl.question(fullQuestion, (answer) => { resolve(answer.trim() || defaultValue || ''); }); }); }; // 执行命令并处理错误 const executeCommand = (command, successMessage, errorMessage = '') => { try { (0, child_process_1.execSync)(command, { stdio: 'inherit' }); console.log(`\x1b[32m${successMessage}\x1b[0m`); return true; } catch (error) { console.error(`\x1b[31m${errorMessage || `Command failed: ${command}`}\x1b[0m`); return false; } }; // Git 管理功能 const gitManagement = async () => { console.log('\x1b[34m=== Git Management ===\x1b[0m'); console.log('Easily add Git scripts to your package.json for common Git workflows.'); console.log('These scripts will help you quickly commit and push your changes.'); console.log(''); const action = await askQuestion('Select Git action:\n 1. Add "git" script (prompts for commit message, then adds, commits, and pushes)\n 2. Add "git:all" script (same as option 1)\n 3. Both scripts\nEnter your choice', '3'); try { const packageJsonPath = './package.json'; const packageJsonData = fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }); const packageJson = JSON.parse(packageJsonData); if (!packageJson.scripts) { packageJson.scripts = {}; } const isWindows = process.platform === 'win32'; if (action === '1' || action === '3') { const gitCommand = isWindows ? 'powershell -Command "$msg = Read-Host \'Enter commit message\'; git add .; git commit -m \\\"$msg\\\"; git push"' : 'read -p \'Enter commit message: \' msg && git add . && git commit -m "$msg" && git push origin main'; packageJson.scripts['git'] = gitCommand; console.log(`\x1b[32mAdded "git" script: ${gitCommand}\x1b[0m`); } if (action === '2' || action === '3') { const gitAllCommand = isWindows ? 'powershell -Command "$msg = Read-Host \'Enter commit message\'; git add .; git commit -m \\\"$msg\\\"; git push"' : 'read -p \'Enter commit message: \' msg && git add . && git commit -m "$msg" && git push origin main'; packageJson.scripts['git:all'] = gitAllCommand; console.log(`\x1b[32mAdded "git:all" script: ${gitAllCommand}\x1b[0m`); } fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' }); console.log('\x1b[32mGit scripts added successfully! You can now run "npm run git" or "npm run git:all"\x1b[0m'); } catch (error) { console.error('\x1b[31mFailed to update package.json for Git management\x1b[0m'); console.error(error); } }; // SVN 管理功能 const svnManagement = async () => { console.log('\x1b[34m=== SVN Management ===\x1b[0m'); console.log('Easily add SVN scripts to your package.json for common SVN workflows.'); console.log('These scripts will help you quickly commit and push your changes with SVN.'); console.log(''); const action = await askQuestion('Select SVN action:\n 1. Add "svn" script (commits changes with default message)\n 2. Add "svn:all" script (updates, commits, and pushes changes)\n 3. Both scripts\nEnter your choice', '3'); try { const packageJsonPath = './package.json'; const packageJsonData = fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }); const packageJson = JSON.parse(packageJsonData); if (!packageJson.scripts) { packageJson.scripts = {}; } if (action === '1' || action === '3') { const svnCommand = 'svn commit -m "Update"'; packageJson.scripts['svn'] = svnCommand; console.log(`\x1b[32mAdded "svn" script: ${svnCommand}\x1b[0m`); } if (action === '2' || action === '3') { const svnAllCommand = 'svn update && svn commit -m "Update" && svn push'; packageJson.scripts['svn:all'] = svnAllCommand; console.log(`\x1b[32mAdded "svn:all" script: ${svnAllCommand}\x1b[0m`); } fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' }); console.log('\x1b[32mSVN scripts added successfully! You can now run "npm run svn" or "npm run svn:all"\x1b[0m'); } catch (error) { console.error('\x1b[31mFailed to update package.json for SVN management\x1b[0m'); console.error(error); } }; // NPM 发布管理功能 const npmPublishManagement = async () => { console.log('\x1b[34m=== NPM Publish Management ===\x1b[0m'); console.log('Easily add NPM publish scripts to your package.json for package publishing.'); console.log('These scripts will help you publish your package to NPM with proper preparation.'); console.log(''); const action = await askQuestion('Select NPM action:\n 1. Add "publish" script (publishes package to NPM)\n 2. Add "prepublishOnly" script (runs build before publishing)\n 3. Both scripts\nEnter your choice', '3'); try { const packageJsonPath = './package.json'; const packageJsonData = fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }); const packageJson = JSON.parse(packageJsonData); if (!packageJson.scripts) { packageJson.scripts = {}; } if (action === '1' || action === '3') { const publishCommand = 'npm publish'; packageJson.scripts['publish'] = publishCommand; console.log(`\x1b[32mAdded "publish" script: ${publishCommand}\x1b[0m`); } if (action === '2' || action === '3') { const prepublishCommand = 'npm run build'; packageJson.scripts['prepublishOnly'] = prepublishCommand; console.log(`\x1b[32mAdded "prepublishOnly" script: ${prepublishCommand}\x1b[0m`); } fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' }); console.log('\x1b[32mNPM publish scripts added successfully! You can now run "npm run publish" or "npm run prepublishOnly"\x1b[0m'); } catch (error) { console.error('\x1b[31mFailed to update package.json for NPM publish management\x1b[0m'); console.error(error); } }; // 显示主菜单 const showMainMenu = async () => { console.log('\n\x1b[34m=== Create Scripts Management Tool ===\x1b[0m'); console.log('Easily add useful scripts to your package.json file.'); console.log(''); console.log('Available options:'); console.log(' 1. Git Management - Add scripts for Git workflows'); console.log(' 2. SVN Management - Add scripts for SVN workflows'); console.log(' 3. NPM Publish Management - Add scripts for NPM publishing'); console.log(' 4. Exit - Exit the tool'); console.log(''); return await askQuestion('Please select an option (1-4)'); }; // 主函数 const main = async () => { console.log('\x1b[34mWelcome to Create Scripts - A multi-tool script manager\x1b[0m\n'); console.log('This tool helps you quickly add common scripts to your package.json file.'); console.log('You can add Git, SVN, or NPM scripts to simplify your development workflow.'); console.log(''); // 检查 package.json 是否存在 if (!fs.existsSync('./package.json')) { console.log('\x1b[31mError: package.json not found.\x1b[0m'); console.log('\x1b[31mPlease run this command in a Node.js project directory that contains a package.json file.\x1b[0m'); process.exit(1); } let continueLoop = true; while (continueLoop) { const choice = await showMainMenu(); switch (choice) { case '1': await gitManagement(); break; case '2': await svnManagement(); break; case '3': await npmPublishManagement(); break; case '4': console.log('\x1b[32mThank you for using Create Scripts! Goodbye!\x1b[0m'); continueLoop = false; break; default: console.log('\x1b[31mInvalid option, please try again. Please enter a number between 1 and 4.\x1b[0m'); } } rl.close(); }; // 启动程序 main().catch((error) => { console.error('\x1b[31mProgram error occurred\x1b[0m'); console.error(error); rl.close(); process.exit(1); });