@zinn-dev/core 0.0.1 → 0.0.4
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/README.md +3 -0
- package/index.ts +30 -2
- package/package.json +9 -2
- package/src/config.ts +14 -0
- package/src/constant.ts +18 -0
- package/src/db.ts +34 -0
package/README.md
ADDED
package/index.ts
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// TODO: do not initialize before the first "valid" command
|
|
2
|
+
import "./src/config";
|
|
3
|
+
import { db, addProject, getProjectByKey, deleteProjectByKey } from "./src/db";
|
|
4
|
+
|
|
5
|
+
function standardizeKey(key: string) {
|
|
6
|
+
// TODO: maybe force latin characters only to prevent unexpected stuff from charaters like Ğ, İ, etc.
|
|
7
|
+
return key.toUpperCase();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// TODO: switch to object param
|
|
11
|
+
export function createProject(key: string, name: string) {
|
|
12
|
+
const standardizedKey = standardizeKey(key);
|
|
13
|
+
const project = getProjectByKey(standardizedKey);
|
|
14
|
+
|
|
15
|
+
if (project != null) {
|
|
16
|
+
throw new Error(`Project with key "${standardizedKey}" already exists!`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
addProject(standardizedKey, name);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function deleteProject(key: string) {
|
|
23
|
+
const standardizedKey = standardizeKey(key);
|
|
24
|
+
const project = getProjectByKey(standardizedKey);
|
|
25
|
+
|
|
26
|
+
if (project == null) {
|
|
27
|
+
throw new Error(`Project with key "${standardizedKey}" does not exist!`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
deleteProjectByKey(standardizedKey);
|
|
3
31
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zinn-dev/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Shared logic for zinn, a kanban workflow in the terminal.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/yethranayeh/zinn.git",
|
|
8
|
+
"directory": "packages/core"
|
|
9
|
+
},
|
|
4
10
|
"private": false,
|
|
5
11
|
"type": "module",
|
|
6
12
|
"module": "index.ts",
|
|
@@ -11,7 +17,8 @@
|
|
|
11
17
|
}
|
|
12
18
|
},
|
|
13
19
|
"files": [
|
|
14
|
-
"index.ts"
|
|
20
|
+
"index.ts",
|
|
21
|
+
"src"
|
|
15
22
|
],
|
|
16
23
|
"publishConfig": {
|
|
17
24
|
"access": "public"
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { DATA_DIR, MAIN_DIR, CONFIG_PATH, defaultConfig } from "./constant";
|
|
3
|
+
|
|
4
|
+
if (!existsSync(MAIN_DIR)) {
|
|
5
|
+
mkdirSync(MAIN_DIR);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (!existsSync(DATA_DIR)) {
|
|
9
|
+
mkdirSync(DATA_DIR);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
13
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(defaultConfig) + "\n", { encoding: "utf-8" });
|
|
14
|
+
}
|
package/src/constant.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export const MAIN_FOLDER_NAME = ".zinn";
|
|
5
|
+
export const CONFIG_FILE_NAME = "settings.json";
|
|
6
|
+
export const DB_FILE_NAME = "zinn.sqlite";
|
|
7
|
+
|
|
8
|
+
export const MAIN_DIR = join(homedir(), MAIN_FOLDER_NAME);
|
|
9
|
+
export const CONFIG_PATH = join(MAIN_DIR, CONFIG_FILE_NAME);
|
|
10
|
+
export const DATA_DIR = join(MAIN_DIR, "data");
|
|
11
|
+
// TODO: environment variable based path overriding for tests
|
|
12
|
+
export const DB_PATH = join(DATA_DIR, DB_FILE_NAME);
|
|
13
|
+
export const DB_TABLE = {
|
|
14
|
+
project: "project",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// TODO: implement zod schema for setting.json
|
|
18
|
+
export const defaultConfig = {};
|
package/src/db.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { DB_PATH, DB_TABLE } from "./constant";
|
|
3
|
+
import { randomUUIDv7 } from "bun";
|
|
4
|
+
|
|
5
|
+
export const db = new Database(DB_PATH, { create: true });
|
|
6
|
+
db.run("PRAGMA journal_mode = WAL;");
|
|
7
|
+
// TODO: https://bun.com/docs/runtime/sqlite#wal-sidecar-file-cleanup macOS does not auto cleanup
|
|
8
|
+
|
|
9
|
+
const projectTableSetup = db.query(`CREATE TABLE IF NOT EXISTS ${DB_TABLE.project} (
|
|
10
|
+
id TEXT PRIMARY KEY,
|
|
11
|
+
key TEXT NOT NULL UNIQUE,
|
|
12
|
+
name TEXT NOT NULL,
|
|
13
|
+
created_at INTEGER NOT NULL,
|
|
14
|
+
updated_at INTEGER NOT NULL,
|
|
15
|
+
archived_at INTEGER);`);
|
|
16
|
+
projectTableSetup.run();
|
|
17
|
+
|
|
18
|
+
export function getProjectByKey(key: string) {
|
|
19
|
+
return db.query(`SELECT * FROM ${DB_TABLE.project} WHERE key = $key`).get({ $key: key });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// TODO: switch to object param
|
|
23
|
+
export function addProject(key: string, name: string) {
|
|
24
|
+
const query = db.query(`INSERT INTO
|
|
25
|
+
${DB_TABLE.project} (id, key, name, created_at, updated_at)
|
|
26
|
+
VALUES ($id, $key, $name, $created, $updated);`);
|
|
27
|
+
const time = Date.now();
|
|
28
|
+
|
|
29
|
+
return query.run({ $id: randomUUIDv7(), $key: key, $name: name, $created: time, $updated: time });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function deleteProjectByKey(key: string) {
|
|
33
|
+
return db.query(`DELETE FROM ${DB_TABLE.project} WHERE key = $key;`).run({ $key: key });
|
|
34
|
+
}
|