tsc-app 3.1.0

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.
Files changed (2) hide show
  1. package/index.js +65 -0
  2. package/package.json +11 -0
package/index.js ADDED
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Use CommonJS so it works out-of-the-box with npm CLI
4
+ const fs = require("fs");
5
+ const { execSync } = require("child_process");
6
+
7
+ // --- Args ---
8
+ const args = process.argv.slice(2);
9
+ const projectName = args[0] || "my-ts-app";
10
+
11
+ // Defaults
12
+ let packageType = "module"; // package.json type
13
+ let tsModuleKind = "esnext"; // tsc --module value
14
+
15
+
16
+ // Parse -m flag
17
+ if (args.includes("-m")) {
18
+ const val = args[args.indexOf("-m") + 1]?.toLowerCase();
19
+ if (["module", "esm", "esnext"].includes(val)) {
20
+ packageType = "module";
21
+ tsModuleKind = "esnext";
22
+ } else if (["commonjs", "cjs"].includes(val)) {
23
+ packageType = "commonjs";
24
+ tsModuleKind = "commonjs";
25
+ } else {
26
+ console.warn(`⚠ Unknown module type "${val}", defaulting to module`);
27
+ }
28
+ }
29
+
30
+ console.log(`Creating TypeScript app: ${projectName} [package.json type=${packageType}, tsc module=${tsModuleKind}] ...`);
31
+
32
+ // Create project folder
33
+ fs.mkdirSync(projectName);
34
+ process.chdir(projectName);
35
+
36
+ // Init package.json
37
+ execSync("npm init -y", { stdio: "inherit" });
38
+
39
+ // Install dev deps
40
+ execSync("npm i -D typescript ts-node @types/node", { stdio: "inherit" });
41
+
42
+ // Init tsconfig.json
43
+ execSync(
44
+ `npx tsc --init --rootDir src --outDir dist --esModuleInterop --resolveJsonModule --module ${tsModuleKind} --target es2020`,
45
+ { stdio: "inherit" }
46
+ );
47
+
48
+ // Create src + starter file
49
+ fs.mkdirSync("src");
50
+ fs.writeFileSync("src/main.ts", `console.log("Hello TypeScript CLI");`);
51
+
52
+ // Update package.json scripts + type
53
+ const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
54
+ pkg.type = packageType;
55
+ pkg.scripts = {
56
+ start: "tsc src/main.ts",
57
+ build: "tsc",
58
+ serve: "node dist/main.js"
59
+ };
60
+ fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2));
61
+
62
+ console.log("Done!");
63
+ console.log(`cd ${projectName}`);
64
+ console.log(`bpm run start`);
65
+ console.log("npm run serve");
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "tsc-app",
3
+ "version": "3.1.0",
4
+ "description": "Quick TypeScript project initializer",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "init-ts-app": "index.js"
8
+ },
9
+ "license": "MIT",
10
+ "type": "commonjs"
11
+ }