|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { exec } from 'node:child_process' // 确保导入 exec |
| 7 | +import { Command } from 'commander' |
| 8 | + |
| 9 | +const program = new Command() |
| 10 | +const rootDir = process.cwd() |
| 11 | +const gitignorePath = path.join(rootDir, '.gitignore') |
| 12 | + |
| 13 | +const configFileName = 'deploy.config.js' |
| 14 | +const gitignoreFileName = 'deploy.config' |
| 15 | + |
| 16 | +program |
| 17 | + .name('deploy') |
| 18 | + .description('A simple deployment tool') |
| 19 | + .version('1.0.0') |
| 20 | + |
| 21 | +// 定义 init 命令 |
| 22 | +program.command('init').description('Initialize the deployment configuration').action(() => { |
| 23 | + const configPath = path.join(rootDir, configFileName) |
| 24 | + |
| 25 | + if (!fs.existsSync(configPath)) { |
| 26 | + const configContent = `// Deployment configuration |
| 27 | +export default { |
| 28 | + host: '192.xxx', |
| 29 | + port: 10022, |
| 30 | + username: 'xxx', |
| 31 | + password: 'xxx', |
| 32 | + wwwPath: '/usr/xxx/xxx', |
| 33 | + rootDir: '/dist' |
| 34 | +};\n` |
| 35 | + |
| 36 | + fs.writeFile(configPath, configContent, (err) => { |
| 37 | + if (err) { |
| 38 | + console.error(`Error creating ${configFileName}:`, err) |
| 39 | + } |
| 40 | + else { |
| 41 | + console.log(`${configFileName} created successfully!`) |
| 42 | + addToGitignore() |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | + else { |
| 47 | + console.log(`${configFileName} already exists.`) |
| 48 | + addToGitignore() |
| 49 | + } |
| 50 | +}) |
| 51 | + |
| 52 | +if (process.argv.length === 2) { |
| 53 | + exec('npm run start', (error, stdout, stderr) => { |
| 54 | + if (error) { |
| 55 | + console.error(`Error executing script: ${error.message}`) |
| 56 | + return |
| 57 | + } |
| 58 | + if (stderr) { |
| 59 | + console.error(`Script error: ${stderr}`) |
| 60 | + return |
| 61 | + } |
| 62 | + console.log(stdout) |
| 63 | + }) |
| 64 | +} |
| 65 | +else { |
| 66 | + program.parse(process.argv) |
| 67 | +} |
| 68 | + |
| 69 | +function addToGitignore() { |
| 70 | + if (fs.existsSync(gitignorePath)) { |
| 71 | + const gitignoreContent = fs.readFileSync(gitignorePath, 'utf-8') |
| 72 | + if (!gitignoreContent.includes(gitignoreFileName)) { |
| 73 | + fs.appendFile(gitignorePath, `\n${gitignoreFileName}.*`, (err) => { |
| 74 | + if (err) { |
| 75 | + console.error('Error updating .gitignore:', err) |
| 76 | + } |
| 77 | + else { |
| 78 | + console.log(`${gitignoreFileName} added to .gitignore.`) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + else { |
| 83 | + console.log(`${gitignoreFileName} is already in .gitignore.`) |
| 84 | + } |
| 85 | + } |
| 86 | + else { |
| 87 | + console.log('.gitignore does not exist.') |
| 88 | + } |
| 89 | +} |
0 commit comments