@kawalir/extension-node 0.1.0 → 0.3.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/Core/Classes/FileSystem/File.ts +9 -0
  3. package/Core/Classes/FileSystem/Folder.ts +19 -0
  4. package/Core/Classes/FileSystem/JsonFile.ts +18 -0
  5. package/Core/Classes/FileSystem/TextFile.ts +10 -0
  6. package/Core/Classes/FileSystem/index.ts +3 -0
  7. package/Core/Classes/index.ts +1 -0
  8. package/Core/Functions/File/Basic/BasicFunctions.ts +8 -0
  9. package/Core/Functions/File/Basic/index.ts +1 -0
  10. package/Core/Functions/File/File/FileFunctions.ts +30 -0
  11. package/Core/Functions/File/File/index.ts +1 -0
  12. package/Core/Functions/File/FileFunctions.ts +7 -0
  13. package/Core/Functions/File/Folder/FolderFunctions.ts +22 -0
  14. package/Core/Functions/File/Folder/index.ts +1 -0
  15. package/Core/Functions/File/index.ts +1 -0
  16. package/Core/Functions/Process/Arguments/ArgumentsFunctions.ts +6 -0
  17. package/Core/Functions/Process/Arguments/index.ts +1 -0
  18. package/Core/Functions/Process/Command/CommandFunctions.ts +18 -0
  19. package/Core/Functions/Process/Command/index.ts +1 -0
  20. package/Core/Functions/Process/Life/LifeFunctions.ts +23 -0
  21. package/Core/Functions/Process/Life/index.ts +1 -0
  22. package/Core/Functions/Process/ProcessFunctions.ts +9 -0
  23. package/Core/Functions/Process/index.ts +1 -0
  24. package/Core/Functions/index.ts +2 -0
  25. package/External/index.ts +2 -0
  26. package/Global/Classes/GlobalClasses.d.ts +16 -1
  27. package/Global/Classes/index.ts +9 -0
  28. package/Global/Methods/Console/Console.d.ts +7 -0
  29. package/Global/Methods/Console/index.ts +8 -0
  30. package/Global/Methods/index.ts +1 -0
  31. package/index.d.ts +2 -1
  32. package/index.ts +3 -2
  33. package/package.json +4 -1
  34. package/tsconfig.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1 +1,2 @@
