Skip to content

Commit

Permalink
Free the npm package from third party dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Jan 6, 2023
1 parent 352ae1e commit 50a72dd
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 1,061 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
**/*.rs.bk
/node_modules
/bin
/unpacked_bin
/packages/*/elm-json
/packages/*/elm-json.exe
/unpacked_bin
9 changes: 9 additions & 0 deletions bin/elm-json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node
var spawn = require("child_process").spawn;

var binPath = require("../binary.js")();

spawn(binPath, process.argv.slice(2), { stdio: "inherit" }).on(
"exit",
process.exit
);
95 changes: 95 additions & 0 deletions binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var fs = require("fs");
var package = require("./package.json");
var path = require("path");

module.exports = function () {
var os = process.env.BINWRAP_PLATFORM || process.platform;
var arch = process.env.BINWRAP_ARCH || process.arch;

var requested = `${os}-${arch}`;
var current = `${process.platform}-${process.arch}`;
if (requested !== current) {
console.error(
`WARNING: Using binaries for the requested platform (${requested}) instead of for the actual platform (${current}).`
);
}

var subPackageName = `@zwilias/elm-json-${requested}`;

if (!(subPackageName in package.optionalDependencies)) {
exitFailure(
`The elm-json npm package does not support your platform (${requested}).`
);
}

var fileName = process.platform === "win32" ? "elm-json.exe" : "elm-json";

try {
var subBinaryPath = require.resolve(`${subPackageName}/${fileName}`);
} catch (error) {
if (error && error.code === "MODULE_NOT_FOUND") {
exitFailure(missingSubPackageHelp(subPackageName));
} else {
exitFailure(
`I had trouble requiring the binary package for your platform (${subPackageName}):\n\n${error}`
);
}
}

// Yarn 2 and later ("Berry") always invokes `node` (regardless of configuration)
// so we cannot do any optimizations there.
var isYarnBerry = /\byarn\/(?!1\.)/.test(
process.env.npm_config_user_agent || ""
);

// On Windows, npm always invokes `node` so we cannot do any optimizations there either.
if (process.platform === "win32" || isYarnBerry) {
return subBinaryPath;
}

var binaryPath = path.resolve(__dirname, package.bin.elm);
var tmpPath = binaryPath + ".tmp";

try {
// Atomically replace the file with a hard link to the binary as an optimization.
fs.linkSync(subBinaryPath, tmpPath);
fs.renameSync(tmpPath, binaryPath);
} catch (error) {
exitFailure(
`I had some trouble writing file to disk. It is saying:\n\n${error}`
);
}

return binaryPath;
};

function exitFailure(message) {
console.error(
`
-- ERROR -----------------------------------------------------------------------
${message}
NOTE: You can avoid npm entirely by downloading directly from:
https://github.com/zwilias/elm-json/releases/tag/${package.version}
All this package does is distributing a file from there.
--------------------------------------------------------------------------------
`.trim()
);
process.exit(1);
}

function missingSubPackageHelp(subPackageName) {
return `
I support your platform, but I could not find the binary package (${subPackageName}) for it!
This can happen if you use the "--omit=optional" (or "--no-optional") npm flag.
The "optionalDependencies" package.json feature is used by elm-json to install the correct
binary executable for your current platform. Remove that flag to use elm-json.
This can also happen if the "node_modules" folder was copied between two operating systems
that need different binaries - including "virtual" operating systems like Docker and WSL.
If so, try installing with npm rather than copying "node_modules".
`.trim();
}
34 changes: 11 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
var binwrap = require("binwrap");
var path = require("path");
function dummy() {
return Promise.resolve();
}

var packageInfo = require(path.join(__dirname, "package.json"));
var version = packageInfo.version;
var root =
"https://github.com/zwilias/elm-json/releases/download/v" +
version +
"/elm-json-v" +
version;

module.exports = binwrap({
dirname: __dirname,
binaries: ["elm-json"],
urls: {
"darwin-arm64": root + "-aarch64-apple-darwin.tar.gz",
"darwin-x64": root + "-x86_64-apple-darwin.tar.gz",
"linux-x64": root + "-x86_64-unknown-linux-musl.tar.gz",
"linux-arm": root + "-armv7-unknown-linux-musleabihf.tar.gz",
"linux-arm64": root + "-armv7-unknown-linux-musleabihf.tar.gz",
"win32-x64": root + "-x86_64-pc-windows-msvc.tar.gz",
"win32-ia32": root + "-x86_64-pc-windows-msvc.tar.gz"
}
});
module.exports = {
paths: {
"elm-json": require("./binary.js")(),
},
install: dummy,
prepare: dummy,
test: dummy,
};
Loading

0 comments on commit 50a72dd

Please sign in to comment.