Skip to content

Commit bb63c4d

Browse files
committed
feat: add scripts cli
1 parent 0f315bc commit bb63c4d

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

bin/deploy.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
]
3232
}
3333
},
34+
"bin": {
35+
"deploy": "./bin/deploy.js"
36+
},
3437
"files": [
3538
"dist"
3639
],
@@ -47,6 +50,7 @@
4750
},
4851
"dependencies": {
4952
"chalk": "^5.3.0",
53+
"commander": "^12.1.0",
5054
"cpu-features": "^0.0.10",
5155
"net": "^1.0.2",
5256
"ora": "^8.0.1",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/local-fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function readLoaclDir(localDir: string) {
55
return new Promise<string[]>((resolve) => {
66
fs.readdir(localDir, (err, files) => {
77
if (err)
8-
throw err
8+
throw new Error(`read ${localDir} error`)
99

1010
resolve(files)
1111
})

0 commit comments

Comments
 (0)