/
add-script.js
47 lines (43 loc) · 1.37 KB
/
add-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env scripta
import { Script } from 'scripta'
import fs from 'fs'
import path from 'path'
import { paramCase } from 'change-case'
const nameifyScript = (name) => name.split('/').map(s => paramCase(s)).join('/')
export default new Script({
name: 'add-script',
options: [
{
name: 'name',
type: 'text',
message: 'Please enter the name of the new script',
positionalArgument: true
},
{
name: 'editor',
type: 'text',
message: 'Please enter your favorite editor command',
// storeDefault: true
setupOnce: true
}
],
command: async ({ name, editor }, { installPath, configPath }) => {
const filename = `${nameifyScript(name)}.js`
const source = path.join(installPath, 'dist', 'boilerplate.js')
const destination = path.join(configPath, 'scripts', filename)
const folder = path.dirname(destination)
if (fs.existsSync(destination)) {
console.log('Script already exists, opening existing script')
}
else {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true})
}
const buffer = await fs.promises.readFile(source)
const newBody = buffer.toString().replace('__SCRIPT_NAME__', nameifyScript(name))
await fs.promises.writeFile(destination, newBody)
}
await $`chmod +x ${destination}`
await $`${editor} ${destination}`
}
})