Skip to content

Commit

Permalink
feat: set type:module in package.json on install
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 20, 2024
1 parent 671c7ee commit ef53803
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import {
findProjectDir,
JsrPackage,
JsrPackageNameError,
PkgJson,
prettyTime,
readJson,
setDebug,
writeJson,
} from "./utils";
import { PkgManagerName } from "./pkg_manager";

Expand Down Expand Up @@ -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<PkgJson>(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"]
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ export interface PkgJson {
name?: string;
version?: string;
license?: string;
type?: "module" | "commonjs";

dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
Expand Down
4 changes: 4 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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"), "");
Expand Down

0 comments on commit ef53803

Please sign in to comment.