create-easy-cms 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.js +1 -1
- package/dist/{chunk-F7EP27M3.js → chunk-DQXRGO3T.js} +79 -23
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -3,13 +3,13 @@ import { spawn } from "child_process";
|
|
|
3
3
|
import { randomBytes } from "crypto";
|
|
4
4
|
import { existsSync, readFileSync } from "fs";
|
|
5
5
|
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
6
|
-
import { dirname, join, relative, resolve } from "path";
|
|
6
|
+
import { basename, dirname, join, relative, resolve } from "path";
|
|
7
7
|
import { createInterface } from "readline/promises";
|
|
8
8
|
import { fileURLToPath } from "url";
|
|
9
9
|
import { parseArgs } from "util";
|
|
10
10
|
|
|
11
11
|
// src/templates.ts
|
|
12
|
-
function configTemplate(db) {
|
|
12
|
+
function configTemplate(db, options = {}) {
|
|
13
13
|
const database = db === "sqlite" ? `import { sqlite } from '@easy-cms/db-sqlite'` : `import { postgres } from '@easy-cms/db-postgres'`;
|
|
14
14
|
const adapter = db === "sqlite" ? ` db: sqlite({ url: process.env.DATABASE_URL ?? 'file:./cms.db' }),` : ` // A Postgres server when DATABASE_URL is set; otherwise PGlite (Postgres in WebAssembly) in .pglite.
|
|
15
15
|
db: process.env.DATABASE_URL
|
|
@@ -21,7 +21,9 @@ ${database}
|
|
|
21
21
|
export default defineConfig({
|
|
22
22
|
secret: process.env.EASY_CMS_SECRET ?? '',
|
|
23
23
|
${adapter}
|
|
24
|
-
admin: { locale: 'en' }
|
|
24
|
+
admin: { locale: 'en' },${options.standalone ? `
|
|
25
|
+
// Frontends on other origins that call the API from the browser.
|
|
26
|
+
cors: ['http://localhost:5173'],` : ""}
|
|
25
27
|
collections: [
|
|
26
28
|
{
|
|
27
29
|
slug: 'posts',
|
|
@@ -90,8 +92,11 @@ var defaultIO = {
|
|
|
90
92
|
var HELP = `Usage: create-easy-cms [dir] [options]
|
|
91
93
|
|
|
92
94
|
Adds Easy CMS to the Nuxt or Next.js project in [dir] (default: current directory).
|
|
95
|
+
In an empty or new directory it sets up a standalone Easy CMS server instead, the
|
|
96
|
+
backend for a Vite, React, Vue or static frontend.
|
|
93
97
|
|
|
94
98
|
Options:
|
|
99
|
+
--standalone Set up a standalone server even in an existing project
|
|
95
100
|
--db <sqlite|postgres> Database (default: ask, or sqlite with --yes)
|
|
96
101
|
--yes, -y Accept the defaults without asking
|
|
97
102
|
--skip-install Write files only; install the packages yourself
|
|
@@ -122,12 +127,12 @@ function ownVersion() {
|
|
|
122
127
|
return "latest";
|
|
123
128
|
}
|
|
124
129
|
}
|
|
125
|
-
function packagesFor(
|
|
130
|
+
function packagesFor(target, db) {
|
|
126
131
|
const v = ownVersion();
|
|
127
|
-
const deps = [`@easy-cms/core@${v}`, `@easy-cms/${
|
|
132
|
+
const deps = target === "standalone" ? [`@easy-cms/core@${v}`, `easy-cms@${v}`] : [`@easy-cms/core@${v}`, `@easy-cms/${target}@${v}`];
|
|
128
133
|
if (db === "sqlite") deps.push(`@easy-cms/db-sqlite@${v}`);
|
|
129
134
|
else deps.push(`@easy-cms/db-postgres@${v}`, "@electric-sql/pglite");
|
|
130
|
-
return { deps, devDeps: [`easy-cms@${v}`] };
|
|
135
|
+
return { deps, devDeps: target === "standalone" ? [] : [`easy-cms@${v}`] };
|
|
131
136
|
}
|
|
132
137
|
function installArgs(pm, packages, dev) {
|
|
133
138
|
const add = pm === "npm" ? "install" : "add";
|
|
@@ -145,6 +150,7 @@ async function run(argv, io = defaultIO) {
|
|
|
145
150
|
options: {
|
|
146
151
|
db: { type: "string" },
|
|
147
152
|
yes: { type: "boolean", short: "y" },
|
|
153
|
+
standalone: { type: "boolean" },
|
|
148
154
|
"skip-install": { type: "boolean" },
|
|
149
155
|
help: { type: "boolean", short: "h" }
|
|
150
156
|
}
|
|
@@ -160,15 +166,22 @@ async function run(argv, io = defaultIO) {
|
|
|
160
166
|
}
|
|
161
167
|
const dir = resolve(positionals[0] ?? process.cwd());
|
|
162
168
|
const pkgFile = join(dir, "package.json");
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (!
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
169
|
+
const hasPackage = existsSync(pkgFile);
|
|
170
|
+
const framework = hasPackage ? detectFramework(JSON.parse(await readFile(pkgFile, "utf8"))) : void 0;
|
|
171
|
+
let target = values.standalone ? "standalone" : framework;
|
|
172
|
+
if (!target && !hasPackage) {
|
|
173
|
+
target = "standalone";
|
|
174
|
+
} else if (!target) {
|
|
175
|
+
const question = "This is not a Nuxt or Next.js project. Set up a standalone Easy CMS server here? [Y/n] ";
|
|
176
|
+
const yes = !values.yes && io.interactive && io.prompt ? !/^n/i.test(await io.prompt(question)) : false;
|
|
177
|
+
if (!yes) {
|
|
178
|
+
io.err('This does not look like a Nuxt or Next.js project (no "nuxt" or "next" dependency).');
|
|
179
|
+
io.err(
|
|
180
|
+
"Create one first (`npx nuxi init my-app`, `npx create-next-app my-app`), or run with --standalone."
|
|
181
|
+
);
|
|
182
|
+
return 1;
|
|
183
|
+
}
|
|
184
|
+
target = "standalone";
|
|
172
185
|
}
|
|
173
186
|
let db = values.db;
|
|
174
187
|
if (db !== void 0 && db !== "sqlite" && db !== "postgres") {
|
|
@@ -183,14 +196,17 @@ async function run(argv, io = defaultIO) {
|
|
|
183
196
|
db = "sqlite";
|
|
184
197
|
}
|
|
185
198
|
}
|
|
186
|
-
io.out(
|
|
199
|
+
io.out(
|
|
200
|
+
target === "standalone" ? `Setting up a standalone Easy CMS server in ${relative(process.cwd(), dir) || "."} (${db}).` : `Adding Easy CMS to this ${target === "nuxt" ? "Nuxt" : "Next.js"} project (${db}).`
|
|
201
|
+
);
|
|
187
202
|
const changes = [];
|
|
188
203
|
const notes = [];
|
|
189
204
|
const configFile = join(dir, "easy-cms.config.ts");
|
|
190
205
|
if (existsSync(configFile)) {
|
|
191
206
|
notes.push("easy-cms.config.ts already exists; left unchanged.");
|
|
192
207
|
} else {
|
|
193
|
-
await
|
|
208
|
+
await mkdir(dir, { recursive: true });
|
|
209
|
+
await writeFile(configFile, configTemplate(db, { standalone: target === "standalone" }));
|
|
194
210
|
changes.push("created easy-cms.config.ts");
|
|
195
211
|
}
|
|
196
212
|
const envFile = join(dir, ".env");
|
|
@@ -220,20 +236,21 @@ ${wanted.join("\n")}
|
|
|
220
236
|
);
|
|
221
237
|
changes.push(`added ${wanted.join(", ")} to .gitignore`);
|
|
222
238
|
}
|
|
223
|
-
if (
|
|
224
|
-
else await setupNext(dir, changes, notes);
|
|
239
|
+
if (target === "nuxt") await setupNuxt(dir, changes, notes);
|
|
240
|
+
else if (target === "next") await setupNext(dir, changes, notes);
|
|
241
|
+
else await setupStandalone(dir, changes);
|
|
225
242
|
for (const change of changes) io.out(` \u2713 ${change}`);
|
|
226
243
|
for (const note of notes) io.out(` \u2022 ${note}`);
|
|
227
244
|
const pm = detectPackageManager(dir);
|
|
228
|
-
const { deps, devDeps } = packagesFor(
|
|
245
|
+
const { deps, devDeps } = packagesFor(target, db);
|
|
229
246
|
if (values["skip-install"]) {
|
|
230
247
|
io.out("\nInstall the packages:");
|
|
231
248
|
io.out(` ${pm} ${installArgs(pm, deps, false).join(" ")}`);
|
|
232
|
-
io.out(` ${pm} ${installArgs(pm, devDeps, true).join(" ")}`);
|
|
249
|
+
if (devDeps.length) io.out(` ${pm} ${installArgs(pm, devDeps, true).join(" ")}`);
|
|
233
250
|
} else if (io.exec) {
|
|
234
251
|
io.out(`
|
|
235
252
|
Installing with ${pm}\u2026`);
|
|
236
|
-
const code = await io.exec(pm, installArgs(pm, deps, false), dir) || await io.exec(pm, installArgs(pm, devDeps, true), dir);
|
|
253
|
+
const code = await io.exec(pm, installArgs(pm, deps, false), dir) || (devDeps.length ? await io.exec(pm, installArgs(pm, devDeps, true), dir) : 0);
|
|
237
254
|
if (code !== 0) {
|
|
238
255
|
io.err(
|
|
239
256
|
`
|
|
@@ -244,14 +261,53 @@ Installing failed. Run it yourself:
|
|
|
244
261
|
}
|
|
245
262
|
}
|
|
246
263
|
const devCommand = pm === "npm" ? "npm run dev" : `${pm} dev`;
|
|
264
|
+
if (target === "standalone") {
|
|
265
|
+
const cd = relative(process.cwd(), dir);
|
|
266
|
+
io.out(`
|
|
267
|
+
Done. Next:
|
|
268
|
+
1. ${cd ? `cd ${cd} && ` : ""}${devCommand} (http://localhost:4000/admin)
|
|
269
|
+
2. Create the first admin there (or run: npx easy-cms create-admin)
|
|
270
|
+
3. Point your frontend at http://localhost:4000/api/cms and list its origin in \`cors\`
|
|
271
|
+
in easy-cms.config.ts.
|
|
272
|
+
4. Before deploying: npx easy-cms migrate:create init, commit easy-cms/migrations,
|
|
273
|
+
run npx easy-cms migrate, then NODE_ENV=production ${pm === "npm" ? "npm start" : `${pm} start`}.`);
|
|
274
|
+
return 0;
|
|
275
|
+
}
|
|
247
276
|
io.out(`
|
|
248
277
|
Done. Next:
|
|
249
278
|
1. ${devCommand}
|
|
250
279
|
2. Open /admin and create the first admin (or run: npx easy-cms create-admin)
|
|
251
280
|
3. Before deploying: npx easy-cms migrate:create init, commit easy-cms/migrations,
|
|
252
|
-
and run npx easy-cms migrate where you deploy
|
|
281
|
+
and run npx easy-cms migrate where you deploy.
|
|
282
|
+
4. Set EASY_CMS_SECRET in the production environment.${target === "nuxt" ? `
|
|
283
|
+
Nuxt's production server does not read .env: set it on the host, or start with
|
|
284
|
+
node --env-file=.env .output/server/index.mjs` : ""}`);
|
|
253
285
|
return 0;
|
|
254
286
|
}
|
|
287
|
+
var STANDALONE_SCRIPTS = {
|
|
288
|
+
dev: "easy-cms serve --watch",
|
|
289
|
+
start: "easy-cms serve",
|
|
290
|
+
migrate: "easy-cms migrate",
|
|
291
|
+
"migrate:create": "easy-cms migrate:create"
|
|
292
|
+
};
|
|
293
|
+
async function setupStandalone(dir, changes) {
|
|
294
|
+
const file = join(dir, "package.json");
|
|
295
|
+
const existing = existsSync(file);
|
|
296
|
+
const pkg = existing ? JSON.parse(await readFile(file, "utf8")) : {
|
|
297
|
+
name: basename(dir).toLowerCase().replace(/[^a-z0-9-]+/g, "-"),
|
|
298
|
+
private: true,
|
|
299
|
+
type: "module"
|
|
300
|
+
};
|
|
301
|
+
const scripts = { ...pkg.scripts };
|
|
302
|
+
const added = Object.entries(STANDALONE_SCRIPTS).filter(([name]) => !(name in scripts));
|
|
303
|
+
if (existing && added.length === 0) return;
|
|
304
|
+
for (const [name, command] of added) scripts[name] = command;
|
|
305
|
+
await writeFile(file, `${JSON.stringify({ ...pkg, scripts }, null, 2)}
|
|
306
|
+
`);
|
|
307
|
+
changes.push(
|
|
308
|
+
existing ? `added ${added.map(([name]) => name).join(", ")} scripts to package.json` : "created package.json"
|
|
309
|
+
);
|
|
310
|
+
}
|
|
255
311
|
async function setupNuxt(dir, changes, notes) {
|
|
256
312
|
const file = ["nuxt.config.ts", "nuxt.config.js", "nuxt.config.mjs"].map((f) => join(dir, f)).find(existsSync);
|
|
257
313
|
const manual = "add '@easy-cms/nuxt' to `modules` in nuxt.config";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
type DatabaseChoice = 'sqlite' | 'postgres';
|
|
2
2
|
|
|
3
3
|
type Framework = 'nuxt' | 'next';
|
|
4
|
+
/** Where Easy CMS runs: inside a Nuxt or Next.js app, or as its own server. */
|
|
5
|
+
type Target = Framework | 'standalone';
|
|
4
6
|
type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun';
|
|
5
7
|
interface IO {
|
|
6
8
|
readonly out: (line: string) => void;
|
|
@@ -14,11 +16,11 @@ interface IO {
|
|
|
14
16
|
declare function detectFramework(pkg: Record<string, unknown>): Framework | undefined;
|
|
15
17
|
/** Detects the package manager from the lockfile, falling back to the one running us. */
|
|
16
18
|
declare function detectPackageManager(dir: string): PackageManager;
|
|
17
|
-
declare function packagesFor(
|
|
19
|
+
declare function packagesFor(target: Target, db: DatabaseChoice): {
|
|
18
20
|
deps: string[];
|
|
19
21
|
devDeps: string[];
|
|
20
22
|
};
|
|
21
23
|
/** Runs create-easy-cms and returns the exit code. */
|
|
22
24
|
declare function run(argv: readonly string[], io?: IO): Promise<number>;
|
|
23
25
|
|
|
24
|
-
export { type Framework, type IO, type PackageManager, detectFramework, detectPackageManager, packagesFor, run };
|
|
26
|
+
export { type Framework, type IO, type PackageManager, type Target, detectFramework, detectPackageManager, packagesFor, run };
|
package/dist/index.js
CHANGED