-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathhelpers.js
37 lines (31 loc) · 895 Bytes
/
helpers.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
const fs = require('fs')
function updateBabelConfig (api, callback) {
let config, configPath
const rcPath = api.resolve('./babel.config.js')
const pkgPath = api.resolve('./package.json')
if (fs.existsSync(rcPath)) {
configPath = rcPath
config = callback(require(rcPath))
} else if (fs.existsSync(pkgPath)) {
configPath = pkgPath
config = JSON.parse(fs.readFileSync(pkgPath, { encoding: 'utf8' }))
if (config.babel) {
config.babel = callback(config.babel)
} else {
// TODO: error handling here?
}
}
if (configPath) {
const moduleExports = configPath !== pkgPath ? 'module.exports = ' : ''
fs.writeFileSync(
configPath,
`${moduleExports}${JSON.stringify(config, null, 2)}`,
{ encoding: 'utf8' },
)
} else {
// TODO: handle if babel config doesn't exist
}
}
module.exports = {
updateBabelConfig,
}