@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.
- package/CHANGELOG.md +2 -1
- package/Core/Classes/FileSystem/File.ts +9 -0
- package/Core/Classes/FileSystem/Folder.ts +19 -0
- package/Core/Classes/FileSystem/JsonFile.ts +18 -0
- package/Core/Classes/FileSystem/TextFile.ts +10 -0
- package/Core/Classes/FileSystem/index.ts +3 -0
- package/Core/Classes/index.ts +1 -0
- package/Core/Functions/File/Basic/BasicFunctions.ts +8 -0
- package/Core/Functions/File/Basic/index.ts +1 -0
- package/Core/Functions/File/File/FileFunctions.ts +30 -0
- package/Core/Functions/File/File/index.ts +1 -0
- package/Core/Functions/File/FileFunctions.ts +7 -0
- package/Core/Functions/File/Folder/FolderFunctions.ts +22 -0
- package/Core/Functions/File/Folder/index.ts +1 -0
- package/Core/Functions/File/index.ts +1 -0
- package/Core/Functions/Process/Arguments/ArgumentsFunctions.ts +6 -0
- package/Core/Functions/Process/Arguments/index.ts +1 -0
- package/Core/Functions/Process/Command/CommandFunctions.ts +18 -0
- package/Core/Functions/Process/Command/index.ts +1 -0
- package/Core/Functions/Process/Life/LifeFunctions.ts +23 -0
- package/Core/Functions/Process/Life/index.ts +1 -0
- package/Core/Functions/Process/ProcessFunctions.ts +9 -0
- package/Core/Functions/Process/index.ts +1 -0
- package/Core/Functions/index.ts +2 -0
- package/External/index.ts +2 -0
- package/Global/Classes/GlobalClasses.d.ts +16 -1
- package/Global/Classes/index.ts +9 -0
- package/Global/Methods/Console/Console.d.ts +7 -0
- package/Global/Methods/Console/index.ts +8 -0
- package/Global/Methods/index.ts +1 -0
- package/index.d.ts +2 -1
- package/index.ts +3 -2
- package/package.json +4 -1
- package/tsconfig.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
1. console.ReadLine
|
|
2
|
+
2. Global process functions class Process
|
|
@@ -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
|
+
}
|
package/Core/Classes/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./FileSystem";
|
|
@@ -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,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 @@
|
|
|
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";
|
package/Core/Functions/index.ts
CHANGED
package/External/index.ts
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
|
|
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 {};
|
package/Global/Classes/index.ts
CHANGED
|
@@ -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
|
+
};
|
package/Global/Methods/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./Console";
|
package/index.d.ts
CHANGED
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"Node",
|
|
8
8
|
"Node Extension"
|
|
9
9
|
],
|
|
10
|
-
"version": "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
|
}
|