-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Npm installer #210
Merged
Merged
Npm installer #210
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can test this out by running Which should spit out |
The two files of interest are package.json and binary.js, which come out as follows: package.json{
"name": "axodotdev/cargo-dist",
"version": "0.0.5",
"description": "Shippable application packaging for Rust",
"repository": "https://github.com/axodotdev/cargo-dist",
"license": "MIT OR Apache-2.0",
"main": "index.js",
"bin": {
"rover": "run.js"
},
"scripts": {
"postinstall": "node ./install.js",
"fmt": "prettier --write **/*.js",
"fmt:check": "prettier --check **/*.js"
},
"engines": {
"node": ">=14",
"npm": ">=6"
},
"volta": {
"node": "18.14.1",
"npm": "9.5.0"
},
"dependencies": {
"axios-proxy-builder": "^0.1.1",
"binary-install": "^1.0.6",
"console.table": "^0.10.0",
"detect-libc": "^2.0.0"
},
"devDependencies": {
"prettier": "2.8.4"
}
} binary.jsconst { Binary } = require("binary-install");
const os = require("os");
const cTable = require("console.table");
const libc = require("detect-libc");
const { configureProxy } = require("axios-proxy-builder");
const error = (msg) => {
console.error(msg);
process.exit(1);
};
const { version } = require("./package.json");
// These are injected by cargo-dist
const name = "cargo-dist";
const artifact_download_url = "https://github.com/axodotdev/cargo-dist/releases/download/v0.0.5"
// These are injected by cargo-dist
const supportedPlatforms = {
"aarch64-apple-darwin": {
"artifact_name": "cargo-dist-v0.0.5-aarch64-apple-darwin.tar.xz",
"bins": ["cargo-dist"],
"zip_ext": ".tar.xz"
},
"x86_64-apple-darwin": {
"artifact_name": "cargo-dist-v0.0.5-x86_64-apple-darwin.tar.xz",
"bins": ["cargo-dist"],
"zip_ext": ".tar.xz"
},
"x86_64-pc-windows-msvc": {
"artifact_name": "cargo-dist-v0.0.5-x86_64-pc-windows-msvc.zip",
"bins": ["cargo-dist.exe"],
"zip_ext": ".zip"
},
"x86_64-unknown-linux-gnu": {
"artifact_name": "cargo-dist-v0.0.5-x86_64-unknown-linux-gnu.tar.xz",
"bins": ["cargo-dist"],
"zip_ext": ".tar.xz"
}};
const getPlatform = () => {
const raw_os_type = os.type();
const raw_architecture = os.arch();
// We want to use rust-style target triples as the canonical key
// for a platform, so translate the "os" library's concepts into rust ones
let os_type = "";
switch (raw_os_type) {
case "Windows_NT":
os_type = "pc-windows-msvc";
break;
case "Darwin":
os_type = "apple-darwin";
break;
case "Linux":
os_type = "unknown-linux-gnu"
break;
}
let arch = "";
switch (raw_architecture) {
case "x64":
arch = "x86_64";
break;
case "arm64":
arch = "aarch64";
break;
}
// Assume the above succeeded and build a target triple to look things up with.
// If any of it failed, this lookup will fail and we'll handle it like normal.
let target_triple = `${arch}-${os_type}`;
let platform = supportedPlatforms[target_triple];
if (!!platform) {
error(
`Platform with type "${raw_os_type}" and architecture "${raw_architecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${supportedPlatforms}`
);
}
// These are both situation where you might toggle to unknown-linux-musl but we don't support that yet
if (supportedPlatform.TYPE === "Linux") {
if (libc.isNonGlibcLinuxSync()) {
error("This operating system does not support dynamic linking to glibc.");
} else {
let libc_version = libc.versionSync();
let split_libc_version = libc_version.split(".");
let libc_major_version = split_libc_version[0];
let libc_minor_version = split_libc_version[1];
let min_major_version = 2;
let min_minor_version = 17;
if (
libc_major_version < min_major_version ||
libc_minor_version < min_minor_version
) {
error(
`This operating system needs glibc >= ${min_major_version}.${min_minor_version}, but only has ${libc_version} installed.`
);
}
}
}
};
const getBinary = () => {
const platform = getPlatform();
const url = `${artifact_download_url}/${platform.artifact_name}`;
if (platform.bins.length > 1) {
// Not yet supported
error("this app has multiple binaries, which isn't yet implemented");
}
let binary = new Binary(platform.bins[0], url);
return binary;
};
const install = () => {
const binary = getBinary();
const proxy = configureProxy(binary.url);
return binary.install(proxy, suppressLogs);
};
const run = () => {
const binary = getBinary();
binary.run();
};
module.exports = {
install,
run,
getBinary,
}; |
zkat
reviewed
Apr 12, 2023
Co-authored-by: Kat Marchán <[email protected]>
This was referenced May 3, 2023
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This introduces minimal experimental support for generating an NPM installer with
installer = ["npm"]
. Adding the installer to your config will result in a new global artifact,RELEASE_ID-npm-package.tar.gz
containing a ready-to-publish npm package for your application. The installer works similar to the shell and powershell installers, but instead of installing to PATH via the cargo home, it installs the binaries into your node_modules so that things likenpx
will work.npm publish
actually supports taking a path or url to a gzipped package to publish, so all you need to do is copy the URL ofnpm-package.tar.gz
from your Github Release and pass it tonpm publish
.The npm package will inherit its properties from your Cargo.toml and cargo-dist config (package name, version, authors, license, description, repository, homepage...). Static assets for executable-zips (README, CHANGELOG, LICENSE...) will also be copied into the package (in the future we might need to introduce configs to tune this).
This also introduces a new optional config key,
npm-scope
, which can be used to prefix the package name with a scope. e.g. the package axolotlsay withnpm-scope = "@axodotdev"
will get the package name@axodotdev/axolotlsay
.The current largest caveat is that the npm installer uses the
binary-install
npm library to unpack the tarballs/zips containings your binaries, and that library only supports unpacking.tar.gz
. So you must setwindows-archive
andunix-archive
to ".tar.gz" in your config for this feature to work. In the future we will improve this.Note that you can in principle retroactively generate the npm packages completely locally with
cargo dist build --artifacts=global
, but the above caveats about tarball/zip format means that no existing cargo-dist-based Github Releases will have usable contents. Still, you can at least inspect the package locally to see if you like what we generate.