forked from team-innovation/vue-sfc-rollup
-
Notifications
You must be signed in to change notification settings - Fork 36
/
init.js
executable file
·96 lines (79 loc) · 2.01 KB
/
init.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
#! /usr/bin/env node
const { logger, kebabcasify } = require('./utils')
const FileActions = require('./lib/fileActions')
const parseArgs = require('./lib/parseArgs')
const kleur = require('kleur')
const path = require('path')
const readline = require('readline-sync')
const { setShellFilePermission } = require('./utils/update-files')
const argv = parseArgs(process.argv.slice(2))
let pkg = {}
let pkgName = ''
/**
* Prompt user for input to populate template files
*/
let npmName = argv.get('name')
let ownerName = argv.get('owner')
const OWNER_NAME = 'FEMessage'
function isUpgrade() {
return argv.has('u') || argv.has('upgrade')
}
if (isUpgrade()) {
try {
pkg = require(path.join(process.cwd(), 'package.json'))
pkgName = pkg.name.replace(/^@[\w-]*\//, '')
} catch {}
}
if (argv.has('test')) {
npmName = 'v-test'
ownerName = OWNER_NAME
}
const promptAngle = kleur.dim('> ')
if (!npmName) {
console.log(
`The component name: ${pkgName ? kleur.dim(`(${pkgName})`) : ''}`
)
npmName = readline.prompt({
defaultInput: pkgName,
prompt: promptAngle
})
}
if (!ownerName) {
console.log(
`The owner: ${kleur.dim(`(${OWNER_NAME})`)}`
)
ownerName = readline.prompt({
prompt: promptAngle,
defaultInput: OWNER_NAME
})
}
const componentName = kebabcasify(npmName)
const outDir = path.join(getOutputDir(), componentName)
const fileActions = new FileActions({
argv,
pkg,
componentName,
ownerName,
outDir,
templatesDir: path.join(__dirname, 'templates')
})
if (!isUpgrade()) {
fileActions.create()
fileActions.move({
patterns: {
gitignore: '.gitignore',
'_package.json': 'package.json',
'src/component.vue': `src/${componentName}.vue`,
'src/component.d.ts': `src/${componentName}.d.ts`
}
})
setShellFilePermission(outDir)
logger.success(`Generated into ${kleur.underline(outDir)}`)
}
if (isUpgrade()) {
fileActions.upgrade()
}
function getOutputDir() {
let output = argv.get('output')
return output ? path.resolve(output) : process.cwd()
}