@adonisjs/inertia
Version:
Official Inertia.js adapter for AdonisJS
55 lines (54 loc) • 2.67 kB
JavaScript
import { readdir } from "node:fs/promises";
import { BaseCommand, args, flags } from "@adonisjs/core/ace";
import { join } from "node:path";
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
const stubsRoot = join(import.meta.dirname, "../stubs");
var MakePage = class extends BaseCommand {
static commandName = "make:page";
static description = "Create a new Inertia page component";
static options = { allowUnknownFlags: true };
pagesDir = "inertia/pages";
async #detectFramework() {
try {
const files = await readdir(this.app.makePath(this.pagesDir), { recursive: true });
const hasVue = files.some((file) => file.endsWith(".vue"));
const hasReact = files.some((file) => file.endsWith(".tsx") || file.endsWith(".jsx"));
if (hasVue && !hasReact) return "vue";
if (hasReact && !hasVue) return "react";
return null;
} catch {
return null;
}
}
async #resolveFramework() {
if (this.vue) return "vue";
if (this.react) return "react";
const detected = await this.#detectFramework();
if (detected) return detected;
return this.prompt.choice("Select the frontend framework", ["vue", "react"]);
}
async run() {
const framework = await this.#resolveFramework();
const codemods = await this.createCodemods();
codemods.overwriteExisting = this.force === true;
await codemods.makeUsingStub(stubsRoot, `make/page/${framework}.stub`, {
flags: this.parsed.flags,
pagesDir: this.pagesDir,
entity: this.app.generators.createEntity(this.name)
}, { contentsFromFile: this.contentsFrom });
}
};
__decorate([args.string({ description: "Name of the page component" })], MakePage.prototype, "name", void 0);
__decorate([flags.boolean({ description: "Create a Vue page component" })], MakePage.prototype, "vue", void 0);
__decorate([flags.boolean({ description: "Create a React page component" })], MakePage.prototype, "react", void 0);
__decorate([flags.string({ description: "Use the contents of the given file as the generated output" })], MakePage.prototype, "contentsFrom", void 0);
__decorate([flags.boolean({
description: "Forcefully overwrite existing files",
alias: "f"
})], MakePage.prototype, "force", void 0);
export { MakePage as default };