Skip to content

Commit

Permalink
fix: skip unstable flags on publishing Deno project (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Mar 11, 2024
1 parent 20d5f1e commit 6f9de22
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
22 changes: 17 additions & 5 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import {
runScript,
showPackageInfo,
} from "./commands";
import { JsrPackage, JsrPackageNameError, prettyTime, setDebug } from "./utils";
import {
ExecError,
findProjectDir,
JsrPackage,
JsrPackageNameError,
prettyTime,
setDebug,
} from "./utils";
import { PkgManagerName } from "./pkg_manager";

const args = process.argv.slice(2);
Expand Down Expand Up @@ -138,12 +145,14 @@ if (args.length === 0) {
// frequently.
if (cmd === "publish") {
const binFolder = path.join(__dirname, "..", ".download");
run(() =>
publish(process.cwd(), {
run(async () => {
const projectInfo = await findProjectDir(process.cwd());
return publish(process.cwd(), {
binFolder,
publishArgs: args.slice(1),
})
);
pkgJsonPath: projectInfo.pkgJsonPath,
});
});
} else if (cmd === "view" || cmd === "show" || cmd === "info") {
const pkgName = args[1];
if (pkgName === undefined) {
Expand Down Expand Up @@ -260,6 +269,9 @@ async function run(fn: () => Promise<void>) {
if (err instanceof JsrPackageNameError) {
console.log(kl.red(err.message));
process.exit(1);
} else if (err instanceof ExecError) {
console.log(kl.red(err.message));
process.exit(err.code);
}

throw err;
Expand Down
28 changes: 19 additions & 9 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export async function remove(packages: JsrPackage[], options: BaseOptions) {

export interface PublishOptions {
binFolder: string;
pkgJsonPath: string | null;
publishArgs: string[];
}

Expand Down Expand Up @@ -156,16 +157,25 @@ export async function publish(cwd: string, options: PublishOptions) {
// Ready to publish now!
const args = [
"publish",
"--unstable-bare-node-builtins",
"--unstable-sloppy-imports",
"--unstable-byonm",
"--no-check",
...options.publishArgs,
];
await exec(binPath, args, cwd, {
...process.env,
DENO_DISABLE_PEDANTIC_NODE_WARNINGS: "true",
});
const env = { ...process.env };

// These commands should only be added for a node project,
// not a Deno project.
if (options.pkgJsonPath !== null) {
args.push(
"--unstable-bare-node-builtins",
"--unstable-sloppy-imports",
"--unstable-byonm",
"--no-check",
);

env.DENO_DISABLE_PEDANTIC_NODE_WARNINGS = "true";
}

args.push(...options.publishArgs);

await exec(binPath, args, cwd, env);
}

export async function runScript(
Expand Down
11 changes: 8 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export function timeAgo(diff: number) {
return "just now";
}

export class ExecError extends Error {
constructor(public code: number) {
super(`Child process exited with: ${code}`);
}
}

export async function exec(
cmd: string,
args: string[],
Expand Down Expand Up @@ -214,11 +220,10 @@ export async function exec(
});
}

return new Promise<string>((resolve) => {
return new Promise<string>((resolve, reject) => {
cp.on("exit", (code) => {
console.log(output);
if (code === 0) resolve(output);
else process.exit(code ?? 1);
else reject(new ExecError(code ?? 1));
});
});
}
Expand Down
28 changes: 28 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import * as assert from "node:assert/strict";
import {
exec,
ExecError,
PkgJson,
readJson,
readTextFile,
Expand Down Expand Up @@ -517,6 +518,33 @@ describe("publish", () => {
});
}).timeout(600000);

it("should not add unstable publish flags for a Deno project", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
await fs.promises.rm(pkgJsonPath);

await writeTextFile(
path.join(dir, "mod.ts"),
["import * as fs from 'fs';", "console.log(fs)"].join("\n"),
);

await writeJson<DenoJson>(path.join(dir, "deno.json"), {
name: "@deno/jsr-cli-test",
version: "0.0.1",
exports: {
".": "./mod.ts",
},
});

try {
await runJsr(["publish", "--dry-run", "--token", "dummy-token"], dir);
assert.fail();
} catch (err) {
assert.ok(err instanceof ExecError, `Unknown exec error thrown`);
}
});
}).timeout(600000);

it("should leave node_modules as is", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
Expand Down

0 comments on commit 6f9de22

Please sign in to comment.