-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-transform.ts
More file actions
36 lines (32 loc) · 1.05 KB
/
import-transform.ts
File metadata and controls
36 lines (32 loc) · 1.05 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
/**
* Assumptions:
* 1. no imports from 'foo/foo' where foo/foo/index.js exists
*/
import { spawnSync } from 'child_process'
import { exit } from 'process'
const findCmd = [`find`, `dist`, `-type`, `f`, `-exec`]
function exec(args: string[]): string[] {
console.info(args.join(' '))
const first = args[0]
const rest = args.slice(1)
return spawnSync(first, rest)
.stdout.toString()
.trim()
.toString()
.split('\n')
.filter(s => s.length > 0)
}
if (exec([`grep`, `-r`, `.js'`, `dist`]).length > 10) {
exit()
}
const dirImports = exec([`find`, `dist`, `-name`, `index.js`])
.map(p => p.split('/'))
.filter(p => p.length > 2)
.map(p => p.at(-2)!)
dirImports.forEach(d => {
exec([...findCmd, `sed`, `-i`, `-E`, `s|(from '.*\./${d})(')|\\1/index\\2|g`, `{}`, `+`])
})
// fix from '.' -> from './index.js'
exec([...findCmd, `sed`, `-i`, `-E`, `s|(from '\\.)(')|\\1/index\\2|g`, `{}`, `+`])
// add .js to relative paths
exec([...findCmd, `sed`, `-i`, `-E`, `s|(from '\\..+)(')|\\1.js\\2|g`, `{}`, `+`])