Skip to content

Commit

Permalink
feat: add package.json file generation on init (#61)
Browse files Browse the repository at this point in the history
* feat: add package.json file on init

* format

* refactor

* format

* refactor the file generation

* format
  • Loading branch information
imrishabh18 authored Feb 14, 2025
1 parent 6b9ea4f commit eff8453
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/bun-test.yml

This file was deleted.

14 changes: 6 additions & 8 deletions cli/init/register.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Command } from "commander"
import { execSync } from "node:child_process"
import * as fs from "node:fs"
import * as path from "node:path"
import { setupTsciProject } from "lib/shared/setup-tsci-packages"
import { generateTsConfig } from "lib/shared/generate-ts-config"
import { writeFileIfNotExists } from "lib/shared/write-file-if-not-exists"
import { generateGitIgnoreFile } from "lib/shared/generate-gitignore-file"
import { generatePackageJson } from "lib/shared/generate-package-json"

export const registerInit = (program: Command) => {
program
Expand Down Expand Up @@ -47,18 +49,14 @@ export default () => (
`,
)

// Setup project dependencies
try {
setupTsciProject(projectDir)
} catch (error) {
console.error("Failed to install dependencies:", error)
process.exit(1)
}

// Generate package.json
generatePackageJson(projectDir)
// Generate tsconfig.json
generateTsConfig(projectDir)
// Create .gitignore file
generateGitIgnoreFile(projectDir)
// Setup project dependencies
setupTsciProject(projectDir)

console.info(
`🎉 Initialization complete! Run ${directory ? `"cd ${directory}" & ` : ""}"tsci dev" to start developing.`,
Expand Down
22 changes: 22 additions & 0 deletions lib/shared/generate-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as path from "node:path"
import { writeFileIfNotExists } from "./write-file-if-not-exists"

export const generatePackageJson = (dir: string) => {
const packageJsonPath = path.join(dir, "package.json")
const packageJsonContent = {
name: path.basename(dir),
version: "1.0.0",
description: "A TSCircuit project",
main: "index.tsx",
keywords: ["tscircuit", "electronics"],
scripts: {
dev: "tsci dev",
build: "tsci build",
},
}

writeFileIfNotExists(
packageJsonPath,
JSON.stringify(packageJsonContent, null, 2),
)
}
5 changes: 0 additions & 5 deletions lib/shared/setup-tsci-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ export function setupTsciProject(
const packageJsonPath = path.join(projectPath, "package.json")
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"))

// Remove unwanted fields
delete packageJson.keywords
delete packageJson.author
delete packageJson.main

fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
console.log("Updated package.json to remove unnecessary fields.")

Expand Down
40 changes: 34 additions & 6 deletions tests/cli/init/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"
import { test, expect } from "bun:test"
import { join } from "node:path"
import { expect, test } from "bun:test"
import { execSync } from "node:child_process"
import { join } from "node:path"
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture"

test("init command installs @types/react and passes type-checking", async () => {
const { tmpDir, runCommand } = await getCliTestFixture()
Expand All @@ -10,9 +10,37 @@ test("init command installs @types/react and passes type-checking", async () =>
console.log(stdout)

const pkgJsonPath = join(tmpDir, "package.json")
const pkgJson = JSON.parse(await Bun.file(pkgJsonPath).text())
expect(pkgJson.devDependencies["@types/react"]).toBeDefined()
expect(pkgJson.devDependencies["@tscircuit/core"]).toBeDefined()
const pkgJson = await Bun.file(pkgJsonPath).json()

expect(pkgJson).toMatchInlineSnapshot(
{
name: expect.any(String),
devDependencies: {
"@tscircuit/core": expect.any(String),
"@types/react": expect.any(String),
},
},
`
{
"description": "A TSCircuit project",
"devDependencies": {
"@tscircuit/core": Any<String>,
"@types/react": Any<String>,
},
"keywords": [
"tscircuit",
"electronics",
],
"main": "index.tsx",
"name": Any<String>,
"scripts": {
"build": "tsci build",
"dev": "tsci dev",
},
"version": "1.0.0",
}
`,
)

const npmrcPath = join(tmpDir, ".npmrc")
const npmrcContent = await Bun.file(npmrcPath).text()
Expand Down

0 comments on commit eff8453

Please sign in to comment.