forked from jaredLunde/stellar-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditFile.js
More file actions
53 lines (43 loc) 路 1.44 KB
/
Copy patheditFile.js
File metadata and controls
53 lines (43 loc) 路 1.44 KB
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
const getPackages = require('./getPackages')
const fs = require('fs')
const chalk = require('chalk')
const {promisify} = require('util')
const path = require('path')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const rename = promisify(fs.rename)
function editFile (filename, find, replace, opt = {}) {
const {ignore, dry = false} = opt
return Promise.all(
getPackages(ignore).map(
async dir => {
const source = path.join(dir, filename)
if (fs.existsSync(source) === false) {
return
}
const tmpSource = `${source}.tmp`
const data = await readFile(source, 'utf8')
const replacement = data.replace(new RegExp(find, 'g'), replace)
if (replacement === data) {
return
}
if (dry) {
console.log(
`[Dry]`,
'\n------------------------------------------------------------------\n',
chalk.bold('Found\n'),
data,
'\n------------------------------------------------------------------\n',
chalk.bold('Replaced\n'),
replacement,
'\n__________________________________________________________________\n\n',
)
return
}
console.log('Edited', chalk.bold(source))
return writeFile(tmpSource, replacement).then(() => rename(tmpSource, source))
}
)
)
}
module.exports = editFile