-
Notifications
You must be signed in to change notification settings - Fork 614
fix(scripts): run downlevel-dts script in parallel #2837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aaa3df9
chore(deps-dev): add run-parallel-limit
trivikr 797e634
chore(deps-dev): add [email protected]
trivikr 58e0b22
feat(scripts): run downlevel-dts in parallel
trivikr 3bc2986
chore: move downlevelWorkspace to different file
trivikr e137f7f
chore(scripts): use mjs for downlevel-dts scripts
trivikr 223f8e3
chore(scripts): split downlevel-dts to multiple files
trivikr 7ba6027
chore(deps-dev): remove unused run-parallel-limit
trivikr 106e231
chore: use async operations in getAllFiles
trivikr 4de7f60
chore: use for loop for processing downlevel files
trivikr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2601,6 +2601,11 @@ [email protected], async@^3.0.1: | |
resolved "https://registry.yarnpkg.com/async/-/async-3.2.0.tgz#b3a2685c5ebb641d3de02d161002c60fc9f85720" | ||
integrity sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw== | ||
|
||
[email protected]: | ||
version "3.2.1" | ||
resolved "https://registry.yarnpkg.com/async/-/async-3.2.1.tgz#d3274ec66d107a47476a4c49136aacdb00665fc8" | ||
integrity sha512-XdD5lRO/87udXCMC9meWdYiR+Nq6ZjUfXidViUZGu2F1MO4T3XwZ1et0hb2++BgLfhyJwy44BGB/yx80ABx8hg== | ||
|
||
asynckit@^0.4.0: | ||
version "0.4.0" | ||
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async module is pretty awesome!