forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-update.ts
executable file
·82 lines (73 loc) · 2.67 KB
/
batch-update.ts
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
#!/usr/bin/env TS_NODE_COMPILER_OPTIONS={"downlevelIteration":true} ts-node
import chalk from 'chalk'
import * as cp from 'child_process'
import * as fs from 'fs'
import * as fse from 'fs-extra'
import * as glob from 'glob'
import * as path from 'path'
const IS_DRY_RUN = !process.argv.includes('--apply')
const files = glob.sync('**/package.json', { ignore: ['**/node_modules/**', 'examples/**'] })
function* amendFiles(yarnInstall: boolean) {
let count = 0
for (const file of files) {
const content = fse.readJsonSync(file)
if (content && content.author && content.author.toLowerCase().includes('botpress')) {
let save = false
const saveFn = () => (save = true)
yield [content, file, saveFn]
if (!save) {
console.info(chalk.grey(`Skipping ${file} (no changes)`))
continue
}
count++
if (IS_DRY_RUN) {
console.info(chalk.bold(`==> File ${chalk.bold(file)} has changes (DRY RUN)`))
} else {
console.info(chalk.bold(`==> Applying changes to ${chalk.bold(file)}`))
fse.writeJsonSync(file, content, { spaces: 2 })
}
if (!IS_DRY_RUN && yarnInstall) {
console.info(chalk.grey('==> yarn install --force'))
try {
cp.execSync('yarn install --force', {
env: process.env,
cwd: path.dirname(file),
stdio: [
0, // Use parent's stdin for child.
'ignore', // Pipe child's stdout to parent.
fs.openSync('err.out', 'w') // Direct child's stderr to a file.
]
})
console.info(chalk.green(' success'))
} catch (err) {
console.error(chalk.red('==> ERROR running yarn install (see err.out)'))
}
}
}
}
console.info(chalk.bold(`Done processing ${files.length} file${files.length === 1 ? '' : 's'}`))
if (count > 0) {
console.info(chalk.green(`Changed ${chalk.bold(count.toString())} file${count === 1 ? '' : 's'} successfully`))
if (IS_DRY_RUN) {
console.info(chalk.red.bold('THIS WAS A DRY RUN, SO NO FILE WAS ACTUALLY CHANGED'))
console.info(chalk.red(`Run this again with ${chalk.bold('--apply')} to execute the changes`))
}
} else {
console.info(chalk.green('There are no changes'))
}
}
/**
* Content : package.json content
* file: file name
* save: function to be called if you want to apply changes
*
* Amend the content to of the package.json as you please and call save if necessary
*/
for (const [content, file, save] of amendFiles(true)) {
// fstream vulnerability CVE-2019-13173
content.resolutions = Object.assign({}, content.resolutions, {
fstream: '>=1.0.12',
lodash: '>=4.17.19'
})
save()
}