1
- Global Node modules
1
+ 1. console.ReadLine
2
+ 2. Global process functions class Process
@@ -0,0 +1,9 @@
1
+ import { FileFunctions as FF } from "@kawalir/extension-node/Core/Functions";
2
+
3
+ export class File {
4
+ constructor(public path: string) {}
5
+
6
+ public testExistence = () => FF.File.testExistence(this.path);
7
+
8
+ public delete = () => FF.File.delete(this.path);
9
+ }
@@ -0,0 +1,19 @@
1
+ import { FileFunctions } from "@kawalir/extension-node/Core/Functions";
2
+ import { TextFile } from "./TextFile";
3
+
4
+ export class Folder {
5
+ constructor(public path: string) {}
6
+
7
+ public getFile = (name: string) => new TextFile(this.getPath(name));
8
+
9
+ public getSubFolder = (relativePath: string) =>
10
+ new Folder(this.getPath(relativePath));
11
+
12
+ public getPath = (relative: string) => Path.join(this.path, relative);
13
+
14
+ public ensure = () => FileFunctions.Folder.ensure(this.path);
15
+
16
+ public get parent(): Folder {
17
+ return new Folder(Path.dirname(this.path));
18
+ }
19
+ }
@@ -0,0 +1,18 @@
1
+ import { TextFile } from "./TextFile";
2
+ import { File } from "./File";
3
+
4
+ export class JsonFile<T> extends File {
5
+ private textFile: TextFile;
6
+ constructor(path: string) {
7
+ super(path);
8
+ this.textFile = new TextFile(path);
9
+ }
10
+
11
+ public write = (data: T, pretty: boolean = true) =>
12
+ this.textFile.write(JSON.stringify(data, null, pretty ? 2 : undefined));
13
+
14
+ public readOrNull = (): Nullable<T> =>
15
+ this.testExistence() ? this.read() : null;
16
+
17
+ public read = (): T => JSON.parse(this.textFile.read());
18
+ }
@@ -0,0 +1,10 @@
1
+ import { FileFunctions as FF } from "@kawalir/extension-node/Core/Functions";
2
+ import { File } from "./File";
3
+
4
+ export class TextFile extends File {
5
+ public write = (content: string) => FF.File.write(this.path, content);
6
+
7
+ public readOrNull = (): StringOrNull => FF.File.readOrNull(this.path);
8
+
9
+ public read = (): string => FF.File.read(this.path);
10
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./Folder";
2
+ export * from "./JsonFile";
3
+ export * from "./TextFile";
@@ -0,0 +1 @@
1
+ export * from "./FileSystem";
@@ -0,0 +1,8 @@
1
+ import { FileSystem as FS } from "@kawalir/extension-node/External";
2
+
3
+ export class BasicFunctions {
4
+ public static testExistence = (
5
+ path: string,
6
+ test: (stats: FS.Stats) => boolean,
7
+ ) => FS.existsSync(path) && test(FS.lstatSync(path));
8
+ }
@@ -0,0 +1 @@
1
+ export * from "./BasicFunctions";
@@ -0,0 +1,30 @@
1
+ import { Path, FileSystem as FS } from "@kawalir/extension-node/External";
2
+ import { FolderFunctions as FF } from "../Folder";
3
+ import { BasicFunctions as BF } from "../Basic";
4
+
5
+ type Content = string | NodeJS.ArrayBufferView;
6
+
7
+ export class FileFunctions {
8
+ public static rewrite = (path: string, content: Content) => {
9
+ this.delete(path);
10
+ this.write(path, content);
11
+ };
12
+
13
+ public static write = (path: string, content: Content) => {
14
+ FF.ensureContaining(path);
15
+ FS.writeFileSync(path, content, "utf8");
16
+ };
17
+
18
+ public static delete = (path: string) =>
19
+ FS.rmSync(path, {
20
+ force: true,
21
+ });
22
+
23
+ public static readOrNull = (path: string): StringOrNull =>
24
+ this.testExistence(path) ? this.read(path) : null;
25
+
26
+ public static read = (path: string): string => FS.readFileSync(path, "utf-8");
27
+
28
+ public static testExistence = (path: string) =>
29
+ BF.testExistence(path, (e) => e.isFile());
30
+ }
@@ -0,0 +1 @@
1
+ export * from "./FileFunctions";
@@ -0,0 +1,7 @@
1
+ import { FileFunctions as FF } from "./File";
2
+ import { FolderFunctions } from "./Folder";
3
+
4
+ export class FileFunctions {
5
+ public static Folder = FolderFunctions;
6
+ public static File = FF;
7
+ }
@@ -0,0 +1,22 @@
1
+ import { BasicFunctions as BF } from "../Basic";
2
+
3
+ export class FolderFunctions {
4
+ public static ensureContaining = (path: string) =>
5
+ this.ensure(Path.dirname(path));
6
+
7
+ public static ensure = (path: string) => {
8
+ if (this.testExistence(path)) {
9
+ return;
10
+ }
11
+ this.create(path);
12
+ };
13
+
14
+ public static create = (path: string) => {
15
+ FileSystem.mkdirSync(path, {
16
+ recursive: true,
17
+ });
18
+ };
19
+
20
+ public static testExistence = (path: string) =>
21
+ BF.testExistence(path, (stats) => stats.isDirectory());
22
+ }
@@ -0,0 +1 @@
1
+ export * from "./FolderFunctions";
@@ -0,0 +1 @@
1
+ export * from "./FileFunctions";
@@ -0,0 +1,6 @@
1
+ export class ArgumentsFunctions {
2
+ public static all: string[];
3
+ static {
4
+ this.all = process.argv.slice(2);
5
+ }
6
+ }
@@ -0,0 +1 @@
1
+ export * from "./ArgumentsFunctions";
@@ -0,0 +1,18 @@
1
+ import { ChildProcess } from "@kawalir/extension-node/External";
2
+
3
+ export class CommandFunctions {
4
+ public static pwsh = (request: {
5
+ command: string;
6
+ folderPath: string;
7
+ }): void => {
8
+ const result = ChildProcess.spawnSync(request.command, {
9
+ cwd: request.folderPath,
10
+ shell: "pwsh",
11
+ stdio: "inherit",
12
+ });
13
+ if (result.status === 0) {
14
+ return;
15
+ }
16
+ process.exit(result.status);
17
+ };
18
+ }
@@ -0,0 +1 @@
1
+ export * from "./CommandFunctions";
@@ -0,0 +1,23 @@
1
+ type Action = () => void;
2
+
3
+ class EnsuredAction {
4
+ public hasBeenCalled: boolean = false;
5
+ constructor(public action: Action) {}
6
+ public ensure = () => {
7
+ if (this.hasBeenCalled) {
8
+ return;
9
+ }
10
+ this.action();
11
+ this.hasBeenCalled = true;
12
+ };
13
+ }
14
+
15
+ export class LifeFunctions {
16
+ public static doBeforeEnd = (action: () => void) => {
17
+ const ea = new EnsuredAction(() => action());
18
+ process.on("SIGINT", () => ea.ensure());
19
+ process.on("SIGTERM", () => ea.ensure());
20
+ process.on("SIGHUP", () => ea.ensure());
21
+ process.on("exit", () => ea.ensure());
22
+ };
23
+ }
@@ -0,0 +1 @@
1
+ export * from "./LifeFunctions";
@@ -0,0 +1,9 @@
1
+ import { ArgumentsFunctions } from "./Arguments";
2
+ import { CommandFunctions } from "./Command";
3
+ import { LifeFunctions } from "./Life";
4
+
5
+ export class ProcessFunctions {
6
+ public static arguments = ArgumentsFunctions;
7
+ public static command = CommandFunctions;
8
+ public static life = LifeFunctions;
9
+ }
@@ -0,0 +1 @@
1
+ export * from "./ProcessFunctions";
@@ -0,0 +1,2 @@
1
+ export * from "./File";
2
+ export * from "./Process";
package/External/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export * as ReadLine from "readline/promises";
2
+ export * as ChildProcess from "child_process";
1
3
  export * as FileSystem from "fs";
2
4
 
3
5
  import * as Path from "path";
@@ -1,3 +1,18 @@
1
- declare global {}
1
+ import * as CORE from "@kawalir/extension-node/Core";
2
+
3
+ declare global {
4
+ //? Static
5
+ var Process: typeof CORE.ProcessFunctions;
6
+
7
+ //? Proper classses
8
+ interface TextFile extends CORE.TextFile {}
9
+ var TextFile: typeof CORE.TextFile;
10
+
11
+ interface JsonFile<T> extends CORE.JsonFile<T> {}
12
+ var JsonFile: typeof CORE.JsonFile;
13
+
14
+ interface Folder extends CORE.Folder {}
15
+ var Folder: typeof CORE.Folder;
16
+ }
2
17
 
3
18
  export {};
@@ -0,0 +1,9 @@
1
+ import * as CORE from "@kawalir/extension-node/Core";
2
+
3
+ //? Static
4
+ globalThis.Process = CORE.ProcessFunctions;
5
+
6
+ //? Proper classses
7
+ globalThis.TextFile = CORE.TextFile;
8
+ globalThis.JsonFile = CORE.JsonFile;
9
+ globalThis.Folder = CORE.Folder;
@@ -0,0 +1,7 @@
1
+ declare global {
2
+ interface Console {
3
+ ReadLine(prompt?: string): Promise<string>;
4
+ }
5
+ }
6
+
7
+ export {};
@@ -0,0 +1,8 @@
1
+ import { ReadLine } from "@kawalir/extension-node/External";
2
+
3
+ console.ReadLine = async (prompt?: string) => {
4
+ const int = ReadLine.createInterface(process.stdin, process.stdout);
5
+ const answer = await int.question(prompt ?? "");
6
+ int.close();
7
+ return answer;
8
+ };
@@ -0,0 +1 @@
1
+ import "./Console";
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  //? [Global]
2
2
  /// <reference path="Global/Classes/GlobalClasses.d.ts" />
3
- /// <reference path="Global/Modules/GlobalModules.d.ts" />
3
+ /// <reference path="Global/Modules/GlobalModules.d.ts" />
4
+ /// <reference path="Global/Methods/Console/Console.d.ts" />
package/index.ts CHANGED
@@ -1,3 +1,4 @@
1
- export * from "./Core";
2
-
1
+ import "@kawalir/extension";
3
2
  import "./Global";
3
+
4
+ export * from "./Core";
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "Node",
8
8
  "Node Extension"
9
9
  ],
10
- "version": "0.1.0",
10
+ "version": "0.3.0",
11
11
  "author": "Denis Mijatovic",
12
12
  "license": "MIT",
13
13
  "type": "commonjs",
@@ -34,5 +34,8 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "^25.3.0"
37
+ },
38
+ "dependencies": {
39
+ "@kawalir/extension": "^0.8.0"
37
40
  }
38
41
  }
package/tsconfig.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "module": "nodenext",
11
11
  "target": "esnext",
12
12
  "lib": ["esnext"],
13
- "types": ["node"],
13
+ "types": ["node", "@kawalir/extension"],
14
14
 
15
15
  // Other Outputs
16
16
  "sourceMap": true,