Skip to content

Commit

Permalink
chore: add generate-patches.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon Si committed Sep 28, 2023
1 parent 3b2eb3f commit fe4c75d
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 9 deletions.
14 changes: 10 additions & 4 deletions packages/patching/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
{
"name": "@-/patching",
"type": "module",
"type": "module",
"dependencies": {
"pathe": "^1.1.1",
"replace-in-file": "^7.0.1",
"tmp-promise": "^3.0.3",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.1",
"@ts-monorepo/get-monorepo-packages": "^1.0.0",
"acorn": "^8.10.0",
"desm": "^1.3.0",
"escape-string-regexp": "^5.0.0",
"estree-walker": "^3.0.3",
"execa": "^8.0.1",
"map-obj": "^5.0.2",
"execa": "^8.0.1",
"glob": "^10.3.10",
"map-obj": "^5.0.2",
"outdent": "^0.8.0",
"tilde-imports": "^3.1.0",
"tilde-imports": "^3.1.0",
"rollup": "^3.29.3",
"rollup-plugin-esbuild": "^6.0.1"
},
"devDependencies": {
"commander": "^11.0.0"
}
}
41 changes: 41 additions & 0 deletions packages/patching/scripts/generate-patches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env -S pnpm exec tsx

import { program } from "commander";

import { patches } from "~/patches/$.ts";
import { generatePatch } from "~/utils/patch.ts";

await program
.requiredOption(
"--monorepo-dir <monorepoDirpath>",
"The path to the monorepo directory"
)
.requiredOption(
"--patches-dir <patchesDirpath>",
"The path to the patches directory"
)
.argument(
"[patch-ids...]",
"The IDs of the patches to generate. If not provided, all patches will be generates."
)
.action(
async (
patchIds: string[],
options: { patchesDir: string; monorepoDir: string }
) => {
const patchIdsToGenerate =
patchIds.length > 0 ? patchIds : Object.keys(patches);

for (const patchId of patchIdsToGenerate) {
console.info(`Generating patch "${patchId}"...`);
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-unnecessary-condition -- We need to apply patches synchronously to prevent race conditions for updating files
await generatePatch({
patchId: patchId as keyof typeof patches,
monorepoDirpath: options.monorepoDir,
patchesDirpath: options.patchesDir,
});
console.info(`Successfully generated patch "${patchId}"!`);
}
}
)
.parseAsync();
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-check

// @ts-expect-error: works
const { getMonorepoDirpath } = require('get-monorepo-root');
// @ts-expect-error: works
const { createTildeImportExpander } = require('tilde-imports');

module.exports = function createGetPackagePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,63 @@ const { globSync } = require('glob');
const trimExtension = require('trim-extension');
// @ts-expect-error: works
const { getMonorepoDirpath } = require('get-monorepo-root');
const {
getMonorepoPackagesDirpathsSync
// @ts-expect-error: works
} = require('@ts-monorepo/get-monorepo-packages');

module.exports = function createReadFileWrapper() {
const monorepoDirpath = getMonorepoDirpath(__dirname)
const monorepoDirpath = getMonorepoDirpath(__dirname);
if (monorepoDirpath === undefined) {
throw new Error('Could not find monorepo directory');
}

const monorepoPackagesDirpaths = getMonorepoPackagesDirpathsSync({
monorepoDirpath
});

/** @param {string} filepath */
const pathToIdentifier = (filepath) =>
`__${filepath.replaceAll(/[^\w$]/g, '_')}`;

// @ts-expect-error: This function is defined in the TypeScript patch file
// eslint-disable-next-line no-undef -- This function is defined in the TypeScript patch file
const _readFile = readFile;

/**
@param {string} filepath
*/
return function (filepath) {
if (path.basename(filepath).startsWith('__virtual__:')) {
if (/tsconfig\..*\.json$/.test(path.basename(filepath))) {
const tsconfig = JSON.parse(_readFile(filepath));
if (tsconfig.tsconfigInclude !== undefined) {
tsconfig.include ??= [];
const tsconfigIncludes = [tsconfig.tsconfigInclude].flat();

for (const monorepoPackageDirpath of monorepoPackagesDirpaths) {
const monorepoPackageJson = JSON.parse(
_readFile(path.join(monorepoPackageDirpath, 'package.json'))
);

if (
tsconfigIncludes.some(
(tsconfigInclude) =>
(tsconfigInclude === 'default' &&
monorepoPackageJson.tsconfig === undefined) ||
monorepoPackageJson.tsconfig === tsconfigInclude
)
) {
tsconfig.include.push(
path.relative(monorepoDirpath, monorepoPackageDirpath) + '/**/*'
);
}
}

delete tsconfig.tsconfigInclude;
}

return JSON.stringify(tsconfig, null, '\t');
} else if (path.basename(filepath).startsWith('__virtual__:')) {
const virtualFileType = /** @type {'matches' | 'files' | 'filepaths'} */ (
(trimExtension.default ?? trimExtension)(
path.basename(filepath).replace('__virtual__:', '')
Expand Down Expand Up @@ -104,9 +145,7 @@ module.exports = function createReadFileWrapper() {
virtualFileContentLines.push('export {};');
return virtualFileContentLines.join('\n');
} else {
// @ts-expect-error: This function is defined in the TypeScript patch file
// eslint-disable-next-line no-undef -- This function is defined in the TypeScript patch file
return readFile(filepath);
return _readFile(filepath);
}
};
};
Loading

0 comments on commit fe4c75d

Please sign in to comment.