diff --git a/packages/cli/commands/build.mjs b/packages/cli/commands/build.mjs
index 66bf312..2d78fe3 100644
--- a/packages/cli/commands/build.mjs
+++ b/packages/cli/commands/build.mjs
@@ -2,7 +2,6 @@
import { consola } from "consola";
import { defineCommand } from "citty";
-import { commonArgs } from "../common.mjs";
import { execSync as exec } from "child_process";
export default defineCommand({
@@ -10,14 +9,13 @@ export default defineCommand({
name: "prepare",
description: "Builds the McFly project for production.",
},
- args: {
- ...commonArgs,
- },
- async run({ args }) {
+
+ async run() {
+ consola.start("Building project...");
try {
exec(`npx nitropack build`, { stdio: "inherit" });
- } catch (e) {
- consola.error(e);
+ } catch (err) {
+ consola.error(err);
}
},
});
diff --git a/packages/cli/commands/generate.mjs b/packages/cli/commands/generate.mjs
index 84f5fab..8379f9c 100644
--- a/packages/cli/commands/generate.mjs
+++ b/packages/cli/commands/generate.mjs
@@ -2,17 +2,13 @@
import { consola } from "consola";
import { defineCommand } from "citty";
-import { commonArgs } from "../common.mjs";
export default defineCommand({
meta: {
name: "prepare",
description: "Generates building blocks for a McFly app.",
},
- args: {
- ...commonArgs,
- },
- async run({ args }) {
+ async run() {
consola.box("Generate a McFly building block (In-progress)");
},
});
diff --git a/packages/cli/commands/prepare.mjs b/packages/cli/commands/prepare.mjs
index 6989910..36f37cc 100755
--- a/packages/cli/commands/prepare.mjs
+++ b/packages/cli/commands/prepare.mjs
@@ -2,53 +2,56 @@
import { consola } from "consola";
import { defineCommand } from "citty";
-import { commonArgs } from "../common.mjs";
-import { existsSync, writeFileSync, appendFileSync } from "node:fs";
+import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
+import { createRequire } from "node:module";
import { execSync as exec } from "child_process";
+import { tryCatch } from "../utils/try-catch.mjs";
+import path from "node:path";
export default defineCommand({
meta: {
name: "prepare",
description: "Prepares the McFly workspace.",
},
- args: {
- ...commonArgs,
- },
- async run({ args }) {
- const typeDefinition = `\n/// `;
- const globalDefinition = `import {WebComponent as W} from "web-component-base/WebComponent.mjs"; declare global {class WebComponent extends W {}}export {WebComponent} from 'web-component-base/WebComponent.mjs';`;
- let hasErrors = false;
-
+ async run() {
consola.start("Preparing McFly workspace...");
- try {
- exec("npx nitropack prepare", { stdio: "inherit" });
- } catch (e) {
- consola.error(e);
- hasErrors = true;
- }
- if (existsSync(".nitro/types/nitro.d.ts")) {
- try {
- writeFileSync(".nitro/types/mcfly-imports.d.ts", globalDefinition);
- } catch (e) {
- consola.error(e);
- hasErrors = true;
- }
- try {
- appendFileSync(".nitro/types/nitro.d.ts", typeDefinition);
- } catch (e) {
- consola.error(e);
- hasErrors = true;
- }
- } else {
+ const require = createRequire(import.meta.url);
+ const globalsPath = path.dirname(require.resolve("@mcflyjs/cli"));
+
+ const steps = [
+ () => exec("npx nitropack prepare", { stdio: "inherit" }),
+ () =>
+ copyFileSync(
+ `${globalsPath}/globals/mcfly-imports.d.ts`,
+ ".nitro/types/mcfly-imports.d.ts"
+ ),
+ () =>
+ copyFileSync(
+ `${globalsPath}/globals/mcfly.d.ts`,
+ ".nitro/types/mcfly.d.ts"
+ ),
+ () => {
+ const path = ".nitro/types/tsconfig.json";
+ const tsconfig = readFileSync(path);
+ const configStr = tsconfig.toString();
+ const config = JSON.parse(configStr);
+ config.include.push("./mcfly.d.ts");
+ writeFileSync(path, JSON.stringify(config));
+ },
+ ].map((fn) => () => tryCatch(fn));
+
+ let err;
+ steps.every((step) => {
+ err = step();
+ return !err;
+ });
+
+ if (err) {
+ consola.error(err);
consola.fail(
- "Preparation Failed. Please run:\n> npx nitropack prepare && npx mcfly prepare"
+ "McFly workspace preparation failed. Please make sure dependencies are installed.\n"
);
- hasErrors = true;
- }
-
- if (!hasErrors) {
- consola.success("Done!");
- }
+ } else consola.success("Done\n");
},
});
diff --git a/packages/cli/commands/serve.mjs b/packages/cli/commands/serve.mjs
index 7b0c0ce..8c6ffed 100644
--- a/packages/cli/commands/serve.mjs
+++ b/packages/cli/commands/serve.mjs
@@ -2,7 +2,6 @@
import { consola } from "consola";
import { defineCommand } from "citty";
-import { commonArgs } from "../common.mjs";
import { execSync as exec } from "child_process";
export default defineCommand({
@@ -10,10 +9,7 @@ export default defineCommand({
name: "prepare",
description: "Runs the dev server.",
},
- args: {
- ...commonArgs,
- },
- async run({ args }) {
+ async run() {
try {
exec(`npx nitropack dev`, { stdio: "inherit" });
} catch (e) {
diff --git a/packages/cli/common.mjs b/packages/cli/common.mjs
deleted file mode 100644
index 8a6fd2d..0000000
--- a/packages/cli/common.mjs
+++ /dev/null
@@ -1,14 +0,0 @@
-/**
- * @type {import('citty').ArgsDef}
- */
-export const commonArgs = {
- dir: {
- type: "string",
- description: "project root directory",
- },
- _dir: {
- type: "positional",
- default: ".",
- description: "project root directory (prefer using `--dir`)",
- },
-};
diff --git a/packages/cli/globals/mcfly-imports.d.ts b/packages/cli/globals/mcfly-imports.d.ts
new file mode 100644
index 0000000..550c0a4
--- /dev/null
+++ b/packages/cli/globals/mcfly-imports.d.ts
@@ -0,0 +1,6 @@
+declare global {
+ class WebComponent extends import("web-component-base/WebComponent.mjs") {}
+ const defineRoute: typeof import("@mcflyjs/core/event-handler.mjs").defineRoute;
+}
+export { WebComponent } from "web-component-base/WebComponent.mjs";
+export { defineRoute } from "@mcflyjs/core/event-handler.mjs";
diff --git a/packages/cli/globals/mcfly.d.ts b/packages/cli/globals/mcfly.d.ts
new file mode 100644
index 0000000..0469d87
--- /dev/null
+++ b/packages/cli/globals/mcfly.d.ts
@@ -0,0 +1 @@
+///
diff --git a/packages/cli/utils/try-catch.mjs b/packages/cli/utils/try-catch.mjs
new file mode 100644
index 0000000..d5df7d6
--- /dev/null
+++ b/packages/cli/utils/try-catch.mjs
@@ -0,0 +1,9 @@
+export function tryCatch(fn) {
+ let error;
+ try {
+ fn();
+ } catch (e) {
+ error = e;
+ }
+ return error;
+}
diff --git a/site/routes/[...index].js b/site/routes/[...index].js
index 77a52a2..8e5cd8a 100644
--- a/site/routes/[...index].js
+++ b/site/routes/[...index].js
@@ -1,3 +1,4 @@
+// @ts-check
/**
* McFly SSR logic
* 👋 this is not the route you're looking for
@@ -5,6 +6,5 @@
* ...reusable code are in ./src/components
* @see https://ayco.io/gh/McFly#special-directories
*/
-import { defineRoute } from "@mcflyjs/core/event-handler.mjs";
import config from "../mcfly.config.mjs";
export default defineRoute({ config, storage: useStorage() });