From f82c75d1f4db95ce0b3d28f809da3cabd9d97af3 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Fri, 19 Feb 2021 16:57:13 -0500 Subject: [PATCH 1/2] Improve command line parsing Implements https://github.com/neon-bindings/cargo-cp-artifact/issues/3 --- README.md | 36 +- bin/cargo-cp-artifact.js | 6 + index.js | 177 --------- package-lock.json | 761 +++++++++++++++++++++++++++++++++++++++ package.json | 13 +- src/args.js | 112 ++++++ src/index.js | 128 +++++++ test/args.js | 110 ++++++ 8 files changed, 1154 insertions(+), 189 deletions(-) create mode 100755 bin/cargo-cp-artifact.js delete mode 100755 index.js create mode 100644 package-lock.json create mode 100644 src/args.js create mode 100644 src/index.js create mode 100644 test/args.js diff --git a/README.md b/README.md index 06191aa..01bfdbb 100644 --- a/README.md +++ b/README.md @@ -11,25 +11,49 @@ npm install -g cargo-cp-artifact ## Usage ``` -cargo-cp-artifact [crate-name=artifact-kind=output-file] -- [wrapped-command] +cargo-cp-artifact --artifact [artifact-kind] [crate-name] [output-file] -- [wrapped-command] ``` `cargo-cp-artifact` accepts a list of crate name and artifact kind to output file mappings and a command to wrap.`cargo-cp-artifact` will read `stdout` of the wrapped command and parse it as [cargo metadata](https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages). Compiler artifacts that match arguments provided will be copied to the target destination. When wrapping a `cargo` command, it is necessary to include a `json` format to `--message-format`. +### Arguments + +Multiple arguments are allowed to copy multiple build artifacts. + +#### `--artifact` + +_Alias: `-a`_ + +Followed by *three* arguments: `[artifact-kind] [crate-name] [output-file]` + +#### `--npm` + +_Alias: `-n`_ + +Followed by *two* arguments: `[artifact-kind] [output-file]` + +The crate name will be read from the `npm_package_name` environment variable. + +### Artifact Kind + +Valid artifact kinds are `bin`, `cdylib`, and `dylib`. They may be abbreviated as `b`, `c`, and `d` respectively. + +For example, `-ac` is the equivalent of `--artifact cdylib`. + ## Examples ### Wrapping cargo ```sh -cargo-cp-artifact my-crate=cdylib=lib/index.node -- cargo build --message-format=json-render-diagnostics +cargo-cp-artifact -a cdylib my-crate lib/index.node -- cargo build --message-format=json-render-diagnostics ``` ### Parsing a file ```sh -cargo-cp-artifact my-crate=cdylib=lib/index.node -- cat build-output.txt +cargo-cp-artifact -a cdylib my-crate lib/index.node -- cat build-output.txt ``` ### `npm` script @@ -39,17 +63,15 @@ cargo-cp-artifact my-crate=cdylib=lib/index.node -- cat build-output.txt { "name": "my-crate", "scripts": { - "build": "cargo-cp-artifact $npm_package_name=cdylib=lib/index.node -- cargo build --message-format=json-render-diagnostics" + "build": "cargo-cp-artifact -nc lib/index.node -- cargo build --message-format=json-render-diagnostics" } } ``` -_Note: `cargo-cp-artifact` will interpolate the special name `$npm_package_name` for portability across platforms (e.g., Windows)._ - ```sh npm run build -# Additional arguments ma be passed +# Additional arguments may be passed npm run build -- --feature=serde ``` diff --git a/bin/cargo-cp-artifact.js b/bin/cargo-cp-artifact.js new file mode 100755 index 0000000..0da6ee7 --- /dev/null +++ b/bin/cargo-cp-artifact.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node +"use strict"; + +const run = require("..") + +run(process.argv.slice(2), process.env); diff --git a/index.js b/index.js deleted file mode 100755 index aafd0b1..0000000 --- a/index.js +++ /dev/null @@ -1,177 +0,0 @@ -#!/usr/bin/env node -"use strict"; - -const { spawn } = require("child_process"); -const { promises: { copyFile, mkdir, stat } } = require("fs"); -const { dirname } = require("path"); -const readline = require("readline"); - -const options = parseArgs(process.argv.slice(2)); -const copied = {}; - -const cp = spawn(options.command, options.arguments, { - stdio: ["inherit", "pipe", "inherit"] -}); - -const rl = readline.createInterface({ input: cp.stdout }); - -cp.on("error", (err) => { - console.error(err); - process.exitCode = 1; -}); - -cp.on("exit", (code) => { - if (!process.exitCode) { - process.exitCode = code; - } -}); - -rl.on("line", (line) => { - try { - processCargoBuildLine(line); - } catch (err) { - console.error(err); - process.exitCode = 1; - } -}); - -process.on("exit", () => { - Object.keys(options.outputFiles).forEach((name) => { - if (!copied[name]) { - console.error(`Did not copy "${name}"`); - - if (!process.exitCode) { - process.exitCode = 1; - } - } - }); -}); - -function processCargoBuildLine(line) { - const data = JSON.parse(line); - - if (!data || data.reason !== "compiler-artifact" || !data.target) { - return; - } - - const { kind: kinds, name } = data.target; - - if (!Array.isArray(kinds) || !kinds.length) { - return; - } - - const [kind] = kinds; - const key = makeKey(name, kind); - const artifactConfig = options.outputFiles[key]; - - if (!artifactConfig || !Array.isArray(data.filenames)) { - return; - } - - const [filename] = data.filenames; - const { outputFile } = artifactConfig; - - if (!filename) { - return; - } - - copyArtifact(filename, outputFile) - .then(() => { - copied[key] = true; - }) - .catch((err) => { - process.exitCode = 1; - console.error(err); - }); -} - -async function isNewer(filename, outputFile) { - try { - const prevStats = await stat(outputFile); - const nextStats = await stat(filename); - - return nextStats.mtime > prevStats.mtime; - } catch (_err) {} - - return true; -} - -async function copyArtifact(filename, outputFile) { - if (!(await isNewer(filename, outputFile))) { - return; - } - - await mkdir(dirname(outputFile), { recursive: true }); - await copyFile(filename, outputFile); -} - -// Expects: Options and command separated by "--" -// Example: "arguments to CLI -- command to execute" -function parseArgs(args) { - const splitAt = args.indexOf("--"); - const options = splitAt >= 0 ? args.slice(0, splitAt) : args; - const command = splitAt >= 0 ? args.slice(splitAt + 1) : []; - const outputFiles = parseOutputFiles(options); - - if (!command.length) { - quitError([ - "Missing command to execute.", - [ - "cargo-cp-artifct my-crate=cdylib=index.node", - "--", - "cargo build --message-format=json-render-diagnostics" - ].join(" ") - ].join("\n")); - } - - return { - command: command[0], - arguments: command.slice(1), - outputFiles - }; -} - -// Expects: List of "crate-name=kind=output_file_path" sets -function parseOutputFiles(args) { - return args - .map(parseOutputFile) - .reduce((acc, opts) => ({ - ...acc, - [makeKey(opts.crateName, opts.kind)]: opts - }), {}); -} - -// Expects: "crate-name=kind=output_file_path" set -function parseOutputFile(opts) { - const nameSplitAt = opts.indexOf("="); - - if (nameSplitAt < 0) { - quitError(`Missing artifact kind: ${opts}`); - } - - const crateNameRaw = opts.slice(0, nameSplitAt); - const crateName = crateNameRaw === "$npm_package_name" - ? process.env.npm_package_name - : crateNameRaw; - - const remainder = opts.slice(nameSplitAt + 1); - const kindSplitAt = remainder.indexOf("="); - - if (kindSplitAt < 0) { - quitError(`Missing output file name: ${opts}`); - } - - const kind = remainder.slice(0, kindSplitAt); - const outputFile = remainder.slice(kindSplitAt + 1); - - return { crateName, kind, outputFile }; -} - -function makeKey(crateName, kind) { - return `${crateName}=${kind}`; -} - -function quitError(msg) { - console.error(msg); - process.exit(1); -} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..0565133 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,761 @@ +{ + "name": "cargo-cp-artifact", + "version": "0.0.2", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@ungap/promise-all-settled": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", + "integrity": "sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==", + "dev": true + }, + "ansi-colors": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", + "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", + "dev": true + }, + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "anymatch": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz", + "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==", + "dev": true, + "requires": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + } + }, + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "requires": { + "fill-range": "^7.0.1" + } + }, + "browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, + "camelcase": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", + "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==", + "dev": true + }, + "chalk": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", + "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "dependencies": { + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, + "chokidar": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.1.tgz", + "integrity": "sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==", + "dev": true, + "requires": { + "anymatch": "~3.1.1", + "braces": "~3.0.2", + "fsevents": "~2.3.1", + "glob-parent": "~5.1.0", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.5.0" + } + }, + "cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz", + "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "debug": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", + "dev": true, + "requires": { + "ms": "2.1.2" + }, + "dependencies": { + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + } + } + }, + "decamelize": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", + "dev": true + }, + "diff": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", + "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "dev": true + }, + "emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true + }, + "escalade": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", + "dev": true + }, + "escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true + }, + "fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "requires": { + "to-regex-range": "^5.0.1" + } + }, + "find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", + "dev": true, + "requires": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + } + }, + "flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "glob-parent": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.1.tgz", + "integrity": "sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==", + "dev": true, + "requires": { + "is-glob": "^4.0.1" + } + }, + "growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", + "dev": true + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true + }, + "he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "requires": { + "binary-extensions": "^2.0.0" + } + }, + "is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, + "is-glob": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", + "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==", + "dev": true, + "requires": { + "is-extglob": "^2.1.1" + } + }, + "is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true + }, + "is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", + "dev": true + }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", + "dev": true + }, + "js-yaml": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.0.0.tgz", + "integrity": "sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==", + "dev": true, + "requires": { + "argparse": "^2.0.1" + } + }, + "locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", + "dev": true, + "requires": { + "p-locate": "^5.0.0" + } + }, + "log-symbols": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz", + "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==", + "dev": true, + "requires": { + "chalk": "^4.0.0" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "mocha": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.3.0.tgz", + "integrity": "sha512-TQqyC89V1J/Vxx0DhJIXlq9gbbL9XFNdeLQ1+JsnZsVaSOV1z3tWfw0qZmQJGQRIfkvZcs7snQnZnOCKoldq1Q==", + "dev": true, + "requires": { + "@ungap/promise-all-settled": "1.1.2", + "ansi-colors": "4.1.1", + "browser-stdout": "1.3.1", + "chokidar": "3.5.1", + "debug": "4.3.1", + "diff": "5.0.0", + "escape-string-regexp": "4.0.0", + "find-up": "5.0.0", + "glob": "7.1.6", + "growl": "1.10.5", + "he": "1.2.0", + "js-yaml": "4.0.0", + "log-symbols": "4.0.0", + "minimatch": "3.0.4", + "ms": "2.1.3", + "nanoid": "3.1.20", + "serialize-javascript": "5.0.1", + "strip-json-comments": "3.1.1", + "supports-color": "8.1.1", + "which": "2.0.2", + "wide-align": "1.1.3", + "workerpool": "6.1.0", + "yargs": "16.2.0", + "yargs-parser": "20.2.4", + "yargs-unparser": "2.0.0" + } + }, + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true + }, + "nanoid": { + "version": "3.1.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.20.tgz", + "integrity": "sha512-a1cQNyczgKbLX9jwbS/+d7W8fX/RfgYR7lVWwWOGIPNgK2m0MWvrGF6/m4kk6U3QcFMnZf3RIhL0v2Jgh/0Uxw==", + "dev": true + }, + "normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "dev": true, + "requires": { + "yocto-queue": "^0.1.0" + } + }, + "p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", + "dev": true, + "requires": { + "p-limit": "^3.0.2" + } + }, + "path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "picomatch": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", + "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==", + "dev": true + }, + "randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dev": true, + "requires": { + "safe-buffer": "^5.1.0" + } + }, + "readdirp": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz", + "integrity": "sha512-cMhu7c/8rdhkHXWsY+osBhfSy0JikwpHK/5+imo+LpeasTF8ouErHrlYkwT0++njiyuDvc7OFY5T3ukvZ8qmFQ==", + "dev": true, + "requires": { + "picomatch": "^2.2.1" + } + }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=", + "dev": true + }, + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true + }, + "serialize-javascript": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz", + "integrity": "sha512-SaaNal9imEO737H2c05Og0/8LUXG7EnsZyMa8MzkmuHoELfT6txuj0cMqRj6zfPKnmQ1yasR4PCJc8x+M4JSPA==", + "dev": true, + "requires": { + "randombytes": "^2.1.0" + } + }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + } + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } + }, + "strip-json-comments": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", + "dev": true + }, + "supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dev": true, + "requires": { + "has-flag": "^4.0.0" + } + }, + "to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "requires": { + "is-number": "^7.0.0" + } + }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "requires": { + "isexe": "^2.0.0" + } + }, + "wide-align": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", + "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", + "dev": true, + "requires": { + "string-width": "^1.0.2 || 2" + } + }, + "workerpool": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.1.0.tgz", + "integrity": "sha512-toV7q9rWNYha963Pl/qyeZ6wG+3nnsyvolaNUS8+R5Wtw6qJPTxIlOP1ZSvcGhEJw+l3HMMmtiNo9Gl61G4GVg==", + "dev": true + }, + "wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "requires": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz", + "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + }, + "y18n": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz", + "integrity": "sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==", + "dev": true + }, + "yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dev": true, + "requires": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "dependencies": { + "ansi-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", + "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "string-width": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz", + "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.0" + } + }, + "strip-ansi": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", + "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.0" + } + } + } + }, + "yargs-parser": { + "version": "20.2.4", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz", + "integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==", + "dev": true + }, + "yargs-unparser": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", + "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", + "dev": true, + "requires": { + "camelcase": "^6.0.0", + "decamelize": "^4.0.0", + "flat": "^5.0.2", + "is-plain-obj": "^2.1.0" + } + }, + "yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 0ba54c3..1f29ba3 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "cargo-cp-artifact", - "version": "0.0.2", + "version": "0.1.0", "description": "Copies compiler artifacts emitted by rustc by parsing Cargo metadata", - "main": "index.js", + "main": "src/index.js", "bin": { - "cargo-cp-artifact": "index.js" + "cargo-cp-artifact": "bin/cargo-cp-artifact.js" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha test" }, "repository": { "type": "git", @@ -23,5 +23,8 @@ "bugs": { "url": "https://github.com/neon-bindings/cargo-cp-artifact/issues" }, - "homepage": "https://github.com/neon-bindings/cargo-cp-artifact#readme" + "homepage": "https://github.com/neon-bindings/cargo-cp-artifact#readme", + "devDependencies": { + "mocha": "^8.3.0" + } } diff --git a/src/args.js b/src/args.js new file mode 100644 index 0000000..3fbf56b --- /dev/null +++ b/src/args.js @@ -0,0 +1,112 @@ +"use strict"; + +class ParseError extends Error {} + +const NPM_ENV = "npm_package_name"; +const EXPECTED_COMMAND = [ + "Missing command to execute.", + [ + "cargo-cp-artifct -a cdylib my-crate index.node", + "--", + "cargo build --message-format=json-render-diagnostics" + ].join(" ") +].join("\n"); + +function validateArtifactType(artifactType) { + switch (artifactType) { + case "b": case "bin": return "bin"; + case "c": case "cdylib": return "cdylib"; + case "d": case "dylib": return "dylib"; + default: + } + + throw new ParseError(`Unexpected artifact type: ${artifactType}`); +} + +function getArtifactName({ artifactType, crateName }) { + return `${artifactType}:${crateName}`; +} + +function getCrateNameFromEnv(env) { + if (!env.hasOwnProperty(NPM_ENV)) { + throw new ParseError([ + `Could not find the \`${NPM_ENV}\` environment variable.`, + "Expected to be executed from an `npm` command." + ].join(" ")); + } + + return env[NPM_ENV]; +} + +function parse(argv, env) { + const artifacts = {}; + let tokens = argv; + + function getNext() { + if (!tokens.length) { + throw new ParseError(EXPECTED_COMMAND); + } + + const next = tokens[0]; + tokens = tokens.slice(1); + return next; + } + + function getArtifactType(token) { + if (token[1] !== "-" && token.length === 3) { + return validateArtifactType(token[2]); + } + + return validateArtifactType(getNext()); + } + + function pushArtifact(artifact) { + const name = getArtifactName(artifact); + + artifacts[name] = artifacts[name] || []; + artifacts[name].push(artifact.outputFile); + } + + while (tokens.length) { + const token = getNext(); + + // End of CLI arguments + if (token === "--") { + break; + } + + if (token === "--artifact" || (token.length <= 3 && token.startsWith("-a"))) { + const artifactType = getArtifactType(token); + const crateName = getNext(); + const outputFile = getNext(); + + pushArtifact({ artifactType, crateName, outputFile }); + continue; + } + + if (token === "--npm" || (token.length <= 3 && token.startsWith("-n"))) { + const artifactType = getArtifactType(token); + const crateName = getCrateNameFromEnv(env); + const outputFile = getNext(); + + pushArtifact({ artifactType, crateName, outputFile }); + continue; + } + + throw new ParseError(`Unexpected option: ${token}`); + } + + if (!tokens.length) { + throw new ParseError(EXPECTED_COMMAND); + } + + const cmd = getNext(); + + return { + artifacts, + cmd, + args: tokens + } +} + +module.exports = { ParseError, getArtifactName, parse }; diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..dc4ed52 --- /dev/null +++ b/src/index.js @@ -0,0 +1,128 @@ +"use strict"; + +const { spawn } = require("child_process"); +const { promises: { copyFile, mkdir, stat } } = require("fs"); +const { dirname } = require("path"); +const readline = require("readline"); + +const { ParseError, getArtifactName, parse } = require("./args"); + +function run(argv, env) { + const options = parseArgs(argv, env); + const copied = {}; + + const cp = spawn(options.cmd, options.args, { + stdio: ["inherit", "pipe", "inherit"] + }); + + const rl = readline.createInterface({ input: cp.stdout }); + + cp.on("error", (err) => { + console.error(err); + process.exitCode = 1; + }); + + cp.on("exit", (code) => { + if (!process.exitCode) { + process.exitCode = code; + } + }); + + rl.on("line", (line) => { + try { + processCargoBuildLine(options, copied, line); + } catch (err) { + console.error(err); + process.exitCode = 1; + } + }); + + process.on("exit", () => { + Object.keys(options.artifacts).forEach((name) => { + if (!copied[name]) { + console.error(`Did not copy "${name}"`); + + if (!process.exitCode) { + process.exitCode = 1; + } + } + }); + }); +} + +function processCargoBuildLine(options, copied, line) { + const data = JSON.parse(line); + + if (!data || data.reason !== "compiler-artifact" || !data.target) { + return; + } + + const { kind: kinds, name } = data.target; + + if (!Array.isArray(kinds) || !kinds.length) { + return; + } + + const [kind] = kinds; + const key = getArtifactName({ artifactType: kind, crateName: name }); + const outputFiles = options.artifacts[key]; + + if (!outputFiles || !Array.isArray(data.filenames)) { + return; + } + + const [filename] = data.filenames; + + if (!filename) { + return; + } + + Promise + .all(outputFiles.map(outputFile => copyArtifact(filename, outputFile))) + .then(() => { + copied[key] = true; + }) + .catch((err) => { + process.exitCode = 1; + console.error(err); + }); +} + +async function isNewer(filename, outputFile) { + try { + const prevStats = await stat(outputFile); + const nextStats = await stat(filename); + + return nextStats.mtime > prevStats.mtime; + } catch (_err) {} + + return true; +} + +async function copyArtifact(filename, outputFile) { + if (!(await isNewer(filename, outputFile))) { + return; + } + + await mkdir(dirname(outputFile), { recursive: true }); + await copyFile(filename, outputFile); +} + +function parseArgs(argv, env) { + try { + return parse(argv, env); + } catch (err) { + if (err instanceof ParseError) { + quitError(err.message); + } else { + throw err; + } + } +} + +function quitError(msg) { + console.error(msg); + process.exit(1); +} + +module.exports = run; diff --git a/test/args.js b/test/args.js new file mode 100644 index 0000000..58e6ee8 --- /dev/null +++ b/test/args.js @@ -0,0 +1,110 @@ +"use strict"; + +const assert = require("assert"); + +const { parse } = require("../src/args"); + +describe("Argument Parsing", () => { + it("throws on invalid artifact type", () => { + assert.throws(() => parse(["-an", "a", "b", "--"]), /artifact type/); + }); + + it("npm must have an environment variable", () => { + assert.throws(() => parse(["-nc", "a", "b", "--"], {}), /environment/); + }); + + it("must provide a command", () => { + assert.throws(() => parse(["-ac", "a", "b"]), /Missing command/); + assert.throws(() => parse(["-ac", "a", "b", "--"]), /Missing command/); + }); + + it("cannot provide invalid option", () => { + assert.throws(() => parse(["-q"], {}), /Unexpected option/); + }); + + it("should be able to use --artifact", () => { + const args = "bin my-crate my-bin -- a b c".split(" "); + const expected = { + artifacts: { + "bin:my-crate": ["my-bin"] + }, + cmd: "a", + args: ["b", "c"] + }; + + assert.deepStrictEqual(parse(["--artifact", ...args]), expected); + assert.deepStrictEqual(parse(["-a", ...args]), expected); + }); + + it("should be able to use --npm", () => { + const args = "bin my-bin -- a b c".split(" "); + const env = { + npm_package_name: "my-crate" + }; + + const expected = { + artifacts: { + "bin:my-crate": ["my-bin"] + }, + cmd: "a", + args: ["b", "c"] + }; + + assert.deepStrictEqual(parse(["--npm", ...args], env), expected); + assert.deepStrictEqual(parse(["-n", ...args], env), expected); + }); + + it("should be able to use short-hand for crate type with -a", () => { + const args = "-ab my-crate my-bin -- a b c".split(" "); + const expected = { + artifacts: { + "bin:my-crate": ["my-bin"] + }, + cmd: "a", + args: ["b", "c"] + }; + + assert.deepStrictEqual(parse(args), expected); + }); + + it("should be able to use short-hand for crate type with -n", () => { + const args = "-nb my-bin -- a b c".split(" "); + const env = { + npm_package_name: "my-crate" + }; + + const expected = { + artifacts: { + "bin:my-crate": ["my-bin"] + }, + cmd: "a", + args: ["b", "c"] + }; + + assert.deepStrictEqual(parse(args, env), expected); + }); + + it("should be able to provide multiple artifacts", () => { + const args = ` + -nb my-bin + --artifact d a b + -ac my-crate index.node + --npm bin other-copy + -- a b c + `.trim().split("\n").map(line => line.trim()).join(" ").split(" "); + + const env = { + npm_package_name: "my-crate" + }; + + assert.deepStrictEqual(parse(args, env), { + artifacts: { + "bin:my-crate": ["my-bin", "other-copy"], + "dylib:a": ["b"], + "cdylib:my-crate": ["index.node"] + }, + cmd: "a", + args: ["b", "c"] + }); + }); +}); From 54bc2c5564b9eb41a4593cc0992a79c30670c507 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Wed, 24 Feb 2021 11:55:27 -0500 Subject: [PATCH 2/2] Review feedback --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 01bfdbb..b3aec29 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ npm install -g cargo-cp-artifact ## Usage ``` -cargo-cp-artifact --artifact [artifact-kind] [crate-name] [output-file] -- [wrapped-command] +cargo-cp-artifact --artifact artifact-kind crate-name output-file -- wrapped-command ``` `cargo-cp-artifact` accepts a list of crate name and artifact kind to output file mappings and a command to wrap.`cargo-cp-artifact` will read `stdout` of the wrapped command and parse it as [cargo metadata](https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages). Compiler artifacts that match arguments provided will be copied to the target destination. @@ -26,13 +26,13 @@ Multiple arguments are allowed to copy multiple build artifacts. _Alias: `-a`_ -Followed by *three* arguments: `[artifact-kind] [crate-name] [output-file]` +Followed by *three* arguments: `artifact-kind crate-name output-file` #### `--npm` _Alias: `-n`_ -Followed by *two* arguments: `[artifact-kind] [output-file]` +Followed by *two* arguments: `artifact-kind output-file` The crate name will be read from the `npm_package_name` environment variable.