easy-cms 0.1.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/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/bin.js +7 -0
- package/dist/chunk-3JYUADKB.js +220 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +6 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# easy-cms
|
|
2
|
+
|
|
3
|
+
Command line tool for [Easy CMS](https://github.com/maritonx/easy-crm). Install it as a dev
|
|
4
|
+
dependency; every command loads `.env` from the project root.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npx easy-cms migrate:create <name> # write a migration for config changes
|
|
8
|
+
npx easy-cms migrate # apply pending migrations
|
|
9
|
+
npx easy-cms migrate:status
|
|
10
|
+
npx easy-cms generate:types [--out file.ts]
|
|
11
|
+
npx easy-cms create-admin [--email you@example.com] [--name] [--role]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Options: `--config <file>`, `--cwd <dir>`, `--help`.
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { writeFile } from "fs/promises";
|
|
4
|
+
import { isAbsolute, join, resolve } from "path";
|
|
5
|
+
import { createInterface } from "readline/promises";
|
|
6
|
+
import { parseArgs } from "util";
|
|
7
|
+
import {
|
|
8
|
+
ConfigError,
|
|
9
|
+
createEasyCMS,
|
|
10
|
+
EasyCMSError,
|
|
11
|
+
generateTypes,
|
|
12
|
+
loadConfig,
|
|
13
|
+
ValidationError
|
|
14
|
+
} from "@easy-cms/core";
|
|
15
|
+
async function ask(question, options = {}) {
|
|
16
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
17
|
+
if (options.hidden) {
|
|
18
|
+
const output = rl;
|
|
19
|
+
let asked = false;
|
|
20
|
+
output._writeToOutput = (text) => {
|
|
21
|
+
if (!asked) {
|
|
22
|
+
process.stdout.write(text);
|
|
23
|
+
asked = true;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
return (await rl.question(question)).trim();
|
|
29
|
+
} finally {
|
|
30
|
+
if (options.hidden) process.stdout.write("\n");
|
|
31
|
+
rl.close();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
var defaultIO = {
|
|
35
|
+
out: (line) => console.log(line),
|
|
36
|
+
err: (line) => console.error(line),
|
|
37
|
+
interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
|
|
38
|
+
prompt: ask
|
|
39
|
+
};
|
|
40
|
+
var HELP = `Usage: easy-cms <command> [options]
|
|
41
|
+
|
|
42
|
+
Commands:
|
|
43
|
+
migrate Apply pending migrations
|
|
44
|
+
migrate:create <name> Create a migration from config changes
|
|
45
|
+
migrate:status List migrations and whether they are applied
|
|
46
|
+
generate:types Write TypeScript types for your collections and globals
|
|
47
|
+
create-admin Create an admin user
|
|
48
|
+
|
|
49
|
+
Options:
|
|
50
|
+
--config <file> Config file (default: easy-cms.config.ts)
|
|
51
|
+
--cwd <dir> Project root (default: current directory); its .env is loaded
|
|
52
|
+
-h, --help Show help
|
|
53
|
+
`;
|
|
54
|
+
var COMMAND_HELP = {
|
|
55
|
+
migrate: `Usage: easy-cms migrate [options]
|
|
56
|
+
|
|
57
|
+
Applies every migration in easy-cms/migrations that the database has not run yet.
|
|
58
|
+
Each migration runs in its own transaction; a failure rolls it back and stops.
|
|
59
|
+
`,
|
|
60
|
+
"migrate:create": `Usage: easy-cms migrate:create <name> [options]
|
|
61
|
+
|
|
62
|
+
Compares the config with the latest migration and writes a new migration
|
|
63
|
+
(easy-cms/migrations/<timestamp>_<name>.sql) if anything changed.
|
|
64
|
+
In a terminal you are asked whether changed fields were renamed.
|
|
65
|
+
`,
|
|
66
|
+
"migrate:status": `Usage: easy-cms migrate:status [options]
|
|
67
|
+
|
|
68
|
+
Lists migration files and whether each has been applied.
|
|
69
|
+
`,
|
|
70
|
+
"generate:types": `Usage: easy-cms generate:types [--out <file>] [options]
|
|
71
|
+
|
|
72
|
+
Writes interfaces for every collection and global (default: easy-cms-types.ts).
|
|
73
|
+
The file has no imports, so a frontend in another repository can copy it.
|
|
74
|
+
`,
|
|
75
|
+
"create-admin": `Usage: easy-cms create-admin [--email <email>] [--name <name>] [--role <role>] [options]
|
|
76
|
+
|
|
77
|
+
Creates a user (role "admin" unless --role is given). The password is asked for in the
|
|
78
|
+
terminal, or read from EASY_CMS_ADMIN_PASSWORD when there is no terminal.
|
|
79
|
+
`
|
|
80
|
+
};
|
|
81
|
+
async function run(argv, io = defaultIO) {
|
|
82
|
+
let parsed;
|
|
83
|
+
try {
|
|
84
|
+
parsed = parse(argv);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
io.err(error.message);
|
|
87
|
+
io.err(HELP);
|
|
88
|
+
return 1;
|
|
89
|
+
}
|
|
90
|
+
const { values, positionals } = parsed;
|
|
91
|
+
const [command, ...rest] = positionals;
|
|
92
|
+
if (!command) {
|
|
93
|
+
;
|
|
94
|
+
(values.help ? io.out : io.err)(HELP);
|
|
95
|
+
return values.help ? 0 : 1;
|
|
96
|
+
}
|
|
97
|
+
if (!(command in COMMAND_HELP)) {
|
|
98
|
+
io.err(`Unknown command "${command}".
|
|
99
|
+
`);
|
|
100
|
+
io.err(HELP);
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
if (values.help) {
|
|
104
|
+
io.out(COMMAND_HELP[command]);
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
const cwd = values.cwd ?? process.cwd();
|
|
108
|
+
loadDotEnv(cwd);
|
|
109
|
+
const logger = { info: io.out, warn: (m) => io.err(`warning: ${m}`), error: io.err };
|
|
110
|
+
try {
|
|
111
|
+
const config = await loadConfig({
|
|
112
|
+
cwd,
|
|
113
|
+
...values.config ? { configFile: values.config } : {}
|
|
114
|
+
});
|
|
115
|
+
if (command === "generate:types") {
|
|
116
|
+
const out = values.out ?? "easy-cms-types.ts";
|
|
117
|
+
const file = isAbsolute(out) ? out : resolve(cwd, out);
|
|
118
|
+
await writeFile(file, generateTypes(config));
|
|
119
|
+
io.out(`Wrote ${file}`);
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
const schema = command === "create-admin" && process.env.NODE_ENV !== "production" ? "push" : "skip";
|
|
123
|
+
const cms = await createEasyCMS(config, { cwd, schema, logger, interactive: io.interactive });
|
|
124
|
+
try {
|
|
125
|
+
switch (command) {
|
|
126
|
+
case "migrate": {
|
|
127
|
+
const applied = await cms.db.migrate();
|
|
128
|
+
io.out(
|
|
129
|
+
applied.length === 0 ? "No pending migrations." : `Applied ${applied.length} migration(s).`
|
|
130
|
+
);
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
case "migrate:create": {
|
|
134
|
+
const name = rest.join(" ").trim();
|
|
135
|
+
if (!name) {
|
|
136
|
+
io.err("Missing migration name.\n");
|
|
137
|
+
io.err(COMMAND_HELP["migrate:create"]);
|
|
138
|
+
return 1;
|
|
139
|
+
}
|
|
140
|
+
const created = await cms.db.createMigration({ name });
|
|
141
|
+
if (!created) {
|
|
142
|
+
io.out("No changes; nothing to migrate.");
|
|
143
|
+
} else {
|
|
144
|
+
io.out(`Created ${created.file} (${created.statements.length} statements).`);
|
|
145
|
+
io.out("Review it, commit it, then run `easy-cms migrate` where you deploy.");
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
case "create-admin": {
|
|
150
|
+
const email = values.email ?? (io.interactive && io.prompt ? await io.prompt("Email: ") : "");
|
|
151
|
+
if (!email) {
|
|
152
|
+
io.err("Missing --email.\n");
|
|
153
|
+
io.err(COMMAND_HELP["create-admin"]);
|
|
154
|
+
return 1;
|
|
155
|
+
}
|
|
156
|
+
const password = process.env.EASY_CMS_ADMIN_PASSWORD ?? (io.interactive && io.prompt ? await io.prompt("Password (8+ characters): ", { hidden: true }) : "");
|
|
157
|
+
if (!password) {
|
|
158
|
+
io.err("Missing password: run in a terminal or set EASY_CMS_ADMIN_PASSWORD.");
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const user = await cms.create("users", {
|
|
163
|
+
email,
|
|
164
|
+
password,
|
|
165
|
+
role: values.role ?? "admin",
|
|
166
|
+
...values.name ? { name: values.name } : {}
|
|
167
|
+
});
|
|
168
|
+
io.out(`Created ${user.role} ${user.email}. Log in at ${cms.config.admin.path}`);
|
|
169
|
+
return 0;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error instanceof ValidationError) {
|
|
172
|
+
for (const e of error.errors) io.err(`${e.field}: ${e.message}`);
|
|
173
|
+
return 1;
|
|
174
|
+
}
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
case "migrate:status": {
|
|
179
|
+
const list = await cms.db.migrationStatus();
|
|
180
|
+
if (list.length === 0)
|
|
181
|
+
io.out("No migrations yet. Create one with `easy-cms migrate:create init`.");
|
|
182
|
+
for (const m of list) io.out(`${m.applied ? "\u2713 applied" : "\u2022 pending"} ${m.name}`);
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return 1;
|
|
187
|
+
} finally {
|
|
188
|
+
await cms.destroy();
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
const known = error instanceof ConfigError || error instanceof EasyCMSError;
|
|
192
|
+
if (error instanceof Error)
|
|
193
|
+
io.err(!known && process.env.DEBUG ? error.stack ?? error.message : error.message);
|
|
194
|
+
else io.err(String(error));
|
|
195
|
+
return 1;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function parse(argv) {
|
|
199
|
+
return parseArgs({
|
|
200
|
+
args: [...argv],
|
|
201
|
+
allowPositionals: true,
|
|
202
|
+
options: {
|
|
203
|
+
config: { type: "string" },
|
|
204
|
+
cwd: { type: "string" },
|
|
205
|
+
out: { type: "string" },
|
|
206
|
+
email: { type: "string" },
|
|
207
|
+
name: { type: "string" },
|
|
208
|
+
role: { type: "string" },
|
|
209
|
+
help: { type: "boolean", short: "h" }
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function loadDotEnv(cwd) {
|
|
214
|
+
const file = join(cwd, ".env");
|
|
215
|
+
if (existsSync(file)) process.loadEnvFile(file);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export {
|
|
219
|
+
run
|
|
220
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface IO {
|
|
2
|
+
readonly out: (line: string) => void;
|
|
3
|
+
readonly err: (line: string) => void;
|
|
4
|
+
readonly interactive: boolean;
|
|
5
|
+
/** Asks a question in the terminal. `hidden` does not echo what is typed (passwords). */
|
|
6
|
+
readonly prompt?: (question: string, options?: {
|
|
7
|
+
hidden?: boolean;
|
|
8
|
+
}) => Promise<string>;
|
|
9
|
+
}
|
|
10
|
+
/** Runs the CLI and returns the exit code. */
|
|
11
|
+
declare function run(argv: readonly string[], io?: IO): Promise<number>;
|
|
12
|
+
|
|
13
|
+
export { type IO, run };
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "easy-cms",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Command line tool for Easy CMS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"easy-cms": "./dist/bin.js"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.12"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@easy-cms/core": "^0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@easy-cms/db-sqlite": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/maritonx/easy-crm.git",
|
|
34
|
+
"directory": "packages/cli"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"easy-cms",
|
|
38
|
+
"cms",
|
|
39
|
+
"cli"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"test": "vitest run"
|
|
45
|
+
}
|
|
46
|
+
}
|