@bemoje/fs 0.0.12 → 0.1.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 (151) hide show
  1. package/README.md +6 -34
  2. package/package.json +9 -9
  3. package/src/index.d.ts +35 -0
  4. package/src/index.js +39 -0
  5. package/src/index.js.map +1 -0
  6. package/src/lib/AppData.d.ts +10 -0
  7. package/src/lib/AppData.js +24 -0
  8. package/src/lib/AppData.js.map +1 -0
  9. package/src/lib/JsonFile.d.ts +78 -0
  10. package/src/lib/JsonFile.js +146 -0
  11. package/src/lib/JsonFile.js.map +1 -0
  12. package/src/lib/LogFile.d.ts +50 -0
  13. package/src/lib/LogFile.js +158 -0
  14. package/src/lib/LogFile.js.map +1 -0
  15. package/src/lib/{absoluteToRelativePath.ts → absoluteToRelativePath.d.ts} +13 -20
  16. package/src/lib/absoluteToRelativePath.js +25 -0
  17. package/src/lib/absoluteToRelativePath.js.map +1 -0
  18. package/src/lib/appendLineToFile.d.ts +11 -0
  19. package/src/lib/appendLineToFile.js +28 -0
  20. package/src/lib/appendLineToFile.js.map +1 -0
  21. package/src/lib/{cleanDirectorySync.ts → cleanDirectorySync.d.ts} +16 -24
  22. package/src/lib/cleanDirectorySync.js +29 -0
  23. package/src/lib/cleanDirectorySync.js.map +1 -0
  24. package/src/lib/{createDirectory.ts → createDirectory.d.ts} +1 -5
  25. package/src/lib/createDirectory.js +25 -0
  26. package/src/lib/createDirectory.js.map +1 -0
  27. package/src/lib/{createDirectorySync.ts → createDirectorySync.d.ts} +1 -6
  28. package/src/lib/createDirectorySync.js +20 -0
  29. package/src/lib/createDirectorySync.js.map +1 -0
  30. package/src/lib/{createFileExtensionFilter.ts → createFileExtensionFilter.d.ts} +1 -14
  31. package/src/lib/createFileExtensionFilter.js +32 -0
  32. package/src/lib/createFileExtensionFilter.js.map +1 -0
  33. package/src/lib/{cwdpath.ts → cwdpath.d.ts} +12 -16
  34. package/src/lib/cwdpath.js +21 -0
  35. package/src/lib/cwdpath.js.map +1 -0
  36. package/src/lib/{deleteDirectory.ts → deleteDirectory.d.ts} +1 -5
  37. package/src/lib/deleteDirectory.js +23 -0
  38. package/src/lib/deleteDirectory.js.map +1 -0
  39. package/src/lib/{deleteDirectorySafe.ts → deleteDirectorySafe.d.ts} +1 -5
  40. package/src/lib/deleteDirectorySafe.js +25 -0
  41. package/src/lib/deleteDirectorySafe.js.map +1 -0
  42. package/src/lib/{deleteDirectorySafeSync.ts → deleteDirectorySafeSync.d.ts} +1 -7
  43. package/src/lib/deleteDirectorySafeSync.js +23 -0
  44. package/src/lib/deleteDirectorySafeSync.js.map +1 -0
  45. package/src/lib/{deleteDirectorySync.ts → deleteDirectorySync.d.ts} +1 -6
  46. package/src/lib/deleteDirectorySync.js +22 -0
  47. package/src/lib/deleteDirectorySync.js.map +1 -0
  48. package/src/lib/{ensureValidWindowsPath.ts → ensureValidWindowsPath.d.ts} +22 -57
  49. package/src/lib/ensureValidWindowsPath.js +54 -0
  50. package/src/lib/ensureValidWindowsPath.js.map +1 -0
  51. package/src/lib/getDiskDrivesWindows.d.ts +1 -0
  52. package/src/lib/getDiskDrivesWindows.js +19 -0
  53. package/src/lib/getDiskDrivesWindows.js.map +1 -0
  54. package/src/lib/getDownloadsDirectory.d.ts +1 -0
  55. package/src/lib/getDownloadsDirectory.js +11 -0
  56. package/src/lib/getDownloadsDirectory.js.map +1 -0
  57. package/src/lib/getHomeDirectory.d.ts +1 -0
  58. package/src/lib/getHomeDirectory.js +13 -0
  59. package/src/lib/getHomeDirectory.js.map +1 -0
  60. package/src/lib/getLastMofifiedFileIn.d.ts +1 -0
  61. package/src/lib/getLastMofifiedFileIn.js +22 -0
  62. package/src/lib/getLastMofifiedFileIn.js.map +1 -0
  63. package/src/lib/getRootDir.d.ts +1 -0
  64. package/src/lib/getRootDir.js +10 -0
  65. package/src/lib/getRootDir.js.map +1 -0
  66. package/src/lib/isWindows.d.ts +1 -0
  67. package/src/lib/isWindows.js +8 -0
  68. package/src/lib/isWindows.js.map +1 -0
  69. package/src/lib/{normalizeFileExtension.ts → normalizeFileExtension.d.ts} +22 -29
  70. package/src/lib/normalizeFileExtension.js +36 -0
  71. package/src/lib/normalizeFileExtension.js.map +1 -0
  72. package/src/lib/readFileLastLines.d.ts +7 -0
  73. package/src/lib/readFileLastLines.js +18 -0
  74. package/src/lib/readFileLastLines.js.map +1 -0
  75. package/src/lib/{readJsonFile.ts → readJsonFile.d.ts} +12 -16
  76. package/src/lib/readJsonFile.js +23 -0
  77. package/src/lib/readJsonFile.js.map +1 -0
  78. package/src/lib/{readJsonFileSync.ts → readJsonFileSync.d.ts} +12 -16
  79. package/src/lib/readJsonFileSync.js +21 -0
  80. package/src/lib/readJsonFileSync.js.map +1 -0
  81. package/src/lib/readPackageJson.d.ts +1 -0
  82. package/src/lib/readPackageJson.js +15 -0
  83. package/src/lib/readPackageJson.js.map +1 -0
  84. package/src/lib/removeIllegalFilenameCharacters.d.ts +1 -0
  85. package/src/lib/removeIllegalFilenameCharacters.js +8 -0
  86. package/src/lib/removeIllegalFilenameCharacters.js.map +1 -0
  87. package/src/lib/tempFile.d.ts +6 -0
  88. package/src/lib/tempFile.js +26 -0
  89. package/src/lib/tempFile.js.map +1 -0
  90. package/src/lib/types/IEnsureValidWindowsPathOptions.d.ts +10 -0
  91. package/src/lib/types/IEnsureValidWindowsPathOptions.js +3 -0
  92. package/src/lib/types/IEnsureValidWindowsPathOptions.js.map +1 -0
  93. package/src/lib/types/ILogFileData.d.ts +5 -0
  94. package/src/lib/types/ILogFileData.js +3 -0
  95. package/src/lib/types/ILogFileData.js.map +1 -0
  96. package/src/lib/unzipFile.d.ts +1 -0
  97. package/src/lib/unzipFile.js +12 -0
  98. package/src/lib/unzipFile.js.map +1 -0
  99. package/src/lib/unzipMergeFiles.d.ts +4 -0
  100. package/src/lib/unzipMergeFiles.js +29 -0
  101. package/src/lib/unzipMergeFiles.js.map +1 -0
  102. package/src/lib/wipeDirectorySafe.d.ts +1 -0
  103. package/src/lib/wipeDirectorySafe.js +14 -0
  104. package/src/lib/wipeDirectorySafe.js.map +1 -0
  105. package/src/lib/{writeJsonFile.ts → writeJsonFile.d.ts} +1 -5
  106. package/src/lib/writeJsonFile.js +25 -0
  107. package/src/lib/writeJsonFile.js.map +1 -0
  108. package/src/lib/{writeJsonFileSync.ts → writeJsonFileSync.d.ts} +1 -5
  109. package/src/lib/writeJsonFileSync.js +22 -0
  110. package/src/lib/writeJsonFileSync.js.map +1 -0
  111. package/LICENSE +0 -21
  112. package/jest.config.ts +0 -13
  113. package/project.json +0 -30
  114. package/src/index.ts +0 -35
  115. package/src/lib/AppData.ts +0 -25
  116. package/src/lib/JsonFile.ts +0 -158
  117. package/src/lib/LogFile.ts +0 -117
  118. package/src/lib/absoluteToRelativePath.test.ts +0 -11
  119. package/src/lib/appendLineToFile.test.ts +0 -49
  120. package/src/lib/appendLineToFile.ts +0 -21
  121. package/src/lib/cleanDirectorySync.test.ts +0 -57
  122. package/src/lib/createDirectory.test.ts +0 -14
  123. package/src/lib/createDirectorySync.test.ts +0 -14
  124. package/src/lib/createFileExtensionFilter.test.ts +0 -17
  125. package/src/lib/cwdpath.test.ts +0 -48
  126. package/src/lib/deleteDirectory.test.ts +0 -24
  127. package/src/lib/deleteDirectorySafe.test.ts +0 -40
  128. package/src/lib/deleteDirectorySafeSync.test.ts +0 -30
  129. package/src/lib/deleteDirectorySync.test.ts +0 -23
  130. package/src/lib/ensureValidWindowsPath.test.ts +0 -80
  131. package/src/lib/getDiskDrivesWindows.ts +0 -13
  132. package/src/lib/getDownloadsDirectory.ts +0 -6
  133. package/src/lib/getHomeDirectory.ts +0 -7
  134. package/src/lib/getLastMofifiedFileIn.ts +0 -15
  135. package/src/lib/getRootDir.ts +0 -5
  136. package/src/lib/isWindows.ts +0 -3
  137. package/src/lib/normalizeFileExtension.test.ts +0 -15
  138. package/src/lib/readFileLastLines.ts +0 -12
  139. package/src/lib/readJsonFile.test.ts +0 -34
  140. package/src/lib/readJsonFileSync.test.ts +0 -36
  141. package/src/lib/readPackageJson.ts +0 -9
  142. package/src/lib/removeIllegalFilenameCharacters.ts +0 -3
  143. package/src/lib/tempFile.ts +0 -20
  144. package/src/lib/types/IEnsureValidWindowsPathOptions.ts +0 -10
  145. package/src/lib/types/ILogFileData.ts +0 -5
  146. package/src/lib/unzipFile.ts +0 -5
  147. package/src/lib/unzipMergeFiles.ts +0 -20
  148. package/src/lib/wipeDirectorySafe.ts +0 -7
  149. package/src/lib/writeJsonFile.test.ts +0 -26
  150. package/src/lib/writeJsonFileSync.test.ts +0 -28
  151. package/tsconfig.spec.json +0 -9
