-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathpatch-ts.js
More file actions
23 lines (17 loc) · 835 Bytes
/
patch-ts.js
File metadata and controls
23 lines (17 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const shell = require('shelljs')
const fs = require('fs')
const files = shell.find('dist/**/*.d.ts')
for (const file of files) {
const cts = file.replace('.d.ts', '.d.cts')
// Add type qualifiers in annotations
shell.sed('-i', /(import|export)\s+(?!(type|enum|interface|class|function|const|declare)\b)/, '$1 type ', file)
// Remove .js extensions
shell.sed('-i', /from '(\.[^.]+)\.js'/, "from '$1'", file)
fs.copyFileSync(file, cts)
// Add explicit .d.ts extensions for ESM
shell.sed('-i', / from '(\.[^']+)'/, " from '$1.d.ts'", file)
shell.sed('-i', /^declare module '(\.[^']+)'/, "declare module '$1.d.ts'", file)
// Add explicit .d.cts extensions for CJS
shell.sed('-i', / from '(\.[^']+)'/, " from '$1.d.cts'", cts)
shell.sed('-i', /^declare module '(\.[^']+)'/, "declare module '$1.d.cts'", cts)
}