-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scripts): run downlevel-dts script in parallel (#2837)
- Loading branch information
Showing
10 changed files
with
146 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// @ts-check | ||
import { exec } from "child_process"; | ||
import { access, readFile, writeFile } from "fs/promises"; | ||
import { join } from "path"; | ||
import stripComments from "strip-comments"; | ||
import { promisify } from "util"; | ||
|
||
import { getAllFiles } from "./getAllFiles.mjs"; | ||
import { getDeclarationDirname } from "./getDeclarationDirname.mjs"; | ||
import { getDownlevelDirname } from "./getDownlevelDirname.mjs"; | ||
|
||
const execPromise = promisify(exec); | ||
|
||
export const downlevelWorkspace = async (workspacesDir, workspaceName) => { | ||
const workspaceDir = join(workspacesDir, workspaceName); | ||
const downlevelDirname = await getDownlevelDirname(workspaceDir); | ||
const declarationDirname = await getDeclarationDirname(workspaceDir); | ||
|
||
const declarationDir = join(workspaceDir, declarationDirname); | ||
try { | ||
await access(declarationDir); | ||
} catch (error) { | ||
throw new Error( | ||
`The types for "${workspaceName}" do not exist.\n` + | ||
`Please build types for workspace "${workspaceDir}" before running downlevel-dts script.` | ||
); | ||
} | ||
|
||
const downlevelDir = join(declarationDir, downlevelDirname); | ||
// Create downlevel-dts folder if it doesn't exist | ||
try { | ||
await access(downlevelDir); | ||
} catch (error) { | ||
await execPromise(["yarn", "downlevel-dts"].join(" "), { cwd: workspaceDir }); | ||
} | ||
|
||
// Strip comments from downlevel-dts files | ||
try { | ||
await access(downlevelDir); | ||
const files = await getAllFiles(downlevelDir); | ||
for (const downlevelTypesFilepath of files) { | ||
try { | ||
const content = await readFile(downlevelTypesFilepath, "utf8"); | ||
await writeFile(downlevelTypesFilepath, stripComments(content)); | ||
} catch (error) { | ||
console.error(`Error while stripping comments from "${downlevelTypesFilepath.replace(process.cwd(), "")}"`); | ||
console.error(error); | ||
} | ||
} | ||
} catch (error) { | ||
// downlevelDir is not present, do nothing. | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { readdir, stat } from "fs/promises"; | ||
|
||
export const getAllFiles = async (dirPath, arrayOfFiles = []) => { | ||
const files = await readdir(dirPath); | ||
|
||
for (const file of files) { | ||
const { isDirectory } = await stat(dirPath + "/" + file); | ||
if (isDirectory()) { | ||
const filesInDirectory = await getAllFiles(dirPath + "/" + file, arrayOfFiles); | ||
arrayOfFiles.push(filesInDirectory); | ||
} else { | ||
arrayOfFiles.push(join(dirPath, "/", file)); | ||
} | ||
} | ||
|
||
return arrayOfFiles; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { readFile } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
export const getDeclarationDirname = async (workspaceDir) => { | ||
const tsTypesConfigPath = join(workspaceDir, "tsconfig.types.json"); | ||
const tsTypesConfigJson = JSON.parse((await readFile(tsTypesConfigPath)).toString()); | ||
|
||
const declarationDirname = tsTypesConfigJson.compilerOptions.declarationDir; | ||
if (!declarationDirname) { | ||
throw new Error(`The declarationDir is not defined in "${tsTypesConfigPath}".`); | ||
} | ||
|
||
return declarationDirname; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { readFile } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
export const getDownlevelDirname = async (workspaceDir) => { | ||
const packageJsonPath = join(workspaceDir, "package.json"); | ||
const packageJson = JSON.parse((await readFile(packageJsonPath)).toString()); | ||
if (!packageJson.scripts["downlevel-dts"]) { | ||
console.error(`The "downlevel-dts" script is not defined for "${workspaceDir}"`); | ||
return; | ||
} | ||
const downlevelArgs = packageJson.scripts["downlevel-dts"].split(" "); | ||
return downlevelArgs[2].replace(`${downlevelArgs[1]}/`, ""); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { readdirSync, readFileSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
export const getWorkspaces = (rootDir) => { | ||
const { packages } = JSON.parse(readFileSync(join(rootDir, "package.json")).toString()).workspaces; | ||
return packages | ||
.map((dir) => dir.replace("/*", "")) | ||
.flatMap((workspacesDir) => | ||
readdirSync(join(rootDir, workspacesDir), { withFileTypes: true }) | ||
.filter((dirent) => dirent.isDirectory()) | ||
.map((dirent) => ({ workspacesDir, workspaceName: dirent.name })) | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @ts-check | ||
import parallelLimit from "async/parallelLimit"; | ||
import { cpus } from "os"; | ||
import yargs from "yargs"; | ||
|
||
import { downlevelWorkspace } from "./downlevelWorkspace.mjs"; | ||
import { getWorkspaces } from "./getWorkspaces.mjs"; | ||
|
||
// ToDo: Write downlevel-dts as a yargs command, and import yargs in scripts instead. | ||
yargs | ||
.usage( | ||
"Runs downlevel-dts npm script (if present) in each workspace of monorepo," + | ||
" and strips comments from *.d.ts files.\n\n" + | ||
"Usage: index.mjs" | ||
) | ||
.help() | ||
.alias("h", "help").argv; | ||
|
||
const workspaces = getWorkspaces(process.cwd()); | ||
const tasks = workspaces.map(({ workspacesDir, workspaceName }) => async () => { | ||
await downlevelWorkspace(workspacesDir, workspaceName); | ||
}); | ||
|
||
parallelLimit(tasks, cpus().length, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters