-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (98 loc) · 3.09 KB
/
index.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
const path = require('path')
const shellExec = require('child_process').exec
const intersects = require('semver').intersects
const mergeDirectory = require('./lib/merge-directory')
const {
collectPlaceholders,
collectAnswers,
collectAnswer
} = require('./lib/placeholders')
const {
readjson,
writejson
} = require('./lib/json')
const mkdirp = require('mkdirp')
const { forEach } = require('p-iteration')
module.exports = class StackUpgrade {
constructor ({ destDir = process.cwd(), name, version, packagejson }) {
this.destDir = destDir
this.name = name
this.version = version
this.packagejson = packagejson
}
async ensureDestDir () {
return mkdirp(this.destDir)
}
async ask (question, defaultValue) {
return collectAnswer({ question, variableName: 'ask', defaultValue })
}
async configure ({ sourceDirs, answers }) {
let placeholders = []
await forEach(sourceDirs, async (value) => {
const p = await collectPlaceholders({ sourceDir: value })
placeholders = placeholders.concat(p)
})
// figure out placeholders which has already answers provided
const result_answers = Object.assign({}, answers)
for (let i = 0; i < placeholders.length; i++) {
if (result_answers[placeholders[i]]) {
placeholders.splice(i, 1)
i -= 1
}
}
// ask only for answers about missing placeholders if any
if (placeholders.length) {
Object.assign(result_answers, await collectAnswers({ placeholders }))
}
return result_answers
}
async merge ({ sourceDir, answers, forceOverride, destSubDir }) {
let destDir = this.destDir
if (destSubDir) {
destDir = path.join(destDir, destSubDir)
}
await mergeDirectory({ sourceDir, destDir, answers, forceOverride })
}
async updateJSON () {
const jsonfilepath = path.join(this.destDir, 'package.json')
let json
try {
json = await readjson(jsonfilepath)
} catch (e) {
json = {} // ignore any errors
}
let name = this.name
let version = this.version
if (this.packagejson) {
const packagejson = await readjson(this.packagejson)
name = packagejson.name
version = packagejson.version
}
json.stackUpgrades = json.stackUpgrades || {}
json.stackUpgrades[name] = version
return writejson(jsonfilepath, json)
}
async checkUpgrade (name, version) {
const jsonfilepath = path.join(this.destDir, 'package.json')
const destJson = await readjson(jsonfilepath)
const destVersion = destJson.stackUpgrades[name]
if (!destVersion) return false
return intersects(version, destVersion)
}
async exec (cmd) {
await this.ensureDestDir()
return new Promise((resolve, reject) => {
console.log('exec', this.destDir, cmd)
const child = shellExec(cmd, {
cwd: this.destDir,
env: process.env
})
child.stdout.on('data', chunk => console.log(chunk.toString()))
child.stderr.on('data', chunk => console.log(chunk.toString()))
child.on('close', (status) => {
if (status === 0) return resolve()
reject(status)
})
})
}
}