Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for user-defined wrappers in the SDK #6370

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .yarn/sdks/eslint/bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real eslint/bin/eslint.js your application uses
module.exports = absRequire(`eslint/bin/eslint.js`);
module.exports = wrapWithUserWrapper(absRequire(`eslint/bin/eslint.js`));
7 changes: 6 additions & 1 deletion .yarn/sdks/eslint/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real eslint your application uses
module.exports = absRequire(`eslint`);
module.exports = wrapWithUserWrapper(absRequire(`eslint`));
7 changes: 6 additions & 1 deletion .yarn/sdks/eslint/lib/unsupported-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real eslint/use-at-your-own-risk your application uses
module.exports = absRequire(`eslint/use-at-your-own-risk`);
module.exports = wrapWithUserWrapper(absRequire(`eslint/use-at-your-own-risk`));
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/bin/tsc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/bin/tsc your application uses
module.exports = absRequire(`typescript/bin/tsc`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsc`));
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/bin/tsserver
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/bin/tsserver your application uses
module.exports = absRequire(`typescript/bin/tsserver`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/bin/tsserver`));
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/lib/tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript/lib/tsc.js your application uses
module.exports = absRequire(`typescript/lib/tsc.js`);
module.exports = wrapWithUserWrapper(absRequire(`typescript/lib/tsc.js`));
11 changes: 10 additions & 1 deletion .yarn/sdks/typescript/lib/tsserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const moduleWrapper = tsserver => {
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

const moduleWrapper = exports => {
return wrapWithUserWrapper(moduleWrapperFn(exports));
};

const moduleWrapperFn = tsserver => {
if (!process.versions.pnp) {
return tsserver;
}
Expand Down
11 changes: 10 additions & 1 deletion .yarn/sdks/typescript/lib/tsserverlibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const moduleWrapper = tsserver => {
const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

const moduleWrapper = exports => {
return wrapWithUserWrapper(moduleWrapperFn(exports));
};

const moduleWrapperFn = tsserver => {
if (!process.versions.pnp) {
return tsserver;
}
Expand Down
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/lib/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {pathToFileURL} = require(`url`);
const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absUserWrapperPath = resolve(__dirname, `./sdk.user.cjs`);
const absRequire = createRequire(absPnpApiPath);

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

const wrapWithUserWrapper = existsSync(absUserWrapperPath)
? exports => absRequire(absUserWrapperPath)(exports)
: exports => exports;

// Defer to the real typescript your application uses
module.exports = absRequire(`typescript`);
module.exports = wrapWithUserWrapper(absRequire(`typescript`));
2 changes: 2 additions & 0 deletions .yarn/versions/14723858.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@yarnpkg/sdks": minor
17 changes: 15 additions & 2 deletions packages/yarnpkg-sdks/sources/generateSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const TEMPLATE = (relPnpApiPath: PortablePath, module: string, {setupEnv = false
`const relPnpApiPath = ${JSON.stringify(npath.fromPortablePath(relPnpApiPath))};\n`,
`\n`,
`const absPnpApiPath = resolve(__dirname, relPnpApiPath);\n`,
`const absUserWrapperPath = resolve(__dirname, \`./sdk.user.cjs\`);\n`,
`const absRequire = createRequire(absPnpApiPath);\n`,
`\n`,
`const absPnpLoaderPath = resolve(absPnpApiPath, \`../.pnp.loader.mjs\`);\n`,
Expand Down Expand Up @@ -167,12 +168,24 @@ const TEMPLATE = (relPnpApiPath: PortablePath, module: string, {setupEnv = false
] : []),
`}\n`,
`\n`,
`const wrapWithUserWrapper = existsSync(absUserWrapperPath)\n`,
` ? exports => absRequire(absUserWrapperPath)(exports)\n`,
` : exports => exports;\n`,
`\n`,
...(wrapModule ? [
`const moduleWrapper = ${wrapModule.trim().replace(/^ {4}/gm, ``)}\n`,
`const moduleWrapper = exports => {\n`,
` return wrapWithUserWrapper(moduleWrapperFn(exports));\n`,
`};\n`,
`\n`,
`const moduleWrapperFn = ${wrapModule.trim().replace(/^ {4}/gm, ``)}\n`,
`\n`,
] : []),
`// Defer to the real ${module} your application uses\n`,
wrapModule ? `module.exports = moduleWrapper(absRequire(\`${module}\`));\n` : `module.exports = absRequire(\`${module}\`);\n`,
...(wrapModule ? [
`module.exports = moduleWrapper(absRequire(\`${module}\`));\n`,
] : [
`module.exports = wrapWithUserWrapper(absRequire(\`${module}\`));\n`,
]),
].join(``);

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