Skip to content

Commit

Permalink
Remove namespace from package name before matching crates (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik authored Feb 24, 2021
1 parent 10ff337 commit 593d580
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _Alias: `-n`_

Followed by *two* arguments: `artifact-kind output-file`

The crate name will be read from the `npm_package_name` environment variable.
The crate name will be read from the `npm_package_name` environment variable. If the package name includes a namespace (`@namespace/package`), the namespace will be removed when matching the crate name (`package`).

### Artifact Kind

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cargo-cp-artifact",
"version": "0.1.0",
"version": "0.1.1",
"description": "Copies compiler artifacts emitted by rustc by parsing Cargo metadata",
"main": "src/index.js",
"bin": {
Expand Down
10 changes: 9 additions & 1 deletion src/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ function getCrateNameFromEnv(env) {
].join(" "));
}

return env[NPM_ENV];
const name = env[NPM_ENV];
const firstSlash = name.indexOf("/");

// This is a namespaced package; assume the crate is the un-namespaced version
if (name[0] === "@" && firstSlash > 0) {
return name.slice(firstSlash + 1);
}

return name;
}

function parse(argv, env) {
Expand Down
17 changes: 17 additions & 0 deletions test/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ describe("Argument Parsing", () => {
assert.deepStrictEqual(parse(args, env), expected);
});

it("should remove namespace from package name", () => {
const args = "-nc index.node -- a b c".split(" ");
const env = {
npm_package_name: "@my-namespace/my-crate"
};

const expected = {
artifacts: {
"cdylib:my-crate": ["index.node"]
},
cmd: "a",
args: ["b", "c"]
};

assert.deepStrictEqual(parse(args, env), expected);
});

it("should be able to provide multiple artifacts", () => {
const args = `
-nb my-bin
Expand Down

0 comments on commit 593d580

Please sign in to comment.