@nu-art/ts-common 0.400.7 → 0.400.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/ts-common",
3
- "version": "0.400.7",
3
+ "version": "0.400.8",
4
4
  "description": "js and ts infra",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -7,8 +7,12 @@ export declare const FileSystemUtils: {
7
7
  isFile: (pathToFile: string) => Promise<boolean>;
8
8
  exists: (pathToFile: string) => Promise<boolean>;
9
9
  delete: (pathToFile: string, mustExist?: boolean) => Promise<void>;
10
- write: (pathToFile: string, content: string) => Promise<void>;
11
- read: (pathToFile: string) => Promise<string>;
10
+ write: ((pathToFile: string, content: string) => Promise<void>) & {
11
+ json: <T>(pathToFile: string, value: T) => Promise<void>;
12
+ };
13
+ read: ((pathToFile: string) => Promise<string>) & {
14
+ json: <T>(pathToFile: string, defaultValue?: T) => Promise<T>;
15
+ };
12
16
  copy: (sourcePath: string, targetPath: string) => Promise<void>;
13
17
  template: {
14
18
  read: (pathToFile: string, params: StringMap, pattern?: RegExp) => Promise<string>;
@@ -54,14 +54,29 @@ export const FileSystemUtils = {
54
54
  await assertFile(pathToFile);
55
55
  return _fs.rm(pathToFile);
56
56
  },
57
- write: async (pathToFile, content) => {
57
+ write: Object.assign(async (pathToFile, content) => {
58
58
  await FileSystemUtils.folder.create(resolve(pathToFile, '..'));
59
59
  return _fs.writeFile(pathToFile, content, 'utf-8');
60
- },
61
- read: async (pathToFile) => {
60
+ }, {
61
+ json: async (pathToFile, value) => {
62
+ return await FileSystemUtils.file.write(pathToFile, JSON.stringify(value, null, 2));
63
+ }
64
+ }),
65
+ read: Object.assign(async (pathToFile) => {
62
66
  await assertFile(pathToFile);
63
67
  return _fs.readFile(pathToFile, 'utf-8');
64
- },
68
+ }, {
69
+ json: async (pathToFile, defaultValue) => {
70
+ try {
71
+ return JSON.parse(await FileSystemUtils.file.read(pathToFile));
72
+ }
73
+ catch (e) {
74
+ if (!exists(defaultValue))
75
+ throw e;
76
+ return defaultValue;
77
+ }
78
+ }
79
+ }),
65
80
  copy: async (sourcePath, targetPath) => {
66
81
  await assertExists(targetPath, false, 'File');
67
82
  await assertFile(sourcePath);