@mxpicture/gcp-functions-fs 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/config/ConfigJson.d.ts +17 -0
- package/dist/config/ConfigJson.js +47 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/config/index.js +2 -0
- package/dist/history/HistoryJson.d.ts +36 -0
- package/dist/history/HistoryJson.js +101 -0
- package/dist/history/index.d.ts +1 -0
- package/dist/history/index.js +2 -0
- package/package.json +43 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export type ConfigContent = Record<string, unknown>;
|
|
3
|
+
export declare class ConfigJson<T extends ConfigContent> {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly shape: z.ZodRawShape;
|
|
6
|
+
readonly configDir: string;
|
|
7
|
+
protected _schema: z.ZodObject | null;
|
|
8
|
+
protected _content: T | null;
|
|
9
|
+
constructor(path: string, shape: z.ZodRawShape);
|
|
10
|
+
get content(): T;
|
|
11
|
+
set content(data: T);
|
|
12
|
+
read(): Promise<void>;
|
|
13
|
+
write(): Promise<void>;
|
|
14
|
+
protected schema(): z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>;
|
|
15
|
+
protected validate(content: Partial<T>): Promise<T>;
|
|
16
|
+
protected validateSync(content: Partial<T>): T;
|
|
17
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import pkg from "json5";
|
|
5
|
+
import { formatJson } from "@mxpicture/gcp-functions-code/common";
|
|
6
|
+
export class ConfigJson {
|
|
7
|
+
path;
|
|
8
|
+
shape;
|
|
9
|
+
configDir;
|
|
10
|
+
_schema = null;
|
|
11
|
+
_content = null;
|
|
12
|
+
constructor(path, shape) {
|
|
13
|
+
this.path = path;
|
|
14
|
+
this.shape = shape;
|
|
15
|
+
this.configDir = dirname(path);
|
|
16
|
+
}
|
|
17
|
+
get content() {
|
|
18
|
+
if (!this._content)
|
|
19
|
+
throw new Error("Content not available");
|
|
20
|
+
return this._content;
|
|
21
|
+
}
|
|
22
|
+
set content(data) {
|
|
23
|
+
this._content = this.validateSync(data);
|
|
24
|
+
}
|
|
25
|
+
async read() {
|
|
26
|
+
this._content = await this.validate(pkg.parse(await readFile(this.path, "utf8")));
|
|
27
|
+
}
|
|
28
|
+
async write() {
|
|
29
|
+
const [, content] = await Promise.all([
|
|
30
|
+
mkdir(this.configDir, { recursive: true }),
|
|
31
|
+
this.validate(this.content),
|
|
32
|
+
]);
|
|
33
|
+
this._content = content;
|
|
34
|
+
return writeFile(this.path, await formatJson(pkg.stringify(this._content)));
|
|
35
|
+
}
|
|
36
|
+
schema() {
|
|
37
|
+
if (!this._schema)
|
|
38
|
+
this._schema = z.object(this.shape);
|
|
39
|
+
return this._schema;
|
|
40
|
+
}
|
|
41
|
+
async validate(content) {
|
|
42
|
+
return this.schema().parseAsync(content);
|
|
43
|
+
}
|
|
44
|
+
validateSync(content) {
|
|
45
|
+
return this.schema().parse(content);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ConfigJson.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export type HistoryItem = Record<string, unknown>;
|
|
3
|
+
export interface HistoryRawItem<T extends HistoryItem> {
|
|
4
|
+
timestamp: string;
|
|
5
|
+
timestampRaw: number;
|
|
6
|
+
data: T;
|
|
7
|
+
}
|
|
8
|
+
export declare class HistoryJson<T extends HistoryItem> {
|
|
9
|
+
readonly path: string;
|
|
10
|
+
readonly itemShape: z.ZodRawShape;
|
|
11
|
+
readonly historyDir: string;
|
|
12
|
+
protected _raws: HistoryRawItem<T>[];
|
|
13
|
+
protected _itemSchema: z.ZodObject | null;
|
|
14
|
+
protected _rawItemSchema: z.ZodObject | null;
|
|
15
|
+
protected _fileSchema: z.ZodArray | null;
|
|
16
|
+
constructor(path: string, itemShape: z.ZodRawShape);
|
|
17
|
+
get items(): T[];
|
|
18
|
+
get raws(): HistoryRawItem<T>[];
|
|
19
|
+
addItem(item: T): void;
|
|
20
|
+
addItems(items: T[]): void;
|
|
21
|
+
addRaw(raw: HistoryRawItem<T>): void;
|
|
22
|
+
addRaws(raws: HistoryRawItem<T>[]): void;
|
|
23
|
+
read(): Promise<void>;
|
|
24
|
+
write(): Promise<void>;
|
|
25
|
+
protected itemSchema(): z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>;
|
|
26
|
+
protected rawItemSchema(): z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>;
|
|
27
|
+
protected fileSchema(): z.ZodArray<z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
|
|
28
|
+
protected validateItem(item: Partial<T>): Promise<T>;
|
|
29
|
+
protected validateRawItem(raw: Partial<HistoryRawItem<T>>): Promise<HistoryRawItem<T>>;
|
|
30
|
+
protected validateFile(raws: Partial<HistoryRawItem<T>>[]): Promise<HistoryRawItem<T>[]>;
|
|
31
|
+
protected sort(): void;
|
|
32
|
+
protected toItem(raw: HistoryRawItem<T>): T;
|
|
33
|
+
protected toItems(raws: HistoryRawItem<T>[]): T[];
|
|
34
|
+
protected toRaw(item: T): HistoryRawItem<T>;
|
|
35
|
+
protected toRaws(items: T[]): HistoryRawItem<T>[];
|
|
36
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import pkg from "json5";
|
|
5
|
+
import { formatJson } from "@mxpicture/gcp-functions-code/common";
|
|
6
|
+
export class HistoryJson {
|
|
7
|
+
path;
|
|
8
|
+
itemShape;
|
|
9
|
+
historyDir;
|
|
10
|
+
_raws = [];
|
|
11
|
+
_itemSchema = null;
|
|
12
|
+
_rawItemSchema = null;
|
|
13
|
+
_fileSchema = null;
|
|
14
|
+
constructor(path, itemShape) {
|
|
15
|
+
this.path = path;
|
|
16
|
+
this.itemShape = itemShape;
|
|
17
|
+
this.historyDir = dirname(path);
|
|
18
|
+
}
|
|
19
|
+
get items() {
|
|
20
|
+
return this._raws.map((raw) => this.toItem(raw));
|
|
21
|
+
}
|
|
22
|
+
get raws() {
|
|
23
|
+
return [...this._raws];
|
|
24
|
+
}
|
|
25
|
+
addItem(item) {
|
|
26
|
+
this.addRaw(this.toRaw(item));
|
|
27
|
+
}
|
|
28
|
+
addItems(items) {
|
|
29
|
+
this.addRaws(this.toRaws(items));
|
|
30
|
+
}
|
|
31
|
+
addRaw(raw) {
|
|
32
|
+
this._raws.unshift(raw);
|
|
33
|
+
this.sort();
|
|
34
|
+
}
|
|
35
|
+
addRaws(raws) {
|
|
36
|
+
this._raws.unshift(...raws);
|
|
37
|
+
this.sort();
|
|
38
|
+
}
|
|
39
|
+
async read() {
|
|
40
|
+
const items = pkg.parse(await readFile(this.path, "utf8"));
|
|
41
|
+
this._raws = await this.validateFile(items);
|
|
42
|
+
this.sort();
|
|
43
|
+
}
|
|
44
|
+
async write() {
|
|
45
|
+
const [, raws] = await Promise.all([
|
|
46
|
+
mkdir(this.historyDir, { recursive: true }),
|
|
47
|
+
this.validateFile(this._raws),
|
|
48
|
+
]);
|
|
49
|
+
this._raws = raws;
|
|
50
|
+
this.sort();
|
|
51
|
+
return writeFile(this.path, await formatJson(pkg.stringify(raws)));
|
|
52
|
+
}
|
|
53
|
+
itemSchema() {
|
|
54
|
+
if (!this._itemSchema)
|
|
55
|
+
this._itemSchema = z.object(this.itemShape);
|
|
56
|
+
return this._itemSchema;
|
|
57
|
+
}
|
|
58
|
+
rawItemSchema() {
|
|
59
|
+
if (!this._rawItemSchema)
|
|
60
|
+
this._rawItemSchema = z.object({
|
|
61
|
+
timestamp: z.string(),
|
|
62
|
+
timestampRaw: z.number(),
|
|
63
|
+
data: this.itemSchema(),
|
|
64
|
+
});
|
|
65
|
+
return this._rawItemSchema;
|
|
66
|
+
}
|
|
67
|
+
fileSchema() {
|
|
68
|
+
if (!this._fileSchema)
|
|
69
|
+
this._fileSchema = z.array(this.rawItemSchema());
|
|
70
|
+
return this._fileSchema;
|
|
71
|
+
}
|
|
72
|
+
async validateItem(item) {
|
|
73
|
+
return this.itemSchema().parseAsync(item);
|
|
74
|
+
}
|
|
75
|
+
async validateRawItem(raw) {
|
|
76
|
+
return this.rawItemSchema().parseAsync(raw);
|
|
77
|
+
}
|
|
78
|
+
async validateFile(raws) {
|
|
79
|
+
return this.fileSchema().parseAsync(raws);
|
|
80
|
+
}
|
|
81
|
+
sort() {
|
|
82
|
+
this._raws.sort((a, b) => b.timestampRaw - a.timestampRaw);
|
|
83
|
+
}
|
|
84
|
+
toItem(raw) {
|
|
85
|
+
return raw.data;
|
|
86
|
+
}
|
|
87
|
+
toItems(raws) {
|
|
88
|
+
return raws.map((raw) => raw.data);
|
|
89
|
+
}
|
|
90
|
+
toRaw(item) {
|
|
91
|
+
return this.toRaws([item])[0];
|
|
92
|
+
}
|
|
93
|
+
toRaws(items) {
|
|
94
|
+
const now = new Date();
|
|
95
|
+
return items.map((item) => ({
|
|
96
|
+
timestamp: now.toISOString(),
|
|
97
|
+
timestampRaw: now.getTime(),
|
|
98
|
+
data: item,
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./HistoryJson.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mxpicture/gcp-functions-fs",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Tools for google cloud functions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "MXPicture",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/MXPicture/npm-gcp-functions.git"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
"./history": "./dist/history/index.js",
|
|
14
|
+
"./config": "./dist/config/index.js",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "rm -rf dist .tsbuildinfo tsconfig.tsbuildinfo node_modules",
|
|
25
|
+
"lint": "eslint \"src/**/*.{ts,tsx}\" --ext .ts,.tsx",
|
|
26
|
+
"build": "tsc -b .",
|
|
27
|
+
"test:clean": "rm -rf src/4testing/generator-test-*",
|
|
28
|
+
"run:test": "pnpm exec tsx src/scripts/test.mts"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@mxpicture/gcp-functions-code": "^0.2.0",
|
|
35
|
+
"json5": "^1.0.2",
|
|
36
|
+
"zod": "^4.3.6"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.2.0",
|
|
40
|
+
"eslint": "^9.39.2",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
}
|
|
43
|
+
}
|