@naturalcycles/nodejs-lib 12.91.1 → 12.92.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.
@@ -25,6 +25,7 @@ export declare function _pathExists(filePath: string): Promise<boolean>;
25
25
  export declare function _pathExistsSync(filePath: string): boolean;
26
26
  export declare function _ensureDir(dirPath: string): Promise<void>;
27
27
  export declare function _ensureDirSync(dirPath: string): void;
28
+ export declare function _ensureFile(filePath: string): Promise<void>;
28
29
  export declare function _ensureFileSync(filePath: string): void;
29
30
  export declare function _removePath(fileOrDirPath: string): Promise<void>;
30
31
  export declare function _removePathSync(fileOrDirPath: string): void;
@@ -15,7 +15,7 @@ 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._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._outputJsonFile = exports._writeJsonFileSync = exports._writeJsonFile = exports._outputFileSync = exports._outputFile = exports._writeFileSync = exports._writeFile = exports._readJsonFileSync = exports._readJsonFile = 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");
@@ -117,7 +117,31 @@ function _ensureDirSync(dirPath) {
117
117
  });
118
118
  }
119
119
  exports._ensureDirSync = _ensureDirSync;
120
- // Skipping `_ensureFile` (async), until it's needed
120
+ async function _ensureFile(filePath) {
121
+ let stats;
122
+ try {
123
+ stats = await fsp.stat(filePath);
124
+ }
125
+ catch { }
126
+ if (stats?.isFile())
127
+ return;
128
+ const dir = path.dirname(filePath);
129
+ try {
130
+ if (!(await fsp.stat(dir)).isDirectory()) {
131
+ // parent is not a directory
132
+ // This is just to cause an internal ENOTDIR error to be thrown
133
+ await fsp.readdir(dir);
134
+ }
135
+ }
136
+ catch (err) {
137
+ // If the stat call above failed because the directory doesn't exist, create it
138
+ if (err?.code === 'ENOENT')
139
+ return await _ensureDir(dir);
140
+ throw err;
141
+ }
142
+ await fsp.writeFile(filePath, '');
143
+ }
144
+ exports._ensureFile = _ensureFile;
121
145
  function _ensureFileSync(filePath) {
122
146
  let stats;
123
147
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.91.1",
3
+ "version": "12.92.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
package/src/fs/fs.util.ts CHANGED
@@ -130,7 +130,28 @@ export function _ensureDirSync(dirPath: string): void {
130
130
  })
131
131
  }
132
132
 
133
- // Skipping `_ensureFile` (async), until it's needed
133
+ export async function _ensureFile(filePath: string): Promise<void> {
134
+ let stats
135
+ try {
136
+ stats = await fsp.stat(filePath)
137
+ } catch {}
138
+ if (stats?.isFile()) return
139
+
140
+ const dir = path.dirname(filePath)
141
+ try {
142
+ if (!(await fsp.stat(dir)).isDirectory()) {
143
+ // parent is not a directory
144
+ // This is just to cause an internal ENOTDIR error to be thrown
145
+ await fsp.readdir(dir)
146
+ }
147
+ } catch (err) {
148
+ // If the stat call above failed because the directory doesn't exist, create it
149
+ if ((err as any)?.code === 'ENOENT') return await _ensureDir(dir)
150
+ throw err
151
+ }
152
+
153
+ await fsp.writeFile(filePath, '')
154
+ }
134
155
 
135
156
  export function _ensureFileSync(filePath: string): void {
136
157
  let stats