Skip to content

Commit 223f8e3

Browse files
committed
chore(scripts): split downlevel-dts to multiple files
1 parent e137f7f commit 223f8e3

File tree

6 files changed

+65
-42
lines changed

6 files changed

+65
-42
lines changed

scripts/downlevel-dts/downlevelWorkspace.mjs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1+
// @ts-check
12
import { exec } from "child_process";
23
import { access, readFile, writeFile } from "fs/promises";
34
import { join } from "path";
45
import stripComments from "strip-comments";
56
import { promisify } from "util";
67

8+
import { getAllFiles } from "./getAllFiles.mjs";
9+
import { getDeclarationDirname } from "./getDeclarationDirname.mjs";
10+
import { getDownlevelDirname } from "./getDownlevelDirname.mjs";
11+
712
const execPromise = promisify(exec);
813

914
export const downlevelWorkspace = async (workspacesDir, workspaceName) => {
1015
const workspaceDir = join(workspacesDir, workspaceName);
11-
12-
const packageJsonPath = join(workspaceDir, "package.json");
13-
const packageJson = JSON.parse((await readFile(packageJsonPath)).toString());
14-
if (!packageJson.scripts["downlevel-dts"]) {
15-
console.error(`The "downlevel-dts" script is not defined for "${workspaceDir}"`);
16-
return;
17-
}
18-
const downlevelArgs = packageJson.scripts["downlevel-dts"].split(" ");
19-
const downlevelDirname = downlevelArgs[2].replace(`${downlevelArgs[1]}/`, "");
20-
21-
const tsTypesConfigPath = join(workspaceDir, "tsconfig.types.json");
22-
const declarationDirname = JSON.parse((await readFile(tsTypesConfigPath)).toString()).compilerOptions.declarationDir;
23-
24-
if (!declarationDirname) {
25-
throw new Error(`The declarationDir is not defined in "${tsTypesConfigPath}".`);
26-
}
16+
const downlevelDirname = await getDownlevelDirname(workspaceDir);
17+
const declarationDirname = await getDeclarationDirname(workspaceDir);
2718

2819
const declarationDir = join(workspaceDir, declarationDirname);
2920
try {

scripts/downlevel-dts/getAllFiles.mjs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { readdirSync, statSync } from "fs";
2+
3+
export const getAllFiles = (dirPath, arrayOfFiles = []) => {
4+
const files = readdirSync(dirPath);
5+
6+
files.forEach((file) => {
7+
const { isDirectory } = statSync(dirPath + "/" + file);
8+
if (isDirectory()) {
9+
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
10+
} else {
11+
arrayOfFiles.push(join(dirPath, "/", file));
12+
}
13+
});
14+
15+
return arrayOfFiles;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { readFile } from "fs/promises";
2+
import { join } from "path";
3+
4+
export const getDeclarationDirname = async (workspaceDir) => {
5+
const tsTypesConfigPath = join(workspaceDir, "tsconfig.types.json");
6+
const tsTypesConfigJson = JSON.parse((await readFile(tsTypesConfigPath)).toString());
7+
8+
const declarationDirname = tsTypesConfigJson.compilerOptions.declarationDir;
9+
if (!declarationDirname) {
10+
throw new Error(`The declarationDir is not defined in "${tsTypesConfigPath}".`);
11+
}
12+
13+
return declarationDirname;
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readFile } from "fs/promises";
2+
import { join } from "path";
3+
4+
export const getDownlevelDirname = async (workspaceDir) => {
5+
const packageJsonPath = join(workspaceDir, "package.json");
6+
const packageJson = JSON.parse((await readFile(packageJsonPath)).toString());
7+
if (!packageJson.scripts["downlevel-dts"]) {
8+
console.error(`The "downlevel-dts" script is not defined for "${workspaceDir}"`);
9+
return;
10+
}
11+
const downlevelArgs = packageJson.scripts["downlevel-dts"].split(" ");
12+
return downlevelArgs[2].replace(`${downlevelArgs[1]}/`, "");
13+
};
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readdirSync, readFileSync } from "fs";
2+
import { join } from "path";
3+
4+
export const getWorkspaces = (rootDir) => {
5+
const { packages } = JSON.parse(readFileSync(join(rootDir, "package.json")).toString()).workspaces;
6+
return packages
7+
.map((dir) => dir.replace("/*", ""))
8+
.flatMap((workspacesDir) =>
9+
readdirSync(join(rootDir, workspacesDir), { withFileTypes: true })
10+
.filter((dirent) => dirent.isDirectory())
11+
.map((dirent) => ({ workspacesDir, workspaceName: dirent.name }))
12+
);
13+
};

scripts/downlevel-dts/index.mjs

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @ts-check
22
import parallelLimit from "async/parallelLimit";
3-
import { readdirSync, readFileSync, statSync } from "fs";
43
import { cpus } from "os";
5-
import { join } from "path";
64
import yargs from "yargs";
75

86
import { downlevelWorkspace } from "./downlevelWorkspace.mjs";
7+
import { getWorkspaces } from "./getWorkspaces.mjs";
98

109
// ToDo: Write downlevel-dts as a yargs command, and import yargs in scripts instead.
1110
yargs
@@ -17,30 +16,7 @@ yargs
1716
.help()
1817
.alias("h", "help").argv;
1918

20-
const { packages } = JSON.parse(readFileSync(join(process.cwd(), "package.json")).toString()).workspaces;
21-
22-
const getAllFiles = (dirPath, arrayOfFiles = []) => {
23-
const files = readdirSync(dirPath);
24-
25-
files.forEach((file) => {
26-
if (statSync(dirPath + "/" + file).isDirectory()) {
27-
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
28-
} else {
29-
arrayOfFiles.push(join(dirPath, "/", file));
30-
}
31-
});
32-
33-
return arrayOfFiles;
34-
};
35-
36-
const workspaces = packages
37-
.map((dir) => dir.replace("/*", ""))
38-
.flatMap((workspacesDir) =>
39-
readdirSync(join(process.cwd(), workspacesDir), { withFileTypes: true })
40-
.filter((dirent) => dirent.isDirectory())
41-
.map((dirent) => ({ workspacesDir, workspaceName: dirent.name }))
42-
);
43-
19+
const workspaces = getWorkspaces(process.cwd());
4420
const tasks = workspaces.map(({ workspacesDir, workspaceName }) => async () => {
4521
await downlevelWorkspace(workspacesDir, workspaceName);
4622
});

0 commit comments

Comments
 (0)