feat(cli): initial commands new and generate

This commit is contained in:
Ayo 2023-10-25 22:00:07 +02:00
parent 82d4131b7e
commit 9584c2731c
6 changed files with 63 additions and 38 deletions

View file

@ -1,5 +0,0 @@
#!/usr/bin/env node
const { consola } = require("consola");
consola.prompt("Name your new custom element");

View file

@ -0,0 +1,18 @@
#!/usr/bin/env node
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 }) {
consola.box("Generate a McFly building block");
},
});

View file

@ -8,18 +8,17 @@ import { commonArgs } from "../common.mjs";
export default defineCommand({
meta: {
name: "prepare",
description: "Generate types for the project",
description: "Creates a new McFly project.",
},
args: {
...commonArgs,
},
async run({ args }) {
const [directory] = args._;
consola.info(directory);
// try {
// exec("npm create mcfly@latest", { stdio: "inherit" });
// } catch (e) {
// consola.error(e);
// }
try {
exec(`npm create mcfly@latest ${directory ?? ""}`, { stdio: "inherit" });
} catch (e) {
consola.error(e);
}
},
});

View file

@ -1,24 +0,0 @@
#!/usr/bin/env node
const { existsSync, writeFileSync, appendFileSync } = require("node:fs");
const typeDefinition = `\n/// <reference path="./mcfly-imports.d.ts" />`;
const globalDefinition = `import {WebComponent as W} from "web-component-base/WebComponent.mjs"; declare global {const WebComponent: typeof W;}export {WebComponent};`;
if (existsSync(".nitro/types/nitro.d.ts")) {
console.log("has directory");
try {
writeFileSync(".nitro/types/mcfly-imports.d.ts", globalDefinition);
} catch (e) {
console.error(e);
}
try {
appendFileSync(".nitro/types/nitro.d.ts", typeDefinition);
} catch (e) {
console.error(e);
}
} else {
console.log(
"Preparation Failed. Please run:\n> npx nitropack prepare && npx mcfly prepare"
);
}

View file

@ -0,0 +1,37 @@
#!/usr/bin/env node
import { consola } from "consola";
import { defineCommand } from "citty";
import { commonArgs } from "../common.mjs";
import { existsSync, writeFileSync, appendFileSync } from "node:fs";
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 {const WebComponent: typeof W;}export {WebComponent};`;
if (existsSync(".nitro/types/nitro.d.ts")) {
try {
writeFileSync(".nitro/types/mcfly-imports.d.ts", globalDefinition);
} catch (e) {
consola.error(e);
}
try {
appendFileSync(".nitro/types/nitro.d.ts", typeDefinition);
} catch (e) {
consola.error(e);
}
} else {
consola.log(
"Preparation Failed. Please run:\n> npx nitropack prepare && npx mcfly prepare"
);
}
},
});

View file

@ -9,8 +9,8 @@ const main = defineCommand({
version: pkg.version,
},
subCommands: {
prepare: () => import("./commands/prepare.js").then((r) => r.default),
generate: () => import("./commands/generate.js").then((r) => r.default),
prepare: () => import("./commands/prepare.mjs").then((r) => r.default),
generate: () => import("./commands/generate.mjs").then((r) => r.default),
new: () => import("./commands/new.mjs").then((r) => r.default),
},
});