@kizunacms/cli 0.0.0-stage → 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.MD +9 -0
- package/README.md +19 -2
- package/dist/cli.mjs +108 -0
- package/package.json +52 -3
package/LICENSE.MD
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sondre Ørland
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kizunacms/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `kizunacms` command: write and run the database migrations for a Kizuna CMS config.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/kizunajs/kizuna)
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
pnpm add -D @kizunacms/cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Write a migration from what changed in the config since the last one, then run the ones the database hasn't run yet:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
pnpm kizunacms migrate new add-summary
|
|
15
|
+
pnpm kizunacms migrate
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
It reads `kizuna.cms.config.ts` and keeps the migrations in a `migrations` folder beside it. Pass `--config` or `--dir` to use other paths.
|
|
19
|
+
|
|
20
|
+
[Documentation](https://kizunajs.com/docs/cms)
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { loadConfig } from "@kizunajs/cli";
|
|
6
|
+
//#region src/migrations.ts
|
|
7
|
+
/**
|
|
8
|
+
* The file beside the migrations that holds the tables the last one left, which the next one is written against.
|
|
9
|
+
*/
|
|
10
|
+
const SNAPSHOT_FILE = "snapshot.json";
|
|
11
|
+
/**
|
|
12
|
+
* A migration's name: lowercase words joined by hyphens.
|
|
13
|
+
*/
|
|
14
|
+
const MIGRATION_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the time as `20261125143000` in UTC, so migrations sort in the order they were written.
|
|
17
|
+
*/
|
|
18
|
+
const stampOf = (time) => time.toISOString().replace(/[-:T]/g, "").slice(0, 14);
|
|
19
|
+
const readSnapshot = (directory) => {
|
|
20
|
+
const file = join(directory, SNAPSHOT_FILE);
|
|
21
|
+
return existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : void 0;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Returns the migrations in a folder, in the order they run.
|
|
25
|
+
*/
|
|
26
|
+
const readMigrations = (directory) => existsSync(directory) ? readdirSync(directory).filter((file) => file.endsWith(".sql")).sort().map((file) => ({
|
|
27
|
+
name: file.slice(0, -4),
|
|
28
|
+
sql: readFileSync(join(directory, file), "utf8")
|
|
29
|
+
})) : [];
|
|
30
|
+
/**
|
|
31
|
+
* Writes the SQL that takes the database from the last migration to the config, and returns the file it wrote, or
|
|
32
|
+
* `undefined` when the config has not changed since.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const file = createMigration(db, 'migrations', 'add-summary');
|
|
36
|
+
*/
|
|
37
|
+
const createMigration = (db, directory, name, time = /* @__PURE__ */ new Date()) => {
|
|
38
|
+
if (!MIGRATION_NAME.test(name)) throw new Error(`Name the migration in lowercase words joined by hyphens, like \`add-summary\`. '${name}' is not.`);
|
|
39
|
+
const { statements, snapshot } = db.migrations.create(readSnapshot(directory));
|
|
40
|
+
if (statements.length === 0) return void 0;
|
|
41
|
+
mkdirSync(directory, { recursive: true });
|
|
42
|
+
const file = join(directory, `${stampOf(time)}_${name}.sql`);
|
|
43
|
+
writeFileSync(file, `${statements.join("\n")}\n`);
|
|
44
|
+
writeFileSync(join(directory, SNAPSHOT_FILE), `${JSON.stringify(snapshot, null, 4)}\n`);
|
|
45
|
+
return file;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Runs the migrations in a folder that the database has not run yet, and returns their names.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const ran = await runMigrations(db, 'migrations');
|
|
52
|
+
*/
|
|
53
|
+
const runMigrations = (db, directory) => db.migrations.run(readMigrations(directory));
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/cli.ts
|
|
56
|
+
const usage = `Usage: kizunacms <command> [options]
|
|
57
|
+
|
|
58
|
+
Commands:
|
|
59
|
+
migrate Run the migrations the database has not run yet.
|
|
60
|
+
migrate new <name> Write a migration from what changed in the config since
|
|
61
|
+
the last one, like \`kizunacms migrate new add-summary\`.
|
|
62
|
+
|
|
63
|
+
Options:
|
|
64
|
+
--config <path> Path to the config. Default: kizuna.cms.config.ts
|
|
65
|
+
--dir <path> The migrations folder. Default: migrations, beside the config.
|
|
66
|
+
`;
|
|
67
|
+
const die = (message, code = 1) => {
|
|
68
|
+
process.stderr.write(`${message}\n`);
|
|
69
|
+
process.exit(code);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A path as the person at the terminal knows it: relative to where they ran the command, unless that climbs out.
|
|
73
|
+
*/
|
|
74
|
+
const displayPath = (file) => {
|
|
75
|
+
const shown = relative(process.cwd(), file);
|
|
76
|
+
return shown.startsWith("..") ? file : shown;
|
|
77
|
+
};
|
|
78
|
+
const { values, positionals } = parseArgs({
|
|
79
|
+
allowPositionals: true,
|
|
80
|
+
options: {
|
|
81
|
+
config: {
|
|
82
|
+
type: "string",
|
|
83
|
+
default: "kizuna.cms.config.ts"
|
|
84
|
+
},
|
|
85
|
+
dir: { type: "string" },
|
|
86
|
+
help: { type: "boolean" }
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const [command, subcommand, name] = positionals;
|
|
90
|
+
if (values.help === true || command !== "migrate") die(usage, values.help === true ? 0 : 1);
|
|
91
|
+
if (subcommand !== void 0 && subcommand !== "new") die(`Unknown command \`migrate ${subcommand}\`.\n\n${usage}`);
|
|
92
|
+
if (subcommand === "new" && name === void 0) die("Name the migration, like `kizunacms migrate new add-summary`.");
|
|
93
|
+
const configPath = resolve(values.config);
|
|
94
|
+
if (!existsSync(configPath)) die(`No config at ${displayPath(configPath)}. Pass its path with \`--config\`.`);
|
|
95
|
+
const directory = values.dir === void 0 ? resolve(dirname(configPath), "migrations") : resolve(values.dir);
|
|
96
|
+
const [loaded] = await loadConfig(configPath);
|
|
97
|
+
const db = (loaded?.config["cms"])?.db;
|
|
98
|
+
if (db?.migrations === void 0) die(`The config at ${displayPath(configPath)} has no database that keeps migrations. Pass \`db: postgresAdapter(...)\` to \`defineConfig\`.`);
|
|
99
|
+
if (subcommand === "new") {
|
|
100
|
+
const file = createMigration(db, directory, name);
|
|
101
|
+
process.stdout.write(file === void 0 ? "Nothing changed since the last migration.\n" : `Wrote ${displayPath(file)}\n`);
|
|
102
|
+
} else {
|
|
103
|
+
const ran = await runMigrations(db, directory);
|
|
104
|
+
process.stdout.write(ran.length === 0 ? "Nothing to run.\n" : ran.map((each) => `Ran ${each}\n`).join(""));
|
|
105
|
+
}
|
|
106
|
+
process.exit(0);
|
|
107
|
+
//#endregion
|
|
108
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunacms/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The kizunacms command: write and run the database migrations for a Kizuna CMS config",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"kizunacms",
|
|
7
|
+
"kizunajs",
|
|
8
|
+
"cms",
|
|
9
|
+
"headless-cms",
|
|
10
|
+
"typescript",
|
|
11
|
+
"cli",
|
|
12
|
+
"migrations",
|
|
13
|
+
"postgres"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://kizunajs.com/docs/cms",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/kizunajs/cms.git",
|
|
20
|
+
"directory": "packages/cli"
|
|
21
|
+
},
|
|
22
|
+
"bugs": "https://github.com/kizunajs/cms/issues",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=24"
|
|
27
|
+
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"kizunacms": "./dist/cli.mjs"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"kizunacms": "0.1.0"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@kizunajs/cli": "^2.0.0-beta.41"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@electric-sql/pglite": "^0.5.16",
|
|
45
|
+
"@types/node": "^24.19.2",
|
|
46
|
+
"drizzle-orm": "^0.45.4",
|
|
47
|
+
"kizunacms": "0.1.0",
|
|
48
|
+
"tsdown": "^0.21.10",
|
|
49
|
+
"typescript": "^6.0.3"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsdown src/cli.ts --format esm --clean --external kizunacms --external @kizunajs/cli",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
}
|
|
6
55
|
}
|