-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmts-types.mjs
More file actions
23 lines (17 loc) · 739 Bytes
/
mts-types.mjs
File metadata and controls
23 lines (17 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { readFileSync, writeFileSync, readdirSync, unlinkSync } from 'node:fs';
const packagePath = process.cwd();
const typesDir = `${packagePath}/types`;
const from = /\bfrom (['"])(\.[^'"]*)\1;/g;
const tsExt = /\.ts(\.map)?$/;
for (const file of readdirSync(typesDir)) {
if (!file.endsWith('.d.ts') && !file.endsWith('.d.ts.map')) continue;
const path = `${typesDir}/${file}`;
const contents = readFileSync(path, 'utf8');
const cts = contents
.replace(/^export default/gm, 'export = ')
.replace(from, 'from $1$2.cts$1;');
const mts = contents.replace(from, 'from $1$2.mts$1;');
unlinkSync(path);
writeFileSync(path.replace(tsExt, `.cts$1`), cts);
writeFileSync(path.replace(tsExt, `.mts$1`), mts);
}