@naturalcycles/nodejs-lib 12.92.0 → 12.93.1

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/dist/fs/del.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.delSync = exports.del = void 0;
4
+ const fs = require("node:fs");
5
+ const fsp = require("node:fs/promises");
4
6
  const js_lib_1 = require("@naturalcycles/js-lib");
5
- const fs = require("fs-extra");
6
7
  const colors_1 = require("../colors");
7
8
  const index_1 = require("../index");
8
9
  const DEF_OPT = {
@@ -42,7 +43,7 @@ async function del(_opt) {
42
43
  }
43
44
  if (dry)
44
45
  return;
45
- await (0, js_lib_1.pMap)(filenames, filepath => fs.remove(filepath), { concurrency });
46
+ await (0, js_lib_1.pMap)(filenames, filepath => fsp.unlink(filepath), { concurrency });
46
47
  // 2. glob only dirs, expand, delete only empty!
47
48
  let dirnames = await (0, index_1.globby)(patterns, {
48
49
  dot: true,
@@ -51,7 +52,7 @@ async function del(_opt) {
51
52
  });
52
53
  // Add original patterns (if any of them are dirs)
53
54
  dirnames = dirnames.concat(await (0, js_lib_1.pFilter)(patterns, async (pattern) => {
54
- return (await fs.pathExists(pattern)) && (await fs.lstat(pattern)).isDirectory();
55
+ return (await (0, index_1._pathExists)(pattern)) && (await fsp.lstat(pattern)).isDirectory();
55
56
  }));
56
57
  const dirnamesSorted = dirnames.sort().reverse();
57
58
  // console.log({ dirnamesSorted })
@@ -59,7 +60,7 @@ async function del(_opt) {
59
60
  for await (const dirpath of dirnamesSorted) {
60
61
  if (await isEmptyDir(dirpath)) {
61
62
  // console.log(`empty dir: ${dirpath}`)
62
- await fs.remove(dirpath);
63
+ await fsp.unlink(dirpath);
63
64
  deletedDirs.push(dirpath);
64
65
  }
65
66
  }
@@ -97,7 +98,7 @@ function delSync(_opt) {
97
98
  }
98
99
  if (dry)
99
100
  return;
100
- filenames.forEach(filepath => fs.removeSync(filepath));
101
+ filenames.forEach(filepath => fs.unlinkSync(filepath));
101
102
  // 2. glob only dirs, expand, delete only empty!
102
103
  let dirnames = index_1.globby.sync(patterns, {
103
104
  dot: true,
@@ -105,14 +106,14 @@ function delSync(_opt) {
105
106
  onlyDirectories: true,
106
107
  });
107
108
  // Add original patterns (if any of them are dirs)
108
- dirnames = dirnames.concat(patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()));
109
+ dirnames = dirnames.concat(patterns.filter(p => (0, index_1._pathExistsSync)(p) && fs.lstatSync(p).isDirectory()));
109
110
  const dirnamesSorted = dirnames.sort().reverse();
110
111
  // console.log({ dirnamesSorted })
111
112
  const deletedDirs = [];
112
113
  for (const dirpath of dirnamesSorted) {
113
114
  if (isEmptyDirSync(dirpath)) {
114
115
  // console.log(`empty dir: ${dirpath}`)
115
- fs.removeSync(dirpath);
116
+ fs.unlinkSync(dirpath);
116
117
  deletedDirs.push(dirpath);
117
118
  }
118
119
  }
@@ -128,7 +129,7 @@ exports.delSync = delSync;
128
129
  // 2. glob only dirs, expand, delete only empty!
129
130
  // 3. test each original pattern, if it exists and is directory and is empty - delete
130
131
  async function isEmptyDir(dir) {
131
- return (await fs.readdir(dir)).length === 0;
132
+ return (await fsp.readdir(dir)).length === 0;
132
133
  }
133
134
  function isEmptyDirSync(dir) {
134
135
  return fs.readdirSync(dir).length === 0;
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
- import type { CopyOptions, CopyOptionsSync, MoveOptions } from 'fs-extra';
2
+ /// <reference types="node" />
3
+ import * as fs from 'node:fs';
3
4
  export interface JsonOptions {
4
5
  spaces?: number;
5
6
  }
@@ -11,16 +12,40 @@ export declare function _readFile(filePath: string): Promise<string>;
11
12
  * Convenience wrapper that defaults to utf-8 string output.
12
13
  */
13
14
  export declare function _readFileSync(filePath: string): string;
14
- export declare function _readJsonFile<T = unknown>(filePath: string): Promise<T>;
15
- export declare function _readJsonFileSync<T = unknown>(filePath: string): T;
15
+ export declare function _readJson<T = unknown>(filePath: string): Promise<T>;
16
+ /**
17
+ * @deprecated use _readJson
18
+ */
19
+ export declare const _readJsonFile: typeof _readJson;
20
+ export declare function _readJsonSync<T = unknown>(filePath: string): T;
21
+ /**
22
+ * @deprecated use _readJsonSync
23
+ */
24
+ export declare const _readJsonFileSync: typeof _readJsonSync;
16
25
  export declare function _writeFile(filePath: string, data: string | Buffer): Promise<void>;
17
26
  export declare function _writeFileSync(filePath: string, data: string | Buffer): void;
18
27
  export declare function _outputFile(filePath: string, data: string | Buffer): Promise<void>;
19
28
  export declare function _outputFileSync(filePath: string, data: string | Buffer): void;
20
- export declare function _writeJsonFile(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
21
- export declare function _writeJsonFileSync(filePath: string, data: any, opt?: JsonOptions): void;
22
- export declare function _outputJsonFile(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
23
- export declare function _outputJsonFileSync(filePath: string, data: any, opt?: JsonOptions): void;
29
+ export declare function _writeJson(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
30
+ /**
31
+ * @deprecated use _writeJson
32
+ */
33
+ export declare const _writeJsonFile: typeof _writeJson;
34
+ export declare function _writeJsonSync(filePath: string, data: any, opt?: JsonOptions): void;
35
+ /**
36
+ * @deprecated use _writeJsonSync
37
+ */
38
+ export declare const _writeJsonFileSync: typeof _writeJsonSync;
39
+ export declare function _outputJson(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
40
+ /**
41
+ * @deprecated use _outputJson
42
+ */
43
+ export declare const _outputJsonFile: typeof _outputJson;
44
+ export declare function _outputJsonSync(filePath: string, data: any, opt?: JsonOptions): void;
45
+ /**
46
+ * @deprecated use _outputJsonSync
47
+ */
48
+ export declare const _outputJsonFileSync: typeof _outputJsonSync;
24
49
  export declare function _pathExists(filePath: string): Promise<boolean>;
25
50
  export declare function _pathExistsSync(filePath: string): boolean;
26
51
  export declare function _ensureDir(dirPath: string): Promise<void>;
@@ -31,7 +56,13 @@ export declare function _removePath(fileOrDirPath: string): Promise<void>;
31
56
  export declare function _removePathSync(fileOrDirPath: string): void;
32
57
  export declare function _emptyDir(dirPath: string): Promise<void>;
33
58
  export declare function _emptyDirSync(dirPath: string): void;
34
- export declare function _copyPath(src: string, dest: string, opt?: CopyOptions): Promise<void>;
35
- export declare function _copyPathSync(src: string, dest: string, opt?: CopyOptionsSync): void;
36
- export declare function _movePath(src: string, dest: string, opt?: MoveOptions): Promise<void>;
37
- export declare function _movePathSync(src: string, dest: string, opt?: MoveOptions): void;
59
+ /**
60
+ * Cautious, underlying Node function is currently Experimental.
61
+ */
62
+ export declare function _copyPath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void>;
63
+ /**
64
+ * Cautious, underlying Node function is currently Experimental.
65
+ */
66
+ export declare function _copyPathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void;
67
+ export declare function _movePath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void>;
68
+ export declare function _movePathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void;
@@ -15,12 +15,11 @@ Credit to: fs-extra (https://github.com/jprichardson/node-fs-extra)
15
15
 
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports._movePathSync = exports._movePath = exports._copyPathSync = exports._copyPath = exports._emptyDirSync = exports._emptyDir = exports._removePathSync = exports._removePath = exports._ensureFileSync = exports._ensureFile = exports._ensureDirSync = exports._ensureDir = exports._pathExistsSync = exports._pathExists = exports._outputJsonFileSync = exports._outputJsonFile = exports._writeJsonFileSync = exports._writeJsonFile = exports._outputFileSync = exports._outputFile = exports._writeFileSync = exports._writeFile = exports._readJsonFileSync = exports._readJsonFile = exports._readFileSync = exports._readFile = void 0;
18
+ exports._movePathSync = exports._movePath = exports._copyPathSync = exports._copyPath = exports._emptyDirSync = exports._emptyDir = exports._removePathSync = exports._removePath = exports._ensureFileSync = exports._ensureFile = exports._ensureDirSync = exports._ensureDir = exports._pathExistsSync = exports._pathExists = exports._outputJsonFileSync = exports._outputJsonSync = exports._outputJsonFile = exports._outputJson = exports._writeJsonFileSync = exports._writeJsonSync = exports._writeJsonFile = exports._writeJson = exports._outputFileSync = exports._outputFile = exports._writeFileSync = exports._writeFile = exports._readJsonFileSync = exports._readJsonSync = exports._readJsonFile = exports._readJson = exports._readFileSync = exports._readFile = void 0;
19
19
  const fs = require("node:fs");
20
20
  const fsp = require("node:fs/promises");
21
21
  const path = require("node:path");
22
22
  const js_lib_1 = require("@naturalcycles/js-lib");
23
- const fse = require("fs-extra");
24
23
  /**
25
24
  * Convenience wrapper that defaults to utf-8 string output.
26
25
  */
@@ -35,16 +34,24 @@ function _readFileSync(filePath) {
35
34
  return fs.readFileSync(filePath, 'utf8');
36
35
  }
37
36
  exports._readFileSync = _readFileSync;
38
- async function _readJsonFile(filePath) {
37
+ async function _readJson(filePath) {
39
38
  const str = await fsp.readFile(filePath, 'utf8');
40
39
  return (0, js_lib_1._jsonParse)(str);
41
40
  }
42
- exports._readJsonFile = _readJsonFile;
43
- function _readJsonFileSync(filePath) {
41
+ exports._readJson = _readJson;
42
+ /**
43
+ * @deprecated use _readJson
44
+ */
45
+ exports._readJsonFile = _readJson;
46
+ function _readJsonSync(filePath) {
44
47
  const str = fs.readFileSync(filePath, 'utf8');
45
48
  return (0, js_lib_1._jsonParse)(str);
46
49
  }
47
- exports._readJsonFileSync = _readJsonFileSync;
50
+ exports._readJsonSync = _readJsonSync;
51
+ /**
52
+ * @deprecated use _readJsonSync
53
+ */
54
+ exports._readJsonFileSync = _readJsonSync;
48
55
  async function _writeFile(filePath, data) {
49
56
  await fsp.writeFile(filePath, data);
50
57
  }
@@ -69,26 +76,42 @@ function _outputFileSync(filePath, data) {
69
76
  fs.writeFileSync(filePath, data);
70
77
  }
71
78
  exports._outputFileSync = _outputFileSync;
72
- async function _writeJsonFile(filePath, data, opt) {
79
+ async function _writeJson(filePath, data, opt) {
73
80
  const str = JSON.stringify(data, null, opt?.spaces);
74
81
  await fsp.writeFile(filePath, str);
75
82
  }
76
- exports._writeJsonFile = _writeJsonFile;
77
- function _writeJsonFileSync(filePath, data, opt) {
83
+ exports._writeJson = _writeJson;
84
+ /**
85
+ * @deprecated use _writeJson
86
+ */
87
+ exports._writeJsonFile = _writeJson;
88
+ function _writeJsonSync(filePath, data, opt) {
78
89
  const str = JSON.stringify(data, null, opt?.spaces);
79
90
  fs.writeFileSync(filePath, str);
80
91
  }
81
- exports._writeJsonFileSync = _writeJsonFileSync;
82
- async function _outputJsonFile(filePath, data, opt) {
92
+ exports._writeJsonSync = _writeJsonSync;
93
+ /**
94
+ * @deprecated use _writeJsonSync
95
+ */
96
+ exports._writeJsonFileSync = _writeJsonSync;
97
+ async function _outputJson(filePath, data, opt) {
83
98
  const str = JSON.stringify(data, null, opt?.spaces);
84
99
  await _outputFile(filePath, str);
85
100
  }
86
- exports._outputJsonFile = _outputJsonFile;
87
- function _outputJsonFileSync(filePath, data, opt) {
101
+ exports._outputJson = _outputJson;
102
+ /**
103
+ * @deprecated use _outputJson
104
+ */
105
+ exports._outputJsonFile = _outputJson;
106
+ function _outputJsonSync(filePath, data, opt) {
88
107
  const str = JSON.stringify(data, null, opt?.spaces);
89
108
  _outputFileSync(filePath, str);
90
109
  }
91
- exports._outputJsonFileSync = _outputJsonFileSync;
110
+ exports._outputJsonSync = _outputJsonSync;
111
+ /**
112
+ * @deprecated use _outputJsonSync
113
+ */
114
+ exports._outputJsonFileSync = _outputJsonSync;
92
115
  async function _pathExists(filePath) {
93
116
  try {
94
117
  await fsp.access(filePath);
@@ -197,20 +220,33 @@ function _emptyDirSync(dirPath) {
197
220
  items.forEach(item => _removePathSync(path.join(dirPath, item)));
198
221
  }
199
222
  exports._emptyDirSync = _emptyDirSync;
200
- // copyFile/moveFile - let's keep using fs-extra for now
223
+ /**
224
+ * Cautious, underlying Node function is currently Experimental.
225
+ */
201
226
  async function _copyPath(src, dest, opt) {
202
- await fse.copy(src, dest, opt);
227
+ await fsp.cp(src, dest, {
228
+ recursive: true,
229
+ ...opt,
230
+ });
203
231
  }
204
232
  exports._copyPath = _copyPath;
233
+ /**
234
+ * Cautious, underlying Node function is currently Experimental.
235
+ */
205
236
  function _copyPathSync(src, dest, opt) {
206
- fse.copySync(src, dest, opt);
237
+ fs.cpSync(src, dest, {
238
+ recursive: true,
239
+ ...opt,
240
+ });
207
241
  }
208
242
  exports._copyPathSync = _copyPathSync;
209
243
  async function _movePath(src, dest, opt) {
210
- await fse.move(src, dest, opt);
244
+ await _copyPath(src, dest, opt);
245
+ await _removePath(src);
211
246
  }
212
247
  exports._movePath = _movePath;
213
248
  function _movePathSync(src, dest, opt) {
214
- fse.moveSync(src, dest, opt);
249
+ _copyPathSync(src, dest, opt);
250
+ _removePathSync(src);
215
251
  }
216
252
  exports._movePathSync = _movePathSync;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.objectToGithubActionsEnv = exports.objectToShellExport = exports.json2env = void 0;
4
- const fs = require("fs-extra");
4
+ const fs = require("node:fs");
5
5
  const colors_1 = require("../colors");
6
+ const fs_util_1 = require("./fs.util");
6
7
  const JSON2ENV_OPT_DEF = {
7
8
  saveEnvFile: true,
8
9
  bashEnv: true,
@@ -14,7 +15,7 @@ function json2env(opt) {
14
15
  ...JSON2ENV_OPT_DEF,
15
16
  ...opt,
16
17
  };
17
- if (!fs.pathExistsSync(jsonPath)) {
18
+ if (!(0, fs_util_1._pathExistsSync)(jsonPath)) {
18
19
  if (fail) {
19
20
  throw new Error(`Path doesn't exist: ${jsonPath}`);
20
21
  }
@@ -27,7 +28,7 @@ function json2env(opt) {
27
28
  return;
28
29
  }
29
30
  // read file
30
- const json = fs.readJsonSync(jsonPath);
31
+ const json = (0, fs_util_1._readJsonSync)(jsonPath);
31
32
  const exportStr = objectToShellExport(json, prefix);
32
33
  const githubStr = objectToGithubActionsEnv(json, prefix);
33
34
  if (debug) {
@@ -35,7 +36,7 @@ function json2env(opt) {
35
36
  }
36
37
  if (saveEnvFile) {
37
38
  const shPath = `${jsonPath}.sh`;
38
- fs.writeFileSync(shPath, exportStr);
39
+ (0, fs_util_1._writeFileSync)(shPath, exportStr);
39
40
  if (!silent) {
40
41
  console.log(`json2env created ${(0, colors_1.dimGrey)(shPath)}:`);
41
42
  console.log(exportStr);
package/dist/fs/kpy.js CHANGED
@@ -20,10 +20,12 @@ async function kpy(opt) {
20
20
  const destFilename = path.resolve(opt.outputDir, opt.flat ? basename : filename);
21
21
  if (!opt.dry) {
22
22
  if (opt.move) {
23
- await (0, index_1._movePath)(srcFilename, destFilename, { overwrite });
23
+ await (0, index_1._movePath)(srcFilename, destFilename, {
24
+ force: overwrite,
25
+ });
24
26
  }
25
27
  else {
26
- await (0, index_1._copyPath)(srcFilename, destFilename, { overwrite });
28
+ await (0, index_1._copyPath)(srcFilename, destFilename, { force: overwrite });
27
29
  }
28
30
  }
29
31
  if (opt.verbose) {
@@ -48,10 +50,10 @@ function kpySync(opt) {
48
50
  const destFilename = path.resolve(opt.outputDir, opt.flat ? basename : filename);
49
51
  if (!opt.dry) {
50
52
  if (opt.move) {
51
- (0, index_1._movePathSync)(srcFilename, destFilename, { overwrite });
53
+ (0, index_1._movePathSync)(srcFilename, destFilename, { force: overwrite });
52
54
  }
53
55
  else {
54
- (0, index_1._copyPathSync)(srcFilename, destFilename, { overwrite });
56
+ (0, index_1._copyPathSync)(srcFilename, destFilename, { force: overwrite });
55
57
  }
56
58
  }
57
59
  if (opt.verbose) {
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.secretsDecrypt = void 0;
4
4
  const path = require("node:path");
5
+ const fs = require("node:fs");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
- const fs = require("fs-extra");
7
7
  const colors_1 = require("../colors");
8
8
  const index_1 = require("../index");
9
9
  const crypto_util_1 = require("../security/crypto.util");
@@ -24,8 +24,8 @@ function secretsDecrypt(dir, file, encKey, del = false, jsonMode = false) {
24
24
  (0, js_lib_1._assert)(filename.endsWith('.json'), `${path.basename(filename)} MUST end with '.json'`);
25
25
  (0, js_lib_1._assert)(!filename.endsWith('.plain.json'), `${path.basename(filename)} MUST NOT end with '.plain.json'`);
26
26
  plainFilename = filename.replace('.json', '.plain.json');
27
- const json = (0, crypto_util_1.decryptObject)(JSON.parse(fs.readFileSync(filename, 'utf8')), encKey);
28
- fs.writeFileSync(plainFilename, JSON.stringify(json, null, 2));
27
+ const json = (0, crypto_util_1.decryptObject)((0, index_1._readJsonSync)(filename), encKey);
28
+ (0, index_1._writeJsonSync)(plainFilename, json, { spaces: 2 });
29
29
  }
30
30
  else {
31
31
  const enc = fs.readFileSync(filename);
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.secretsEncrypt = void 0;
4
+ const fs = require("node:fs");
4
5
  const path = require("node:path");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
- const fs = require("fs-extra");
7
7
  const colors_1 = require("../colors");
8
8
  const index_1 = require("../index");
9
9
  const crypto_util_1 = require("../security/crypto.util");
@@ -24,8 +24,8 @@ function secretsEncrypt(pattern, file, encKey, del = false, jsonMode = false) {
24
24
  if (jsonMode) {
25
25
  (0, js_lib_1._assert)(filename.endsWith('.plain.json'), `${path.basename(filename)} MUST end with '.plain.json'`);
26
26
  encFilename = filename.replace('.plain', '');
27
- const json = (0, crypto_util_1.encryptObject)(JSON.parse(fs.readFileSync(filename, 'utf8')), encKey);
28
- fs.writeFileSync(encFilename, JSON.stringify(json, null, 2));
27
+ const json = (0, crypto_util_1.encryptObject)((0, index_1._readJsonSync)(filename), encKey);
28
+ (0, index_1._writeJsonSync)(encFilename, json, { spaces: 2 });
29
29
  }
30
30
  else {
31
31
  const plain = fs.readFileSync(filename);
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipelineFromNDJsonFile = void 0;
4
4
  const node_zlib_1 = require("node:zlib");
5
+ const fs = require("node:fs");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
- const fs = require("fs-extra");
7
7
  const __1 = require("../..");
8
8
  const colors_1 = require("../../colors");
9
9
  const ndjson_model_1 = require("./ndjson.model");
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipelineToNDJsonFile = void 0;
4
4
  const node_zlib_1 = require("node:zlib");
5
+ const fs = require("node:fs");
5
6
  const js_lib_1 = require("@naturalcycles/js-lib");
6
- const fs = require("fs-extra");
7
7
  const __1 = require("../..");
8
8
  const colors_1 = require("../../colors");
9
9
  const ndjson_model_1 = require("./ndjson.model");
@@ -15,12 +15,12 @@ const transformToNDJson_1 = require("./transformToNDJson");
15
15
  */
16
16
  async function pipelineToNDJsonFile(streams, opt) {
17
17
  const { filePath, gzip, protectFromOverwrite = false } = opt;
18
- if (protectFromOverwrite && fs.pathExistsSync(filePath)) {
18
+ if (protectFromOverwrite && (0, __1._pathExistsSync)(filePath)) {
19
19
  throw new js_lib_1.AppError(`pipelineToNDJsonFile: output file exists: ${filePath}`);
20
20
  }
21
21
  const started = Date.now();
22
22
  let rows = 0;
23
- fs.ensureFileSync(filePath);
23
+ (0, __1._ensureFileSync)(filePath);
24
24
  console.log(`>> ${(0, colors_1.grey)(filePath)} started...`);
25
25
  await (0, __1._pipeline)([
26
26
  ...streams,
@@ -12,7 +12,7 @@ const ajvSchema_1 = require("./ajvSchema");
12
12
  * @experimental
13
13
  */
14
14
  function readJsonSchemas(patterns, opt) {
15
- return __1.fastGlob.sync(patterns, opt).map(fileName => (0, __1._readJsonFileSync)(fileName));
15
+ return __1.fastGlob.sync(patterns, opt).map(fileName => (0, __1._readJsonSync)(fileName));
16
16
  }
17
17
  exports.readJsonSchemas = readJsonSchemas;
18
18
  /**
@@ -59,7 +59,7 @@ class AjvSchema {
59
59
  */
60
60
  static readJsonSync(filePath, cfg = {}) {
61
61
  (0, index_1.requireFileToExist)(filePath);
62
- const schema = (0, index_1._readJsonFileSync)(filePath);
62
+ const schema = (0, index_1._readJsonSync)(filePath);
63
63
  return new AjvSchema(schema, cfg);
64
64
  }
65
65
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.92.0",
3
+ "version": "12.93.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -15,7 +15,6 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@naturalcycles/js-lib": "^14.0.0",
18
- "@types/fs-extra": "^11.0.1",
19
18
  "@types/jsonwebtoken": "^9.0.0",
20
19
  "@types/through2-concurrent": "^2.0.0",
21
20
  "ajv": "^8.6.2",
@@ -28,7 +27,6 @@
28
27
  "dotenv": "^16.0.0",
29
28
  "execa": "^5.0.0",
30
29
  "fast-glob": "^3.2.11",
31
- "fs-extra": "^11.0.0",
32
30
  "globby": "^11.0.0",
33
31
  "got": "^11.0.1",
34
32
  "joi": "17.4.2",
package/src/fs/del.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import * as fs from 'node:fs'
2
+ import * as fsp from 'node:fs/promises'
1
3
  import { pFilter, pMap, _since } from '@naturalcycles/js-lib'
2
- import * as fs from 'fs-extra'
3
4
  import { dimGrey, yellow } from '../colors'
4
- import { globby } from '../index'
5
+ import { _pathExists, _pathExistsSync, globby } from '../index'
5
6
 
6
7
  export interface DelOptions {
7
8
  /**
@@ -70,7 +71,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
70
71
 
71
72
  if (dry) return
72
73
 
73
- await pMap(filenames, filepath => fs.remove(filepath), { concurrency })
74
+ await pMap(filenames, filepath => fsp.unlink(filepath), { concurrency })
74
75
 
75
76
  // 2. glob only dirs, expand, delete only empty!
76
77
  let dirnames = await globby(patterns, {
@@ -82,7 +83,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
82
83
  // Add original patterns (if any of them are dirs)
83
84
  dirnames = dirnames.concat(
84
85
  await pFilter(patterns, async pattern => {
85
- return (await fs.pathExists(pattern)) && (await fs.lstat(pattern)).isDirectory()
86
+ return (await _pathExists(pattern)) && (await fsp.lstat(pattern)).isDirectory()
86
87
  }),
87
88
  )
88
89
 
@@ -94,7 +95,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
94
95
  for await (const dirpath of dirnamesSorted) {
95
96
  if (await isEmptyDir(dirpath)) {
96
97
  // console.log(`empty dir: ${dirpath}`)
97
- await fs.remove(dirpath)
98
+ await fsp.unlink(dirpath)
98
99
  deletedDirs.push(dirpath)
99
100
  }
100
101
  }
@@ -144,7 +145,7 @@ export function delSync(_opt: DelOptions | DelSingleOption): void {
144
145
 
145
146
  if (dry) return
146
147
 
147
- filenames.forEach(filepath => fs.removeSync(filepath))
148
+ filenames.forEach(filepath => fs.unlinkSync(filepath))
148
149
 
149
150
  // 2. glob only dirs, expand, delete only empty!
150
151
  let dirnames = globby.sync(patterns, {
@@ -155,7 +156,7 @@ export function delSync(_opt: DelOptions | DelSingleOption): void {
155
156
 
156
157
  // Add original patterns (if any of them are dirs)
157
158
  dirnames = dirnames.concat(
158
- patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()),
159
+ patterns.filter(p => _pathExistsSync(p) && fs.lstatSync(p).isDirectory()),
159
160
  )
160
161
 
161
162
  const dirnamesSorted = dirnames.sort().reverse()
@@ -166,7 +167,7 @@ export function delSync(_opt: DelOptions | DelSingleOption): void {
166
167
  for (const dirpath of dirnamesSorted) {
167
168
  if (isEmptyDirSync(dirpath)) {
168
169
  // console.log(`empty dir: ${dirpath}`)
169
- fs.removeSync(dirpath)
170
+ fs.unlinkSync(dirpath)
170
171
  deletedDirs.push(dirpath)
171
172
  }
172
173
  }
@@ -188,7 +189,7 @@ export function delSync(_opt: DelOptions | DelSingleOption): void {
188
189
  // 3. test each original pattern, if it exists and is directory and is empty - delete
189
190
 
190
191
  async function isEmptyDir(dir: string): Promise<boolean> {
191
- return (await fs.readdir(dir)).length === 0
192
+ return (await fsp.readdir(dir)).length === 0
192
193
  }
193
194
 
194
195
  function isEmptyDirSync(dir: string): boolean {
package/src/fs/fs.util.ts CHANGED
@@ -18,8 +18,6 @@ import * as fs from 'node:fs'
18
18
  import * as fsp from 'node:fs/promises'
19
19
  import * as path from 'node:path'
20
20
  import { _jsonParse } from '@naturalcycles/js-lib'
21
- import type { CopyOptions, CopyOptionsSync, MoveOptions } from 'fs-extra'
22
- import * as fse from 'fs-extra'
23
21
 
24
22
  export interface JsonOptions {
25
23
  spaces?: number
@@ -39,16 +37,26 @@ export function _readFileSync(filePath: string): string {
39
37
  return fs.readFileSync(filePath, 'utf8')
40
38
  }
41
39
 
42
- export async function _readJsonFile<T = unknown>(filePath: string): Promise<T> {
40
+ export async function _readJson<T = unknown>(filePath: string): Promise<T> {
43
41
  const str = await fsp.readFile(filePath, 'utf8')
44
42
  return _jsonParse(str)
45
43
  }
46
44
 
47
- export function _readJsonFileSync<T = unknown>(filePath: string): T {
45
+ /**
46
+ * @deprecated use _readJson
47
+ */
48
+ export const _readJsonFile = _readJson
49
+
50
+ export function _readJsonSync<T = unknown>(filePath: string): T {
48
51
  const str = fs.readFileSync(filePath, 'utf8')
49
52
  return _jsonParse(str)
50
53
  }
51
54
 
55
+ /**
56
+ * @deprecated use _readJsonSync
57
+ */
58
+ export const _readJsonFileSync = _readJsonSync
59
+
52
60
  export async function _writeFile(filePath: string, data: string | Buffer): Promise<void> {
53
61
  await fsp.writeFile(filePath, data)
54
62
  }
@@ -75,34 +83,46 @@ export function _outputFileSync(filePath: string, data: string | Buffer): void {
75
83
  fs.writeFileSync(filePath, data)
76
84
  }
77
85
 
78
- export async function _writeJsonFile(
79
- filePath: string,
80
- data: any,
81
- opt?: JsonOptions,
82
- ): Promise<void> {
86
+ export async function _writeJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
83
87
  const str = JSON.stringify(data, null, opt?.spaces)
84
88
  await fsp.writeFile(filePath, str)
85
89
  }
86
90
 
87
- export function _writeJsonFileSync(filePath: string, data: any, opt?: JsonOptions): void {
91
+ /**
92
+ * @deprecated use _writeJson
93
+ */
94
+ export const _writeJsonFile = _writeJson
95
+
96
+ export function _writeJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
88
97
  const str = JSON.stringify(data, null, opt?.spaces)
89
98
  fs.writeFileSync(filePath, str)
90
99
  }
91
100
 
92
- export async function _outputJsonFile(
93
- filePath: string,
94
- data: any,
95
- opt?: JsonOptions,
96
- ): Promise<void> {
101
+ /**
102
+ * @deprecated use _writeJsonSync
103
+ */
104
+ export const _writeJsonFileSync = _writeJsonSync
105
+
106
+ export async function _outputJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
97
107
  const str = JSON.stringify(data, null, opt?.spaces)
98
108
  await _outputFile(filePath, str)
99
109
  }
100
110
 
101
- export function _outputJsonFileSync(filePath: string, data: any, opt?: JsonOptions): void {
111
+ /**
112
+ * @deprecated use _outputJson
113
+ */
114
+ export const _outputJsonFile = _outputJson
115
+
116
+ export function _outputJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
102
117
  const str = JSON.stringify(data, null, opt?.spaces)
103
118
  _outputFileSync(filePath, str)
104
119
  }
105
120
 
121
+ /**
122
+ * @deprecated use _outputJsonSync
123
+ */
124
+ export const _outputJsonFileSync = _outputJsonSync
125
+
106
126
  export async function _pathExists(filePath: string): Promise<boolean> {
107
127
  try {
108
128
  await fsp.access(filePath)
@@ -206,20 +226,32 @@ export function _emptyDirSync(dirPath: string): void {
206
226
  items.forEach(item => _removePathSync(path.join(dirPath, item)))
207
227
  }
208
228
 
209
- // copyFile/moveFile - let's keep using fs-extra for now
210
-
211
- export async function _copyPath(src: string, dest: string, opt?: CopyOptions): Promise<void> {
212
- await fse.copy(src, dest, opt)
229
+ /**
230
+ * Cautious, underlying Node function is currently Experimental.
231
+ */
232
+ export async function _copyPath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void> {
233
+ await fsp.cp(src, dest, {
234
+ recursive: true,
235
+ ...opt,
236
+ })
213
237
  }
214
238
 
215
- export function _copyPathSync(src: string, dest: string, opt?: CopyOptionsSync): void {
216
- fse.copySync(src, dest, opt)
239
+ /**
240
+ * Cautious, underlying Node function is currently Experimental.
241
+ */
242
+ export function _copyPathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void {
243
+ fs.cpSync(src, dest, {
244
+ recursive: true,
245
+ ...opt,
246
+ })
217
247
  }
218
248
 
219
- export async function _movePath(src: string, dest: string, opt?: MoveOptions): Promise<void> {
220
- await fse.move(src, dest, opt)
249
+ export async function _movePath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void> {
250
+ await _copyPath(src, dest, opt)
251
+ await _removePath(src)
221
252
  }
222
253
 
223
- export function _movePathSync(src: string, dest: string, opt?: MoveOptions): void {
224
- fse.moveSync(src, dest, opt)
254
+ export function _movePathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void {
255
+ _copyPathSync(src, dest, opt)
256
+ _removePathSync(src)
225
257
  }
@@ -1,5 +1,6 @@
1
- import * as fs from 'fs-extra'
1
+ import * as fs from 'node:fs'
2
2
  import { dimGrey } from '../colors'
3
+ import { _pathExistsSync, _readJsonSync, _writeFileSync } from './fs.util'
3
4
 
4
5
  export interface Json2EnvOptions {
5
6
  jsonPath: string
@@ -42,7 +43,7 @@ export function json2env(opt: Json2EnvOptions): void {
42
43
  ...opt,
43
44
  }
44
45
 
45
- if (!fs.pathExistsSync(jsonPath)) {
46
+ if (!_pathExistsSync(jsonPath)) {
46
47
  if (fail) {
47
48
  throw new Error(`Path doesn't exist: ${jsonPath}`)
48
49
  }
@@ -59,7 +60,7 @@ export function json2env(opt: Json2EnvOptions): void {
59
60
  }
60
61
 
61
62
  // read file
62
- const json = fs.readJsonSync(jsonPath)
63
+ const json = _readJsonSync(jsonPath)
63
64
 
64
65
  const exportStr = objectToShellExport(json, prefix)
65
66
  const githubStr = objectToGithubActionsEnv(json, prefix)
@@ -70,7 +71,7 @@ export function json2env(opt: Json2EnvOptions): void {
70
71
 
71
72
  if (saveEnvFile) {
72
73
  const shPath = `${jsonPath}.sh`
73
- fs.writeFileSync(shPath, exportStr)
74
+ _writeFileSync(shPath, exportStr)
74
75
 
75
76
  if (!silent) {
76
77
  console.log(`json2env created ${dimGrey(shPath)}:`)
package/src/fs/kpy.ts CHANGED
@@ -72,9 +72,11 @@ export async function kpy(opt: KpyOptions): Promise<void> {
72
72
 
73
73
  if (!opt.dry) {
74
74
  if (opt.move) {
75
- await _movePath(srcFilename, destFilename, { overwrite })
75
+ await _movePath(srcFilename, destFilename, {
76
+ force: overwrite,
77
+ })
76
78
  } else {
77
- await _copyPath(srcFilename, destFilename, { overwrite })
79
+ await _copyPath(srcFilename, destFilename, { force: overwrite })
78
80
  }
79
81
  }
80
82
 
@@ -108,9 +110,9 @@ export function kpySync(opt: KpyOptions): void {
108
110
 
109
111
  if (!opt.dry) {
110
112
  if (opt.move) {
111
- _movePathSync(srcFilename, destFilename, { overwrite })
113
+ _movePathSync(srcFilename, destFilename, { force: overwrite })
112
114
  } else {
113
- _copyPathSync(srcFilename, destFilename, { overwrite })
115
+ _copyPathSync(srcFilename, destFilename, { force: overwrite })
114
116
  }
115
117
  }
116
118
 
@@ -1,8 +1,8 @@
1
1
  import * as path from 'node:path'
2
+ import * as fs from 'node:fs'
2
3
  import { _assert } from '@naturalcycles/js-lib'
3
- import * as fs from 'fs-extra'
4
4
  import { dimGrey, yellow } from '../colors'
5
- import { fastGlob } from '../index'
5
+ import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
6
6
  import { decryptObject, decryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface DecryptCLIOptions {
@@ -44,9 +44,9 @@ export function secretsDecrypt(
44
44
  )
45
45
  plainFilename = filename.replace('.json', '.plain.json')
46
46
 
47
- const json = decryptObject(JSON.parse(fs.readFileSync(filename, 'utf8')), encKey)
47
+ const json = decryptObject(_readJsonSync(filename), encKey)
48
48
 
49
- fs.writeFileSync(plainFilename, JSON.stringify(json, null, 2))
49
+ _writeJsonSync(plainFilename, json, { spaces: 2 })
50
50
  } else {
51
51
  const enc = fs.readFileSync(filename)
52
52
  const plain = decryptRandomIVBuffer(enc, encKey)
@@ -1,8 +1,8 @@
1
+ import * as fs from 'node:fs'
1
2
  import * as path from 'node:path'
2
3
  import { _assert } from '@naturalcycles/js-lib'
3
- import * as fs from 'fs-extra'
4
4
  import { dimGrey, yellow } from '../colors'
5
- import { fastGlob } from '../index'
5
+ import { _readJsonSync, _writeJsonSync, fastGlob } from '../index'
6
6
  import { encryptObject, encryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface EncryptCLIOptions {
@@ -41,9 +41,9 @@ export function secretsEncrypt(
41
41
  )
42
42
  encFilename = filename.replace('.plain', '')
43
43
 
44
- const json = encryptObject(JSON.parse(fs.readFileSync(filename, 'utf8')), encKey)
44
+ const json = encryptObject(_readJsonSync(filename), encKey)
45
45
 
46
- fs.writeFileSync(encFilename, JSON.stringify(json, null, 2))
46
+ _writeJsonSync(encFilename, json, { spaces: 2 })
47
47
  } else {
48
48
  const plain = fs.readFileSync(filename)
49
49
  const enc = encryptRandomIVBuffer(plain, encKey)
@@ -122,7 +122,7 @@ export function secretOptional<T = string>(k: string, parseJson = false): T | un
122
122
  if (!v) return
123
123
 
124
124
  if (parseJson) {
125
- v = _jsonParseIfPossible(v) as any
125
+ v = _jsonParseIfPossible(v)
126
126
  }
127
127
 
128
128
  return v as T
@@ -1,6 +1,6 @@
1
1
  import { createUnzip, ZlibOptions } from 'node:zlib'
2
+ import * as fs from 'node:fs'
2
3
  import { _hb } from '@naturalcycles/js-lib'
3
- import * as fs from 'fs-extra'
4
4
  import { transformTap, _pipeline, transformSplit } from '../..'
5
5
  import { dimWhite, grey } from '../../colors'
6
6
  import { NDJsonStats } from './ndjson.model'
@@ -1,7 +1,7 @@
1
1
  import { createGzip, ZlibOptions } from 'node:zlib'
2
+ import * as fs from 'node:fs'
2
3
  import { AppError } from '@naturalcycles/js-lib'
3
- import * as fs from 'fs-extra'
4
- import { transformTap, _pipeline } from '../..'
4
+ import { transformTap, _pipeline, _pathExistsSync, _ensureFileSync } from '../..'
5
5
  import { grey } from '../../colors'
6
6
  import { NDJsonStats } from './ndjson.model'
7
7
  import { transformToNDJson, TransformToNDJsonOptions } from './transformToNDJson'
@@ -37,14 +37,14 @@ export async function pipelineToNDJsonFile(
37
37
  ): Promise<NDJsonStats> {
38
38
  const { filePath, gzip, protectFromOverwrite = false } = opt
39
39
 
40
- if (protectFromOverwrite && fs.pathExistsSync(filePath)) {
40
+ if (protectFromOverwrite && _pathExistsSync(filePath)) {
41
41
  throw new AppError(`pipelineToNDJsonFile: output file exists: ${filePath}`)
42
42
  }
43
43
 
44
44
  const started = Date.now()
45
45
  let rows = 0
46
46
 
47
- fs.ensureFileSync(filePath)
47
+ _ensureFileSync(filePath)
48
48
 
49
49
  console.log(`>> ${grey(filePath)} started...`)
50
50
 
@@ -1,5 +1,5 @@
1
1
  import { JsonSchema } from '@naturalcycles/js-lib'
2
- import { _readJsonFileSync, fastGlob } from '../..'
2
+ import { _readJsonSync, fastGlob } from '../..'
3
3
  import type { FastGlobOptions } from '../..'
4
4
  import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
5
5
 
@@ -12,7 +12,7 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
12
12
  * @experimental
13
13
  */
14
14
  export function readJsonSchemas(patterns: string | string[], opt?: FastGlobOptions): JsonSchema[] {
15
- return fastGlob.sync(patterns, opt).map(fileName => _readJsonFileSync(fileName))
15
+ return fastGlob.sync(patterns, opt).map(fileName => _readJsonSync(fileName))
16
16
  }
17
17
 
18
18
  /**
@@ -8,7 +8,7 @@ import {
8
8
  CommonLogger,
9
9
  } from '@naturalcycles/js-lib'
10
10
  import Ajv, { ValidateFunction } from 'ajv'
11
- import { _readJsonFileSync, inspectAny, requireFileToExist } from '../../index'
11
+ import { _readJsonSync, inspectAny, requireFileToExist } from '../../index'
12
12
  import { AjvValidationError } from './ajvValidationError'
13
13
  import { getAjv } from './getAjv'
14
14
 
@@ -131,7 +131,7 @@ export class AjvSchema<T = unknown> {
131
131
  cfg: Partial<AjvSchemaCfg> = {},
132
132
  ): AjvSchema<T> {
133
133
  requireFileToExist(filePath)
134
- const schema = _readJsonFileSync<JsonSchema<T>>(filePath)
134
+ const schema = _readJsonSync<JsonSchema<T>>(filePath)
135
135
  return new AjvSchema<T>(schema, cfg)
136
136
  }
137
137