@naturalcycles/nodejs-lib 12.74.0 → 12.76.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/dist/fs/del.d.ts CHANGED
@@ -19,3 +19,4 @@ export declare type DelSingleOption = string;
19
19
  * @experimental
20
20
  */
21
21
  export declare function del(_opt: DelOptions | DelSingleOption): Promise<void>;
22
+ export declare function delSync(_opt: DelOptions | DelSingleOption): void;
package/dist/fs/del.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.del = void 0;
3
+ exports.delSync = exports.del = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const fs = require("fs-extra");
6
- const globby = require("globby");
7
6
  const colors_1 = require("../colors");
7
+ const index_1 = require("../index");
8
8
  const DEF_OPT = {
9
9
  patterns: [],
10
10
  concurrency: Number.POSITIVE_INFINITY,
@@ -32,7 +32,7 @@ async function del(_opt) {
32
32
  console.log(opt);
33
33
  }
34
34
  // 1. glob only files, expand dirs, delete
35
- const filenames = await globby(patterns, {
35
+ const filenames = await (0, index_1.globby)(patterns, {
36
36
  dot: true,
37
37
  expandDirectories: true,
38
38
  onlyFiles: true,
@@ -44,7 +44,7 @@ async function del(_opt) {
44
44
  return;
45
45
  await (0, js_lib_1.pMap)(filenames, filepath => fs.remove(filepath), { concurrency });
46
46
  // 2. glob only dirs, expand, delete only empty!
47
- let dirnames = await globby(patterns, {
47
+ let dirnames = await (0, index_1.globby)(patterns, {
48
48
  dot: true,
49
49
  expandDirectories: true,
50
50
  onlyDirectories: true,
@@ -70,6 +70,59 @@ async function del(_opt) {
70
70
  }
71
71
  }
72
72
  exports.del = del;
73
+ function delSync(_opt) {
74
+ const started = Date.now();
75
+ // Convert DelSingleOption to DelOptions
76
+ if (typeof _opt === 'string') {
77
+ _opt = {
78
+ patterns: [_opt],
79
+ };
80
+ }
81
+ const opt = {
82
+ ...DEF_OPT,
83
+ ..._opt,
84
+ };
85
+ const { patterns, verbose, silent, debug, dry } = opt;
86
+ if (debug) {
87
+ console.log(opt);
88
+ }
89
+ // 1. glob only files, expand dirs, delete
90
+ const filenames = index_1.globby.sync(patterns, {
91
+ dot: true,
92
+ expandDirectories: true,
93
+ onlyFiles: true,
94
+ });
95
+ if (verbose || debug || dry) {
96
+ console.log(`Will delete ${(0, colors_1.yellow)(filenames.length)} files:`, filenames);
97
+ }
98
+ if (dry)
99
+ return;
100
+ filenames.forEach(filepath => fs.removeSync(filepath));
101
+ // 2. glob only dirs, expand, delete only empty!
102
+ let dirnames = index_1.globby.sync(patterns, {
103
+ dot: true,
104
+ expandDirectories: true,
105
+ onlyDirectories: true,
106
+ });
107
+ // Add original patterns (if any of them are dirs)
108
+ dirnames = dirnames.concat(patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()));
109
+ const dirnamesSorted = dirnames.sort().reverse();
110
+ // console.log({ dirnamesSorted })
111
+ const deletedDirs = [];
112
+ for (const dirpath of dirnamesSorted) {
113
+ if (isEmptyDirSync(dirpath)) {
114
+ // console.log(`empty dir: ${dirpath}`)
115
+ fs.removeSync(dirpath);
116
+ deletedDirs.push(dirpath);
117
+ }
118
+ }
119
+ if (verbose || debug)
120
+ console.log({ deletedDirs });
121
+ if (!silent) {
122
+ console.log(`del deleted ${(0, colors_1.yellow)(filenames.length)} files and ${(0, colors_1.yellow)(deletedDirs.length)} dirs ${(0, colors_1.dimGrey)((0, js_lib_1._since)(started))}`);
123
+ }
124
+ }
125
+ exports.delSync = delSync;
73
126
  // Improved algorithm:
74
127
  // 1. glob only files, expand dirs, delete
75
128
  // 2. glob only dirs, expand, delete only empty!
@@ -77,3 +130,6 @@ exports.del = del;
77
130
  async function isEmptyDir(dir) {
78
131
  return (await fs.readdir(dir)).length === 0;
79
132
  }
133
+ function isEmptyDirSync(dir) {
134
+ return fs.readdirSync(dir).length === 0;
135
+ }
@@ -1,5 +1,5 @@
1
- import { del, DelOptions } from './del';
1
+ import { del, delSync, DelOptions } from './del';
2
2
  import { json2env, objectToShellExport } from './json2env';
3
3
  import { kpy, KpyOptions, kpySync } from './kpy';
4
4
  export type { KpyOptions, DelOptions };
5
- export { kpy, kpySync, del, objectToShellExport, json2env };
5
+ export { kpy, kpySync, del, delSync, objectToShellExport, json2env };
package/dist/fs/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.json2env = exports.objectToShellExport = exports.del = exports.kpySync = exports.kpy = void 0;
3
+ exports.json2env = exports.objectToShellExport = exports.delSync = exports.del = exports.kpySync = exports.kpy = void 0;
4
4
  const del_1 = require("./del");
5
5
  Object.defineProperty(exports, "del", { enumerable: true, get: function () { return del_1.del; } });
6
+ Object.defineProperty(exports, "delSync", { enumerable: true, get: function () { return del_1.delSync; } });
6
7
  const json2env_1 = require("./json2env");
7
8
  Object.defineProperty(exports, "json2env", { enumerable: true, get: function () { return json2env_1.json2env; } });
8
9
  Object.defineProperty(exports, "objectToShellExport", { enumerable: true, get: function () { return json2env_1.objectToShellExport; } });
package/dist/fs/kpy.js CHANGED
@@ -5,13 +5,13 @@ const path = require("path");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const cpFile = require("cp-file");
7
7
  const fs = require("fs-extra");
8
- const globby = require("globby");
9
8
  const moveFile = require("move-file");
10
9
  const colors_1 = require("../colors");
10
+ const index_1 = require("../index");
11
11
  async function kpy(opt) {
12
12
  const started = Date.now();
13
13
  kpyPrepare(opt);
14
- const filenames = await globby(opt.inputPatterns, {
14
+ const filenames = await (0, index_1.globby)(opt.inputPatterns, {
15
15
  cwd: opt.baseDir,
16
16
  dot: opt.dotfiles,
17
17
  });
@@ -39,7 +39,7 @@ exports.kpy = kpy;
39
39
  function kpySync(opt) {
40
40
  const started = Date.now();
41
41
  kpyPrepare(opt);
42
- const filenames = globby.sync(opt.inputPatterns, {
42
+ const filenames = index_1.globby.sync(opt.inputPatterns, {
43
43
  cwd: opt.baseDir,
44
44
  dot: opt.dotfiles,
45
45
  });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import Ajv from 'ajv';
2
+ import * as fastGlob from 'fast-glob';
3
+ import { Options as FastGlobOptions } from 'fast-glob';
4
+ import * as globby from 'globby';
5
+ import { GlobbyOptions } from 'globby';
2
6
  import { RequestError, TimeoutError } from 'got';
3
7
  import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got';
4
8
  import { AnySchema, ValidationErrorItem } from 'joi';
@@ -67,5 +71,5 @@ export * from './validation/joi/joi.shared.schemas';
67
71
  import { JoiValidationError, JoiValidationErrorData } from './validation/joi/joi.validation.error';
68
72
  import { convert, getValidationResult, isValid, JoiValidationResult, undefinedIfInvalid, validate } from './validation/joi/joi.validation.util';
69
73
  import { runScript, RunScriptOptions } from './script';
70
- export type { RunScriptOptions, JoiValidationErrorData, JoiValidationResult, ValidationErrorItem, ExtendedJoi, SchemaTyped, AnySchema, AnySchemaTyped, ArraySchemaTyped, BooleanSchemaTyped, NumberSchemaTyped, ObjectSchemaTyped, StringSchemaTyped, IDebug, IDebugger, SlackServiceCfg, SlackMessage, SlackMessageProps, SlackApiBody, SlackMessagePrefixHook, ReadableTyped, WritableTyped, TransformTyped, PipelineFromNDJsonFileOptions, PipelineToNDJsonFileOptions, TransformJsonParseOptions, TransformToNDJsonOptions, TransformMapOptions, TransformMapSyncOptions, NDJSONStreamForEachOptions, TransformOptions, TransformMultiThreadedOptions, WorkerClassInterface, WorkerInput, WorkerOutput, TableDiffOptions, InspectAnyOptions, Got, GetGotOptions, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AjvValidationOptions, AjvSchemaCfg, AjvValidationErrorData, };
71
- export { JoiValidationError, validate, getValidationResult, isValid, undefinedIfInvalid, convert, Joi, Debug, SlackService, slackDefaultMessagePrefixHook, ndjsonStreamForEach, pipelineFromNDJsonFile, pipelineToNDJsonFile, NDJsonStats, streamToNDJsonFile, transformJsonParse, bufferReviver, transformToNDJson, transformMap, transformMapSync, transformMultiThreaded, BaseWorkerClass, tableDiff, inspectAny, inspectAnyStringifyFn, RequestError, TimeoutError, _chunkBuffer, Ajv, AjvSchema, AjvValidationError, readJsonSchemas, readAjvSchemas, runScript, };
74
+ export type { GlobbyOptions, FastGlobOptions, RunScriptOptions, JoiValidationErrorData, JoiValidationResult, ValidationErrorItem, ExtendedJoi, SchemaTyped, AnySchema, AnySchemaTyped, ArraySchemaTyped, BooleanSchemaTyped, NumberSchemaTyped, ObjectSchemaTyped, StringSchemaTyped, IDebug, IDebugger, SlackServiceCfg, SlackMessage, SlackMessageProps, SlackApiBody, SlackMessagePrefixHook, ReadableTyped, WritableTyped, TransformTyped, PipelineFromNDJsonFileOptions, PipelineToNDJsonFileOptions, TransformJsonParseOptions, TransformToNDJsonOptions, TransformMapOptions, TransformMapSyncOptions, NDJSONStreamForEachOptions, TransformOptions, TransformMultiThreadedOptions, WorkerClassInterface, WorkerInput, WorkerOutput, TableDiffOptions, InspectAnyOptions, Got, GetGotOptions, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AjvValidationOptions, AjvSchemaCfg, AjvValidationErrorData, };
75
+ export { globby, fastGlob, JoiValidationError, validate, getValidationResult, isValid, undefinedIfInvalid, convert, Joi, Debug, SlackService, slackDefaultMessagePrefixHook, ndjsonStreamForEach, pipelineFromNDJsonFile, pipelineToNDJsonFile, NDJsonStats, streamToNDJsonFile, transformJsonParse, bufferReviver, transformToNDJson, transformMap, transformMapSync, transformMultiThreaded, BaseWorkerClass, tableDiff, inspectAny, inspectAnyStringifyFn, RequestError, TimeoutError, _chunkBuffer, Ajv, AjvSchema, AjvValidationError, readJsonSchemas, readAjvSchemas, runScript, };
package/dist/index.js CHANGED
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runScript = exports.readAjvSchemas = exports.readJsonSchemas = exports.AjvValidationError = exports.AjvSchema = exports.Ajv = exports._chunkBuffer = exports.TimeoutError = exports.RequestError = exports.inspectAnyStringifyFn = exports.inspectAny = exports.tableDiff = exports.BaseWorkerClass = exports.transformMultiThreaded = exports.transformMapSync = exports.transformMap = exports.transformToNDJson = exports.bufferReviver = exports.transformJsonParse = exports.streamToNDJsonFile = exports.NDJsonStats = exports.pipelineToNDJsonFile = exports.pipelineFromNDJsonFile = exports.ndjsonStreamForEach = exports.slackDefaultMessagePrefixHook = exports.SlackService = exports.Debug = exports.Joi = exports.convert = exports.undefinedIfInvalid = exports.isValid = exports.getValidationResult = exports.validate = exports.JoiValidationError = void 0;
3
+ exports.runScript = exports.readAjvSchemas = exports.readJsonSchemas = exports.AjvValidationError = exports.AjvSchema = exports.Ajv = exports._chunkBuffer = exports.TimeoutError = exports.RequestError = exports.inspectAnyStringifyFn = exports.inspectAny = exports.tableDiff = exports.BaseWorkerClass = exports.transformMultiThreaded = exports.transformMapSync = exports.transformMap = exports.transformToNDJson = exports.bufferReviver = exports.transformJsonParse = exports.streamToNDJsonFile = exports.NDJsonStats = exports.pipelineToNDJsonFile = exports.pipelineFromNDJsonFile = exports.ndjsonStreamForEach = exports.slackDefaultMessagePrefixHook = exports.SlackService = exports.Debug = exports.Joi = exports.convert = exports.undefinedIfInvalid = exports.isValid = exports.getValidationResult = exports.validate = exports.JoiValidationError = exports.fastGlob = exports.globby = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ajv_1 = require("ajv");
6
6
  exports.Ajv = ajv_1.default;
7
+ const fastGlob = require("fast-glob");
8
+ exports.fastGlob = fastGlob;
9
+ const globby = require("globby");
10
+ exports.globby = globby;
7
11
  const got_1 = require("got");
8
12
  Object.defineProperty(exports, "RequestError", { enumerable: true, get: function () { return got_1.RequestError; } });
9
13
  Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return got_1.TimeoutError; } });
@@ -4,8 +4,8 @@ exports.secretsDecrypt = void 0;
4
4
  const path = require("path");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const fs = require("fs-extra");
7
- const globby = require("globby");
8
7
  const colors_1 = require("../colors");
8
+ const index_1 = require("../index");
9
9
  const crypto_util_1 = require("../security/crypto.util");
10
10
  // Debug it like this:
11
11
  // yarn tsn ./src/bin/secrets-decrypt.ts --file ./src/test/secrets2.json --jsonMode --encKey MPd/30v0Zcce4I5mfwF4NSXrpTYD9OO4/fIqw6rjNiWp2b1GN9Xm8nQZqr7c9kKSsATqtwe0HkJFDUGzDSow44GDgDICgB1u1rGa5eNqtxnOVGRR+lIinCvN/1OnpjzeoJy2bStXPj1DKx8anMqgA8SoOZdlWRNSkEeZlolru8Ey0ujZo22dfwMyRIEniLcqvBm/iMiAkV82fn/TxYw05GarAoJcrfPeDBvuOXsARnMCyX18qTFL0iojxeTU8JHxr8TX3eXDq9cJJmridEKlwRIAzADwtetI4ttlP8lwJj1pmgsBIN3iqYssZYCkZ3HMV6BoEc7LTI5z/45rKrAT1A==
@@ -17,7 +17,7 @@ const crypto_util_1 = require("../security/crypto.util");
17
17
  function secretsDecrypt(dir, file, encKey, del = false, jsonMode = false) {
18
18
  // If `file` is provided - only this one file is used
19
19
  const patterns = file ? [file] : dir.map(d => `${d}/**/*.enc`);
20
- const filenames = globby.sync(patterns);
20
+ const filenames = index_1.fastGlob.sync(patterns);
21
21
  filenames.forEach(filename => {
22
22
  let plainFilename;
23
23
  if (jsonMode) {
@@ -4,8 +4,8 @@ exports.secretsEncrypt = void 0;
4
4
  const path = require("path");
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const fs = require("fs-extra");
7
- const globby = require("globby");
8
7
  const colors_1 = require("../colors");
8
+ const index_1 = require("../index");
9
9
  const crypto_util_1 = require("../security/crypto.util");
10
10
  /**
11
11
  * Encrypts all files in given directory (except *.enc), saves encrypted versions as filename.ext.enc.
@@ -18,7 +18,7 @@ function secretsEncrypt(pattern, file, encKey, del = false, jsonMode = false) {
18
18
  ...pattern,
19
19
  `!**/*.enc`, // excluding already encoded
20
20
  ];
21
- const filenames = globby.sync(patterns);
21
+ const filenames = index_1.fastGlob.sync(patterns);
22
22
  let encFilename;
23
23
  filenames.forEach(filename => {
24
24
  if (jsonMode) {
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requireFileToExist = exports.requireEnvKeys = void 0;
4
+ const fs = require("fs");
4
5
  require("dotenv/config"); // ensure .env is read before requiring keys
5
- const fs = require("fs-extra");
6
6
  /**
7
7
  * @example
8
8
  *
@@ -22,7 +22,7 @@ function requireEnvKeys(...keys) {
22
22
  }
23
23
  exports.requireEnvKeys = requireEnvKeys;
24
24
  function requireFileToExist(filePath) {
25
- if (!fs.pathExistsSync(filePath)) {
25
+ if (!fs.existsSync(filePath)) {
26
26
  throw new Error(`Required file should exist: ${filePath}`);
27
27
  }
28
28
  }
@@ -1,5 +1,5 @@
1
1
  import { JsonSchema } from '@naturalcycles/js-lib';
2
- import { GlobbyOptions } from 'globby';
2
+ import type { FastGlobOptions } from '../..';
3
3
  import { AjvSchema, AjvSchemaCfg } from './ajvSchema';
4
4
  /**
5
5
  * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
@@ -9,7 +9,7 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema';
9
9
  *
10
10
  * @experimental
11
11
  */
12
- export declare function readJsonSchemas(patterns: string | string[], opt?: GlobbyOptions): JsonSchema[];
12
+ export declare function readJsonSchemas(patterns: string | string[], opt?: FastGlobOptions): JsonSchema[];
13
13
  /**
14
14
  * Reads json schemas from given dir (glob pattern).
15
15
  * Creates new AjvSchema for each of them (ajv validates them upon creation).
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readAjvSchemas = exports.readJsonSchemas = void 0;
4
4
  const fs = require("fs");
5
- const globby = require("globby");
5
+ const __1 = require("../..");
6
6
  const ajvSchema_1 = require("./ajvSchema");
7
7
  /**
8
8
  * Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
@@ -13,7 +13,7 @@ const ajvSchema_1 = require("./ajvSchema");
13
13
  * @experimental
14
14
  */
15
15
  function readJsonSchemas(patterns, opt) {
16
- return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
16
+ return __1.fastGlob.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
17
17
  }
18
18
  exports.readJsonSchemas = readJsonSchemas;
19
19
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.74.0",
3
+ "version": "12.76.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -26,6 +26,7 @@
26
26
  "debug": "^4.1.1",
27
27
  "dotenv": "^16.0.0",
28
28
  "execa": "^5.0.0",
29
+ "fast-glob": "^3.2.11",
29
30
  "fs-extra": "^10.0.0",
30
31
  "globby": "^11.0.0",
31
32
  "got": "^11.0.1",
@@ -38,10 +39,10 @@
38
39
  },
39
40
  "devDependencies": {
40
41
  "@naturalcycles/bench-lib": "^1.0.7",
41
- "@naturalcycles/dev-lib": "^12.0.0",
42
- "@types/node": "^17.0.0",
42
+ "@naturalcycles/dev-lib": "^13.0.0",
43
+ "@types/node": "^18.0.0",
43
44
  "@types/yargs": "^16.0.0",
44
- "jest": "^28.0.3",
45
+ "jest": "^29.0.0",
45
46
  "nock": "^13.0.2",
46
47
  "patch-package": "^6.2.1",
47
48
  "prettier": "^2.0.0",
package/src/fs/del.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { pFilter, pMap, _since } from '@naturalcycles/js-lib'
2
2
  import * as fs from 'fs-extra'
3
- import * as globby from 'globby'
4
3
  import { dimGrey, yellow } from '../colors'
4
+ import { globby } from '../index'
5
5
 
6
6
  export interface DelOptions {
7
7
  /**
@@ -110,6 +110,78 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
110
110
  }
111
111
  }
112
112
 
113
+ export function delSync(_opt: DelOptions | DelSingleOption): void {
114
+ const started = Date.now()
115
+
116
+ // Convert DelSingleOption to DelOptions
117
+ if (typeof _opt === 'string') {
118
+ _opt = {
119
+ patterns: [_opt],
120
+ }
121
+ }
122
+
123
+ const opt = {
124
+ ...DEF_OPT,
125
+ ..._opt,
126
+ }
127
+ const { patterns, verbose, silent, debug, dry } = opt
128
+
129
+ if (debug) {
130
+ console.log(opt)
131
+ }
132
+
133
+ // 1. glob only files, expand dirs, delete
134
+
135
+ const filenames = globby.sync(patterns, {
136
+ dot: true,
137
+ expandDirectories: true,
138
+ onlyFiles: true,
139
+ })
140
+
141
+ if (verbose || debug || dry) {
142
+ console.log(`Will delete ${yellow(filenames.length)} files:`, filenames)
143
+ }
144
+
145
+ if (dry) return
146
+
147
+ filenames.forEach(filepath => fs.removeSync(filepath))
148
+
149
+ // 2. glob only dirs, expand, delete only empty!
150
+ let dirnames = globby.sync(patterns, {
151
+ dot: true,
152
+ expandDirectories: true,
153
+ onlyDirectories: true,
154
+ })
155
+
156
+ // Add original patterns (if any of them are dirs)
157
+ dirnames = dirnames.concat(
158
+ patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()),
159
+ )
160
+
161
+ const dirnamesSorted = dirnames.sort().reverse()
162
+
163
+ // console.log({ dirnamesSorted })
164
+
165
+ const deletedDirs: string[] = []
166
+ for (const dirpath of dirnamesSorted) {
167
+ if (isEmptyDirSync(dirpath)) {
168
+ // console.log(`empty dir: ${dirpath}`)
169
+ fs.removeSync(dirpath)
170
+ deletedDirs.push(dirpath)
171
+ }
172
+ }
173
+
174
+ if (verbose || debug) console.log({ deletedDirs })
175
+
176
+ if (!silent) {
177
+ console.log(
178
+ `del deleted ${yellow(filenames.length)} files and ${yellow(
179
+ deletedDirs.length,
180
+ )} dirs ${dimGrey(_since(started))}`,
181
+ )
182
+ }
183
+ }
184
+
113
185
  // Improved algorithm:
114
186
  // 1. glob only files, expand dirs, delete
115
187
  // 2. glob only dirs, expand, delete only empty!
@@ -118,3 +190,7 @@ export async function del(_opt: DelOptions | DelSingleOption): Promise<void> {
118
190
  async function isEmptyDir(dir: string): Promise<boolean> {
119
191
  return (await fs.readdir(dir)).length === 0
120
192
  }
193
+
194
+ function isEmptyDirSync(dir: string): boolean {
195
+ return fs.readdirSync(dir).length === 0
196
+ }
package/src/fs/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { del, DelOptions } from './del'
1
+ import { del, delSync, DelOptions } from './del'
2
2
  import { json2env, objectToShellExport } from './json2env'
3
3
  import { kpy, KpyOptions, kpySync } from './kpy'
4
4
 
5
5
  export type { KpyOptions, DelOptions }
6
6
 
7
- export { kpy, kpySync, del, objectToShellExport, json2env }
7
+ export { kpy, kpySync, del, delSync, objectToShellExport, json2env }
package/src/fs/kpy.ts CHANGED
@@ -2,9 +2,9 @@ import * as path from 'path'
2
2
  import { _since } from '@naturalcycles/js-lib'
3
3
  import * as cpFile from 'cp-file'
4
4
  import * as fs from 'fs-extra'
5
- import * as globby from 'globby'
6
5
  import * as moveFile from 'move-file'
7
6
  import { boldWhite, dimGrey, grey, yellow } from '../colors'
7
+ import { globby } from '../index'
8
8
 
9
9
  /**
10
10
  * Everything defaults to `undefined`.
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import Ajv from 'ajv'
2
+ import * as fastGlob from 'fast-glob'
3
+ import { Options as FastGlobOptions } from 'fast-glob'
4
+ import * as globby from 'globby'
5
+ import { GlobbyOptions } from 'globby'
2
6
  import { RequestError, TimeoutError } from 'got'
3
7
  import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got'
4
8
  import { AnySchema, ValidationErrorItem } from 'joi'
@@ -111,6 +115,8 @@ import {
111
115
  import { runScript, RunScriptOptions } from './script'
112
116
 
113
117
  export type {
118
+ GlobbyOptions,
119
+ FastGlobOptions,
114
120
  RunScriptOptions,
115
121
  JoiValidationErrorData,
116
122
  JoiValidationResult,
@@ -159,6 +165,8 @@ export type {
159
165
  }
160
166
 
161
167
  export {
168
+ globby,
169
+ fastGlob,
162
170
  JoiValidationError,
163
171
  validate,
164
172
  getValidationResult,
@@ -1,8 +1,8 @@
1
1
  import * as path from 'path'
2
2
  import { _assert } from '@naturalcycles/js-lib'
3
3
  import * as fs from 'fs-extra'
4
- import globby = require('globby')
5
4
  import { dimGrey, yellow } from '../colors'
5
+ import { fastGlob } from '../index'
6
6
  import { decryptObject, decryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface DecryptCLIOptions {
@@ -31,7 +31,7 @@ export function secretsDecrypt(
31
31
  // If `file` is provided - only this one file is used
32
32
  const patterns = file ? [file] : dir.map(d => `${d}/**/*.enc`)
33
33
 
34
- const filenames = globby.sync(patterns)
34
+ const filenames = fastGlob.sync(patterns)
35
35
 
36
36
  filenames.forEach(filename => {
37
37
  let plainFilename
@@ -1,8 +1,8 @@
1
1
  import * as path from 'path'
2
2
  import { _assert } from '@naturalcycles/js-lib'
3
3
  import * as fs from 'fs-extra'
4
- import globby = require('globby')
5
4
  import { dimGrey, yellow } from '../colors'
5
+ import { fastGlob } from '../index'
6
6
  import { encryptObject, encryptRandomIVBuffer } from '../security/crypto.util'
7
7
 
8
8
  export interface EncryptCLIOptions {
@@ -30,7 +30,7 @@ export function secretsEncrypt(
30
30
  ...pattern,
31
31
  `!**/*.enc`, // excluding already encoded
32
32
  ]
33
- const filenames = globby.sync(patterns)
33
+ const filenames = fastGlob.sync(patterns)
34
34
  let encFilename
35
35
 
36
36
  filenames.forEach(filename => {
@@ -1,6 +1,6 @@
1
+ import * as fs from 'fs'
1
2
  import type { ValuesOf } from '@naturalcycles/js-lib'
2
3
  import 'dotenv/config' // ensure .env is read before requiring keys
3
- import * as fs from 'fs-extra'
4
4
 
5
5
  /**
6
6
  * @example
@@ -16,13 +16,13 @@ export function requireEnvKeys<T extends readonly string[]>(
16
16
  return keys.reduce((r, k) => {
17
17
  const v = process.env[k]
18
18
  if (!v) throw new Error(`${k} env variable is required, but missing`)
19
- r[k] = v
19
+ r[k as ValuesOf<T>] = v
20
20
  return r
21
21
  }, {} as { [k in ValuesOf<T>]: string })
22
22
  }
23
23
 
24
24
  export function requireFileToExist(filePath: string): void {
25
- if (!fs.pathExistsSync(filePath)) {
25
+ if (!fs.existsSync(filePath)) {
26
26
  throw new Error(`Required file should exist: ${filePath}`)
27
27
  }
28
28
  }
@@ -1,7 +1,7 @@
1
1
  import * as fs from 'fs'
2
2
  import { JsonSchema } from '@naturalcycles/js-lib'
3
- import { GlobbyOptions } from 'globby'
4
- import * as globby from 'globby'
3
+ import { fastGlob } from '../..'
4
+ import type { FastGlobOptions } from '../..'
5
5
  import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
6
6
 
7
7
  /**
@@ -12,8 +12,8 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
12
12
  *
13
13
  * @experimental
14
14
  */
15
- export function readJsonSchemas(patterns: string | string[], opt?: GlobbyOptions): JsonSchema[] {
16
- return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')))
15
+ export function readJsonSchemas(patterns: string | string[], opt?: FastGlobOptions): JsonSchema[] {
16
+ return fastGlob.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')))
17
17
  }
18
18
 
19
19
  /**
@@ -159,7 +159,7 @@ export class AjvSchema<T = unknown> {
159
159
  const errors = this.validateFunction.errors!
160
160
 
161
161
  const {
162
- objectId = _isObject(obj) ? (obj['id'] as string) : undefined,
162
+ objectId = _isObject(obj) ? (obj['id' as keyof T] as any) : undefined,
163
163
  objectName = this.cfg.objectName,
164
164
  logErrors = this.cfg.logErrors,
165
165
  separator = this.cfg.separator,