console/scripts/patch-manifests.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-05-18 07:26:57 +00:00
/*
https://github.com/octokit/webhooks-methods.js/issues/45
That's why we patch package.json of @octokit/webhooks-methods and replace the value of `main` with the value from `source`.
*/
import fs from 'fs';
import { createRequire } from 'module';
2022-12-28 19:22:54 +00:00
import path from 'path';
2022-05-18 07:26:57 +00:00
function patchPackage(name, patchFn) {
const require = createRequire(import.meta.url);
const indexFile = require.resolve(name, {
paths: [path.join(process.cwd(), 'node_modules', '.pnpm', 'node_modules')],
});
2022-05-18 07:26:57 +00:00
const nameParts = name.split('/');
const packagePath = findPackageJson(path.dirname(indexFile), nameParts[nameParts.length - 1]);
2022-05-18 07:26:57 +00:00
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
patchFn(pkg);
fs.writeFileSync(packagePath, JSON.stringify(pkg, null, 2), 'utf8');
console.log(`[patch-manifests] Patched ${name}`);
}
function findPackageJson(dirname, until) {
const possiblePath = path.join(dirname, 'package.json');
if (fs.existsSync(possiblePath)) {
return possiblePath;
}
if (dirname.endsWith(until)) {
throw new Error(`Package.json file not found. Reached ${dirname}`);
}
return findPackageJson(path.resolve(dirname, '..'), until);
}
/*
https://github.com/octokit/webhooks-methods.js/issues/45
That's why we patch package.json of universal-github-app-jwt and replace the value of `main` with the value from `source`.
*/
patchPackage('universal-github-app-jwt', pkg => {
2022-05-18 07:26:57 +00:00
delete pkg.module;
});
/*
TSUP (but really esbuild) bundles all node_modules, this is expected, we want that.
Unfortunately, `apollo-graphql` and `@apollo/*` libraries are CJS only, and we end up with CJS and ESM versions of graphql.
2022-05-18 07:26:57 +00:00
The very quick fix means we need to patch the graphql module to be CJS-only.
*/
patchPackage('graphql', pkg => {
2022-05-18 07:26:57 +00:00
pkg.main = 'index.js';
delete pkg.module;
});