Skip to content

Commit d463ea4

Browse files
authored
Adds support for user-defined wrappers in the SDK (#6370)
## What's the problem this PR addresses? At work we're patching the SDK files to also send monitoring information to our servers (so we can know how much time TS takes to run within VSCode). This makes updating the SDK a little difficult, as we need to remember to re-add the patched code after each update. ## How did you fix it? The SDK will now automatically require the optional `user.sdk.cjs` file if present, which must be located next to the file to patch. ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [ ] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [ ] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [ ] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 04626c2 commit d463ea4

File tree

11 files changed

+79
-11
lines changed

11 files changed

+79
-11
lines changed

.yarn/sdks/eslint/bin/eslint.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real eslint/bin/eslint.js your application uses
27-
module.exports = absRequire(`eslint/bin/eslint.js`);
32+
module.exports = wrapWithUserWrapper(absRequire(`eslint/bin/eslint.js`));

.yarn/sdks/eslint/lib/api.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real eslint your application uses
27-
module.exports = absRequire(`eslint`);
32+
module.exports = wrapWithUserWrapper(absRequire(`eslint`));

.yarn/sdks/eslint/lib/unsupported-api.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real eslint/use-at-your-own-risk your application uses
27-
module.exports = absRequire(`eslint/use-at-your-own-risk`);
32+
module.exports = wrapWithUserWrapper(absRequire(`eslint/use-at-your-own-risk`));

.yarn/sdks/typescript/bin/tsc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real typescript/bin/tsc your application uses
27-
module.exports = absRequire(`typescript/bin/tsc`);
32+
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsc`));

.yarn/sdks/typescript/bin/tsserver

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real typescript/bin/tsserver your application uses
27-
module.exports = absRequire(`typescript/bin/tsserver`);
32+
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsserver`));

.yarn/sdks/typescript/lib/tsc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real typescript/lib/tsc.js your application uses
27-
module.exports = absRequire(`typescript/lib/tsc.js`);
32+
module.exports = wrapWithUserWrapper(absRequire(`typescript/lib/tsc.js`));

.yarn/sdks/typescript/lib/tsserver.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,7 +24,15 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

26-
const moduleWrapper = tsserver => {
27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
31+
const moduleWrapper = exports => {
32+
return wrapWithUserWrapper(moduleWrapperFn(exports));
33+
};
34+
35+
const moduleWrapperFn = tsserver => {
2736
if (!process.versions.pnp) {
2837
return tsserver;
2938
}

.yarn/sdks/typescript/lib/tsserverlibrary.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,7 +24,15 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

26-
const moduleWrapper = tsserver => {
27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
31+
const moduleWrapper = exports => {
32+
return wrapWithUserWrapper(moduleWrapperFn(exports));
33+
};
34+
35+
const moduleWrapperFn = tsserver => {
2736
if (!process.versions.pnp) {
2837
return tsserver;
2938
}

.yarn/sdks/typescript/lib/typescript.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
88
const relPnpApiPath = "../../../../.pnp.cjs";
99

1010
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
11+
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
1112
const absRequire = createRequire(absPnpApiPath);
1213

1314
const absPnpLoaderPath = resolve(absPnpApiPath, `../.pnp.loader.mjs`);
@@ -23,5 +24,9 @@ if (existsSync(absPnpApiPath)) {
2324
}
2425
}
2526

27+
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
28+
? exports => absRequire(absUserWrapperPath)(exports)
29+
: exports => exports;
30+
2631
// Defer to the real typescript your application uses
27-
module.exports = absRequire(`typescript`);
32+
module.exports = wrapWithUserWrapper(absRequire(`typescript`));

.yarn/versions/14723858.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
releases:
2+
"@yarnpkg/sdks": minor

packages/yarnpkg-sdks/sources/generateSdk.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const TEMPLATE = (relPnpApiPath: PortablePath, module: string, {setupEnv = false
125125
`const relPnpApiPath = ${JSON.stringify(npath.fromPortablePath(relPnpApiPath))};\n`,
126126
`\n`,
127127
`const absPnpApiPath = resolve(__dirname, relPnpApiPath);\n`,
128+
`const absUserWrapperPath = resolve(__dirname, \`./sdk.user.cjs\`);\n`,
128129
`const absRequire = createRequire(absPnpApiPath);\n`,
129130
`\n`,
130131
`const absPnpLoaderPath = resolve(absPnpApiPath, \`../.pnp.loader.mjs\`);\n`,
@@ -167,12 +168,24 @@ const TEMPLATE = (relPnpApiPath: PortablePath, module: string, {setupEnv = false
167168
] : []),
168169
`}\n`,
169170
`\n`,
171+
`const wrapWithUserWrapper = existsSync(absUserWrapperPath)\n`,
172+
` ? exports => absRequire(absUserWrapperPath)(exports)\n`,
173+
` : exports => exports;\n`,
174+
`\n`,
170175
...(wrapModule ? [
171-
`const moduleWrapper = ${wrapModule.trim().replace(/^ {4}/gm, ``)}\n`,
176+
`const moduleWrapper = exports => {\n`,
177+
` return wrapWithUserWrapper(moduleWrapperFn(exports));\n`,
178+
`};\n`,
179+
`\n`,
180+
`const moduleWrapperFn = ${wrapModule.trim().replace(/^ {4}/gm, ``)}\n`,
172181
`\n`,
173182
] : []),
174183
`// Defer to the real ${module} your application uses\n`,
175-
wrapModule ? `module.exports = moduleWrapper(absRequire(\`${module}\`));\n` : `module.exports = absRequire(\`${module}\`);\n`,
184+
...(wrapModule ? [
185+
`module.exports = moduleWrapper(absRequire(\`${module}\`));\n`,
186+
] : [
187+
`module.exports = wrapWithUserWrapper(absRequire(\`${module}\`));\n`,
188+
]),
176189
].join(``);
177190

178191
export type GenerateBaseWrapper = (pnpApi: PnpApi, target: PortablePath) => Promise<Wrapper>;

0 commit comments

Comments
 (0)