Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.writeFile = async function writeFile(target: PortablePath, body: string | Buffer): Promise {
await xfs.mkdirpPromise(ppath.dirname(target));
await xfs.writeFilePromise(target, body);
};
async execute() {
const target = npath.toPortablePath(path.resolve(this.target));
if (await xfs.existsPromise(target)) {
const listing = await xfs.readdirPromise(target);
if (listing.length !== 0) {
throw new UsageError(`The target directory (${this.target}) isn't empty; aborting the scaffolding.`);
}
}
await xfs.mkdirpPromise(target);
await xfs.mkdirpPromise(ppath.join(target, `sources` as Filename));
await xfs.writeFilePromise(ppath.join(target, `sources/index.ts` as Filename), [
`import {CommandContext, Plugin} from '@yarnpkg/core';\n`,
`import {Command} from 'clipanion';\n`,
`\n`,
`class HelloWorldCommand extends Command {\n`,
` @Command.String(\`--name\`)\n`,
` name: string = \`John Doe\`;\n`,
`\n`,
` @Command.Path(\`hello\`, \`world\`)\n`,
` async execute() {\n`,
` console.log(\`Hello \${this.name}!\`);\n`,
` }\n`,
`}\n`,
`\n`,
`const plugin: Plugin = {\n`,
` hooks: {\n`,
` afterAllInstalled: () => {\n`,
async executeProxy(configuration: Configuration) {
if (configuration.get(`yarnPath`) !== null)
throw new UsageError(`Cannot use the --install flag when the current directory already uses yarnPath (from ${configuration.sources.get(`yarnPath`)})`);
if (configuration.projectCwd !== null)
throw new UsageError(`Cannot use the --install flag when the current directory is already part of a project`);
if (!xfs.existsSync(this.context.cwd))
await xfs.mkdirpPromise(this.context.cwd);
const lockfilePath = ppath.join(this.context.cwd, configuration.get(`lockfileFilename`));
if (!xfs.existsSync(lockfilePath))
await xfs.writeFilePromise(lockfilePath, ``);
const versionExitCode = await this.cli.run([`set`, `version`, this.install!]);
if (versionExitCode !== 0)
return versionExitCode;
this.context.stdout.write(`\n`);
const args: Array = [];
if (this.private)
args.push(`-p`);
if (this.yes)
args.push(`-y`);
const {code} = await execUtils.pipevp(`yarn`, [`init`, ...args], {
cwd: this.context.cwd,
stdin: this.context.stdin,
const writeLocatorState = async (locatorStatePath: PortablePath, locatorMap: NodeModulesLocatorMap): Promise => {
let locatorState = '# Warning: This file is automatically generated. Removing it is fine, but will\n';
locatorState += '# cause your node_modules installation to become invalidated.\n';
locatorState += '\n__metadata:\n';
locatorState += ' version: 1\n';
for (const [locator, value] of locatorMap.entries()) {
locatorState += `\n"${locator}":\n`;
locatorState += ` size: ${value.size}\n`;
locatorState += ` locations:\n${Array.from(value.locations).map(loc => ` - "${loc}"\n`).join('')}`;
}
await xfs.writeFilePromise(locatorStatePath, locatorState);
};
}
const vmExports = {} as any;
const vmModule = {exports: vmExports};
runInNewContext(pluginBuffer.toString(), {
module: vmModule,
exports: vmExports,
});
const relativePath = `.yarn/plugins/${vmModule.exports.name}.js` as PortablePath;
const absolutePath = ppath.resolve(project.cwd, relativePath);
report.reportInfo(MessageName.UNNAMED, `Saving the new plugin in ${configuration.format(relativePath, `magenta`)}`);
await xfs.mkdirpPromise(ppath.dirname(absolutePath));
await xfs.writeFilePromise(absolutePath, pluginBuffer);
await Configuration.updateConfiguration(project.cwd, (current: any) => ({
plugins: (current.plugins || []).concat([
relativePath,
]),
}));
});
const generateTypescriptWrapper = async (projectRoot: PortablePath, target: PortablePath) => {
const typescript = ppath.join(target, `typescript` as PortablePath);
const manifest = ppath.join(typescript, `package.json` as PortablePath);
const tsserver = ppath.join(typescript, `lib/tsserver.js` as PortablePath);
const relPnpApiPath = ppath.relative(ppath.dirname(tsserver), ppath.join(projectRoot, `.pnp.js` as Filename));
await xfs.mkdirpPromise(ppath.dirname(tsserver));
await xfs.writeFilePromise(manifest, JSON.stringify({name: 'typescript', version: `${dynamicRequire('typescript/package.json').version}-pnpify`}, null, 2));
await xfs.writeFilePromise(tsserver, TEMPLATE(relPnpApiPath, "typescript/lib/tsserver", {usePnpify: true}));
await addVSCodeWorkspaceSettings(projectRoot, {'typescript.tsdk': npath.fromPortablePath(ppath.relative(projectRoot, ppath.dirname(tsserver)))});
};
export const generateEslintWrapper = async (projectRoot: PortablePath, target: PortablePath) => {
const eslint = ppath.join(target, `eslint` as PortablePath);
const manifest = ppath.join(eslint, `package.json` as PortablePath);
const api = ppath.join(eslint, `lib/api.js` as PortablePath);
const relPnpApiPath = ppath.relative(ppath.dirname(api), ppath.join(projectRoot, `.pnp.js` as Filename));
await xfs.mkdirpPromise(ppath.dirname(api));
await xfs.writeFilePromise(manifest, JSON.stringify({name: 'eslint', version: `${dynamicRequire('eslint/package.json').version}-pnpify`, main: 'lib/api.js'}, null, 2));
await xfs.writeFilePromise(api, TEMPLATE(relPnpApiPath, "eslint", {usePnpify: false}));
await addVSCodeWorkspaceSettings(projectRoot, {'eslint.nodePath': npath.fromPortablePath(ppath.relative(projectRoot, ppath.dirname(eslint)))});
};
async execute() {
const tmpDir = await createTemporaryDirectory(toFilename(`dlx-${process.pid}`));
try {
await xfs.writeFilePromise(ppath.join(tmpDir, toFilename(`package.json`)), `{}\n`);
await xfs.writeFilePromise(ppath.join(tmpDir, toFilename(`yarn.lock`)), ``);
await xfs.writeFilePromise(ppath.join(tmpDir, toFilename(`.yarnrc.yml`)), `enableGlobalCache: true\n`);
const pkgs = typeof this.pkg !== `undefined`
? [this.pkg]
: [this.command];
const command = structUtils.parseDescriptor(this.command).name;
const addExitCode = await this.cli.run([`add`, `--`, ...pkgs], {cwd: tmpDir, quiet: this.quiet});
if (addExitCode !== 0)
return addExitCode;
if (!this.quiet)
this.context.stdout.write(`\n`);
const configuration = await Configuration.find(tmpDir, this.context.plugins);
const {workspace} = await Project.find(configuration, tmpDir);
async function makePathWrapper(location: PortablePath, name: Filename, argv0: NativePath, args: Array = []) {
if (process.platform === `win32`) {
await xfs.writeFilePromise(ppath.format({dir: location, name, ext: '.cmd'}), `@"${argv0}" ${args.join(` `)} %*\n`);
} else {
await xfs.writeFilePromise(ppath.join(location, name), `#!/bin/sh\nexec "${argv0}" ${args.map(arg => `'${arg.replace(/'/g, `'"'"'`)}'`).join(` `)} "$@"\n`);
await xfs.chmodPromise(ppath.join(location, name), 0o755);
}
}
async function makePathWrapper(location: PortablePath, name: Filename, argv0: NativePath, args: Array = []) {
if (process.platform === `win32`) {
await xfs.writeFilePromise(ppath.format({dir: location, name, ext: '.cmd'}), `@"${argv0}" ${args.join(` `)} %*\n`);
} else {
await xfs.writeFilePromise(ppath.join(location, name), `#!/bin/sh\nexec "${argv0}" ${args.map(arg => `'${arg.replace(/'/g, `'"'"'`)}'`).join(` `)} "$@"\n`);
await xfs.chmodPromise(ppath.join(location, name), 0o755);
}
}