2019-07-13 15:00:43 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
const lockfile = require("@yarnpkg/lockfile");
|
|
|
|
const { docopt } = require("docopt");
|
|
|
|
const deepEqual = require("deep-equal");
|
|
|
|
const R = require("ramda");
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const fixPkgAddMissingSha1 = require("../lib/fixPkgAddMissingSha1");
|
|
|
|
const mapObjIndexedReturnArray = require("../lib/mapObjIndexedReturnArray");
|
|
|
|
const generateNix = require("../lib/generateNix");
|
2019-07-13 15:00:43 +02:00
|
|
|
|
|
|
|
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
|
2022-10-27 02:30:06 +02:00
|
|
|
`;
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const options = docopt(USAGE);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const data = fs.readFileSync(options["--lockfile"], "utf8");
|
2019-07-13 15:00:43 +02:00
|
|
|
|
|
|
|
// 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'
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const json = lockfile.parse(data);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
if (json.type !== "success") {
|
|
|
|
throw new Error("yarn.lock parse error");
|
2019-07-13 15:00:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for missing hashes in the yarn.lock and patch if necessary
|
|
|
|
|
2021-10-08 14:18:57 +02:00
|
|
|
let pkgs = R.pipe(
|
2019-07-13 15:00:43 +02:00
|
|
|
mapObjIndexedReturnArray((value, key) => ({
|
|
|
|
...value,
|
2022-10-27 02:30:06 +02:00
|
|
|
nameWithVersion: key
|
2019-07-13 15:00:43 +02:00
|
|
|
})),
|
2022-10-27 02:30:06 +02:00
|
|
|
R.uniqBy(R.prop("resolved"))
|
|
|
|
)(json.object);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
(async () => {
|
|
|
|
if (!options["--no-patch"]) {
|
|
|
|
pkgs = await Promise.all(R.map(fixPkgAddMissingSha1, pkgs));
|
2021-10-08 14:18:57 +02:00
|
|
|
}
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
const origJson = lockfile.parse(data);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
|
|
|
if (!deepEqual(origJson, json)) {
|
2022-10-27 02:30:06 +02:00
|
|
|
console.error("found changes in the lockfile", options["--lockfile"]);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
if (options["--no-patch"]) {
|
|
|
|
console.error("...aborting");
|
|
|
|
process.exit(1);
|
2019-07-13 15:00:43 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
fs.writeFileSync(options["--lockfile"], lockfile.stringify(json.object));
|
2019-07-13 15:00:43 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
if (!options["--no-nix"]) {
|
2019-07-13 15:00:43 +02:00
|
|
|
// print to stdout
|
2022-10-27 02:30:06 +02:00
|
|
|
console.log(generateNix(pkgs, options["--builtin-fetchgit"]));
|
2019-07-13 15:00:43 +02:00
|
|
|
}
|
|
|
|
})().catch(error => {
|
2022-10-27 02:30:06 +02:00
|
|
|
console.error(error);
|
2019-07-13 15:00:43 +02:00
|
|
|
|
2022-10-27 02:30:06 +02:00
|
|
|
process.exit(1);
|
|
|
|
});
|