This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patheject.js
108 lines (89 loc) · 2.93 KB
/
eject.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const fs = require('fs')
const parseArgs = require('./args/run')
const path = require('path')
const chalk = require('chalk')
const { isValidFastifyProject } = require('./lib/utils')
const { promisify } = require('util')
const mkdir = promisify(fs.mkdir)
const copyFile = promisify(fs.copyFile)
const generify = require('generify')
const log = require('./lib/log')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
function showHelp () {
fs.readFile(path.join(__dirname, 'help', 'eject.txt'), 'utf8', (err, data) => {
if (err) {
module.exports.stop(err)
}
console.log(data)
module.exports.stop()
})
}
function stop (error) {
if (error) {
console.error(error)
process.exit(1)
}
process.exit()
}
async function eject (args, cb) {
const opts = parseArgs(args)
if (!fs.existsSync(opts.file)) {
console.error('Missing the required file index.js\n')
return showHelp()
}
if (opts.help) {
return showHelp()
}
const dir = opts.directory || process.cwd()
try {
await isValidFastifyProject(dir, null)
} catch (e) {
return cb(e)
}
try {
await mkdir(path.join(dir, 'lib'))
await mkdir(path.join(dir, 'args'))
generify(path.join(__dirname, 'lib', 'watch'), path.join(dir, 'lib', 'watch'), {}, function (file) {
log('debug', `generated a file in ${path.join(dir, 'lib', 'watch')}`)
})
generify(path.join(__dirname, 'lib', 'plugins'), path.join(dir, 'lib', 'plugins'), {}, function (file) {
log('debug', `generated a file in ${path.join(dir, 'lib', 'plugins')}`)
})
await copyFile(path.join(__dirname, 'args', 'run.js'), path.join(dir, 'args', 'run.js'))
log('debug', `generated ${path.join(dir, 'args', 'run.js')}`)
await copyFile(path.join(__dirname, 'run.js'), path.join(dir, 'run.js'))
log('debug', `generated ${path.join(dir, 'run.js')}`)
let pkgLocal = await readFile(path.join(__dirname, 'package.json'), 'utf8')
pkgLocal = JSON.parse(pkgLocal)
let pkgApp = await readFile(path.join(dir, 'package.json'), 'utf8')
pkgApp = JSON.parse(pkgApp)
Object.assign(pkgApp, {
dependencies: {
...pkgApp.dependencies,
clui: pkgLocal.dependencies.clui,
'tiny-human-time': pkgLocal.dependencies['tiny-human-time'],
chalk: pkgLocal.dependencies.chalk
},
scripts: {
test: 'tap test/**/*.test.js',
start: 'node run.js',
dev: 'node run.js -l info -P -w'
}
})
await writeFile(path.join(dir, 'package.json'), JSON.stringify(pkgApp, null, 2), 'utf8')
log('debug', 'package.json modified')
log('success', `run '${chalk.bold('npm install')}'`)
log('success', `run '${chalk.bold('npm run dev')}' to start the application in dev mode`)
} catch (e) {
module.exports.stop(e)
}
}
function cli (args) {
eject(args)
}
module.exports = { cli, stop }
if (require.main === module) {
cli(process.argv.slice(2))
}