-
Notifications
You must be signed in to change notification settings - Fork 2
/
fix-module.cjs
61 lines (54 loc) · 1.67 KB
/
fix-module.cjs
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
54
55
56
57
58
59
60
61
const { writeFileSync, mkdirSync } = require('node:fs')
const { relative, join } = require('node:path/posix')
const { exports: exp } = require('./package.json')
console.log('fix cjs')
generate(exp, __dirname)
console.log('fix finish')
function generate(exportMap, ROOT_PATH) {
for (const ex of Object.keys(exportMap)) {
if (ex === '.' || exportMap[ex].require === undefined) {
continue
}
const [, ...folders] = ex.split('/')
const fileName = folders.pop()
const [, ...targetFolders] = exportMap[ex].require.split('/')
const targetFileName = targetFolders.pop()
const target = relative(
join(ROOT_PATH, ...folders),
join(ROOT_PATH, ...targetFolders, targetFileName),
)
mkdirSync(join(ROOT_PATH, ...folders), {
recursive: true,
})
writeFileSync(
join(ROOT_PATH, ...folders, `${fileName}.js`),
`module.exports = require('./${target}')`,
)
writeFileSync(
join(ROOT_PATH, ...folders, `${fileName}.d.ts`),
`export * from './${target.split('.')[0]}'`,
)
}
}
// console.log('type check start')
// typecheck()
// console.log('type check finish')
// function typecheck() {
// https://github.com/arethetypeswrong/arethetypeswrong.github.io/tree/main/packages/cli
// try {
// execSync('attw -h')
// } catch (e) {
// console.log('no attw, skip type check, run `npm i -g @arethetypeswrong/cli` to enable')
// return
// }
// const path = `${name}-${version}.tgz`
// execSync('npm pack')
// try {
// const result = execSync(`attw ${path}`, { encoding: 'utf-8' }).toString('utf-8')
// console.log(result)
// } catch (_e) {
// throw new Error('type check fail')
// } finally {
// rmSync(path)
// }
// }