2023-04-01 07:53:44 -05:00
|
|
|
// Merge all `package.extend.json` to the template and write to `package.json`.
|
|
|
|
|
|
|
|
import fs from 'node:fs/promises';
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
|
|
const isDependencyKey = (key) => key.toLowerCase().endsWith('dependencies');
|
|
|
|
const allowedCustomKeys = Object.freeze(['name', 'version', 'description', 'author']);
|
|
|
|
|
|
|
|
// Assuming execution context `packages/connectors`
|
|
|
|
const templateJson = Object.fromEntries(
|
|
|
|
Object.entries(JSON.parse(await fs.readFile('templates/package.json'))).filter(
|
|
|
|
// Filter out dependency fields since they'll be handled by the hook in `.pnpmfile.cjs`
|
|
|
|
([key]) => !isDependencyKey(key)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const templateKeys = Object.keys(templateJson);
|
|
|
|
|
2024-03-19 02:19:41 -05:00
|
|
|
/**
|
|
|
|
* An object that contains exceptions for scripts that are allowed to be different from the template.
|
2024-05-13 04:06:04 -05:00
|
|
|
* Value format: `{ "<connector-name>": ["<script-name>"] }`
|
|
|
|
* Example: `{ "connector-oauth2": ["prepack"] }`
|
2024-03-19 02:19:41 -05:00
|
|
|
*/
|
2024-07-28 00:15:09 -05:00
|
|
|
const scriptExceptions = { 'connector-oauth2': ['prepack', 'build', 'build:test'] };
|
2024-03-19 02:19:41 -05:00
|
|
|
|
2023-04-01 07:53:44 -05:00
|
|
|
const sync = async () => {
|
|
|
|
const packagesDirectory = './';
|
|
|
|
const packages = await fs.readdir(packagesDirectory);
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
packages
|
|
|
|
.filter((packageName) => packageName.startsWith('connector-'))
|
|
|
|
.map(async (packageName) => {
|
|
|
|
const packageJsonPath = path.join(packagesDirectory, packageName, 'package.json');
|
|
|
|
|
|
|
|
// Sync package.json
|
|
|
|
const current = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
|
|
|
|
|
|
|
|
const invalidKeys = Object.keys(current).filter(
|
|
|
|
(key) =>
|
|
|
|
!isDependencyKey(key) && !allowedCustomKeys.includes(key) && !templateKeys.includes(key)
|
|
|
|
);
|
|
|
|
if (invalidKeys.length > 0) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid key${invalidKeys.length === 1 ? '' : 's'} ${invalidKeys
|
|
|
|
.map((key) => `"${key}"`)
|
|
|
|
.join(', ')} found in ${packageName}.` +
|
|
|
|
'\n' +
|
|
|
|
`Allowed custom keys are: ${allowedCustomKeys.map((key) => `"${key}"`).join(', ')}.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-19 02:19:41 -05:00
|
|
|
const scriptOverrides = scriptExceptions[packageName]
|
|
|
|
? Object.fromEntries(
|
|
|
|
scriptExceptions[packageName].map((key) => [key, current.scripts[key]])
|
|
|
|
)
|
|
|
|
: {};
|
|
|
|
|
2023-04-01 07:53:44 -05:00
|
|
|
await fs.writeFile(
|
|
|
|
packageJsonPath,
|
2024-03-19 02:19:41 -05:00
|
|
|
JSON.stringify(
|
|
|
|
{
|
|
|
|
...current,
|
|
|
|
...templateJson,
|
|
|
|
scripts: { ...templateJson.scripts, ...scriptOverrides },
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
2
|
|
|
|
) + '\n'
|
2023-04-01 07:53:44 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Copy preset
|
|
|
|
await fs.cp('templates/preset', path.join(packagesDirectory, packageName), {
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
await sync();
|