-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.js
69 lines (64 loc) · 2.72 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
const inquirer = require('inquirer')
const fs = require('fs-extra')
const pwd = process.cwd()
const path = require('path')
const srcPath = path.join(pwd, '/src')
const srcAlreadyExists = fs.pathExistsSync(srcPath)
const buildPath = path.join(pwd, '/build')
const buildAlreadyExists = fs.pathExistsSync(srcPath)
if (srcAlreadyExists || buildAlreadyExists) {
return console.error('Process stopped as ' + srcAlreadyExists + ' &/or' + buildPath + 'already found. Installation cannot run with these folders existing.')
} else {
const localPkgJsonPath = path.join(pwd, 'package.json')
if (!fs.pathExistsSync(localPkgJsonPath)) {
return console.error('Error: No package.json file found. Please add a package.json file to continue')
}
let localPkgJson = require(localPkgJsonPath)
const questions = [{
type: 'input',
name: 'name',
message: 'Enter the name of the api file, press enter to use the current package.json name attribute:',
default: localPkgJson.name,
validate: function (value) {
return (typeof value === 'string' && value.length > 1) || 'Please enter a name longer than 1 character'
},
}, {
type: 'confirm',
name: 'updateName',
message: 'The current package.json name does not match the api name entered. Would you the package.json name to be updated too?',
when: function (answers) {
return answers.name !== localPkgJson.name
}
}, {
type: 'confirm',
name: 'installConfirm',
message: 'Press Y and enter to install.',
default: false
}]
inquirer.prompt(questions).then((answers) => {
if (answers.installConfirm) {
fs.copySync(__dirname + '/src_2', srcPath)
fs.copySync(__dirname + '/build', buildPath)
console.log('Completed: Installed swagger-chunk skeleton files to ' + srcPath)
const name = answers.name
if (answers.updateName) {
localPkgJson.name = name
}
localPkgJson.scripts['build:json'] = 'swagger-chunk -i ./src/index.yml -o json -D ./build/ -d ' + name
localPkgJson.scripts['build:yaml'] = 'swagger-chunk -i ./src/index.yml -o yaml -e yml -D ./build/ -d ' + name
localPkgJson.scripts['build:all'] = 'npm run build:json && npm run build:yaml'
// Write the new json object to file
fs.writeFileSync(localPkgJsonPath, JSON.stringify(localPkgJson, null, 4))
if (answers.updateName) {
return console.log('Completed: swagger-chunk build scripts added to your package.json file and name updated.')
} else {
return console.log('Completed: swagger-chunk build scripts added to your package.json file.')
}
} else {
return console.log('Installation cancelled.')
}
}).catch((e) => {
console.log('Aborting installation:')
console.error(e)
})
}