refactor(cli): prepare steps, utils, and globals; clean up unused args
This commit is contained in:
parent
c1befc44d8
commit
5003d6bf9d
9 changed files with 64 additions and 69 deletions
|
@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
@ -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)");
|
||||
},
|
||||
});
|
||||
|
|
|
@ -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/// <reference path="./mcfly-imports.d.ts" />`;
|
||||
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");
|
||||
},
|
||||
});
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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`)",
|
||||
},
|
||||
};
|
6
packages/cli/globals/mcfly-imports.d.ts
vendored
Normal file
6
packages/cli/globals/mcfly-imports.d.ts
vendored
Normal file
|
@ -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";
|
1
packages/cli/globals/mcfly.d.ts
vendored
Normal file
1
packages/cli/globals/mcfly.d.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/// <reference path="./mcfly-imports.d.ts" />
|
9
packages/cli/utils/try-catch.mjs
Normal file
9
packages/cli/utils/try-catch.mjs
Normal file
|
@ -0,0 +1,9 @@
|
|||
export function tryCatch(fn) {
|
||||
let error;
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
return error;
|
||||
}
|
|
@ -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() });
|
||||
|
|
Loading…
Reference in a new issue