From ef538037beeb2aab1a73d707d13ee5b0399acfe8 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 20 Mar 2024 21:59:11 +0100 Subject: [PATCH] feat: set type:module in package.json on install --- src/bin.ts | 18 ++++++++++++++++++ src/utils.ts | 1 + test/commands.test.ts | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/src/bin.ts b/src/bin.ts index 9167cea..7430d99 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -16,8 +16,11 @@ import { findProjectDir, JsrPackage, JsrPackageNameError, + PkgJson, prettyTime, + readJson, setDebug, + writeJson, } from "./utils"; import { PkgManagerName } from "./pkg_manager"; @@ -212,6 +215,21 @@ if (args.length === 0) { if (cmd === "i" || cmd === "install" || cmd === "add") { run(async () => { const packages = getPackages(options.positionals, true); + const projectInfo = await findProjectDir(process.cwd()); + + if (projectInfo.pkgJsonPath !== null) { + const pkgJson = await readJson(projectInfo.pkgJsonPath); + if (pkgJson.type !== "module") { + pkgJson.type = "module"; + await writeJson( + projectInfo.pkgJsonPath, + pkgJson, + ); + console.log( + `Setting type:module in package.json...${kl.green("ok")}`, + ); + } + } await install(packages, { mode: options.values["save-dev"] diff --git a/src/utils.ts b/src/utils.ts index 002fb54..dcdab1c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -245,6 +245,7 @@ export interface PkgJson { name?: string; version?: string; license?: string; + type?: "module" | "commonjs"; dependencies?: Record; devDependencies?: Record; diff --git a/test/commands.test.ts b/test/commands.test.ts index 0eb0eef..64db7d6 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -35,6 +35,8 @@ describe("install", () => { /^npm:@jsr\/std__encoding@\^\d+\.\d+\.\d+.*$/, ); + assert.equal(pkgJson.type, "module"); + const depPath = path.join(dir, "node_modules", "@std", "encoding"); assert.ok(await isDirectory(depPath), "Not installed in node_modules"); @@ -45,7 +47,9 @@ describe("install", () => { "Missing npmrc registry", ); }); + }); + it("jsr i @std/encoding - resolve latest version in yarn berry", async () => { await runInTempDir(async (dir) => { await enableYarnBerry(dir); await writeTextFile(path.join(dir, "yarn.lock"), "");