Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set type:module in package.json on install #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading