|
| 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 | + }); |
0 commit comments