4 const fs = require('fs')
5 const process = require('process')
6 const lockfile = require('./yarnpkg-lockfile.js')
7 const { urlToName } = require('./common.js')
9 const fixupYarnLock = async (lockContents, verbose) => {
10 const lockData = lockfile.parse(lockContents)
12 const fixedData = Object.fromEntries(
13 Object.entries(lockData.object)
14 .map(([dep, pkg]) => {
15 if (pkg.resolved === undefined) {
16 console.warn(`no resolved URL for package ${dep}`)
17 var maybeFile = dep.split("@", 2)[1]
18 if (maybeFile.startsWith("file:")) {
19 console.log(`Rewriting URL for local file dependency ${dep}`)
20 pkg.resolved = maybeFile
24 const [ url, hash ] = pkg.resolved.split("#", 2)
26 if (hash || url.startsWith("https://codeload.github.com/")) {
27 if (verbose) console.log(`Removing integrity for git dependency ${dep}`)
31 if (verbose) console.log(`Rewriting URL ${url} for dependency ${dep}`)
32 pkg.resolved = urlToName(url)
34 pkg.resolved += `#${hash}`
40 if (verbose) console.log('Done')
45 const showUsage = async () => {
46 process.stderr.write(`
47 syntax: fixup-yarn-lock [path to yarn.lock] [options]
50 -h --help Show this help
51 -v --verbose Verbose output
56 const main = async () => {
57 const args = process.argv.slice(2)
58 let next, lockFile, verbose
59 while (next = args.shift()) {
60 if (next == '--verbose' || next == '-v') {
62 } else if (next == '--help' || next == '-h') {
64 } else if (!lockFile) {
72 lockContents = await fs.promises.readFile(lockFile || 'yarn.lock', 'utf-8')
77 const fixedData = await fixupYarnLock(lockContents, verbose)
78 await fs.promises.writeFile(lockFile || 'yarn.lock', lockfile.stringify(fixedData))