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: add init command #11

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
5 changes: 4 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as kl from "kolorist";
import * as fs from "node:fs";
import * as path from "node:path";
import { parseArgs } from "node:util";
import { install, remove } from "./commands";
import { init, install, remove } from "./commands";
import { JsrPackage, JsrPackageNameError, prettyTime, setDebug } from "./utils";
import { PkgManagerName } from "./pkg_manager";

Expand Down Expand Up @@ -34,6 +34,7 @@ Commands:
${prettyPrintRow([
["i, install, add", "Install one or more jsr packages"],
["r, uninstall, remove", "Remove one or more jsr packages"],
["init", "Create an empty project"],
])}

Options:
Expand Down Expand Up @@ -130,6 +131,8 @@ if (args.length === 0) {
const packages = getPackages(options.positionals);
await remove(packages, { pkgManagerName });
});
} else if (cmd === "init") {
run(() => init(process.cwd()));
} else {
console.error(kl.red(`Unknown command: ${cmd}`));
console.log();
Expand Down
55 changes: 55 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,58 @@ export async function remove(packages: JsrPackage[], options: BaseOptions) {
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
await pkgManager.remove(packages);
}

async function createFiles(cwd: string, files: Record<string, string>) {
await Promise.all(
Array.from(Object.entries(files)).map(([file, content]) => {
const filePath = path.join(cwd, file);
return fs.promises.writeFile(filePath, content, "utf-8");
})
);
}

export async function init(cwd: string) {
await setupNpmRc(cwd);

await createFiles(cwd, {
"package.json": JSON.stringify(
{
name: "@<scope>/<package_name>",
version: "0.0.1",
description: "",
license: "ISC",
type: "module",
},
null,
2
),
// TODO: Rename to jsr.json, once supported in Deno
// deno.json
"deno.json": JSON.stringify(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be a --lib vs --bin option to init here (is this a library or an application?) like in cargo init. This deno.json/jsr.json only seems necessary in the --lib case.

{
name: "@<scope>/<package_name>",
version: "0.0.1",
description: "",
exports: {
".": "./mod.ts",
},
},
null,
2
),
"mod.ts": [
"export function add(a: number, b: number): number {",
" return a + b;",
"}",
].join("\n"),
"tsconfig.json": JSON.stringify(
{
compilerOptions: {
moduleResolution: "NodeNext",
},
},
null,
2
),
});
}
15 changes: 15 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,18 @@ describe("remove", () => {
);
});
});

describe("init", () => {
it("creates empty project", async () => {
await withTempEnv(["init"], async (_getPkgJson, dir) => {
const files = await fs.promises.readdir(dir);
assert.deepEqual(files.sort(), [
".npmrc",
"deno.json",
"mod.ts",
"package.json",
"tsconfig.json",
]);
});
});
});
Loading