exportc 0.0.4 → 0.0.5

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 (3) hide show
  1. package/README.md +31 -25
  2. package/commands/init.js +65 -31
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # exportc
2
2
 
3
- Add [export](https://github.com/ihasq/export) to existing Vite projects.
3
+ Add [export](https://github.com/ihasq/export) to existing Vite projects. One command sets up server-side functions with full TypeScript support.
4
4
 
5
5
  ## Quick Start
6
6
 
@@ -18,6 +18,13 @@ npm run dev
18
18
  npm run export
19
19
  ```
20
20
 
21
+ ## What You Get
22
+
23
+ - **Single command dev** -- `npm run dev` starts both Vite and Wrangler
24
+ - **Auto-generated types** -- TypeScript definitions from your actual code
25
+ - **Workers Sites deploy** -- Static assets + server exports in one deployment
26
+ - **Zero config** -- Production URL auto-detected from package name
27
+
21
28
  ## Usage
22
29
 
23
30
  After initialization, import your server exports using the `export/` prefix:
@@ -34,24 +41,17 @@ await counter.increment(); // 1
34
41
 
35
42
  ## Commands
36
43
 
37
- ### `exportc init`
38
-
39
- Initialize export in your Vite project:
40
- - Creates `export/` directory with example server code
41
- - Updates `vite.config.ts` with the export plugin
42
- - Adds npm scripts for development and deployment
43
-
44
- ### `exportc dev`
45
-
46
- Start the Wrangler development server for your exports.
47
-
48
- ### `exportc deploy`
49
-
50
- Deploy your exports to Cloudflare Workers.
44
+ | Command | Description |
45
+ |---------|-------------|
46
+ | `npm run dev` | Start Vite + Wrangler together, auto-generate types |
47
+ | `npm run export` | Build Vite app and deploy to Workers Sites |
48
+ | `exportc init` | Initialize export in your project |
49
+ | `exportc dev` | Start Wrangler dev server standalone |
50
+ | `exportc deploy` | Deploy exports only |
51
51
 
52
52
  ## Vite Plugin
53
53
 
54
- The `exportPlugin` automatically starts Wrangler and transforms `export/` imports:
54
+ The `exportPlugin` handles everything automatically:
55
55
 
56
56
  ```typescript
57
57
  // vite.config.ts
@@ -67,15 +67,20 @@ export default defineConfig({
67
67
  });
68
68
  ```
69
69
 
70
- **Development** (`npm run dev`):
71
- 1. Automatically starts Wrangler dev server
72
- 2. Waits for it to be ready
73
- 3. Transforms `export/` imports to `http://localhost:8787`
70
+ ### Development (`npm run dev`)
71
+
72
+ 1. Automatically starts Wrangler dev server in the background
73
+ 2. Waits for it to be ready before serving your app
74
+ 3. Generates `export-env.d.ts` with TypeScript declarations
75
+ 4. Watches for changes and regenerates types automatically
76
+ 5. Transforms `export/` imports to `http://localhost:8787`
77
+
78
+ ### Production (`npm run export`)
74
79
 
75
- **Production** (`npm run export`):
76
- 1. Builds Vite app
80
+ 1. Builds your Vite app
77
81
  2. Deploys to Workers Sites (static assets + server exports)
78
82
  3. `export/` imports resolve to `https://{worker-name}.workers.dev`
83
+ 4. Everything runs on Cloudflare's edge network
79
84
 
80
85
  ## Project Structure
81
86
 
@@ -83,11 +88,12 @@ After running `exportc init`:
83
88
 
84
89
  ```
85
90
  my-vite-app/
86
- ├── src/ # Your Vite app
91
+ ├── src/ # Your Vite app (unchanged)
87
92
  ├── export/ # Server exports (Cloudflare Worker)
88
93
  │ ├── index.ts # Your server code
89
- └── package.json # Worker configuration
90
- ├── export-env.d.ts # TypeScript declarations
94
+ ├── package.json # Worker configuration
95
+ │ └── .gitignore # Generated files excluded
96
+ ├── export-env.d.ts # TypeScript declarations (auto-generated)
91
97
  └── vite.config.ts # Updated with exportPlugin
92
98
  ```
93
99
 
package/commands/init.js CHANGED
@@ -8,14 +8,21 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
8
 
9
9
  export async function init(argv) {
10
10
  const cwd = process.cwd();
11
+ const isNonInteractive = argv.yes || argv.y || process.env.CI === "true";
11
12
 
12
- p.intro(pc.bgCyan(pc.black(" exportc init ")));
13
+ if (!isNonInteractive) {
14
+ p.intro(pc.bgCyan(pc.black(" exportc init ")));
15
+ }
13
16
 
14
17
  // Check for Vite project
15
18
  const viteConfigFiles = ["vite.config.ts", "vite.config.js", "vite.config.mts", "vite.config.mjs"];
16
19
  const viteConfig = viteConfigFiles.find((f) => fs.existsSync(path.join(cwd, f)));
17
20
 
18
21
  if (!viteConfig) {
22
+ if (isNonInteractive) {
23
+ console.error("Error: No Vite config found. exportc currently only supports Vite projects.");
24
+ process.exit(1);
25
+ }
19
26
  p.cancel("No Vite config found. exportc currently only supports Vite projects.");
20
27
  process.exit(1);
21
28
  }
@@ -23,6 +30,10 @@ export async function init(argv) {
23
30
  // Check for package.json
24
31
  const pkgPath = path.join(cwd, "package.json");
25
32
  if (!fs.existsSync(pkgPath)) {
33
+ if (isNonInteractive) {
34
+ console.error("Error: No package.json found.");
35
+ process.exit(1);
36
+ }
26
37
  p.cancel("No package.json found.");
27
38
  process.exit(1);
28
39
  }
@@ -31,7 +42,7 @@ export async function init(argv) {
31
42
 
32
43
  // Check if already initialized
33
44
  const exportDir = path.join(cwd, "export");
34
- if (fs.existsSync(exportDir)) {
45
+ if (fs.existsSync(exportDir) && !isNonInteractive) {
35
46
  const overwrite = await p.confirm({
36
47
  message: "export/ directory already exists. Continue and overwrite?",
37
48
  initialValue: false,
@@ -42,28 +53,40 @@ export async function init(argv) {
42
53
  }
43
54
  }
44
55
 
45
- // Get worker name
46
- const workerName = await p.text({
47
- message: "Worker name:",
48
- placeholder: pkg.name ? `${pkg.name}-api` : "my-api",
49
- defaultValue: pkg.name ? `${pkg.name}-api` : "my-api",
50
- validate: (v) => {
51
- if (!v) return "Worker name is required";
52
- if (!/^[a-z0-9-]+$/.test(v)) return "Use lowercase letters, numbers, and hyphens only";
53
- },
54
- });
56
+ // Get worker name (use default in non-interactive mode)
57
+ const defaultWorkerName = pkg.name ? `${pkg.name}-api` : "my-api";
58
+ let workerName;
59
+
60
+ if (isNonInteractive) {
61
+ workerName = argv.name || defaultWorkerName;
62
+ } else {
63
+ workerName = await p.text({
64
+ message: "Worker name:",
65
+ placeholder: defaultWorkerName,
66
+ defaultValue: defaultWorkerName,
67
+ validate: (v) => {
68
+ if (!v) return "Worker name is required";
69
+ if (!/^[a-z0-9-]+$/.test(v)) return "Use lowercase letters, numbers, and hyphens only";
70
+ },
71
+ });
55
72
 
56
- if (p.isCancel(workerName)) {
57
- p.cancel("Operation cancelled.");
58
- process.exit(0);
73
+ if (p.isCancel(workerName)) {
74
+ p.cancel("Operation cancelled.");
75
+ process.exit(0);
76
+ }
59
77
  }
60
78
 
61
79
  // Check for TypeScript
62
80
  const isTypeScript = viteConfig.endsWith(".ts") || viteConfig.endsWith(".mts") ||
63
81
  fs.existsSync(path.join(cwd, "tsconfig.json"));
64
82
 
65
- const s = p.spinner();
66
- s.start("Initializing export...");
83
+ let s;
84
+ if (!isNonInteractive) {
85
+ s = p.spinner();
86
+ s.start("Initializing export...");
87
+ } else {
88
+ console.log("Initializing export...");
89
+ }
67
90
 
68
91
  // Create export directory
69
92
  if (!fs.existsSync(exportDir)) {
@@ -262,18 +285,22 @@ declare module "export/" {
262
285
  fs.writeFileSync(envDtsPath, envDtsContent);
263
286
  }
264
287
 
265
- // Update tsconfig.json to include the type declarations
288
+ // Update tsconfig to include the type declarations
289
+ // Modern Vite uses tsconfig.app.json for app code, older uses tsconfig.json directly
290
+ const tsconfigAppPath = path.join(cwd, "tsconfig.app.json");
266
291
  const tsconfigPath = path.join(cwd, "tsconfig.json");
267
- if (fs.existsSync(tsconfigPath)) {
292
+ const targetTsconfig = fs.existsSync(tsconfigAppPath) ? tsconfigAppPath : tsconfigPath;
293
+
294
+ if (fs.existsSync(targetTsconfig)) {
268
295
  try {
269
- const tsconfigContent = fs.readFileSync(tsconfigPath, "utf8");
296
+ const tsconfigContent = fs.readFileSync(targetTsconfig, "utf8");
270
297
  const tsconfig = JSON.parse(tsconfigContent);
271
298
 
272
299
  // Add export-env.d.ts to include if not already present
273
300
  tsconfig.include = tsconfig.include || [];
274
301
  if (!tsconfig.include.includes("export-env.d.ts")) {
275
302
  tsconfig.include.push("export-env.d.ts");
276
- fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + "\n");
303
+ fs.writeFileSync(targetTsconfig, JSON.stringify(tsconfig, null, 2) + "\n");
277
304
  }
278
305
  } catch {
279
306
  // Ignore JSON parse errors (might have comments)
@@ -281,22 +308,23 @@ declare module "export/" {
281
308
  }
282
309
  }
283
310
 
284
- s.stop("Export initialized!");
311
+ if (!isNonInteractive) {
312
+ s.stop("Export initialized!");
285
313
 
286
- const filesCreated = isTypeScript
287
- ? `${pc.cyan("export/")}
314
+ const filesCreated = isTypeScript
315
+ ? `${pc.cyan("export/")}
288
316
  ├── index.${ext} ${pc.dim("# Your server exports")}
289
317
  ├── package.json ${pc.dim("# Worker configuration")}
290
318
  └── .gitignore
291
319
 
292
320
  ${pc.cyan("export-env.d.ts")} ${pc.dim("# Type declarations for export/ imports")}`
293
- : `${pc.cyan("export/")}
321
+ : `${pc.cyan("export/")}
294
322
  ├── index.${ext} ${pc.dim("# Your server exports")}
295
323
  ├── package.json ${pc.dim("# Worker configuration")}
296
324
  └── .gitignore`;
297
325
 
298
- p.note(
299
- `${filesCreated}
326
+ p.note(
327
+ `${filesCreated}
300
328
 
301
329
  ${pc.bold("Next steps:")}
302
330
 
@@ -313,8 +341,14 @@ ${pc.bold("Next steps:")}
313
341
  4. Deploy to Cloudflare Workers Sites:
314
342
  ${pc.cyan("npm run export")}
315
343
  ${pc.dim("# Builds Vite + deploys everything to Workers")}`,
316
- "Created"
317
- );
318
-
319
- p.outro(`Run ${pc.cyan("cd export && npm install")} to get started!`);
344
+ "Created"
345
+ );
346
+
347
+ p.outro(`Run ${pc.cyan("cd export && npm install")} to get started!`);
348
+ } else {
349
+ console.log("Export initialized successfully!");
350
+ console.log("\nNext steps:");
351
+ console.log(" cd export && npm install && cd ..");
352
+ console.log(" npm run dev");
353
+ }
320
354
  }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "exportc",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "CLI to add export to existing projects",
5
5
  "scripts": {
6
- "test": "node --test test/*.test.mjs"
6
+ "test": "node --test test/init.test.mjs",
7
+ "test:e2e": "node --test test/e2e.test.mjs"
7
8
  },
8
9
  "keywords": [
9
10
  "cloudflare",