@dreamkit/node-app 0.0.2-next.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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Juanra GM <juanrgm724@gmail.com>
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,22 @@
1
+ # @dreamkit/node-app
2
+
3
+ > DreamKit application for Node.js.
4
+
5
+ [![workflow-badge]](https://github.com/swordev/dreamkit/actions/workflows/ci.yaml) [![npm-badge]](https://www.npmjs.com/package/@dreamkit/node-app)
6
+
7
+ [workflow-badge]: https://img.shields.io/github/actions/workflow/status/swordev/dreamkit/ci.yaml?branch=main
8
+ [npm-badge]: https://img.shields.io/npm/v/@dreamkit/node-app?label=@dreamkit/node-app
9
+
10
+ ## Installation
11
+
12
+ ```sh
13
+ npm install @dreamkit/node-app
14
+ ```
15
+
16
+ ## Documentation
17
+
18
+ https://dreamkit.dev
19
+
20
+ ## License
21
+
22
+ Distributed under the MIT License. See LICENSE for more information.
@@ -0,0 +1,37 @@
1
+ export declare class NodeSettingsHandlerOptions {
2
+ readonly data: {
3
+ path: string;
4
+ };
5
+ constructor(data: {
6
+ path: string;
7
+ });
8
+ }
9
+ declare const NodeSettingsHandler_base: import("@dreamkit/ioc").IocClass<{
10
+ options: import("@dreamkit/ioc/params.js").IocParamBuilder<{
11
+ value: typeof NodeSettingsHandlerOptions;
12
+ optional: true;
13
+ }>;
14
+ }, import("@dreamkit/app").SettingsHandler, never, import("@dreamkit/ioc").IocParams<{
15
+ options: import("@dreamkit/ioc/params.js").IocParamBuilder<{
16
+ value: typeof NodeSettingsHandlerOptions;
17
+ optional: true;
18
+ }>;
19
+ }>>;
20
+ export declare class NodeSettingsHandler extends NodeSettingsHandler_base {
21
+ protected onLoad(): Promise<{}>;
22
+ protected onSave(): Promise<{
23
+ "settings-data": {
24
+ path: string;
25
+ changed: boolean;
26
+ };
27
+ "settings-schema": {
28
+ path: string;
29
+ changed: boolean;
30
+ };
31
+ }>;
32
+ protected getSettingsPath(): string;
33
+ protected getSettingsSchemaPath(): string;
34
+ protected getSettingsSchema(): Record<string, any>;
35
+ }
36
+ export {};
37
+ //# sourceMappingURL=NodeSettingsHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NodeSettingsHandler.d.ts","sourceRoot":"","sources":["../src/NodeSettingsHandler.ts"],"names":[],"mappings":"AAOA,qBAAa,0BAA0B;IAKnC,QAAQ,CAAC,IAAI,EAAE;QACb,IAAI,EAAE,MAAM,CAAC;KACd;gBAFQ,IAAI,EAAE;QACb,IAAI,EAAE,MAAM,CAAC;KACd;CAEJ;;;;;;;;;;;;AAED,qBAAa,mBAAoB,SAAQ,wBAEvC;cAIgB,MAAM;cAIG,MAAM;;;;;;;;;;IA8B/B,SAAS,CAAC,eAAe;IAOzB,SAAS,CAAC,qBAAqB;IAG/B,SAAS,CAAC,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;CAoBnD"}
@@ -0,0 +1,68 @@
1
+ import { tryReadJsonFile, writeFileIfDifferent } from "./utils/fs.js";
2
+ import { SettingsHandlerClass } from "@dreamkit/app";
3
+ import { iocParam } from "@dreamkit/ioc";
4
+ import { kind } from "@dreamkit/kind";
5
+ import { basename } from "path";
6
+ export class NodeSettingsHandlerOptions {
7
+ data;
8
+ static {
9
+ kind(this, "@dreamkit/node-app/NodeSettingsHandlerOptions");
10
+ }
11
+ constructor(data) {
12
+ this.data = data;
13
+ }
14
+ }
15
+ export class NodeSettingsHandler extends SettingsHandlerClass({
16
+ options: iocParam(NodeSettingsHandlerOptions).optional(),
17
+ }) {
18
+ static {
19
+ kind(this, "@dreamkit/node-app/NodeSettingsHandler");
20
+ }
21
+ async onLoad() {
22
+ const settingsPath = this.getSettingsPath();
23
+ return (await tryReadJsonFile(settingsPath)) || {};
24
+ }
25
+ async onSave() {
26
+ const { prev, next } = await this.getData();
27
+ const schemaPath = this.getSettingsSchemaPath();
28
+ return {
29
+ "settings-data": {
30
+ path: this.getSettingsPath(),
31
+ changed: !!next &&
32
+ (await writeFileIfDifferent(this.getSettingsPath(), JSON.stringify({
33
+ $schema: `./${basename(schemaPath)}`,
34
+ ...next,
35
+ }, null, 2), prev ? JSON.stringify(prev, null, 2) : undefined)),
36
+ },
37
+ "settings-schema": {
38
+ path: schemaPath,
39
+ changed: await writeFileIfDifferent(schemaPath, JSON.stringify(this.getSettingsSchema(), null, 2)),
40
+ },
41
+ };
42
+ }
43
+ getSettingsPath() {
44
+ return (this.options?.data.path ??
45
+ process.env.DK_SETTINGS_PATH ??
46
+ "./settings.json");
47
+ }
48
+ getSettingsSchemaPath() {
49
+ return this.getSettingsPath().replace(/\.json$/, ".schema.json");
50
+ }
51
+ getSettingsSchema() {
52
+ const schema = {
53
+ type: "object",
54
+ additionalProperties: false,
55
+ required: [],
56
+ properties: {
57
+ $schema: { type: "string" },
58
+ },
59
+ };
60
+ for (const value of this.settings) {
61
+ const options = value.options;
62
+ if (!options.optional)
63
+ schema.required.push(options.name);
64
+ schema.properties[options.name] = options.params.toJsonSchema();
65
+ }
66
+ return schema;
67
+ }
68
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { IocClass } from "@dreamkit/ioc";
2
+ export { NodeSettingsHandler } from "./NodeSettingsHandler.js";
3
+ export { NodeSettingsHandlerOptions } from "./NodeSettingsHandler.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { IocClass } from "@dreamkit/ioc";
2
+ export { NodeSettingsHandler } from "./NodeSettingsHandler.js";
3
+ export { NodeSettingsHandlerOptions } from "./NodeSettingsHandler.js";
@@ -0,0 +1,5 @@
1
+ export declare function writeFileIfDifferent(path: string, content: string, prev?: string): Promise<boolean>;
2
+ export declare function tryReadFile(path: string): Promise<string | undefined>;
3
+ export declare function tryReadTextFile(path: string): Promise<string | undefined>;
4
+ export declare function tryReadJsonFile<T>(path: string): Promise<T | undefined>;
5
+ //# sourceMappingURL=fs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fs.d.ts","sourceRoot":"","sources":["../../src/utils/fs.ts"],"names":[],"mappings":"AAEA,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,MAAM,oBAMd;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,+BAM7C;AAED,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,+BAEjD;AAED,wBAAsB,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAG7E"}
@@ -0,0 +1,24 @@
1
+ import { readFile, writeFile } from "fs/promises";
2
+ export async function writeFileIfDifferent(path, content, prev) {
3
+ if (!prev)
4
+ prev = await tryReadFile(path);
5
+ const write = prev !== content;
6
+ if (write)
7
+ await writeFile(path, content);
8
+ return write;
9
+ }
10
+ export async function tryReadFile(path) {
11
+ try {
12
+ return (await readFile(path)).toString();
13
+ }
14
+ catch (_) {
15
+ return;
16
+ }
17
+ }
18
+ export async function tryReadTextFile(path) {
19
+ return (await tryReadFile(path))?.toString();
20
+ }
21
+ export async function tryReadJsonFile(path) {
22
+ const json = (await tryReadFile(path))?.toString();
23
+ return typeof json === "string" ? JSON.parse(json) : undefined;
24
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@dreamkit/node-app",
3
+ "version": "0.0.2-next.0",
4
+ "description": "DreamKit application for Node.js.",
5
+ "keywords": [],
6
+ "homepage": "https://dreamkit.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/swordev/dreamkit/tree/main/packages/node-app"
10
+ },
11
+ "license": "MIT",
12
+ "author": "Juanra GM",
13
+ "contributors": [
14
+ {
15
+ "name": "Juanra GM",
16
+ "email": "juanrgm724@gmail.com",
17
+ "url": "https://github.com/juanrgm"
18
+ }
19
+ ],
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": "./lib/index.js",
24
+ "./*": "./lib/*"
25
+ },
26
+ "files": [
27
+ "lib"
28
+ ],
29
+ "dependencies": {
30
+ "@dreamkit/app": "0.0.7-next.1",
31
+ "@dreamkit/ioc": "0.0.2-next.0",
32
+ "@dreamkit/kind": "0.0.2",
33
+ "@dreamkit/schema": "0.0.5-next.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^22.9.0",
37
+ "@dreamkit/tsconfig": "0.0.1"
38
+ },
39
+ "x-dreamkit": {
40
+ "profile": "lib"
41
+ }
42
+ }