@@ -0,0 +1,5 @@
1
+ export interface ILogFileData<T> {
2
+ source: string;
3
+ key: string;
4
+ value: T;
5
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ILogFileData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ILogFileData.js","sourceRoot":"","sources":["../../../../../../packages/fs/src/lib/types/ILogFileData.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export declare function unzipFile(filepath: string, outdir: string): Promise<void>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unzipFile = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const extract_zip_1 = tslib_1.__importDefault(require("extract-zip"));
6
+ function unzipFile(filepath, outdir) {
7
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
8
+ yield (0, extract_zip_1.default)(filepath, { dir: outdir });
9
+ });
10
+ }
11
+ exports.unzipFile = unzipFile;
12
+ //# sourceMappingURL=unzipFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unzipFile.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/unzipFile.ts"],"names":[],"mappings":";;;;AAAA,sEAAiC;AAEjC,SAAsB,SAAS,CAAC,QAAgB,EAAE,MAAc;;QAC9D,MAAM,IAAA,qBAAO,EAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAA;IAC1C,CAAC;CAAA;AAFD,8BAEC"}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @param source Either a directory containing zip files or an array of zip file paths.
3
+ */
4
+ export declare function unzipMergeFiles(source: string | string[], target: string): Promise<void>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unzipMergeFiles = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const extract_zip_1 = tslib_1.__importDefault(require("extract-zip"));
6
+ const fs_1 = tslib_1.__importDefault(require("fs"));
7
+ const path_1 = tslib_1.__importDefault(require("path"));
8
+ /**
9
+ * @param source Either a directory containing zip files or an array of zip file paths.
10
+ */
11
+ function unzipMergeFiles(source, target) {
12
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
13
+ if (typeof source === 'string') {
14
+ source = fs_1.default.readdirSync(source).map((filename) => {
15
+ return path_1.default.join(source, filename);
16
+ });
17
+ }
18
+ for (const filepath of source) {
19
+ const stat = yield fs_1.default.promises.stat(filepath);
20
+ if (stat.isDirectory())
21
+ continue;
22
+ if (!filepath.endsWith('.zip'))
23
+ continue;
24
+ yield (0, extract_zip_1.default)(filepath, { dir: target });
25
+ }
26
+ });
27
+ }
28
+ exports.unzipMergeFiles = unzipMergeFiles;
29
+ //# sourceMappingURL=unzipMergeFiles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unzipMergeFiles.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/unzipMergeFiles.ts"],"names":[],"mappings":";;;;AAAA,sEAAiC;AACjC,oDAAmB;AACnB,wDAAuB;AAEvB;;GAEG;AACH,SAAsB,eAAe,CAAC,MAAyB,EAAE,MAAc;;QAC7E,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;YAC9B,MAAM,GAAG,YAAE,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;gBAC/C,OAAO,cAAI,CAAC,IAAI,CAAC,MAAgB,EAAE,QAAQ,CAAC,CAAA;YAC9C,CAAC,CAAC,CAAA;SACH;QACD,KAAK,MAAM,QAAQ,IAAI,MAAM,EAAE;YAC7B,MAAM,IAAI,GAAG,MAAM,YAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC7C,IAAI,IAAI,CAAC,WAAW,EAAE;gBAAE,SAAQ;YAChC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC;gBAAE,SAAQ;YACxC,MAAM,IAAA,qBAAO,EAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAA;SACzC;IACH,CAAC;CAAA;AAZD,0CAYC"}
@@ -0,0 +1 @@
1
+ export declare function wipeDirectorySafe(dirpath: string): Promise<void>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wipeDirectorySafe = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const createDirectory_1 = require("./createDirectory");
6
+ const deleteDirectorySafe_1 = require("./deleteDirectorySafe");
7
+ function wipeDirectorySafe(dirpath) {
8
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
9
+ yield (0, deleteDirectorySafe_1.deleteDirectorySafe)(dirpath);
10
+ yield (0, createDirectory_1.createDirectory)(dirpath);
11
+ });
12
+ }
13
+ exports.wipeDirectorySafe = wipeDirectorySafe;
14
+ //# sourceMappingURL=wipeDirectorySafe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wipeDirectorySafe.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/wipeDirectorySafe.ts"],"names":[],"mappings":";;;;AAAA,uDAAmD;AACnD,+DAA2D;AAE3D,SAAsB,iBAAiB,CAAC,OAAe;;QACrD,MAAM,IAAA,yCAAmB,EAAC,OAAO,CAAC,CAAA;QAClC,MAAM,IAAA,iCAAe,EAAC,OAAO,CAAC,CAAA;IAChC,CAAC;CAAA;AAHD,8CAGC"}
@@ -1,5 +1,3 @@
1
- import fs from 'fs'
2
-
3
1
  /**
4
2
  * Writes a JSON file to the specified filepath. The data to be written is passed as an argument.
5
3
  * If the 'pretty' argument is set to true, the JSON data will be formatted in a more human-readable way.
@@ -13,6 +11,4 @@ import fs from 'fs'
13
11
  * await writeJsonFile('./data.json', data, true);
14
12
  * ```
15
13
  */
