forked from TheChristophe/generate-esm-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitizeGeneratedClient.mjs
112 lines (94 loc) · 3.11 KB
/
sanitizeGeneratedClient.mjs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import fs from 'node:fs';
import process from 'node:process';
import path from 'node:path';
import stubPackageJson from "./package.json" assert { type: "json" };
const WORK_DIR = './client';
const ifProvided = (value) => {
return value && value.length > 0 ? value : undefined
}
const GIT_REPO_URL = ifProvided(process.env.GIT_REPO_URL);
const AUTHOR = process.env.AUTHOR ?? "User";
const LICENSE = process.env.LICENSE ?? "UNLICENSED";
const sanitizePackageJson = async () => {
const dirtyPackageJson = JSON.parse(fs.readFileSync(`${WORK_DIR}/package.json`, 'utf8'));
delete dirtyPackageJson.main;
dirtyPackageJson.module = './dist/index.js';
dirtyPackageJson.sideEffects = false;
dirtyPackageJson.dependencies = {
...dirtyPackageJson.dependencies,
...stubPackageJson.dependencies
};
dirtyPackageJson.devDependencies = {
...dirtyPackageJson.devDependencies,
...stubPackageJson.devDependencies
};
if (GIT_REPO_URL === undefined) {
delete dirtyPackageJson.repository;
}
else {
dirtyPackageJson.repository = {
type: 'git',
url: GIT_REPO_URL,
};
}
dirtyPackageJson.license = LICENSE;
dirtyPackageJson.author = AUTHOR;
dirtyPackageJson.type = "module";
dirtyPackageJson.exports = {
".": {
types: "./dist/index.d.ts",
import: "./dist/index.js",
default: "./dist/index.js",
}
};
dirtyPackageJson.files = [
"/dist",
"/package.json",
"/README.md",
];
fs.writeFileSync(`${WORK_DIR}/package.json`, JSON.stringify(dirtyPackageJson, null, 2), 'utf8');
};
const sanitizeTsConfig = async () => {
const dirtyTsConfig = JSON.parse(fs.readFileSync(`${WORK_DIR}/tsconfig.json`, 'utf8'));
dirtyTsConfig.compilerOptions.target = 'ES6';
dirtyTsConfig.compilerOptions.module = 'NodeNext';
dirtyTsConfig.compilerOptions.moduleResolution = 'NodeNext';
dirtyTsConfig.compilerOptions.allowSyntheticDefaultImports = true;
delete dirtyTsConfig.compilerOptions.lib;
fs.writeFileSync(`${WORK_DIR}/tsconfig.json`, JSON.stringify(dirtyTsConfig, null, 2), 'utf8');
};
const sanitizeTsImports = async () => {
// https://gist.github.com/lovasoa/8691344?permalink_comment_id=3299089#gistcomment-3299089
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) {
yield* await walk(entry);
}
else if (d.isFile()) {
yield entry;
}
}
}
for await (const p of walk(`${WORK_DIR}/`)) {
if (p.endsWith('.ts')) {
fs.writeFileSync(
p,
fs.readFileSync(p, 'utf8')
// replace imports of files with *.js
.replaceAll(
/(from\s+)(["'])(?!.*\.js)(\.?\.\/(?!(apis|models)["']).*)(["'])/g,
'$1$2$3.js$5')
// replace imports of api/models folders with */index.js
.replaceAll(
/(from\s+)(["'])(?!.*\.js)(\.?\.\/(apis|models))(["'])/g,
'$1$2$3/index.js$5'),
'utf8');
}
}
}
await Promise.all([
sanitizePackageJson(),
sanitizeTsConfig(),
sanitizeTsImports(),
]);