Skip to content

Instantly share code, notes, and snippets.

@GauBen
Created September 20, 2024 15:08
Show Gist options
  • Save GauBen/8a9847abfbee0138ed2c5fa04812a500 to your computer and use it in GitHub Desktop.
Save GauBen/8a9847abfbee0138ed2c5fa04812a500 to your computer and use it in GitHub Desktop.
module.exports = {
name: 'plugin-cache-download',
factory: (require) => {
const { BaseCommand } = require('@yarnpkg/cli');
const { Command } = require('clipanion');
const { Project, Configuration, Cache, ThrowReport } = require('@yarnpkg/core');
class CacheDownloadCommand extends BaseCommand {
static paths = [['cache', 'download']];
static usage = Command.Usage({
description: 'Download all packages from lockfile and cache them',
details:
'This command only needs a yarn.lock to work; it will download all packages listed in the lockfile and cache them. This is useful for Docker images to split the download and linking steps.',
});
async execute() {
// Retrieve the active project
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const { project } = await Project.find(configuration, this.context.cwd);
// This is the thing that will do the fetching
const fetcher = configuration.makeFetcher();
const fetcherOptions = {
checksums: project.storedChecksums,
project,
cache: await Cache.find(configuration),
fetcher,
report: new ThrowReport(),
};
// Get the list of all packages that can be downloaded
const remotePackages = [...project.originalPackages.values()].filter(
({ reference }) => !reference.startsWith('workspace:'),
);
this.context.stdout.write('Starting all downloads in parallel...\n');
// Fetch them all in parallel, without much consideration for limits
await Promise.all(remotePackages.map((pkg) => fetcher.fetch(pkg, fetcherOptions)));
this.context.stdout.write('All packages cached!\n');
}
}
return { commands: [CacheDownloadCommand] };
},
};
@GauBen
Copy link
Author

GauBen commented Sep 20, 2024

Yarn 4 plugin to provide the yarn cache download command as described in yarnpkg/berry#5998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment