alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
84 lines (82 loc) • 2.54 kB
JavaScript
import "../chunks/chunk-U5RRZUYZ.js";
// src/cli/Init.ts
import { createId, outcome } from "alinea/core";
import fs from "node:fs/promises";
import path from "node:path";
import { generate } from "./Generate.js";
import { dirname } from "./util/Dirname.js";
import { findConfigFile } from "./util/FindConfigFile.js";
var __dirname = dirname(import.meta.url);
var lockfiles = {
["bun" /* Bun */]: "bun.lockb",
["pnpm" /* PNPM */]: "pnpm-lock.yaml",
["yarn" /* Yarn */]: "yarn.lock",
["npm" /* NPM */]: "package-lock.json"
};
async function detectPm() {
for (const [pm, lockFile] of Object.entries(lockfiles)) {
if ((await outcome(fs.stat(lockFile))).isSuccess()) {
return pm;
}
}
return "npm" /* NPM */;
}
async function init(options) {
const { cwd = process.cwd(), quiet = false } = options;
const configLocation = findConfigFile(cwd);
if (configLocation) {
console.log(`> An alinea config file already exists in ${cwd}`);
process.exit(1);
}
await fs.mkdir(path.join(cwd, "content/pages"), { recursive: true });
await fs.writeFile(
path.join(cwd, "content/pages/welcome.json"),
JSON.stringify(
{
id: createId(),
type: "Page",
title: "Welcome",
alinea: {
index: "a0",
seeded: true
}
},
null,
2
)
);
await fs.mkdir(path.join(cwd, "content/media"), { recursive: true });
const configFile = await fs.readFile(
path.join(__dirname, "static/init/cms.js"),
"utf-8"
);
const configFileContents = options.next ? configFile.replaceAll("createCMS", "createNextCMS") : configFile;
const hasSrcDir = (await outcome(fs.stat(path.join(cwd, "src")))).isSuccess();
const configFileLocation = path.join(
cwd,
hasSrcDir ? "src/cms.tsx" : "cms.tsx"
);
await fs.writeFile(configFileLocation, configFileContents);
const pm = await detectPm();
for await (const _ of generate({ cwd: path.resolve(cwd), quiet })) {
}
if (options.next) {
let [pkg] = await outcome(
fs.readFile(path.join(cwd, "package.json"), "utf-8")
);
if (pkg) {
pkg = pkg.replace('"dev": "', '"dev": "alinea dev -- ');
pkg = pkg.replace('"build": "', '"build": "alinea build -- ');
await fs.writeFile(path.join(cwd, "package.json"), pkg);
}
}
const runner = pm === "npm" ? "npx" : pm;
const command = `${runner} alinea dev`;
if (!quiet)
console.log(
"Alinea initialized. You can open the dashboard with `" + command + "`"
);
}
export {
init
};