Skip to content

Commit 6f9de22

Browse files
fix: skip unstable flags on publishing Deno project (#59)
1 parent 20d5f1e commit 6f9de22

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

src/bin.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import {
1111
runScript,
1212
showPackageInfo,
1313
} from "./commands";
14-
import { JsrPackage, JsrPackageNameError, prettyTime, setDebug } from "./utils";
14+
import {
15+
ExecError,
16+
findProjectDir,
17+
JsrPackage,
18+
JsrPackageNameError,
19+
prettyTime,
20+
setDebug,
21+
} from "./utils";
1522
import { PkgManagerName } from "./pkg_manager";
1623

1724
const args = process.argv.slice(2);
@@ -138,12 +145,14 @@ if (args.length === 0) {
138145
// frequently.
139146
if (cmd === "publish") {
140147
const binFolder = path.join(__dirname, "..", ".download");
141-
run(() =>
142-
publish(process.cwd(), {
148+
run(async () => {
149+
const projectInfo = await findProjectDir(process.cwd());
150+
return publish(process.cwd(), {
143151
binFolder,
144152
publishArgs: args.slice(1),
145-
})
146-
);
153+
pkgJsonPath: projectInfo.pkgJsonPath,
154+
});
155+
});
147156
} else if (cmd === "view" || cmd === "show" || cmd === "info") {
148157
const pkgName = args[1];
149158
if (pkgName === undefined) {
@@ -260,6 +269,9 @@ async function run(fn: () => Promise<void>) {
260269
if (err instanceof JsrPackageNameError) {
261270
console.log(kl.red(err.message));
262271
process.exit(1);
272+
} else if (err instanceof ExecError) {
273+
console.log(kl.red(err.message));
274+
process.exit(err.code);
263275
}
264276

265277
throw err;

src/commands.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export async function remove(packages: JsrPackage[], options: BaseOptions) {
115115

116116
export interface PublishOptions {
117117
binFolder: string;
118+
pkgJsonPath: string | null;
118119
publishArgs: string[];
119120
}
120121

@@ -156,16 +157,25 @@ export async function publish(cwd: string, options: PublishOptions) {
156157
// Ready to publish now!
157158
const args = [
158159
"publish",
159-
"--unstable-bare-node-builtins",
160-
"--unstable-sloppy-imports",
161-
"--unstable-byonm",
162-
"--no-check",
163-
...options.publishArgs,
164160
];
165-
await exec(binPath, args, cwd, {
166-
...process.env,
167-
DENO_DISABLE_PEDANTIC_NODE_WARNINGS: "true",
168-
});
161+
const env = { ...process.env };
162+
163+
// These commands should only be added for a node project,
164+
// not a Deno project.
165+
if (options.pkgJsonPath !== null) {
166+
args.push(
167+
"--unstable-bare-node-builtins",
168+
"--unstable-sloppy-imports",
169+
"--unstable-byonm",
170+
"--no-check",
171+
);
172+
173+
env.DENO_DISABLE_PEDANTIC_NODE_WARNINGS = "true";
174+
}
175+
176+
args.push(...options.publishArgs);
177+
178+
await exec(binPath, args, cwd, env);
169179
}
170180

171181
export async function runScript(

src/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ export function timeAgo(diff: number) {
185185
return "just now";
186186
}
187187

188+
export class ExecError extends Error {
189+
constructor(public code: number) {
190+
super(`Child process exited with: ${code}`);
191+
}
192+
}
193+
188194
export async function exec(
189195
cmd: string,
190196
args: string[],
@@ -214,11 +220,10 @@ export async function exec(
214220
});
215221
}
216222

217-
return new Promise<string>((resolve) => {
223+
return new Promise<string>((resolve, reject) => {
218224
cp.on("exit", (code) => {
219-
console.log(output);
220225
if (code === 0) resolve(output);
221-
else process.exit(code ?? 1);
226+
else reject(new ExecError(code ?? 1));
222227
});
223228
});
224229
}

test/commands.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import * as assert from "node:assert/strict";
1414
import {
1515
exec,
16+
ExecError,
1617
PkgJson,
1718
readJson,
1819
readTextFile,
@@ -517,6 +518,33 @@ describe("publish", () => {
517518
});
518519
}).timeout(600000);
519520

521+
it("should not add unstable publish flags for a Deno project", async () => {
522+
await runInTempDir(async (dir) => {
523+
const pkgJsonPath = path.join(dir, "package.json");
524+
await fs.promises.rm(pkgJsonPath);
525+
526+
await writeTextFile(
527+
path.join(dir, "mod.ts"),
528+
["import * as fs from 'fs';", "console.log(fs)"].join("\n"),
529+
);
530+
531+
await writeJson<DenoJson>(path.join(dir, "deno.json"), {
532+
name: "@deno/jsr-cli-test",
533+
version: "0.0.1",
534+
exports: {
535+
".": "./mod.ts",
536+
},
537+
});
538+
539+
try {
540+
await runJsr(["publish", "--dry-run", "--token", "dummy-token"], dir);
541+
assert.fail();
542+
} catch (err) {
543+
assert.ok(err instanceof ExecError, `Unknown exec error thrown`);
544+
}
545+
});
546+
}).timeout(600000);
547+
520548
it("should leave node_modules as is", async () => {
521549
await runInTempDir(async (dir) => {
522550
const pkgJsonPath = path.join(dir, "package.json");

0 commit comments

Comments
 (0)