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 { consola } from "consola";
|
||||||
import { defineCommand } from "citty";
|
import { defineCommand } from "citty";
|
||||||
import { commonArgs } from "../common.mjs";
|
|
||||||
import { execSync as exec } from "child_process";
|
import { execSync as exec } from "child_process";
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
|
@ -10,14 +9,13 @@ export default defineCommand({
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
description: "Builds the McFly project for production.",
|
description: "Builds the McFly project for production.",
|
||||||
},
|
},
|
||||||
args: {
|
|
||||||
...commonArgs,
|
async run() {
|
||||||
},
|
consola.start("Building project...");
|
||||||
async run({ args }) {
|
|
||||||
try {
|
try {
|
||||||
exec(`npx nitropack build`, { stdio: "inherit" });
|
exec(`npx nitropack build`, { stdio: "inherit" });
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
consola.error(e);
|
consola.error(err);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,17 +2,13 @@
|
||||||
|
|
||||||
import { consola } from "consola";
|
import { consola } from "consola";
|
||||||
import { defineCommand } from "citty";
|
import { defineCommand } from "citty";
|
||||||
import { commonArgs } from "../common.mjs";
|
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
meta: {
|
meta: {
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
description: "Generates building blocks for a McFly app.",
|
description: "Generates building blocks for a McFly app.",
|
||||||
},
|
},
|
||||||
args: {
|
async run() {
|
||||||
...commonArgs,
|
|
||||||
},
|
|
||||||
async run({ args }) {
|
|
||||||
consola.box("Generate a McFly building block (In-progress)");
|
consola.box("Generate a McFly building block (In-progress)");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,53 +2,56 @@
|
||||||
|
|
||||||
import { consola } from "consola";
|
import { consola } from "consola";
|
||||||
import { defineCommand } from "citty";
|
import { defineCommand } from "citty";
|
||||||
import { commonArgs } from "../common.mjs";
|
import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
|
||||||
import { existsSync, writeFileSync, appendFileSync } from "node:fs";
|
import { createRequire } from "node:module";
|
||||||
import { execSync as exec } from "child_process";
|
import { execSync as exec } from "child_process";
|
||||||
|
import { tryCatch } from "../utils/try-catch.mjs";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
meta: {
|
meta: {
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
description: "Prepares the McFly workspace.",
|
description: "Prepares the McFly workspace.",
|
||||||
},
|
},
|
||||||
args: {
|
async run() {
|
||||||
...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;
|
|
||||||
|
|
||||||
consola.start("Preparing McFly workspace...");
|
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")) {
|
const require = createRequire(import.meta.url);
|
||||||
try {
|
const globalsPath = path.dirname(require.resolve("@mcflyjs/cli"));
|
||||||
writeFileSync(".nitro/types/mcfly-imports.d.ts", globalDefinition);
|
|
||||||
} catch (e) {
|
const steps = [
|
||||||
consola.error(e);
|
() => exec("npx nitropack prepare", { stdio: "inherit" }),
|
||||||
hasErrors = true;
|
() =>
|
||||||
}
|
copyFileSync(
|
||||||
try {
|
`${globalsPath}/globals/mcfly-imports.d.ts`,
|
||||||
appendFileSync(".nitro/types/nitro.d.ts", typeDefinition);
|
".nitro/types/mcfly-imports.d.ts"
|
||||||
} catch (e) {
|
),
|
||||||
consola.error(e);
|
() =>
|
||||||
hasErrors = true;
|
copyFileSync(
|
||||||
}
|
`${globalsPath}/globals/mcfly.d.ts`,
|
||||||
} else {
|
".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(
|
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;
|
} else consola.success("Done\n");
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasErrors) {
|
|
||||||
consola.success("Done!");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import { consola } from "consola";
|
import { consola } from "consola";
|
||||||
import { defineCommand } from "citty";
|
import { defineCommand } from "citty";
|
||||||
import { commonArgs } from "../common.mjs";
|
|
||||||
import { execSync as exec } from "child_process";
|
import { execSync as exec } from "child_process";
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
|
@ -10,10 +9,7 @@ export default defineCommand({
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
description: "Runs the dev server.",
|
description: "Runs the dev server.",
|
||||||
},
|
},
|
||||||
args: {
|
async run() {
|
||||||
...commonArgs,
|
|
||||||
},
|
|
||||||
async run({ args }) {
|
|
||||||
try {
|
try {
|
||||||
exec(`npx nitropack dev`, { stdio: "inherit" });
|
exec(`npx nitropack dev`, { stdio: "inherit" });
|
||||||
} catch (e) {
|
} 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
|
* McFly SSR logic
|
||||||
* 👋 this is not the route you're looking for
|
* 👋 this is not the route you're looking for
|
||||||
|
@ -5,6 +6,5 @@
|
||||||
* ...reusable code are in ./src/components
|
* ...reusable code are in ./src/components
|
||||||
* @see https://ayco.io/gh/McFly#special-directories
|
* @see https://ayco.io/gh/McFly#special-directories
|
||||||
*/
|
*/
|
||||||
import { defineRoute } from "@mcflyjs/core/event-handler.mjs";
|
|
||||||
import config from "../mcfly.config.mjs";
|
import config from "../mcfly.config.mjs";
|
||||||
export default defineRoute({ config, storage: useStorage() });
|
export default defineRoute({ config, storage: useStorage() });
|
||||||
|
|
Loading…
Reference in a new issue