@naturalcycles/nodejs-lib 14.2.0 → 14.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fs/fs2.d.ts CHANGED
@@ -41,6 +41,7 @@ declare class FS2 {
41
41
  outputFileAsync(filePath: string, data: string | Buffer): Promise<void>;
42
42
  pathExists(filePath: string): boolean;
43
43
  pathExistsAsync(filePath: string): Promise<boolean>;
44
+ requireFileToExist(filePath: string): void;
44
45
  ensureDir(dirPath: string): void;
45
46
  ensureDirAsync(dirPath: string): Promise<void>;
46
47
  ensureFile(filePath: string): void;
package/dist/fs/fs2.js CHANGED
@@ -21,7 +21,6 @@ import { _isTruthy, _jsonParse } from '@naturalcycles/js-lib';
21
21
  import yaml from 'js-yaml';
22
22
  import { transformToNDJson } from '../stream/ndjson/transformToNDJson.js';
23
23
  import { transformSplitOnNewline } from '../stream/transform/transformSplit.js';
24
- import { requireFileToExist } from '../util/env.util.js';
25
24
  /**
26
25
  * fs2 conveniently groups filesystem functions together.
27
26
  * Supposed to be almost a drop-in replacement for these things together:
@@ -137,6 +136,11 @@ class FS2 {
137
136
  return false;
138
137
  }
139
138
  }
139
+ requireFileToExist(filePath) {
140
+ if (!fs.existsSync(filePath)) {
141
+ throw new Error(`Required file should exist: ${filePath}`);
142
+ }
143
+ }
140
144
  ensureDir(dirPath) {
141
145
  fs.mkdirSync(dirPath, {
142
146
  mode: 0o777,
@@ -303,7 +307,7 @@ class FS2 {
303
307
  ])
304
308
  */
305
309
  createReadStreamAsNDJSON(inputPath) {
306
- requireFileToExist(inputPath);
310
+ this.requireFileToExist(inputPath);
307
311
  let stream = fs
308
312
  .createReadStream(inputPath, {
309
313
  highWaterMark: 64 * 1024, // no observed speedup
@@ -76,7 +76,7 @@ function checkAndlogEnvironment() {
76
76
  '\n' +
77
77
  formatObject({
78
78
  NODE_OPTIONS: NODE_OPTIONS || 'not defined',
79
- TZ,
79
+ TZ: TZ || 'not defined',
80
80
  })));
81
81
  if (!NODE_OPTIONS) {
82
82
  console.warn(`NODE_OPTIONS env variable is not defined. You may run into out-of-memory issues when running memory-intensive scripts. It's recommended to set it to:\n--max-old-space-size=12000`);
@@ -84,15 +84,17 @@ function checkAndlogEnvironment() {
84
84
  else if (NODE_OPTIONS.includes('max_old')) {
85
85
  console.warn(`It looks like you're using "max_old_space_size" syntax with underscores instead of dashes - it's WRONG and doesn't work in environment variables. Strongly advised to rename it to "max-old-space-size"`);
86
86
  }
87
- if (!TZ) {
88
- console.error([
89
- '!!! TZ environment variable is required to be set, but was not set.',
90
- 'The runScript will exit and not continue further because of that,',
91
- 'please ensure the TZ variable and try again.',
92
- 'If you are running locally, you can add TZ=UTC to the local .env file.',
93
- ].join('\n'));
94
- process.exit(1);
95
- }
87
+ // if (!TZ) {
88
+ // console.error(
89
+ // [
90
+ // '!!! TZ environment variable is required to be set, but was not set.',
91
+ // 'The runScript will exit and not continue further because of that,',
92
+ // 'please ensure the TZ variable and try again.',
93
+ // 'If you are running locally, you can add TZ=UTC to the local .env file.',
94
+ // ].join('\n'),
95
+ // )
96
+ // process.exit(1)
97
+ // }
96
98
  }
97
99
  function formatObject(obj) {
98
100
  return Object.entries(obj)
@@ -10,4 +10,7 @@ import type { ValuesOf } from '@naturalcycles/js-lib';
10
10
  export declare function requireEnvKeys<T extends readonly string[]>(...keys: T): {
11
11
  [k in ValuesOf<T>]: string;
12
12
  };
13
+ /**
14
+ * @deprecated use fs2.requireFileToExist
15
+ */
13
16
  export declare function requireFileToExist(filePath: string): void;
@@ -1,5 +1,5 @@
1
1
  import 'dotenv/config'; // ensure .env is read before requiring keys
2
- import { existsSync } from 'node:fs';
2
+ import { fs2 } from '../fs/fs2.js';
3
3
  /**
4
4
  * @example
5
5
  *
@@ -17,8 +17,9 @@ export function requireEnvKeys(...keys) {
17
17
  return r;
18
18
  }, {});
19
19
  }
20
+ /**
21
+ * @deprecated use fs2.requireFileToExist
22
+ */
20
23
  export function requireFileToExist(filePath) {
21
- if (!existsSync(filePath)) {
22
- throw new Error(`Required file should exist: ${filePath}`);
23
- }
24
+ fs2.requireFileToExist(filePath);
24
25
  }
@@ -1,7 +1,6 @@
1
1
  import { _filterNullishValues, _isObject, _substringBefore, JsonSchemaAnyBuilder, } from '@naturalcycles/js-lib';
2
2
  import { fs2 } from '../../fs/fs2.js';
3
3
  import { _inspect } from '../../string/inspect.js';
4
- import { requireFileToExist } from '../../util/env.util.js';
5
4
  import { AjvValidationError } from './ajvValidationError.js';
6
5
  import { getAjv } from './getAjv.js';
7
6
  /**
@@ -58,7 +57,7 @@ export class AjvSchema {
58
57
  * Convenient method that just does fs.readFileSync for you.
59
58
  */
60
59
  static readJsonSync(filePath, cfg = {}) {
61
- requireFileToExist(filePath);
60
+ fs2.requireFileToExist(filePath);
62
61
  const schema = fs2.readJson(filePath);
63
62
  return new AjvSchema(schema, cfg);
64
63
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "14.2.0",
4
+ "version": "14.3.0",
5
5
  "scripts": {
6
6
  "prepare": "husky",
7
7
  "build": "dev-lib build",
package/src/fs/fs2.ts CHANGED
@@ -25,7 +25,6 @@ import yaml from 'js-yaml'
25
25
  import { transformToNDJson } from '../stream/ndjson/transformToNDJson.js'
26
26
  import type { ReadableTyped, TransformTyped } from '../stream/stream.model.js'
27
27
  import { transformSplitOnNewline } from '../stream/transform/transformSplit.js'
28
- import { requireFileToExist } from '../util/env.util.js'
29
28
 
30
29
  /**
31
30
  * fs2 conveniently groups filesystem functions together.
@@ -168,6 +167,12 @@ class FS2 {
168
167
  }
169
168
  }
170
169
 
170
+ requireFileToExist(filePath: string): void {
171
+ if (!fs.existsSync(filePath)) {
172
+ throw new Error(`Required file should exist: ${filePath}`)
173
+ }
174
+ }
175
+
171
176
  ensureDir(dirPath: string): void {
172
177
  fs.mkdirSync(dirPath, {
173
178
  mode: 0o777,
@@ -350,7 +355,7 @@ class FS2 {
350
355
  ])
351
356
  */
352
357
  createReadStreamAsNDJSON<ROW = any>(inputPath: string): ReadableTyped<ROW> {
353
- requireFileToExist(inputPath)
358
+ this.requireFileToExist(inputPath)
354
359
 
355
360
  let stream: ReadableTyped<ROW> = fs
356
361
  .createReadStream(inputPath, {
@@ -113,7 +113,7 @@ function checkAndlogEnvironment(): void {
113
113
  '\n' +
114
114
  formatObject({
115
115
  NODE_OPTIONS: NODE_OPTIONS || 'not defined',
116
- TZ,
116
+ TZ: TZ || 'not defined',
117
117
  }),
118
118
  ),
119
119
  )
@@ -128,17 +128,17 @@ function checkAndlogEnvironment(): void {
128
128
  )
129
129
  }
130
130
 
131
- if (!TZ) {
132
- console.error(
133
- [
134
- '!!! TZ environment variable is required to be set, but was not set.',
135
- 'The runScript will exit and not continue further because of that,',
136
- 'please ensure the TZ variable and try again.',
137
- 'If you are running locally, you can add TZ=UTC to the local .env file.',
138
- ].join('\n'),
139
- )
140
- process.exit(1)
141
- }
131
+ // if (!TZ) {
132
+ // console.error(
133
+ // [
134
+ // '!!! TZ environment variable is required to be set, but was not set.',
135
+ // 'The runScript will exit and not continue further because of that,',
136
+ // 'please ensure the TZ variable and try again.',
137
+ // 'If you are running locally, you can add TZ=UTC to the local .env file.',
138
+ // ].join('\n'),
139
+ // )
140
+ // process.exit(1)
141
+ // }
142
142
  }
143
143
 
144
144
  function formatObject(obj: AnyObject): string {
@@ -1,6 +1,6 @@
1
1
  import 'dotenv/config' // ensure .env is read before requiring keys
2
- import { existsSync } from 'node:fs'
3
2
  import type { ValuesOf } from '@naturalcycles/js-lib'
3
+ import { fs2 } from '../fs/fs2.js'
4
4
 
5
5
  /**
6
6
  * @example
@@ -24,8 +24,9 @@ export function requireEnvKeys<T extends readonly string[]>(
24
24
  )
25
25
  }
26
26
 
27
+ /**
28
+ * @deprecated use fs2.requireFileToExist
29
+ */
27
30
  export function requireFileToExist(filePath: string): void {
28
- if (!existsSync(filePath)) {
29
- throw new Error(`Required file should exist: ${filePath}`)
30
- }
31
+ fs2.requireFileToExist(filePath)
31
32
  }
@@ -8,7 +8,6 @@ import {
8
8
  import type { Ajv, ValidateFunction } from 'ajv'
9
9
  import { fs2 } from '../../fs/fs2.js'
10
10
  import { _inspect } from '../../string/inspect.js'
11
- import { requireFileToExist } from '../../util/env.util.js'
12
11
  import { AjvValidationError } from './ajvValidationError.js'
13
12
  import { getAjv } from './getAjv.js'
14
13
 
@@ -133,7 +132,7 @@ export class AjvSchema<T = unknown> {
133
132
  filePath: string,
134
133
  cfg: Partial<AjvSchemaCfg> = {},
135
134
  ): AjvSchema<T> {
136
- requireFileToExist(filePath)
135
+ fs2.requireFileToExist(filePath)
137
136
  const schema = fs2.readJson<JsonSchema<T>>(filePath)
138
137
  return new AjvSchema<T>(schema, cfg)
139
138
  }