@naturalcycles/nodejs-lib 12.75.1 → 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 +1 -0
- package/dist/fs/del.js +57 -1
- package/dist/fs/index.d.ts +2 -2
- package/dist/fs/index.js +2 -1
- package/package.json +2 -2
- package/src/fs/del.ts +76 -0
- package/src/fs/index.ts +2 -2
package/dist/fs/del.d.ts
CHANGED
package/dist/fs/del.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
const colors_1 = require("../colors");
|
|
@@ -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
|
+
}
|
package/dist/fs/index.d.ts
CHANGED
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/nodejs-lib",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.76.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepare": "husky install",
|
|
6
6
|
"docs-serve": "vuepress dev docs",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@naturalcycles/dev-lib": "^13.0.0",
|
|
43
43
|
"@types/node": "^18.0.0",
|
|
44
44
|
"@types/yargs": "^16.0.0",
|
|
45
|
-
"jest": "^
|
|
45
|
+
"jest": "^29.0.0",
|
|
46
46
|
"nock": "^13.0.2",
|
|
47
47
|
"patch-package": "^6.2.1",
|
|
48
48
|
"prettier": "^2.0.0",
|
package/src/fs/del.ts
CHANGED
|
@@ -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 }
|