16
- export async function writeJsonFile<T>(filepath: string, data: T, pretty = false): Promise<void> {
17
- await fs.promises.writeFile(filepath, JSON.stringify(data, null, pretty ? 2 : undefined))
18
- }
14
+ export declare function writeJsonFile<T>(filepath: string, data: T, pretty?: boolean): Promise<void>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeJsonFile = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs_1 = tslib_1.__importDefault(require("fs"));
6
+ /**
7
+ * Writes a JSON file to the specified filepath. The data to be written is passed as an argument.
8
+ * If the 'pretty' argument is set to true, the JSON data will be formatted in a more human-readable way.
9
+ * @template T - The type of the data to be written to the file.
10
+ * @param filepath - The path to the file where the data should be written.
11
+ * @param data - The data to be written to the file.
12
+ * @param pretty - Optional. If set to true, formats the JSON data in a more human-readable way. Default is false.
13
+ * @returns A Promise that resolves when the file has been successfully written.
14
+ * @example ```ts
15
+ * const data = { name: 'John', age: 30 };
16
+ * await writeJsonFile('./data.json', data, true);
17
+ * ```
18
+ */
19
+ function writeJsonFile(filepath, data, pretty = false) {
20
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
21
+ yield fs_1.default.promises.writeFile(filepath, JSON.stringify(data, null, pretty ? 2 : undefined));
22
+ });
23
+ }
24
+ exports.writeJsonFile = writeJsonFile;
25
+ //# sourceMappingURL=writeJsonFile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writeJsonFile.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/writeJsonFile.ts"],"names":[],"mappings":";;;;AAAA,oDAAmB;AAEnB;;;;;;;;;;;;GAYG;AACH,SAAsB,aAAa,CAAI,QAAgB,EAAE,IAAO,EAAE,MAAM,GAAG,KAAK;;QAC9E,MAAM,YAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAA;IAC3F,CAAC;CAAA;AAFD,sCAEC"}
@@ -1,5 +1,3 @@
1
- import fs from 'fs'
2
-
3
1
  /**
4
2
  * Writes a JSON object to a file synchronously.
5
3
  * @typeParam T - The type of the JSON object.
@@ -12,6 +10,4 @@ import fs from 'fs'
12
10
  * writeJsonFileSync('./data.json', data, true);
13
11
  * ```
14
12
  */
