@stacksjs/storage 0.57.4 โ 0.58.20
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/README.md +17 -17
- package/dist/index.js +798 -0
- package/package.json +44 -27
- package/LICENSE.md +0 -21
- package/dist/index.d.ts +0 -45
- package/dist/index.mjs +0 -144
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ This package contains the Stacks File System source.
|
|
|
12
12
|
## ๐ค Usage
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
|
-
|
|
15
|
+
bun install -d @stacksjs/storage
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
Now, you can use it in your project:
|
|
@@ -20,33 +20,33 @@ Now, you can use it in your project:
|
|
|
20
20
|
```js
|
|
21
21
|
import {
|
|
22
22
|
_dirname,
|
|
23
|
-
readJsonFile,
|
|
24
|
-
writeJsonFile,
|
|
25
|
-
readTextFile,
|
|
26
|
-
writeTextFile,
|
|
27
|
-
isFolder,
|
|
28
|
-
isFile,
|
|
29
|
-
hasFiles,
|
|
30
|
-
hasComponents,
|
|
31
|
-
hasFunctions,
|
|
32
23
|
copyFolder,
|
|
33
|
-
deleteFolder,
|
|
34
|
-
deleteFiles,
|
|
35
24
|
deleteEmptyFolders,
|
|
25
|
+
deleteFiles,
|
|
26
|
+
deleteFolder,
|
|
36
27
|
doesFolderExist,
|
|
37
|
-
fs, // fs-extra Node module
|
|
38
28
|
fileURLToPath,
|
|
29
|
+
fs, // fs-extra Node module
|
|
30
|
+
hasComponents,
|
|
31
|
+
hasFiles,
|
|
32
|
+
hasFunctions,
|
|
33
|
+
isFile,
|
|
34
|
+
isFolder,
|
|
35
|
+
readJsonFile,
|
|
36
|
+
readTextFile,
|
|
37
|
+
writeJsonFile,
|
|
38
|
+
writeTextFile,
|
|
39
39
|
} from '@stacksjs/storage'
|
|
40
40
|
|
|
41
41
|
// wip
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
To view the full documentation, please visit [https://stacksjs.
|
|
44
|
+
To view the full documentation, please visit [https://stacksjs.org/storage](https://stacksjs.org/storage).
|
|
45
45
|
|
|
46
46
|
## ๐งช Testing
|
|
47
47
|
|
|
48
48
|
```bash
|
|
49
|
-
|
|
49
|
+
bun test
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
## ๐ Changelog
|
|
@@ -65,10 +65,10 @@ For help, discussion about best practices, or any other conversation that would
|
|
|
65
65
|
|
|
66
66
|
For casual chit-chat with others using this package:
|
|
67
67
|
|
|
68
|
-
[Join the Stacks Discord Server](https://discord.
|
|
68
|
+
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
|
|
69
69
|
|
|
70
70
|
## ๐ License
|
|
71
71
|
|
|
72
72
|
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
73
73
|
|
|
74
|
-
Made with
|
|
74
|
+
Made with ๐
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,798 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
set: (newValue) => all[name] = () => newValue
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/files.ts
|
|
14
|
+
import {detectIndent, detectNewline} from "@stacksjs/strings";
|
|
15
|
+
import {dirname, join as join2, path as p} from "@stacksjs/path";
|
|
16
|
+
import {contains} from "@stacksjs/arrays";
|
|
17
|
+
|
|
18
|
+
// src/fs.ts
|
|
19
|
+
import {mkdirSync, writeFileSync} from "fs";
|
|
20
|
+
import * as fs from "fs-extra";
|
|
21
|
+
import {pathExists as existsSync} from "fs-extra";
|
|
22
|
+
async function exists(path) {
|
|
23
|
+
return await existsSync(path);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/files.ts
|
|
27
|
+
async function readJsonFile(name, cwd) {
|
|
28
|
+
const file = await readTextFile(name, cwd);
|
|
29
|
+
const data = JSON.parse(file.data);
|
|
30
|
+
const indent = detectIndent(file.data).indent;
|
|
31
|
+
const newline = detectNewline(file.data);
|
|
32
|
+
return { ...file, data, indent, newline };
|
|
33
|
+
}
|
|
34
|
+
async function readPackageJson(name, cwd) {
|
|
35
|
+
const file = await readJsonFile(name, cwd);
|
|
36
|
+
return file.data;
|
|
37
|
+
}
|
|
38
|
+
async function writeFile(path2, data) {
|
|
39
|
+
if (typeof path2 === "string") {
|
|
40
|
+
const dirPath = dirname(path2);
|
|
41
|
+
if (!await existsSync(dirPath))
|
|
42
|
+
await createFolder(dirPath);
|
|
43
|
+
return await Bun.write(Bun.file(path2), data);
|
|
44
|
+
}
|
|
45
|
+
return await Bun.write(path2, data);
|
|
46
|
+
}
|
|
47
|
+
async function writeJsonFile(file) {
|
|
48
|
+
let json = JSON.stringify(file.data, undefined, file.indent);
|
|
49
|
+
if (file.newline)
|
|
50
|
+
json += file.newline;
|
|
51
|
+
return writeTextFile({ ...file, data: json });
|
|
52
|
+
}
|
|
53
|
+
function readTextFile(name, cwd) {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
let filePath;
|
|
56
|
+
if (cwd)
|
|
57
|
+
filePath = join2(cwd, name);
|
|
58
|
+
else
|
|
59
|
+
filePath = name;
|
|
60
|
+
fs.readFile(filePath, "utf8", (err, text) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
reject(err);
|
|
63
|
+
} else {
|
|
64
|
+
resolve({
|
|
65
|
+
path: filePath,
|
|
66
|
+
data: text
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
async function writeTextFile(file) {
|
|
73
|
+
return await Bun.write(file.path, file.data);
|
|
74
|
+
}
|
|
75
|
+
function isFile(path2) {
|
|
76
|
+
return fs.existsSync(path2);
|
|
77
|
+
}
|
|
78
|
+
function hasFiles(folder) {
|
|
79
|
+
try {
|
|
80
|
+
return fs.default.readdirSync(folder).length > 0;
|
|
81
|
+
} catch (err) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function hasComponents() {
|
|
86
|
+
return hasFiles(p.componentsPath());
|
|
87
|
+
}
|
|
88
|
+
function hasFunctions() {
|
|
89
|
+
return hasFiles(p.functionsPath());
|
|
90
|
+
}
|
|
91
|
+
function deleteFiles(dir, exclude = []) {
|
|
92
|
+
if (fs.existsSync(dir)) {
|
|
93
|
+
fs.readdirSync(dir).forEach((file) => {
|
|
94
|
+
const p2 = join2(dir, file);
|
|
95
|
+
if (fs.statSync(p2).isDirectory()) {
|
|
96
|
+
if (fs.readdirSync(p2).length === 0)
|
|
97
|
+
fs.rmSync(p2, { recursive: true, force: true });
|
|
98
|
+
else
|
|
99
|
+
deleteFiles(p2, exclude);
|
|
100
|
+
} else if (!contains(p2, exclude)) {
|
|
101
|
+
fs.rmSync(p2);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function getFiles(dir, exclude = []) {
|
|
107
|
+
let results = [];
|
|
108
|
+
const list = fs.readdirSync(dir);
|
|
109
|
+
list.forEach((file) => {
|
|
110
|
+
file = join2(dir, file);
|
|
111
|
+
const stat = fs.statSync(file);
|
|
112
|
+
if (stat && stat.isDirectory())
|
|
113
|
+
results = results.concat(getFiles(file, exclude));
|
|
114
|
+
else if (!contains(file, exclude))
|
|
115
|
+
results.push(file);
|
|
116
|
+
});
|
|
117
|
+
return results;
|
|
118
|
+
}
|
|
119
|
+
function put(path2, contents) {
|
|
120
|
+
const dirPath = dirname(path2);
|
|
121
|
+
if (!fs.existsSync(dirPath))
|
|
122
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
123
|
+
fs.writeFileSync(path2, contents, "utf-8");
|
|
124
|
+
}
|
|
125
|
+
async function get(path2) {
|
|
126
|
+
return Bun.file(path2).text();
|
|
127
|
+
}
|
|
128
|
+
var files = {
|
|
129
|
+
readJsonFile,
|
|
130
|
+
readPackageJson,
|
|
131
|
+
readTextFile,
|
|
132
|
+
writeJsonFile,
|
|
133
|
+
writeTextFile,
|
|
134
|
+
isFile,
|
|
135
|
+
hasFiles,
|
|
136
|
+
hasComponents,
|
|
137
|
+
hasFunctions,
|
|
138
|
+
deleteFiles,
|
|
139
|
+
getFiles,
|
|
140
|
+
put,
|
|
141
|
+
get
|
|
142
|
+
};
|
|
143
|
+
// src/folders.ts
|
|
144
|
+
import {join as join3} from "@stacksjs/path";
|
|
145
|
+
function isFolder(path3) {
|
|
146
|
+
try {
|
|
147
|
+
return fs.statSync(path3).isDirectory();
|
|
148
|
+
} catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function isDirectory(path3) {
|
|
153
|
+
return isFolder(path3);
|
|
154
|
+
}
|
|
155
|
+
function isDir(path3) {
|
|
156
|
+
return isFolder(path3);
|
|
157
|
+
}
|
|
158
|
+
function doesFolderExist(path3) {
|
|
159
|
+
return fs.existsSync(path3);
|
|
160
|
+
}
|
|
161
|
+
function createFolder2(dir) {
|
|
162
|
+
return new Promise((resolve, reject) => {
|
|
163
|
+
fs.mkdirs(dir, (err) => {
|
|
164
|
+
if (err)
|
|
165
|
+
reject(err);
|
|
166
|
+
else
|
|
167
|
+
resolve();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function getFolders(dir) {
|
|
172
|
+
return fs.readdirSync(dir).filter((file) => {
|
|
173
|
+
return fs.statSync(join3(dir, file)).isDirectory();
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
var folders = {
|
|
177
|
+
isFolder,
|
|
178
|
+
doesFolderExist,
|
|
179
|
+
createFolder: createFolder2,
|
|
180
|
+
getFolders
|
|
181
|
+
};
|
|
182
|
+
// src/hash.ts
|
|
183
|
+
import {createHash} from "crypto";
|
|
184
|
+
import {path as p2} from "@stacksjs/path";
|
|
185
|
+
function hashFileOrDirectory(path4, hash) {
|
|
186
|
+
if (fs.statSync(path4).isDirectory()) {
|
|
187
|
+
const files2 = fs.readdirSync(path4);
|
|
188
|
+
for (const file of files2) {
|
|
189
|
+
const filePath = p2.join(path4, file);
|
|
190
|
+
hashFileOrDirectory(filePath, hash);
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
hash.update(fs.readFileSync(path4));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function hashDirectory(directory) {
|
|
197
|
+
const hash = createHash("sha256");
|
|
198
|
+
hashFileOrDirectory(directory, hash);
|
|
199
|
+
return hash.digest("hex");
|
|
200
|
+
}
|
|
201
|
+
function hashPath(path4) {
|
|
202
|
+
const hash = createHash("sha256");
|
|
203
|
+
hashFileOrDirectory(path4, hash);
|
|
204
|
+
return hash.digest("hex");
|
|
205
|
+
}
|
|
206
|
+
function hashPaths(paths) {
|
|
207
|
+
const hash = createHash("sha256");
|
|
208
|
+
const pathsArray = Array.isArray(paths) ? paths : [paths];
|
|
209
|
+
for (const path4 of pathsArray)
|
|
210
|
+
hashFileOrDirectory(path4, hash);
|
|
211
|
+
return hash.digest("hex");
|
|
212
|
+
}
|
|
213
|
+
// src/helpers.ts
|
|
214
|
+
import {fileURLToPath} from "url";
|
|
215
|
+
import {dirname as dirname2} from "@stacksjs/path";
|
|
216
|
+
function updateConfigFile(filePath, newConfig) {
|
|
217
|
+
return new Promise((resolve, reject) => {
|
|
218
|
+
const config = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
219
|
+
for (const key in newConfig)
|
|
220
|
+
config[key] = newConfig[key];
|
|
221
|
+
try {
|
|
222
|
+
fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
|
|
223
|
+
resolve();
|
|
224
|
+
} catch (error) {
|
|
225
|
+
reject(error);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
var __dirname = "/Users/chrisbreuer/Code/stacks/storage/framework/core/src/storage/src";
|
|
230
|
+
var _dirname = typeof __dirname !== "undefined" ? __dirname : dirname2(fileURLToPath(import.meta.url));
|
|
231
|
+
var helpers = {
|
|
232
|
+
_dirname,
|
|
233
|
+
updateConfigFile
|
|
234
|
+
};
|
|
235
|
+
// src/copy.ts
|
|
236
|
+
import {contains as contains2} from "@stacksjs/arrays";
|
|
237
|
+
import {join as join4} from "@stacksjs/path";
|
|
238
|
+
function copy(src, dest, exclude = []) {
|
|
239
|
+
if (Array.isArray(src)) {
|
|
240
|
+
src.forEach((file) => {
|
|
241
|
+
copy(file, dest, exclude);
|
|
242
|
+
});
|
|
243
|
+
} else {
|
|
244
|
+
if (fs.statSync(src).isDirectory())
|
|
245
|
+
copyFolder(src, dest, exclude);
|
|
246
|
+
else
|
|
247
|
+
copyFile(src, dest);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function copyFile(src, dest) {
|
|
251
|
+
fs.copyFileSync(src, dest);
|
|
252
|
+
}
|
|
253
|
+
function copyFolder(src, dest, exclude = []) {
|
|
254
|
+
if (!fs.existsSync(dest))
|
|
255
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
256
|
+
if (fs.existsSync(src)) {
|
|
257
|
+
fs.readdirSync(src).forEach((file) => {
|
|
258
|
+
if (!contains2(join4(src, file), exclude)) {
|
|
259
|
+
const srcPath = join4(src, file);
|
|
260
|
+
const destPath = join4(dest, file);
|
|
261
|
+
if (fs.statSync(srcPath).isDirectory())
|
|
262
|
+
copyFolder(srcPath, destPath, exclude);
|
|
263
|
+
else
|
|
264
|
+
fs.copyFileSync(srcPath, destPath);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// src/glob.ts
|
|
270
|
+
import {default as default2} from "fast-glob";
|
|
271
|
+
// src/delete.ts
|
|
272
|
+
import {err, ok} from "@stacksjs/error-handling";
|
|
273
|
+
import {italic, log} from "@stacksjs/cli";
|
|
274
|
+
function deleteFolder(path6) {
|
|
275
|
+
return new Promise((resolve, reject) => {
|
|
276
|
+
try {
|
|
277
|
+
if (isFolder(path6)) {
|
|
278
|
+
fs.rmSync(path6, { recursive: true, force: true });
|
|
279
|
+
return resolve(ok(`Deleted ${path6}`));
|
|
280
|
+
}
|
|
281
|
+
return resolve(ok(`Path ${path6} was not a directory`));
|
|
282
|
+
} catch (error) {
|
|
283
|
+
return reject(err(error));
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
async function isDirectoryEmpty(path6) {
|
|
288
|
+
return new Promise((resolve, reject) => {
|
|
289
|
+
try {
|
|
290
|
+
if (fs.statSync(path6).isDirectory()) {
|
|
291
|
+
if (fs.readdirSync(path6).length === 0)
|
|
292
|
+
return resolve(ok(true));
|
|
293
|
+
else
|
|
294
|
+
return resolve(ok(false));
|
|
295
|
+
}
|
|
296
|
+
return resolve(ok(false));
|
|
297
|
+
} catch (error) {
|
|
298
|
+
return reject(err(error));
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
async function deleteEmptyFolder(path6) {
|
|
303
|
+
return new Promise((resolve, reject) => {
|
|
304
|
+
try {
|
|
305
|
+
if (fs.statSync(path6).isDirectory()) {
|
|
306
|
+
if (fs.readdirSync(path6).length === 0) {
|
|
307
|
+
fs.rmSync(path6, { recursive: true, force: true });
|
|
308
|
+
return resolve(ok(`Deleted ${path6}`));
|
|
309
|
+
} else {
|
|
310
|
+
return resolve(ok(`Path ${path6} was not empty`));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return resolve(ok(`Path ${path6} was not a directory`));
|
|
314
|
+
} catch (error) {
|
|
315
|
+
return reject(err(error));
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
async function deleteEmptyFolders(dir) {
|
|
320
|
+
try {
|
|
321
|
+
if (!fs.existsSync(dir))
|
|
322
|
+
return ok(`Path ${dir} does not exist`);
|
|
323
|
+
const files3 = fs.readdirSync(dir);
|
|
324
|
+
for (const file of files3) {
|
|
325
|
+
const p3 = join(dir, file);
|
|
326
|
+
if (isFolder(p3)) {
|
|
327
|
+
if (fs.readdirSync(p3).length === 0)
|
|
328
|
+
fs.rmSync(p3, { recursive: true, force: true });
|
|
329
|
+
else
|
|
330
|
+
await deleteEmptyFolders(p3);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return ok(`Deleted empty folders located in ${dir}`);
|
|
334
|
+
} catch (error) {
|
|
335
|
+
return err(error);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function deleteFile(path6) {
|
|
339
|
+
return new Promise((resolve, reject) => {
|
|
340
|
+
try {
|
|
341
|
+
if (isFile(path6)) {
|
|
342
|
+
fs.rmSync(path6, { recursive: true, force: true });
|
|
343
|
+
return resolve(ok(`Deleted ${path6}`));
|
|
344
|
+
}
|
|
345
|
+
return resolve(ok(`Path ${path6} was not a file`));
|
|
346
|
+
} catch (error) {
|
|
347
|
+
return reject(err(error));
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
async function deleteGlob(path6) {
|
|
352
|
+
if (!path6.includes("*"))
|
|
353
|
+
return err(handleError(`Path ${path6} does not contain a glob`));
|
|
354
|
+
const directories = await default2([path6], { onlyDirectories: true });
|
|
355
|
+
for (const directory of directories) {
|
|
356
|
+
const result = await deleteFolder(directory);
|
|
357
|
+
if (result.isErr()) {
|
|
358
|
+
log.error(result.error);
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
log.info(`Deleted ${italic(directory)}`);
|
|
362
|
+
}
|
|
363
|
+
return ok(`Deleted ${directories.length} directories`);
|
|
364
|
+
}
|
|
365
|
+
async function del(path6) {
|
|
366
|
+
if (isFile(path6))
|
|
367
|
+
return await deleteFile(path6);
|
|
368
|
+
if (isFolder(path6))
|
|
369
|
+
return await deleteFolder(path6);
|
|
370
|
+
if (path6.includes("*"))
|
|
371
|
+
return await deleteGlob(path6);
|
|
372
|
+
return err(handleError(`Path ${path6} cannot be deleted due to an unhandled condition. Please report this issue.`));
|
|
373
|
+
}
|
|
374
|
+
// src/zip.ts
|
|
375
|
+
import {runCommand} from "@stacksjs/cli";
|
|
376
|
+
async function zip(from, to, options) {
|
|
377
|
+
const toPath = to || "archive.zip";
|
|
378
|
+
const fromPath = Array.isArray(from) ? from.join(" ") : from;
|
|
379
|
+
if (Array.isArray(from))
|
|
380
|
+
return runCommand(`zip -r ${toPath} ${fromPath}`, options);
|
|
381
|
+
return runCommand(`zip -r ${to} ${from}`, options);
|
|
382
|
+
}
|
|
383
|
+
async function unzip(paths) {
|
|
384
|
+
if (Array.isArray(paths))
|
|
385
|
+
return runCommand(`unzip ${paths.join(" ")}`);
|
|
386
|
+
return runCommand(`unzip ${paths}`);
|
|
387
|
+
}
|
|
388
|
+
function archive(paths) {
|
|
389
|
+
return zip(paths);
|
|
390
|
+
}
|
|
391
|
+
function unarchive(paths) {
|
|
392
|
+
return unzip(paths);
|
|
393
|
+
}
|
|
394
|
+
function compress(paths) {
|
|
395
|
+
return zip(paths);
|
|
396
|
+
}
|
|
397
|
+
function decompress(paths) {
|
|
398
|
+
return unzip(paths);
|
|
399
|
+
}
|
|
400
|
+
function gzipSync(data, options) {
|
|
401
|
+
return Bun.gzipSync(data, options);
|
|
402
|
+
}
|
|
403
|
+
function gunzipSync(data) {
|
|
404
|
+
return Bun.gunzipSync(data);
|
|
405
|
+
}
|
|
406
|
+
function deflateSync(data, options) {
|
|
407
|
+
return Bun.deflateSync(data, options);
|
|
408
|
+
}
|
|
409
|
+
function inflateSync(data) {
|
|
410
|
+
return Bun.inflateSync(data);
|
|
411
|
+
}
|
|
412
|
+
// src/storage.ts
|
|
413
|
+
var exports_storage = {};
|
|
414
|
+
__export(exports_storage, {
|
|
415
|
+
zip: () => {
|
|
416
|
+
{
|
|
417
|
+
return zip;
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
writeTextFile: () => {
|
|
421
|
+
{
|
|
422
|
+
return writeTextFile;
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
writeJsonFile: () => {
|
|
426
|
+
{
|
|
427
|
+
return writeJsonFile;
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
writeFileSync: () => {
|
|
431
|
+
{
|
|
432
|
+
return writeFileSync;
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
writeFile: () => {
|
|
436
|
+
{
|
|
437
|
+
return writeFile;
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
updateConfigFile: () => {
|
|
441
|
+
{
|
|
442
|
+
return updateConfigFile;
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
unzip: () => {
|
|
446
|
+
{
|
|
447
|
+
return unzip;
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
unarchive: () => {
|
|
451
|
+
{
|
|
452
|
+
return unarchive;
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
setVisibility: () => {
|
|
456
|
+
{
|
|
457
|
+
return setVisibility;
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
rename: () => {
|
|
461
|
+
{
|
|
462
|
+
return rename;
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
readTextFile: () => {
|
|
466
|
+
{
|
|
467
|
+
return readTextFile;
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
readPackageJson: () => {
|
|
471
|
+
{
|
|
472
|
+
return readPackageJson;
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
readJsonFile: () => {
|
|
476
|
+
{
|
|
477
|
+
return readJsonFile;
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
put: () => {
|
|
481
|
+
{
|
|
482
|
+
return put;
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
move: () => {
|
|
486
|
+
{
|
|
487
|
+
return move;
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
mkdirSync: () => {
|
|
491
|
+
{
|
|
492
|
+
return mkdirSync;
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
isFolder: () => {
|
|
496
|
+
{
|
|
497
|
+
return isFolder;
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
isFile: () => {
|
|
501
|
+
{
|
|
502
|
+
return isFile;
|
|
503
|
+
}
|
|
504
|
+
},
|
|
505
|
+
isDirectoryEmpty: () => {
|
|
506
|
+
{
|
|
507
|
+
return isDirectoryEmpty;
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
isDirectory: () => {
|
|
511
|
+
{
|
|
512
|
+
return isDirectory;
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
isDir: () => {
|
|
516
|
+
{
|
|
517
|
+
return isDir;
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
inflateSync: () => {
|
|
521
|
+
{
|
|
522
|
+
return inflateSync;
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
helpers: () => {
|
|
526
|
+
{
|
|
527
|
+
return helpers;
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
hasFunctions: () => {
|
|
531
|
+
{
|
|
532
|
+
return hasFunctions;
|
|
533
|
+
}
|
|
534
|
+
},
|
|
535
|
+
hasFiles: () => {
|
|
536
|
+
{
|
|
537
|
+
return hasFiles;
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
hasComponents: () => {
|
|
541
|
+
{
|
|
542
|
+
return hasComponents;
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
gzipSync: () => {
|
|
546
|
+
{
|
|
547
|
+
return gzipSync;
|
|
548
|
+
}
|
|
549
|
+
},
|
|
550
|
+
gunzipSync: () => {
|
|
551
|
+
{
|
|
552
|
+
return gunzipSync;
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
getFolders: () => {
|
|
556
|
+
{
|
|
557
|
+
return getFolders;
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
getFiles: () => {
|
|
561
|
+
{
|
|
562
|
+
return getFiles;
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
get: () => {
|
|
566
|
+
{
|
|
567
|
+
return get;
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
fs: () => {
|
|
571
|
+
{
|
|
572
|
+
return fs;
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
folders: () => {
|
|
576
|
+
{
|
|
577
|
+
return folders;
|
|
578
|
+
}
|
|
579
|
+
},
|
|
580
|
+
files: () => {
|
|
581
|
+
{
|
|
582
|
+
return files;
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
existsSync: () => {
|
|
586
|
+
{
|
|
587
|
+
return existsSync;
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
exists: () => {
|
|
591
|
+
{
|
|
592
|
+
return exists;
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
doesFolderExist: () => {
|
|
596
|
+
{
|
|
597
|
+
return doesFolderExist;
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
deleteGlob: () => {
|
|
601
|
+
{
|
|
602
|
+
return deleteGlob;
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
deleteFolder: () => {
|
|
606
|
+
{
|
|
607
|
+
return deleteFolder;
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
deleteFiles: () => {
|
|
611
|
+
{
|
|
612
|
+
return deleteFiles;
|
|
613
|
+
}
|
|
614
|
+
},
|
|
615
|
+
deleteFile: () => {
|
|
616
|
+
{
|
|
617
|
+
return deleteFile;
|
|
618
|
+
}
|
|
619
|
+
},
|
|
620
|
+
deleteEmptyFolders: () => {
|
|
621
|
+
{
|
|
622
|
+
return deleteEmptyFolders;
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
deleteEmptyFolder: () => {
|
|
626
|
+
{
|
|
627
|
+
return deleteEmptyFolder;
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
del: () => {
|
|
631
|
+
{
|
|
632
|
+
return del;
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
deflateSync: () => {
|
|
636
|
+
{
|
|
637
|
+
return deflateSync;
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
decompress: () => {
|
|
641
|
+
{
|
|
642
|
+
return decompress;
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
createFolder: () => {
|
|
646
|
+
{
|
|
647
|
+
return createFolder2;
|
|
648
|
+
}
|
|
649
|
+
},
|
|
650
|
+
copyFolder: () => {
|
|
651
|
+
{
|
|
652
|
+
return copyFolder;
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
copyFile: () => {
|
|
656
|
+
{
|
|
657
|
+
return copyFile;
|
|
658
|
+
}
|
|
659
|
+
},
|
|
660
|
+
copy: () => {
|
|
661
|
+
{
|
|
662
|
+
return copy;
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
compress: () => {
|
|
666
|
+
{
|
|
667
|
+
return compress;
|
|
668
|
+
}
|
|
669
|
+
},
|
|
670
|
+
archive: () => {
|
|
671
|
+
{
|
|
672
|
+
return archive;
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
_dirname: () => {
|
|
676
|
+
{
|
|
677
|
+
return _dirname;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
// src/move.ts
|
|
683
|
+
import {err as err2, ok as ok2} from "@stacksjs/error-handling";
|
|
684
|
+
import {log as log2} from "@stacksjs/logging";
|
|
685
|
+
import {path as path7} from "@stacksjs/path";
|
|
686
|
+
async function move(src, dest, options) {
|
|
687
|
+
try {
|
|
688
|
+
if (Array.isArray(src)) {
|
|
689
|
+
const operations = src.map(async (file) => {
|
|
690
|
+
const from2 = file;
|
|
691
|
+
const to2 = path7.resolve(dest, path7.basename(file));
|
|
692
|
+
const result2 = await rename(from2, to2, options);
|
|
693
|
+
if (result2.isErr()) {
|
|
694
|
+
log2.error(result2.error);
|
|
695
|
+
return err2(handleError(result2.error.message, result2.error));
|
|
696
|
+
}
|
|
697
|
+
});
|
|
698
|
+
await Promise.all(operations);
|
|
699
|
+
return ok2({ message: "Files moved successfully" });
|
|
700
|
+
}
|
|
701
|
+
const from = src;
|
|
702
|
+
const to = dest;
|
|
703
|
+
const result = await rename(from, to, options);
|
|
704
|
+
if (result.isErr()) {
|
|
705
|
+
log2.error(result.error);
|
|
706
|
+
return err2(handleError(result.error));
|
|
707
|
+
}
|
|
708
|
+
return ok2({ message: "File moved successfully" });
|
|
709
|
+
} catch (error) {
|
|
710
|
+
return err2(handleError(error));
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
async function rename(from, to, options) {
|
|
714
|
+
return new Promise((resolve, reject) => {
|
|
715
|
+
try {
|
|
716
|
+
const dir = path7.dirname(to);
|
|
717
|
+
if (!fs.existsSync(dir))
|
|
718
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
719
|
+
if (!fs.existsSync(from))
|
|
720
|
+
return reject(err2(new Error(`File or directory does not exist: ${from}`)));
|
|
721
|
+
if (fs.existsSync(to)) {
|
|
722
|
+
if (!options?.overwrite)
|
|
723
|
+
return reject(err2(new Error(`File or directory already exists: ${to}`)));
|
|
724
|
+
fs.unlinkSync(to);
|
|
725
|
+
}
|
|
726
|
+
fs.renameSync(from, to);
|
|
727
|
+
return resolve(ok2({ message: "File moved successfully" }));
|
|
728
|
+
} catch (error) {
|
|
729
|
+
if (error.code === "ENOENT")
|
|
730
|
+
log2.error("File or directory does not exist\n\n", error);
|
|
731
|
+
else
|
|
732
|
+
log2.error(error);
|
|
733
|
+
return reject(err2(new Error(error)));
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
// src/visibility.ts
|
|
738
|
+
function setVisibility() {
|
|
739
|
+
return "wip";
|
|
740
|
+
}
|
|
741
|
+
export {
|
|
742
|
+
zip,
|
|
743
|
+
writeTextFile,
|
|
744
|
+
writeJsonFile,
|
|
745
|
+
writeFileSync,
|
|
746
|
+
writeFile,
|
|
747
|
+
updateConfigFile,
|
|
748
|
+
unzip,
|
|
749
|
+
unarchive,
|
|
750
|
+
exports_storage as storage,
|
|
751
|
+
readTextFile,
|
|
752
|
+
readPackageJson,
|
|
753
|
+
readJsonFile,
|
|
754
|
+
put,
|
|
755
|
+
mkdirSync,
|
|
756
|
+
isFolder,
|
|
757
|
+
isFile,
|
|
758
|
+
isDirectoryEmpty,
|
|
759
|
+
isDirectory,
|
|
760
|
+
isDir,
|
|
761
|
+
inflateSync,
|
|
762
|
+
helpers,
|
|
763
|
+
hashPaths,
|
|
764
|
+
hashPath,
|
|
765
|
+
hashFileOrDirectory,
|
|
766
|
+
hashDirectory,
|
|
767
|
+
hasFunctions,
|
|
768
|
+
hasFiles,
|
|
769
|
+
hasComponents,
|
|
770
|
+
gzipSync,
|
|
771
|
+
gunzipSync,
|
|
772
|
+
default2 as glob,
|
|
773
|
+
getFolders,
|
|
774
|
+
getFiles,
|
|
775
|
+
get,
|
|
776
|
+
fs,
|
|
777
|
+
folders,
|
|
778
|
+
files,
|
|
779
|
+
existsSync,
|
|
780
|
+
exists,
|
|
781
|
+
doesFolderExist,
|
|
782
|
+
deleteGlob,
|
|
783
|
+
deleteFolder,
|
|
784
|
+
deleteFiles,
|
|
785
|
+
deleteFile,
|
|
786
|
+
deleteEmptyFolders,
|
|
787
|
+
deleteEmptyFolder,
|
|
788
|
+
del,
|
|
789
|
+
deflateSync,
|
|
790
|
+
decompress,
|
|
791
|
+
createFolder2 as createFolder,
|
|
792
|
+
copyFolder,
|
|
793
|
+
copyFile,
|
|
794
|
+
copy,
|
|
795
|
+
compress,
|
|
796
|
+
archive,
|
|
797
|
+
_dirname
|
|
798
|
+
};
|
package/package.json
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/storage",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.6.6",
|
|
4
|
+
"version": "0.58.20",
|
|
6
5
|
"description": "The Stacks file system.",
|
|
7
6
|
"author": "Chris Breuer",
|
|
8
7
|
"license": "MIT",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"import": "./dist/index.mjs"
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
16
|
-
"homepage": "https://github.com/stacksjs/stacks/tree/main
|
|
9
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/src/storage#readme",
|
|
17
10
|
"repository": {
|
|
18
11
|
"type": "git",
|
|
19
12
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
20
|
-
"directory": "
|
|
13
|
+
"directory": "./storage/framework/core/src/storage"
|
|
21
14
|
},
|
|
22
15
|
"bugs": {
|
|
23
16
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
@@ -26,35 +19,59 @@
|
|
|
26
19
|
"storage",
|
|
27
20
|
"fs",
|
|
28
21
|
"file system",
|
|
22
|
+
"glob",
|
|
29
23
|
"api",
|
|
30
24
|
"collections",
|
|
31
25
|
"utilities",
|
|
32
26
|
"functions",
|
|
27
|
+
"bun",
|
|
33
28
|
"stacks"
|
|
34
29
|
],
|
|
35
|
-
"
|
|
30
|
+
".": {
|
|
31
|
+
"bun": "./src/index.ts",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./*": {
|
|
35
|
+
"bun": "./src/*",
|
|
36
|
+
"import": "./dist/*"
|
|
37
|
+
},
|
|
38
|
+
"module": "dist/index.js",
|
|
36
39
|
"types": "dist/index.d.ts",
|
|
37
40
|
"contributors": [
|
|
38
|
-
"Chris Breuer <chris@
|
|
41
|
+
"Chris Breuer <chris@stacksjs.org>"
|
|
39
42
|
],
|
|
40
43
|
"files": [
|
|
41
|
-
"
|
|
42
|
-
"
|
|
44
|
+
"README.md",
|
|
45
|
+
"dist"
|
|
43
46
|
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "bun --bun build.ts",
|
|
49
|
+
"typecheck": "bun --bun tsc --noEmit",
|
|
50
|
+
"prepublishOnly": "bun --bun run build"
|
|
51
|
+
},
|
|
44
52
|
"peerDependencies": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"@stacksjs/
|
|
48
|
-
"@stacksjs/
|
|
49
|
-
"@stacksjs/
|
|
50
|
-
"@stacksjs/
|
|
53
|
+
"@stacksjs/arrays": "workspace:*",
|
|
54
|
+
"@stacksjs/cli": "workspace:*",
|
|
55
|
+
"@stacksjs/error-handling": "workspace:*",
|
|
56
|
+
"@stacksjs/path": "workspace:*",
|
|
57
|
+
"@stacksjs/strings": "workspace:*",
|
|
58
|
+
"@stacksjs/types": "workspace:*",
|
|
59
|
+
"@types/archiver": "^6.0.2",
|
|
60
|
+
"archiver": "^6.0.1"
|
|
51
61
|
},
|
|
52
|
-
"
|
|
53
|
-
"@stacksjs/
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@stacksjs/arrays": "workspace:*",
|
|
64
|
+
"@stacksjs/error-handling": "workspace:*",
|
|
65
|
+
"@stacksjs/path": "workspace:*",
|
|
66
|
+
"@stacksjs/strings": "workspace:*",
|
|
67
|
+
"@stacksjs/types": "workspace:*",
|
|
68
|
+
"@types/archiver": "^6.0.2",
|
|
69
|
+
"archiver": "^6.0.1",
|
|
70
|
+
"fast-glob": "^3.3.2",
|
|
71
|
+
"fs-extra": "^11.2.0",
|
|
72
|
+
"unstorage": "^1.10.1"
|
|
54
73
|
},
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"dev": "unbuild --stub",
|
|
58
|
-
"typecheck": "tsc --noEmit"
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@stacksjs/development": "workspace:*"
|
|
59
76
|
}
|
|
60
|
-
}
|
|
77
|
+
}
|
package/LICENSE.md
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Open Web Foundation
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
|
-
import type { JsonFile, TextFile } from '@stacksjs/types';
|
|
3
|
-
export declare const _dirname: any;
|
|
4
|
-
/**
|
|
5
|
-
* Reads a JSON file and returns the parsed data.
|
|
6
|
-
*/
|
|
7
|
-
export declare function readJsonFile(name: string, cwd?: string): Promise<JsonFile>;
|
|
8
|
-
/**
|
|
9
|
-
* Reads a package.json file and returns the parsed data.
|
|
10
|
-
*/
|
|
11
|
-
export declare function readPackageJson(name: string, cwd?: string): Promise<PackageJson>;
|
|
12
|
-
/**
|
|
13
|
-
* Writes the given data to the specified JSON file.
|
|
14
|
-
*/
|
|
15
|
-
export declare function writeJsonFile(file: JsonFile): Promise<void>;
|
|
16
|
-
/**
|
|
17
|
-
* Reads a text file and returns its contents.
|
|
18
|
-
*/
|
|
19
|
-
export declare function readTextFile(name: string, cwd?: string): Promise<TextFile>;
|
|
20
|
-
/**
|
|
21
|
-
* Writes the given text to the specified file.
|
|
22
|
-
*/
|
|
23
|
-
export declare function writeTextFile(file: TextFile): Promise<void>;
|
|
24
|
-
/**
|
|
25
|
-
* Determine whether a path is a folder.
|
|
26
|
-
*/
|
|
27
|
-
export declare function isFolder(path: string): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* Determine whether a path is a file.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isFile(path: string): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Determine whether a folder has any files in it.
|
|
34
|
-
*/
|
|
35
|
-
export declare function hasFiles(folder: string): boolean;
|
|
36
|
-
export declare function hasComponents(): boolean;
|
|
37
|
-
export declare function hasFunctions(): boolean;
|
|
38
|
-
export declare function copyFolder(src: string, dest: string, exclude?: string[]): void;
|
|
39
|
-
export declare function deleteFolder(path: string): Promise<void>;
|
|
40
|
-
export declare function deleteFiles(dir: string, exclude?: string[]): void;
|
|
41
|
-
export declare function deleteEmptyFolders(dir: string): void;
|
|
42
|
-
export declare function doesFolderExist(path: string): any;
|
|
43
|
-
export declare function createFolder(dir: string): Promise<void>;
|
|
44
|
-
export declare function updateConfigFile(filePath: string, newConfig: Record<string, unknown>): Promise<void>;
|
|
45
|
-
export { fs };
|
package/dist/index.mjs
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
-
import fs from "fs-extra";
|
|
3
|
-
import { detectIndent, detectNewline } from "@stacksjs/strings";
|
|
4
|
-
import { componentsPath, dirname, functionsPath, join, projectPath } from "@stacksjs/path";
|
|
5
|
-
import { contains } from "@stacksjs/arrays";
|
|
6
|
-
export const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
7
|
-
export async function readJsonFile(name, cwd) {
|
|
8
|
-
const file = await readTextFile(name, cwd);
|
|
9
|
-
const data = JSON.parse(file.data);
|
|
10
|
-
const indent = detectIndent(file.data).indent;
|
|
11
|
-
const newline = detectNewline(file.data);
|
|
12
|
-
return { ...file, data, indent, newline };
|
|
13
|
-
}
|
|
14
|
-
export async function readPackageJson(name, cwd) {
|
|
15
|
-
const file = await readJsonFile(name, cwd);
|
|
16
|
-
const data = file.data;
|
|
17
|
-
return data;
|
|
18
|
-
}
|
|
19
|
-
export async function writeJsonFile(file) {
|
|
20
|
-
let json = JSON.stringify(file.data, void 0, file.indent);
|
|
21
|
-
if (file.newline)
|
|
22
|
-
json += file.newline;
|
|
23
|
-
return writeTextFile({ ...file, data: json });
|
|
24
|
-
}
|
|
25
|
-
export function readTextFile(name, cwd) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
let filePath;
|
|
28
|
-
if (cwd)
|
|
29
|
-
filePath = join(cwd, name);
|
|
30
|
-
else
|
|
31
|
-
filePath = name;
|
|
32
|
-
fs.readFile(filePath, "utf8", (err, text) => {
|
|
33
|
-
if (err) {
|
|
34
|
-
reject(err);
|
|
35
|
-
} else {
|
|
36
|
-
resolve({
|
|
37
|
-
path: filePath,
|
|
38
|
-
data: text
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
export function writeTextFile(file) {
|
|
45
|
-
return new Promise((resolve, reject) => {
|
|
46
|
-
fs.writeFile(file.path, file.data, (err) => {
|
|
47
|
-
if (err)
|
|
48
|
-
reject(err);
|
|
49
|
-
else
|
|
50
|
-
resolve();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
export function isFolder(path) {
|
|
55
|
-
try {
|
|
56
|
-
return fs.statSync(path).isDirectory();
|
|
57
|
-
} catch {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
export function isFile(path) {
|
|
62
|
-
return fs.existsSync(projectPath(path));
|
|
63
|
-
}
|
|
64
|
-
export function hasFiles(folder) {
|
|
65
|
-
return fs.readdirSync(folder).length > 0;
|
|
66
|
-
}
|
|
67
|
-
export function hasComponents() {
|
|
68
|
-
return hasFiles(componentsPath());
|
|
69
|
-
}
|
|
70
|
-
export function hasFunctions() {
|
|
71
|
-
return hasFiles(functionsPath());
|
|
72
|
-
}
|
|
73
|
-
export function copyFolder(src, dest, exclude = []) {
|
|
74
|
-
if (!fs.existsSync(dest))
|
|
75
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
76
|
-
if (fs.existsSync(src)) {
|
|
77
|
-
fs.readdirSync(src).forEach((file) => {
|
|
78
|
-
if (!contains(join(src, file), exclude)) {
|
|
79
|
-
const srcPath = join(src, file);
|
|
80
|
-
const destPath = join(dest, file);
|
|
81
|
-
if (fs.statSync(srcPath).isDirectory())
|
|
82
|
-
copyFolder(srcPath, destPath, exclude);
|
|
83
|
-
else
|
|
84
|
-
fs.copyFileSync(srcPath, destPath);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
export async function deleteFolder(path) {
|
|
90
|
-
if (fs.statSync(path).isDirectory())
|
|
91
|
-
await fs.rmSync(path, { recursive: true, force: true });
|
|
92
|
-
}
|
|
93
|
-
export function deleteFiles(dir, exclude = []) {
|
|
94
|
-
if (fs.existsSync(dir)) {
|
|
95
|
-
fs.readdirSync(dir).forEach((file) => {
|
|
96
|
-
const p = join(dir, file);
|
|
97
|
-
if (fs.statSync(p).isDirectory()) {
|
|
98
|
-
if (fs.readdirSync(p).length === 0)
|
|
99
|
-
fs.rmSync(p, { recursive: true, force: true });
|
|
100
|
-
else
|
|
101
|
-
deleteFiles(p, exclude);
|
|
102
|
-
} else if (!contains(p, exclude)) {
|
|
103
|
-
fs.rmSync(p);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
export function deleteEmptyFolders(dir) {
|
|
109
|
-
if (fs.existsSync(dir)) {
|
|
110
|
-
fs.readdirSync(dir).forEach((file) => {
|
|
111
|
-
const p = join(dir, file);
|
|
112
|
-
if (fs.statSync(p).isDirectory()) {
|
|
113
|
-
if (fs.readdirSync(p).length === 0)
|
|
114
|
-
fs.rmSync(p, { recursive: true, force: true });
|
|
115
|
-
else
|
|
116
|
-
deleteEmptyFolders(p);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
export function doesFolderExist(path) {
|
|
122
|
-
return fs.existsSync(path);
|
|
123
|
-
}
|
|
124
|
-
export function createFolder(dir) {
|
|
125
|
-
return new Promise((resolve, reject) => {
|
|
126
|
-
fs.mkdirs(dir, (err) => {
|
|
127
|
-
if (err)
|
|
128
|
-
reject(err);
|
|
129
|
-
else
|
|
130
|
-
resolve();
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
export function updateConfigFile(filePath, newConfig) {
|
|
135
|
-
return new Promise((resolve, reject) => {
|
|
136
|
-
const config = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
137
|
-
for (const key in newConfig) {
|
|
138
|
-
config[key] = newConfig[key];
|
|
139
|
-
}
|
|
140
|
-
fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
|
|
141
|
-
resolve();
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
export { fs };
|