diff --git a/src/bin.ts b/src/bin.ts index f254129..2d6266e 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -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"; @@ -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: @@ -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(); diff --git a/src/commands.ts b/src/commands.ts index 1e66fdc..f43f225 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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) { + 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: "@/", + 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( + { + 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 + ), + }); +} diff --git a/test/commands.test.ts b/test/commands.test.ts index e3877d0..3ed687e 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -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", + ]); + }); + }); +});