Skip to content

Commit 2923d66

Browse files
authored
chore(scripts): add script to downlevel-dts and strip comments (#2834)
1 parent 6dfabb9 commit 2923d66

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"main": "index.js",
77
"scripts": {
88
"copy-models": "node ./scripts/copy-models",
9+
"downlevel-dts": "node ./scripts/downlevel-dts",
910
"generate-clients": "node ./scripts/generate-clients",
1011
"bootstrap": "yarn",
1112
"clean": "yarn clear-build-cache && yarn clear-build-info && lerna clean",
@@ -85,6 +86,7 @@
8586
"mocha": "^8.0.1",
8687
"prettier": "2.3.0",
8788
"puppeteer": "^5.1.0",
89+
"strip-comments": "2.0.1",
8890
"ts-jest": "^26.4.1",
8991
"ts-loader": "^7.0.5",
9092
"typedoc-plugin-lerna-packages": "^0.3.1",
@@ -122,4 +124,4 @@
122124
],
123125
"**/*.{ts,js,md,json}": "prettier --write"
124126
}
125-
}
127+
}

scripts/downlevel-dts/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# How to downlevel-dts
2+
3+
## Prerequisite
4+
5+
- Node.js version >= 12.
6+
7+
## Options
8+
9+
```
10+
Runs downlevel-dts npm script (if present) in each workspace of monorepo, and
11+
strips comments from *.d.ts files.
12+
13+
Usage: index.js
14+
15+
Options:
16+
--version Show version number [boolean]
17+
-h, --help Show help [boolean]
18+
```
19+
20+
## Examples
21+
22+
- Run downlevel-dts for workspaces in monorepo:
23+
24+
`yarn downlevel-dts`

scripts/downlevel-dts/index.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// @ts-check
2+
const yargs = require("yargs");
3+
4+
const { existsSync, readdirSync, readFileSync, statSync, writeFileSync } = require("fs");
5+
const { join } = require("path");
6+
const { execSync } = require("child_process");
7+
const stripComments = require("strip-comments");
8+
9+
// ToDo: Write downlevel-dts as a yargs command, and import yargs in scripts instead.
10+
yargs
11+
.usage(
12+
"Runs downlevel-dts npm script (if present) in each workspace of monorepo," +
13+
" and strips comments from *.d.ts files.\n\n" +
14+
"Usage: index.js"
15+
)
16+
.help()
17+
.alias("h", "help").argv;
18+
19+
const { packages } = JSON.parse(readFileSync(join(process.cwd(), "package.json")).toString()).workspaces;
20+
21+
const getAllFiles = (dirPath, arrayOfFiles = []) => {
22+
const files = readdirSync(dirPath);
23+
24+
files.forEach((file) => {
25+
if (statSync(dirPath + "/" + file).isDirectory()) {
26+
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
27+
} else {
28+
arrayOfFiles.push(join(dirPath, "/", file));
29+
}
30+
});
31+
32+
return arrayOfFiles;
33+
};
34+
35+
packages
36+
.map((dir) => dir.replace("/*", ""))
37+
.forEach((workspacesDir) => {
38+
// Process each workspace in workspace directory
39+
readdirSync(join(process.cwd(), workspacesDir), { withFileTypes: true })
40+
.filter((dirent) => dirent.isDirectory())
41+
.map((dirent) => dirent.name)
42+
.forEach((workspaceName) => {
43+
const workspaceDir = join(workspacesDir, workspaceName);
44+
45+
const packageJsonPath = join(workspaceDir, "package.json");
46+
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
47+
if (!packageJson.scripts["downlevel-dts"]) {
48+
console.error(`The "downlevel-dts" script is not defined for "${workspaceDir}"`);
49+
return;
50+
}
51+
const downlevelArgs = packageJson.scripts["downlevel-dts"].split(" ");
52+
const downlevelDirname = downlevelArgs[2].replace(`${downlevelArgs[1]}/`, "");
53+
54+
const tsTypesConfigPath = join(workspaceDir, "tsconfig.types.json");
55+
const declarationDirname = JSON.parse(readFileSync(tsTypesConfigPath).toString()).compilerOptions
56+
.declarationDir;
57+
58+
if (!declarationDirname) {
59+
throw new Error(`The declarationDir is not defined in "${tsTypesConfigPath}".`);
60+
}
61+
62+
const declarationDir = join(workspaceDir, declarationDirname);
63+
if (!existsSync(declarationDir)) {
64+
throw new Error(
65+
`The types for "${workspaceName}" do not exist.\n` +
66+
`Please build types for workspace "${workspaceDir}" before running downlevel-dts script.`
67+
);
68+
}
69+
70+
const downlevelDir = join(declarationDir, downlevelDirname);
71+
// Create downlevel-dts folder if it doesn't exist
72+
if (!existsSync(downlevelDir)) {
73+
execSync(["yarn", "downlevel-dts"].join(" "), { cwd: workspaceDir });
74+
}
75+
76+
// Strip comments from downlevel-dts files
77+
if (existsSync(downlevelDir)) {
78+
getAllFiles(downlevelDir).forEach((downlevelTypesFilepath) => {
79+
try {
80+
const content = readFileSync(downlevelTypesFilepath, "utf8");
81+
writeFileSync(downlevelTypesFilepath, stripComments(content));
82+
} catch (error) {
83+
console.error(
84+
`Error while stripping comments from "${downlevelTypesFilepath.replace(process.cwd(), "")}"`
85+
);
86+
console.error(error);
87+
}
88+
});
89+
}
90+
});
91+
});

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -11294,6 +11294,11 @@ strip-bom@^4.0.0:
1129411294
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
1129511295
integrity sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==
1129611296

11297+
11298+
version "2.0.1"
11299+
resolved "https://registry.yarnpkg.com/strip-comments/-/strip-comments-2.0.1.tgz#4ad11c3fbcac177a67a40ac224ca339ca1c1ba9b"
11300+
integrity sha512-ZprKx+bBLXv067WTCALv8SSz5l2+XhpYCsVtSqlMnkAXMWDq+/ekVbl1ghqP9rUHTzv6sm/DwCOiYutU/yp1fw==
11301+
1129711302
strip-eof@^1.0.0:
1129811303
version "1.0.0"
1129911304
resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf"

0 commit comments

Comments
 (0)