15
- export function writeJsonFileSync<T>(filepath: string, data: T, pretty = false): void {
16
- fs.writeFileSync(filepath, JSON.stringify(data, null, pretty ? 2 : undefined))
17
- }
13
+ export declare function writeJsonFileSync<T>(filepath: string, data: T, pretty?: boolean): void;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeJsonFileSync = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs_1 = tslib_1.__importDefault(require("fs"));
6
+ /**
7
+ * Writes a JSON object to a file synchronously.
8
+ * @typeParam T - The type of the JSON object.
9
+ * @param filepath - The path of the file where the JSON object will be written.
10
+ * @param data - The JSON object that will be written to the file.
11
+ * @param pretty - If true, the JSON object will be formatted with indentation and line breaks. Default is false.
12
+ * @throws If the file cannot be written.
13
+ * @example ```ts
14
+ * const data = { name: 'John', age: 30 };
15
+ * writeJsonFileSync('./data.json', data, true);
16
+ * ```
17
+ */
18
+ function writeJsonFileSync(filepath, data, pretty = false) {
19
+ fs_1.default.writeFileSync(filepath, JSON.stringify(data, null, pretty ? 2 : undefined));
20
+ }
21
+ exports.writeJsonFileSync = writeJsonFileSync;
22
+ //# sourceMappingURL=writeJsonFileSync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writeJsonFileSync.js","sourceRoot":"","sources":["../../../../../packages/fs/src/lib/writeJsonFileSync.ts"],"names":[],"mappings":";;;;AAAA,oDAAmB;AAEnB;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAAI,QAAgB,EAAE,IAAO,EAAE,MAAM,GAAG,KAAK;IAC5E,YAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAA;AAChF,CAAC;AAFD,8CAEC"}
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Benjamin Møller Jensen
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/jest.config.ts DELETED
@@ -1,13 +0,0 @@
1
- /* eslint-disable */
2
- const pkg = require('./package.json')
3
- const name = pkg.name.split('@')[1]
4
- export default {
5
- displayName: name,
6
- preset: '../../jest.preset.js',
7
- testEnvironment: 'node',
8
- transform: {
9
- '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
10
- },
11
- moduleFileExtensions: ['ts', 'js', 'html'],
12
- coverageDirectory: '../../coverage/pkg/' + name,
13
- }
package/project.json DELETED
@@ -1,30 +0,0 @@
1
- {
2
- "name": "fs",
3
- "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
- "sourceRoot": "pkg/fs/src",
5
- "projectType": "library",
6
- "targets": {
7
- "lint": {
8
- "executor": "@nx/linter:eslint",
9
- "outputs": ["{options.outputFile}"],
10
- "options": {
11
- "lintFilePatterns": ["pkg/fs/**/*.ts"]
12
- }
13
- },
14
- "test": {
15
- "executor": "@nx/jest:jest",
16
- "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
17
- "options": {
18
- "jestConfig": "pkg/fs/jest.config.ts",
19
- "passWithNoTests": true
20
- },
21
- "configurations": {
22
- "ci": {
23
- "ci": true,
24
- "codeCoverage": true
25
- }
26
- }
27
- }
28
- },
29
- "tags": []
30
- }
package/src/index.ts DELETED
@@ -1,35 +0,0 @@
1
- export * from './lib/absoluteToRelativePath'
2
- export * from './lib/AppData'
3
- export * from './lib/appendLineToFile'
4
- export * from './lib/cleanDirectorySync'
5
- export * from './lib/createDirectory'
6
- export * from './lib/createDirectorySync'
7
- export * from './lib/createFileExtensionFilter'
8
- export * from './lib/cwdpath'
9
- export * from './lib/deleteDirectory'
10
- export * from './lib/deleteDirectorySafe'
11
- export * from './lib/deleteDirectorySafeSync'
12
- export * from './lib/deleteDirectorySync'
13
- export * from './lib/ensureValidWindowsPath'
14
- export * from './lib/getDiskDrivesWindows'
15
- export * from './lib/getDownloadsDirectory'
16
- export * from './lib/getHomeDirectory'
17
- export * from './lib/getLastMofifiedFileIn'
18
- export * from './lib/getRootDir'
19
- export * from './lib/isWindows'
20
- export * from './lib/JsonFile'
21
- export * from './lib/LogFile'
22
- export * from './lib/normalizeFileExtension'
23
- export * from './lib/readFileLastLines'
24
- export * from './lib/readJsonFile'
25
- export * from './lib/readJsonFileSync'
26
- export * from './lib/readPackageJson'
27
- export * from './lib/removeIllegalFilenameCharacters'
28
- export * from './lib/tempFile'
29
- export * from './lib/types/IEnsureValidWindowsPathOptions'
30
- export * from './lib/types/ILogFileData'
31
- export * from './lib/unzipFile'
32
- export * from './lib/unzipMergeFiles'
33
- export * from './lib/wipeDirectorySafe'
34
- export * from './lib/writeJsonFile'
35
- export * from './lib/writeJsonFileSync'
@@ -1,25 +0,0 @@
1
- import getAppDataPath from 'appdata-path'
2
- import path from 'path'
3
- import { JsonFile } from './JsonFile'
4
- import { LogFile } from './LogFile'
5
- import { ILogFileData } from './types/ILogFileData'
6
-
7
- export class AppData<A extends Record<string, unknown>, C extends Record<string, unknown>> {
8
- directory: string
9
- app: JsonFile<A>
10
- user: JsonFile<C>
11
- history: LogFile<ILogFileData<C>>
12
-
13
- constructor(appAuthor: string, appName: string, configName: string) {
14
- this.directory = path.join(getAppDataPath(), appAuthor, appName, configName)
15
- this.app = new JsonFile(path.join(this.directory, 'appdata.json'), false)
16
- this.user = new JsonFile(path.join(this.directory, 'config.json'), true)
17
- this.history = new LogFile(path.join(this.directory, 'history.log'))
18
- this.app.on('set', async (time, k: string, v: C) => {
19
- await this.history.append({ source: 'app', key: k, value: v }, time)
20
- })
21
- this.user.on('set', async (time, k: string, v: C) => {
22
- await this.history.append({ source: 'user', key: k, value: v }, time)
23
- })
24
- }
25
- }
@@ -1,158 +0,0 @@
1
- import EventEmitter from 'events'
2
- import fs from 'fs'
3
- import path from 'path'
4
- import { readJsonFileSync } from './readJsonFileSync'
5
- import { writeJsonFileSync } from './writeJsonFileSync'
6
-
7
- /**
8
- * Represents a JSON file manager that ensures immutable data since everything is serialized and deserialized before receiving or returning any data.
9
- */
10
- export class JsonFile<T extends Record<string, any>> extends EventEmitter {
11
- /**
12
- * The filepath of the JSON file.
13
- */
14
- readonly filepath: string
15
-
16
- /**
17
- * The data stored in the JSON file.
18
- */
19
- #data: T
20
-
21
- /**
22
- * Determines whether the JSON file should be pretty-printed.
23
- */
24
- prettyPrint: boolean
25
-
26
- /**
27
- * Gets the data stored in the JSON file.
28
- * @returns A deep copy of the data.
29
- */
30
- get data(): T {
31
- return JSON.parse(JSON.stringify(this.#data))
32
- }
33
-
34
- /**
35
- * Constructs a new instance of the JsonFile class.
36
- * @param filepath - The filepath of the JSON file.
37
- * @param prettyPrint - Determines whether the JSON file should be pretty-printed.
38
- */
39
- constructor(filepath: string, prettyPrint = false) {
40
- super()
41
- this.filepath = filepath
42
- this.#data = {} as T
43
- this.prettyPrint = prettyPrint
44
- this.ensureDirectoryExists()
45
- if (fs.existsSync(this.filepath)) {
46
- this.loadFile()
47
- } else {
48
- writeJsonFileSync(this.filepath, this.#data, this.prettyPrint)
49
- }
50
- }
51
-
52
- /**
53
- * Gets the value associated with the specified key.
54
- * @param key - The key to retrieve the value for.
55
- * @returns The value associated with the key.
56
- */
57
- get(key: keyof T): any {
58
- return JSON.parse(JSON.stringify(this.#data[key]))
59
- }
60
-
61
- /**
62
- * Sets the value associated with the specified key.
63
- * @param key - The key to set the value for.
64
- * @param value - The value to set.
65
- */
66
- set(key: keyof T, value: any): void {
67
- if (this.#data[key] === value) return
68
- const json = JSON.stringify(value)
69
- if (json === JSON.stringify(this.#data[key])) return
70
- value = JSON.parse(json)
71
- this.#data[key] = value
72
- this.saveFile()
73
- this.emit('set', Date.now(), key, value)
74
- }
75
-
76
- /**
77
- * Checks if the specified key exists in the JSON file.
78
- * @param key - The key to check.
79
- * @returns True if the key exists, false otherwise.
80
- */
81
- has(key: keyof T): boolean {
82
- return this.#data[key] !== undefined
83
- }
84
-
85
- /**
86
- * Deletes the value associated with the specified key.
87
- * @param key - The key to delete.
88
- */
89
- delete(key: keyof T): void {
90
- delete this.#data[key]
91
- this.saveFile()
92
- this.emit('set', Date.now(), key, void 0)
93
- }
94
-
95
- /**
96
- * Gets the value associated with the specified key, or sets it to the provided default value if it doesn't exist.
97
- * @param key - The key to get or set.
98
- * @param getValue - A function that returns the default value if the key doesn't exist.
99
- * @returns The value associated with the key, or the default value if it doesn't exist.
100
- */
101
- getOrElse(key: string, getValue: () => any): any {
102
- if (this.has(key)) {
103
- return JSON.parse(JSON.stringify(this.get(key)))
104
- }
105
- const value = JSON.parse(JSON.stringify(getValue()))
106
- this.set(key, value)
107
- return value
108
- }
109
-
110
- /**
111
- * Clears all the data in the JSON file.
112
- */
113
- clear(): void {
114
- const time = Date.now()
115
- for (const key of Object.keys(this.#data)) {
116
- this.emit('set', time, key, void 0)
117
- }
118
- this.#data = {} as T
119
- fs.writeFileSync(this.filepath, '{}', 'utf8')
120
- }
121
-
122
- /**
123
- * Assigns new data to the JSON file.
124
- * @param data - The data to assign.
125
- */
126
- assign(data: T): void {
127
- data = JSON.parse(JSON.stringify(data))
128
- const time = Date.now()
129
- for (const [key, value] of Object.entries(data)) {
130
- if (this.#data[key] && JSON.stringify(this.#data[key]) !== JSON.stringify(value)) {
131
- this.emit('set', time, key, value)
132
- }
133
- }
134
- Object.assign(this.#data, data)
135
- this.saveFile()
136
- }
137
-
138
- /**
139
- * Ensures that the directory containing the JSON file exists.
140
- */
141
- ensureDirectoryExists(): void {
142
- fs.mkdirSync(path.dirname(this.filepath), { recursive: true })
143
- }
144
-
145
- /**
146
- * Saves the data to the JSON file.
147
- */
148
- saveFile(): void {
149
- writeJsonFileSync(this.filepath, this.#data, this.prettyPrint)
150
- }
151
-
152
- /**
153
- * Loads the data from the JSON file.
154
- */
155
- loadFile(): void {
156
- this.#data = readJsonFileSync(this.filepath) as T
157
- }
158
- }
@@ -1,117 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
- import readline from 'readline'
4
-
5
- /**
6
- * Represents a log file manager that appends log entries to a file.
7
- * It does not ensure extremely precise ordering of log entries' timestamps if used asynchronously. It logs them as they are received.
8
- */
9
- export class LogFile<T> {
10
- /**
11
- * The path to the log file.
12
- */
13
- filepath: string
14
-
15
- /**
16
- * Creates a new instance of the LogFile class.
17
- * @param filepath - The path to the log file.
18
- */
19
- constructor(filepath: string) {
20
- this.filepath = filepath
21
- this.ensureDirectoryExists()
22
- }
23
-
24
- /**
25
- * Appends a log entry to the log file.
26
- * @param data - The data to be logged.
27
- * @param time - The timestamp of the log entry. Defaults to the current time.
28
- * @returns A promise that resolves when the log entry is appended.
29
- */
30
- async append(data: T, time = Date.now()): Promise<void> {
31
- await fs.promises.appendFile(this.filepath, time + '|' + JSON.stringify(data) + '\n', 'utf8')
32
- }
33
-
34
- /**
35
- * Resets the log file by removing it.
36
- */
37
- reset(): void {
38
- fs.rmSync(this.filepath)
39
- }
40
-
41
- /**
42
- * Reads the log entire file and returns an array of log entries, sorted by timestamp in descending order.
43
- * @returns An array of log entries, sorted by timestamp in descending order.
44
- */
45
- readFile(): [number, T][] {
46
- const data = fs.readFileSync(this.filepath, 'utf8')
47
- const lines = data.split('\n')
48
- const result = [] as [number, T][]
49
- for (const line of lines) {
50
- const i = line.indexOf('|')
51
- if (i === -1) continue
52
- const time = parseInt(line.substring(0, i))
53
- const data = JSON.parse(line.substring(i + 1)) as T
54
- result.push([time, data])
55
- }
56
- return result.sort((a, b) => b[0] - a[0])
57
- }
58
-
59
- /**
60
- * Returns an async generator that yields the timestamps of log entries in the log file.
61
- * @yields The timestamps of log entries.
62
- */
63
- async *keys(): AsyncGenerator<number> {
64
- const fileStream = fs.createReadStream(this.filepath, 'utf8')
65
- const rl = readline.createInterface({
66
- input: fileStream,
67
- crlfDelay: Infinity,
68
- })
69
- for await (const line of rl) {
70
- const i = line.indexOf('|')
71
- const time = parseInt(line.substring(0, i))
72
- yield time
73
- }
74
- }
75
-
76
- /**
77
- * Returns an async generator that yields the values of log entries in the log file.
78
- * @yields The values of log entries.
79
- */
80
- async *values(): AsyncGenerator<T> {
81
- const fileStream = fs.createReadStream(this.filepath, 'utf8')
82
- const rl = readline.createInterface({
83
- input: fileStream,
84
- crlfDelay: Infinity,
85
- })
86
- for await (const line of rl) {
87
- const i = line.indexOf('|')
88
- const data = JSON.parse(line.substring(i + 1)) as T
89
- yield data
90
- }
91
- }
92
-
93
- /**
94
- * Returns an async generator that yields the entries (timestamp and value) of log entries in the log file.
95
- * @yields The entries (timestamp and value) of log entries.
96
- */
97
- async *entries(): AsyncGenerator<[number, T]> {
98
- const fileStream = fs.createReadStream(this.filepath, 'utf8')
99
- const rl = readline.createInterface({
100
- input: fileStream,
101
- crlfDelay: Infinity,
102
- })
103
- for await (const line of rl) {
104
- const i = line.indexOf('|')
105
- const time = parseInt(line.substring(0, i))
106
- const data = JSON.parse(line.substring(i + 1)) as T
107
- yield [time, data]
108
- }
109
- }
110
-
111
- /**
112
- * Ensures that the directory containing the log file exists.
113
- */
114
- ensureDirectoryExists(): void {
115
- fs.mkdirSync(path.dirname(this.filepath), { recursive: true })
116
- }
117
- }
@@ -1,11 +0,0 @@
1
- import path from 'path'
2
- import { absoluteToRelativePath } from './absoluteToRelativePath'
3
-
4
- describe('absoluteToRelativePath', () => {
5
- it('should convert an absolute path to a relative path', () => {
6
- const filepath = path.join(process.cwd(), 'pkg', 'fs', 'src', 'index.ts')
7
- const expected = 'pkg' + path.sep + 'fs' + path.sep + 'src' + path.sep + 'index.ts'
8
- const result = absoluteToRelativePath(filepath)
9
- expect(result).toEqual(expected)
10
- })
11
- })
@@ -1,49 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
- import { appendLineToFile } from './appendLineToFile'
4
-
5
- describe(appendLineToFile.name, () => {
6
- it('should append the line to the file', async () => {
7
- const dirpath = path.join(process.env.TEMP as string, Date.now().toString())
8
- const filepath = path.join(dirpath, 'file.txt')
9
- const line = 'content'
10
- expect(fs.existsSync(dirpath)).toBe(false)
11
- await appendLineToFile(filepath, line)
12
- expect(fs.existsSync(dirpath)).toBe(true)
13
- expect(fs.existsSync(filepath)).toBe(true)
14
- const result = fs.readFileSync(filepath, 'utf8')
15
- expect(result).toBe(line + '\n')
16
- fs.rmSync(dirpath, { recursive: true, force: true })
17
- expect(fs.existsSync(dirpath)).toBe(false)
18
- })
19
-
20
- it('should append the line to the file with a linebreak before inserted string', async () => {
21
- const dirpath = path.join(process.env.TEMP as string, Date.now().toString())
22
- const filepath = path.join(dirpath, 'file.txt')
23
- const line = 'content'
24
- expect(fs.existsSync(dirpath)).toBe(false)
25
- await appendLineToFile(filepath, line, true)
26
- expect(fs.existsSync(dirpath)).toBe(true)
27
- expect(fs.existsSync(filepath)).toBe(true)
28
- const result = fs.readFileSync(filepath, 'utf8')
29
- expect(result).toBe('\n' + line)
30
- fs.rmSync(dirpath, { recursive: true, force: true })
31
- expect(fs.existsSync(dirpath)).toBe(false)
32
- })
33
-
34
- it('should append multiple lines', async () => {
35
- const dirpath = path.join(process.env.TEMP as string, Date.now().toString())
36
- const filepath = path.join(dirpath, 'file.txt')
37
- const line1 = 'content1'
38
- const line2 = 'content2'
39
- expect(fs.existsSync(dirpath)).toBe(false)
40
- await appendLineToFile(filepath, line1)
41
- expect(fs.existsSync(dirpath)).toBe(true)
42
- expect(fs.existsSync(filepath)).toBe(true)
43
- expect(fs.readFileSync(filepath, 'utf8')).toBe(line1 + '\n')
44
- await appendLineToFile(filepath, line2)
45
- expect(fs.readFileSync(filepath, 'utf8')).toBe(line1 + '\n' + line2 + '\n')
46
- fs.rmSync(dirpath, { recursive: true, force: true })
47
- expect(fs.existsSync(dirpath)).toBe(false)
48
- })
49
- })