@tscircuit/cli 0.1.22 → 0.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.lock +2260 -0
- package/cli/init/register.ts +80 -3
- package/dist/main.js +61 -6
- package/example-dir/snippet2-large-led-matrix.tsx +1 -1
- package/example-dir/tsconfig.json +17 -0
- package/example-dir/types.d.ts +20 -0
- package/package.json +3 -3
- package/tests/cli/init/init.test.ts +34 -0
- package/bun.lockb +0 -0
package/cli/init/register.ts
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
import type { Command } from "commander"
|
|
2
2
|
import * as fs from "node:fs"
|
|
3
3
|
import * as path from "node:path"
|
|
4
|
+
import { execSync } from "node:child_process"
|
|
5
|
+
|
|
6
|
+
// Detect the package manager being used in the project
|
|
7
|
+
const detectPackageManager = (): string => {
|
|
8
|
+
const userAgent = process.env.npm_config_user_agent || ""
|
|
9
|
+
if (userAgent.startsWith("yarn")) return "yarn"
|
|
10
|
+
if (userAgent.startsWith("pnpm")) return "pnpm"
|
|
11
|
+
if (userAgent.startsWith("bun")) return "bun"
|
|
12
|
+
|
|
13
|
+
if (fs.existsSync("yarn.lock")) return "yarn"
|
|
14
|
+
if (fs.existsSync("pnpm-lock.yaml")) return "pnpm"
|
|
15
|
+
if (fs.existsSync("bun.lockb")) return "bun"
|
|
16
|
+
|
|
17
|
+
return "npm" // Default to npm
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Generate a React-compatible tsconfig.json
|
|
21
|
+
const generateTsConfig = (dir: string) => {
|
|
22
|
+
const tsconfigPath = path.join(dir, "tsconfig.json")
|
|
23
|
+
const tsconfigContent = JSON.stringify(
|
|
24
|
+
{
|
|
25
|
+
compilerOptions: {
|
|
26
|
+
target: "ES6",
|
|
27
|
+
module: "ESNext",
|
|
28
|
+
jsx: "react-jsx",
|
|
29
|
+
outDir: "dist",
|
|
30
|
+
strict: true,
|
|
31
|
+
esModuleInterop: true,
|
|
32
|
+
moduleResolution: "node",
|
|
33
|
+
skipLibCheck: true,
|
|
34
|
+
forceConsistentCasingInFileNames: true,
|
|
35
|
+
resolveJsonModule: true,
|
|
36
|
+
sourceMap: true,
|
|
37
|
+
allowSyntheticDefaultImports: true,
|
|
38
|
+
experimentalDecorators: true,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
null,
|
|
42
|
+
2,
|
|
43
|
+
)
|
|
44
|
+
if (!fs.existsSync(tsconfigPath)) {
|
|
45
|
+
fs.writeFileSync(tsconfigPath, tsconfigContent.trimStart())
|
|
46
|
+
console.log(`Created: ${tsconfigPath}`)
|
|
47
|
+
} else {
|
|
48
|
+
console.log(`Skipped: ${tsconfigPath} already exists`)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
4
51
|
|
|
5
52
|
export const registerInit = (program: Command) => {
|
|
6
53
|
program
|
|
@@ -8,11 +55,13 @@ export const registerInit = (program: Command) => {
|
|
|
8
55
|
.description("Initialize a new TSCircuit project in the current directory")
|
|
9
56
|
.action(() => {
|
|
10
57
|
const currentDir = process.cwd()
|
|
11
|
-
|
|
12
58
|
const indexFilePath = path.join(currentDir, "index.tsx")
|
|
13
59
|
const npmrcFilePath = path.join(currentDir, ".npmrc")
|
|
14
60
|
|
|
61
|
+
// Content for index.tsx
|
|
15
62
|
const indexContent = `
|
|
63
|
+
import "@tscircuit/core"
|
|
64
|
+
|
|
16
65
|
export default () => (
|
|
17
66
|
<board width="10mm" height="10mm">
|
|
18
67
|
<resistor
|
|
@@ -32,12 +81,14 @@ export default () => (
|
|
|
32
81
|
<trace from=".R1 > .pin1" to=".C1 > .pin1" />
|
|
33
82
|
</board>
|
|
34
83
|
);
|
|
35
|
-
|
|
84
|
+
`.trim()
|
|
36
85
|
|
|
86
|
+
// Content for .npmrc
|
|
37
87
|
const npmrcContent = `
|
|
38
88
|
@tsci:registry=https://npm.tscircuit.com
|
|
39
|
-
|
|
89
|
+
`.trim()
|
|
40
90
|
|
|
91
|
+
// Create index.tsx if it doesn't exist
|
|
41
92
|
if (!fs.existsSync(indexFilePath)) {
|
|
42
93
|
fs.writeFileSync(indexFilePath, indexContent.trimStart())
|
|
43
94
|
console.log(`Created: ${indexFilePath}`)
|
|
@@ -45,6 +96,7 @@ export default () => (
|
|
|
45
96
|
console.log(`Skipped: ${indexFilePath} already exists`)
|
|
46
97
|
}
|
|
47
98
|
|
|
99
|
+
// Create .npmrc if it doesn't exist
|
|
48
100
|
if (!fs.existsSync(npmrcFilePath)) {
|
|
49
101
|
fs.writeFileSync(npmrcFilePath, npmrcContent.trimStart())
|
|
50
102
|
console.log(`Created: ${npmrcFilePath}`)
|
|
@@ -52,6 +104,31 @@ export default () => (
|
|
|
52
104
|
console.log(`Skipped: ${npmrcFilePath} already exists`)
|
|
53
105
|
}
|
|
54
106
|
|
|
107
|
+
// Detect the package manager
|
|
108
|
+
const packageManager = detectPackageManager()
|
|
109
|
+
console.log(`Detected package manager: ${packageManager}`)
|
|
110
|
+
|
|
111
|
+
// Install deps using the detected package manager
|
|
112
|
+
const dependencies = "@types/react @tscircuit/core"
|
|
113
|
+
try {
|
|
114
|
+
console.log("Installing dependencies...")
|
|
115
|
+
const installCommand =
|
|
116
|
+
packageManager === "yarn"
|
|
117
|
+
? `yarn add -D ${dependencies}`
|
|
118
|
+
: packageManager === "pnpm"
|
|
119
|
+
? `pnpm add -D ${dependencies}`
|
|
120
|
+
: packageManager === "bun"
|
|
121
|
+
? `bun add -D ${dependencies}`
|
|
122
|
+
: `npm install -D ${dependencies}`
|
|
123
|
+
execSync(installCommand, { stdio: "inherit" })
|
|
124
|
+
console.log("Dependencies installed successfully.")
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error("Failed to install dependencies:", error)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Generate tsconfig.json
|
|
130
|
+
generateTsConfig(currentDir)
|
|
131
|
+
|
|
55
132
|
console.log(
|
|
56
133
|
`Initialization complete. Run "tsci dev" to start developing.`,
|
|
57
134
|
)
|