-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·33 lines (26 loc) · 1.17 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
#!/usr/bin/env node
const FS = require('fs')
const globby = require('globby')
const r1 = /^(let|var|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s+\=\s+(require)\((('|")[a-zA-Z0-9-_.\/]+('|"))\)/gm // const createStore = require('redux')
const r2 = /^(let|var|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s+\=\s+(require)\((('|")[a-zA-Z0-9-_.\/]+('|"))\)\.([a-zA-Z][a-zA-Z0-9]+)/gm // const createStore = require('redux').createStore
const r3 = /^(let|var|const)\s+(\{\s*([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\})\s+\=\s+(require)\((('|")[a-zA-Z0-9-_.\/]+('|"))\)/gm // const { createStore } = require('redux')
const args = process.argv.slice(2)
if (!args.length) {
console.error('Please pass a directory glob to "replace-require-with-import"\n')
process.exit(1)
}
const paths = globby.sync(args)
paths.forEach(function (p) {
if (!FS.statSync(p).isDirectory()) {
return replaceInFile(p)
}
})
function replaceInFile(fp) {
const result = FS.writeFileSync(fp, FS.readFileSync(fp, 'utf-8')
.replace(r3, `import { $3 } from $5`)
.replace(r2, `import { $7 as $2 } from $4`)
.replace(r1, `import $2 from $4`), 'utf-8')
console.log(`> ${fp}`)
return result
}
console.info('Done!\n')