Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const cloneModule = async (srcDir: PortablePath, dstDir: PortablePath, options?: { keepNodeModules?: boolean, innerLoop?: boolean }) => {
try {
if (!options || !options.innerLoop) {
await removeDir(dstDir, {excludeNodeModules: options && options.keepNodeModules});
await xfs.mkdirpPromise(dstDir, {chmod: 0o777});
}
const entries = await xfs.readdirPromise(srcDir, {withFileTypes: true});
for (const entry of entries) {
const entryName = toFilename(entry.name);
const src = ppath.join(srcDir, entryName);
const dst = ppath.join(dstDir, entryName);
if (entryName !== NODE_MODULES || !options || !options.keepNodeModules) {
if (entry.isDirectory()) {
await xfs.mkdirPromise(dst);
await xfs.chmodPromise(dst, 0o777);
await cloneModule(src, dst, {keepNodeModules: false, innerLoop: true});
} else {
await xfs.copyFilePromise(src, dst, fs.constants.COPYFILE_FICLONE);
}
}
}
} catch (e) {
if (!options || !options.innerLoop)
async cacheCleanup({cache, report}: InstallOptions) {
const PRESERVED_FILES = new Set([
`.gitignore`,
]);
if (!xfs.existsSync(cache.cwd))
return;
if (!isFolderInside(cache.cwd, this.cwd))
return;
for (const entry of await xfs.readdirPromise(cache.cwd)) {
if (PRESERVED_FILES.has(entry))
continue;
const entryPath = ppath.resolve(cache.cwd, entry);
if (cache.markedFiles.has(entryPath))
continue;
if (cache.immutable) {
report.reportError(MessageName.IMMUTABLE_CACHE, `${this.configuration.format(ppath.basename(entryPath), `magenta`)} appears to be unused and would marked for deletion, but the cache is immutable`);
} else {
report.reportInfo(MessageName.UNUSED_CACHE_ENTRY, `${this.configuration.format(ppath.basename(entryPath), `magenta`)} appears to be unused - removing`);
await xfs.unlinkPromise(entryPath);
}
}
cache.markedFiles.clear();
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`,
const removeDir = async (dir: PortablePath, options?: {innerLoop?: boolean, excludeNodeModules?: boolean}): Promise => {
try {
if (options && !options.innerLoop) {
const stats = await xfs.lstatPromise(dir);
if (!stats.isDirectory()) {
await xfs.unlinkPromise(dir);
return;
}
}
const entries = await xfs.readdirPromise(dir, {withFileTypes: true});
for (const entry of entries) {
const targetPath = ppath.join(dir, toFilename(entry.name));
if (entry.isDirectory()) {
if (entry.name !== NODE_MODULES || !options || !options.excludeNodeModules) {
await removeDir(targetPath, {innerLoop: true});
}
} else {
await xfs.unlinkPromise(targetPath);
}
}
await xfs.rmdirPromise(dir);
} catch (e) {
if (e.code !== 'ENOENT' && e.code !== 'ENOTEMPTY') {
throw e;
}
}
private async shouldWarnNodeModules() {
for (const workspace of this.opts.project.workspaces) {
const nodeModulesPath = ppath.join(workspace.cwd, toFilename(`node_modules`));
if (!xfs.existsSync(nodeModulesPath))
continue;
const directoryListing = await xfs.readdirPromise(nodeModulesPath);
if (directoryListing.every(entry => entry.startsWith(`.`)))
continue;
return true;
}
return false;
}
const pnpSettings = {
blacklistedLocations,
dependencyTreeRoots,
enableTopLevelFallback,
fallbackExclusionList,
ignorePattern,
packageRegistry,
shebang,
virtualRoots,
};
const pnpUnpluggedFolder = this.opts.project.configuration.get(`pnpUnpluggedFolder`);
if (this.unpluggedPaths.size === 0) {
await xfs.removePromise(pnpUnpluggedFolder);
} else {
for (const entry of await xfs.readdirPromise(pnpUnpluggedFolder)) {
const unpluggedPath = ppath.resolve(pnpUnpluggedFolder, entry);
if (!this.unpluggedPaths.has(unpluggedPath)) {
await xfs.removePromise(unpluggedPath);
}
}
}
const nodeFs = new NodeFS(fs);
const baseFs = new ZipOpenFS({
baseFs: nodeFs,
maxOpenFiles: 80,
readOnlyArchives: true,
});
const defaultFsLayer: FakeFS = new VirtualFS({baseFs});
const rootPath = this.opts.project.cwd;