summary refs log tree commit diff
path: root/pkgs/development/tools/yarn2nix-moretea/yarn2nix/bin/yarn2nix.js
blob: d4f160b124d75ca7a8a1382ee0312b70f81fd734 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node

const fs = require('fs')
const lockfile = require('@yarnpkg/lockfile')
const { docopt } = require('docopt')
const deepEqual = require('deep-equal')
const R = require('ramda')

const fixPkgAddMissingSha1 = require('../lib/fixPkgAddMissingSha1')
const mapObjIndexedReturnArray = require('../lib/mapObjIndexedReturnArray')
const generateNix = require('../lib/generateNix')

const USAGE = `
Usage: yarn2nix [options]

Options:
  -h --help           Shows this help.
  --no-nix            Hide the nix output
  --no-patch          Don't patch the lockfile if hashes are missing
  --lockfile=FILE     Specify path to the lockfile [default: ./yarn.lock].
  --builtin-fetchgit  Use builtin fetchGit for git dependencies to support on-the-fly generation of yarn.nix without an internet connection
`

const options = docopt(USAGE)

const data = fs.readFileSync(options['--lockfile'], 'utf8')

// json example:

// {
//   type:'success',
//   object:{
//     'abbrev@1':{
//       version:'1.0.9',
//       resolved:'https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135'
//     },
//     'shell-quote@git+https://github.com/srghma/node-shell-quote.git#without_unlicenced_jsonify':{
//       version:'1.6.0',
//       resolved:'git+https://github.com/srghma/node-shell-quote.git#0aa381896e0cd7409ead15fd444f225807a61e0a'
//     },
//     '@graphile/plugin-supporter@git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git':{
//       version:'1.6.0',
//       resolved:'git+https://1234user:1234pass@git.graphile.com/git/users/1234user/postgraphile-supporter.git#1234commit'
//     },
//   }
// }

const json = lockfile.parse(data)

if (json.type !== 'success') {
  throw new Error('yarn.lock parse error')
}

// Check for missing hashes in the yarn.lock and patch if necessary

const pkgs = R.pipe(
  mapObjIndexedReturnArray((value, key) => ({
    ...value,
    nameWithVersion: key,
  })),
  R.uniqBy(R.prop('resolved')),
)(json.object)

const fixedPkgsPromises = R.map(fixPkgAddMissingSha1, pkgs)

;(async () => {
  const fixedPkgs = await Promise.all(fixedPkgsPromises)

  const origJson = lockfile.parse(data)

  if (!deepEqual(origJson, json)) {
    console.error('found changes in the lockfile', options['--lockfile'])

    if (options['--no-patch']) {
      console.error('...aborting')
      process.exit(1)
    }

    fs.writeFileSync(options['--lockfile'], lockfile.stringify(json.object))
  }

  if (!options['--no-nix']) {
    // print to stdout
    console.log(generateNix(fixedPkgs, options['--builtin-fetchgit']))
  }
})().catch(error => {
  console.error(error)

  process.exit(1)
})