From 6f9de222f26b3c7f813ccf1ff62115e7e31b5573 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 11 Mar 2024 11:42:54 +0100 Subject: [PATCH] fix: skip unstable flags on publishing Deno project (#59) --- src/bin.ts | 22 +++++++++++++++++----- src/commands.ts | 28 +++++++++++++++++++--------- src/utils.ts | 11 ++++++++--- test/commands.test.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index ce7bdb1..3a25dd8 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -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); @@ -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) { @@ -260,6 +269,9 @@ async function run(fn: () => Promise) { 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; diff --git a/src/commands.ts b/src/commands.ts index 091a39b..8d441b6 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -115,6 +115,7 @@ export async function remove(packages: JsrPackage[], options: BaseOptions) { export interface PublishOptions { binFolder: string; + pkgJsonPath: string | null; publishArgs: string[]; } @@ -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( diff --git a/src/utils.ts b/src/utils.ts index 3e9f4f0..002fb54 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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[], @@ -214,11 +220,10 @@ export async function exec( }); } - return new Promise((resolve) => { + return new Promise((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)); }); }); } diff --git a/test/commands.test.ts b/test/commands.test.ts index eba49dc..2ed6635 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -13,6 +13,7 @@ import { import * as assert from "node:assert/strict"; import { exec, + ExecError, PkgJson, readJson, readTextFile, @@ -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(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");