forked from logto-io/logto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.pnpmfile.cjs
55 lines (47 loc) · 1.49 KB
/
.pnpmfile.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// See https://pnpm.io/pnpmfile
const fs = require('node:fs/promises');
const path = require('node:path');
// Types are inspected and edited from https://github.com/pnpm/pnpm/blob/ef6c22e129dc3d76998cee33647b70a66d1f36bf/hooks/pnpmfile/src/requireHooks.ts
/**
* @typedef {Object} HookContext
* @property {(message: string) => void} log
*/
/**
* @typedef {Object} Hooks
* @property {((pkg: unknown, context: HookContext) => unknown)=} readPackage
*/
const isObject = (value) => value !== null && typeof value === 'object';
/** @type Hooks */
const hooks = { readPackage: async (pkg) => {
// Skip if not a connector package
if (!(
isObject(pkg) &&
'name' in pkg &&
String(pkg.name) !== '@logto/connector-kit' &&
String(pkg.name).startsWith('@logto/connector-')
)) {
return pkg;
}
// Apply connector's `package.json` to the template
const result = JSON.parse(
// Use `__dirname` since the `pnpm i` command may be executed in nested workspace directories
await fs.readFile(path.join(__dirname, 'packages/connectors/templates/package.json'), 'utf8')
);
for (const [key, value] of Object.entries(pkg)) {
if (key === '$schema') {
continue;
}
// Shallow merge
if (Array.isArray(result[key])) {
result[key] = [...result[key], ...value];
} else if (typeof value === 'object' && value !== null) {
result[key] = { ...result[key], ...value };
} else {
result[key] = value;
}
}
return result;
} };
module.exports = {
hooks,
};