Created
September 20, 2024 15:08
-
-
Save GauBen/8a9847abfbee0138ed2c5fa04812a500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] }; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yarn 4 plugin to provide the
yarn cache download
command as described in yarnpkg/berry#5998