superfile: 1.1.6 -> 1.1.7 (#371308)
[NixPkgs.git] / pkgs / build-support / node / fetch-yarn-deps / fixup.js
blob0b87393b1fff60e374cd2ecb9e24800a1c50a8f6
1 #!/usr/bin/env node
2 'use strict'
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
21                                 }
22                                 return [dep, pkg]
23                         }
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}`)
28                                 delete pkg.integrity
29                         }
31                         if (verbose) console.log(`Rewriting URL ${url} for dependency ${dep}`)
32                         pkg.resolved = urlToName(url)
33                         if (hash)
34                                 pkg.resolved += `#${hash}`
36                         return [dep, pkg]
37                 })
38         )
40         if (verbose) console.log('Done')
42         return fixedData
45 const showUsage = async () => {
46         process.stderr.write(`
47 syntax: fixup-yarn-lock [path to yarn.lock] [options]
49 Options:
50   -h --help         Show this help
51   -v --verbose      Verbose output
53         process.exit(1)
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') {
61                         verbose = true
62                 } else if (next == '--help' || next == '-h') {
63                         showUsage()
64                 } else if (!lockFile) {
65                         lockFile = next
66                 } else {
67                         showUsage()
68                 }
69         }
70         let lockContents
71         try {
72                 lockContents = await fs.promises.readFile(lockFile || 'yarn.lock', 'utf-8')
73         } catch {
74                 showUsage()
75         }
77         const fixedData = await fixupYarnLock(lockContents, verbose)
78         await fs.promises.writeFile(lockFile || 'yarn.lock', lockfile.stringify(fixedData))
81 main()
82         .catch(e => {
83                 console.error(e)
84                 process.exit(1)
85         })