@stacksjs/storage 0.64.6 → 0.65.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/copy.d.ts +3 -0
- package/dist/delete.d.ts +8 -0
- package/dist/drivers/aws.d.ts +2 -0
- package/dist/drivers/index.d.ts +1 -0
- package/dist/files.d.ts +57 -0
- package/dist/folders.d.ts +16 -0
- package/dist/fs.d.ts +4 -0
- package/dist/glob.d.ts +13 -0
- package/dist/hash.d.ts +5 -0
- package/dist/helpers.d.ts +8 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2 -27
- package/dist/index.js.map +38 -85
- package/dist/move.d.ts +7 -0
- package/dist/storage.d.ts +9 -0
- package/dist/visibility.d.ts +1 -0
- package/dist/zip.d.ts +17 -0
- package/package.json +13 -19
- package/src/copy.ts +8 -4
- package/src/delete.ts +25 -13
- package/src/files.ts +45 -17
- package/src/folders.ts +13 -4
- package/src/fs.ts +1 -1
- package/src/glob.ts +23 -1
- package/src/hash.ts +2 -1
- package/src/helpers.ts +9 -3
- package/src/index.ts +6 -6
- package/src/move.ts +15 -8
- package/src/storage.ts +5 -5
- package/src/visibility.ts +1 -1
- package/src/zip.ts +21 -13
package/dist/move.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Result } from "@stacksjs/error-handling";
|
|
2
|
+
interface MoveOptions {
|
|
3
|
+
overwrite?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function move(src: string | string[], dest: string, options?: MoveOptions): Promise<Result<{ message: string }, Error>>;
|
|
6
|
+
export declare function rename(from: string, to: string, options?: MoveOptions): Promise<Result<{ message: string }, Error>>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setVisibility(): string;
|
package/dist/zip.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Result } from "@stacksjs/error-handling";
|
|
2
|
+
import type { CommandError, Subprocess } from "@stacksjs/types";
|
|
3
|
+
import type { ZlibCompressionOptions } from "bun";
|
|
4
|
+
interface ZipOptions {
|
|
5
|
+
cwd?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function zip(from: string | string[], to?: string, options?: ZipOptions): Promise<Result<Subprocess, CommandError>>;
|
|
8
|
+
export declare function unzip(paths: string | string[]): Promise<Result<Subprocess, CommandError>>;
|
|
9
|
+
export declare function archive(paths: string | string[]): Promise<Result<Subprocess, CommandError>>;
|
|
10
|
+
export declare function unarchive(paths: string | string[]): Promise<Result<Subprocess, CommandError>>;
|
|
11
|
+
export declare function compress(paths: string[]): Promise<Result<Subprocess, CommandError>>;
|
|
12
|
+
export declare function decompress(paths: string | string[]): Promise<Result<Subprocess, CommandError>>;
|
|
13
|
+
export declare function gzipSync(data: Uint8Array, options?: ZlibCompressionOptions): Uint8Array;
|
|
14
|
+
export declare function gunzipSync(data: Uint8Array): Uint8Array;
|
|
15
|
+
export declare function deflateSync(data: Uint8Array, options?: ZlibCompressionOptions): Uint8Array;
|
|
16
|
+
export declare function inflateSync(data: Uint8Array): Uint8Array;
|
|
17
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/storage",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.65.0",
|
|
5
5
|
"description": "The Stacks file system.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
|
+
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
7
8
|
"license": "MIT",
|
|
8
9
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
9
10
|
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/storage#readme",
|
|
@@ -37,32 +38,25 @@
|
|
|
37
38
|
},
|
|
38
39
|
"module": "dist/index.js",
|
|
39
40
|
"types": "dist/index.d.ts",
|
|
40
|
-
"
|
|
41
|
-
"Chris Breuer <chris@stacksjs.org>"
|
|
42
|
-
],
|
|
43
|
-
"files": [
|
|
44
|
-
"README.md",
|
|
45
|
-
"dist",
|
|
46
|
-
"src"
|
|
47
|
-
],
|
|
41
|
+
"files": ["README.md", "dist", "src"],
|
|
48
42
|
"scripts": {
|
|
49
|
-
"build": "bun
|
|
50
|
-
"typecheck": "bun
|
|
43
|
+
"build": "bun build.ts",
|
|
44
|
+
"typecheck": "bun tsc --noEmit",
|
|
51
45
|
"prepublishOnly": "bun run build"
|
|
52
46
|
},
|
|
53
47
|
"dependencies": {
|
|
54
|
-
"@stacksjs/arrays": "
|
|
55
|
-
"@stacksjs/error-handling": "
|
|
56
|
-
"@stacksjs/path": "
|
|
57
|
-
"@stacksjs/strings": "
|
|
58
|
-
"@stacksjs/types": "
|
|
48
|
+
"@stacksjs/arrays": "0.64.6",
|
|
49
|
+
"@stacksjs/error-handling": "0.64.6",
|
|
50
|
+
"@stacksjs/path": "0.64.6",
|
|
51
|
+
"@stacksjs/strings": "0.64.6",
|
|
52
|
+
"@stacksjs/types": "0.64.6",
|
|
59
53
|
"@types/archiver": "^6.0.2",
|
|
60
54
|
"archiver": "^7.0.1",
|
|
61
|
-
"fast-glob": "^3.3.2",
|
|
62
55
|
"fs-extra": "^11.2.0",
|
|
63
|
-
"
|
|
56
|
+
"tinyglobby": "^0.2.9",
|
|
57
|
+
"unstorage": "^1.12.0"
|
|
64
58
|
},
|
|
65
59
|
"devDependencies": {
|
|
66
|
-
"@stacksjs/development": "
|
|
60
|
+
"@stacksjs/development": "0.64.6"
|
|
67
61
|
}
|
|
68
62
|
}
|
package/src/copy.ts
CHANGED
|
@@ -7,8 +7,10 @@ export function copy(src: string | string[], dest: string, exclude: string[] = [
|
|
|
7
7
|
src.forEach((file) => {
|
|
8
8
|
copy(file, dest, exclude)
|
|
9
9
|
})
|
|
10
|
-
}
|
|
11
|
-
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
if (fs.statSync(src).isDirectory())
|
|
13
|
+
copyFolder(src, dest, exclude)
|
|
12
14
|
else copyFile(src, dest)
|
|
13
15
|
}
|
|
14
16
|
}
|
|
@@ -18,7 +20,8 @@ export function copyFile(src: string, dest: string): void {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
export function copyFolder(src: string, dest: string, exclude: string[] = []): void {
|
|
21
|
-
if (!fs.existsSync(dest))
|
|
23
|
+
if (!fs.existsSync(dest))
|
|
24
|
+
fs.mkdirSync(dest, { recursive: true })
|
|
22
25
|
|
|
23
26
|
if (fs.existsSync(src)) {
|
|
24
27
|
fs.readdirSync(src).forEach((file) => {
|
|
@@ -26,7 +29,8 @@ export function copyFolder(src: string, dest: string, exclude: string[] = []): v
|
|
|
26
29
|
const srcPath = join(src, file)
|
|
27
30
|
const destPath = join(dest, file)
|
|
28
31
|
|
|
29
|
-
if (fs.statSync(srcPath).isDirectory())
|
|
32
|
+
if (fs.statSync(srcPath).isDirectory())
|
|
33
|
+
copyFolder(srcPath, destPath, exclude)
|
|
30
34
|
else fs.copyFileSync(srcPath, destPath)
|
|
31
35
|
}
|
|
32
36
|
})
|
package/src/delete.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { italic, log } from '@stacksjs/cli'
|
|
2
1
|
import type { Result } from '@stacksjs/error-handling'
|
|
2
|
+
import { italic, log } from '@stacksjs/cli'
|
|
3
3
|
import { err, handleError, ok } from '@stacksjs/error-handling'
|
|
4
4
|
import { join } from '@stacksjs/path'
|
|
5
5
|
import { isFile } from './files'
|
|
@@ -16,7 +16,8 @@ export function deleteFolder(path: string): Promise<Result<string, Error>> {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
return resolve(ok(`Path ${path} was not a directory`))
|
|
19
|
-
}
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
20
21
|
return reject(err(error))
|
|
21
22
|
}
|
|
22
23
|
})
|
|
@@ -26,12 +27,14 @@ export async function isDirectoryEmpty(path: string): Promise<Result<boolean, Er
|
|
|
26
27
|
return new Promise((resolve, reject) => {
|
|
27
28
|
try {
|
|
28
29
|
if (fs.statSync(path).isDirectory()) {
|
|
29
|
-
if (fs.readdirSync(path).length === 0)
|
|
30
|
+
if (fs.readdirSync(path).length === 0)
|
|
31
|
+
return resolve(ok(true))
|
|
30
32
|
return resolve(ok(false))
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
return resolve(ok(false))
|
|
34
|
-
}
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
35
38
|
return reject(err(error))
|
|
36
39
|
}
|
|
37
40
|
})
|
|
@@ -50,7 +53,8 @@ export async function deleteEmptyFolder(path: string): Promise<Result<string, Er
|
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
return resolve(ok(`Path ${path} was not a directory`))
|
|
53
|
-
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
54
58
|
return reject(err(error))
|
|
55
59
|
}
|
|
56
60
|
})
|
|
@@ -58,19 +62,22 @@ export async function deleteEmptyFolder(path: string): Promise<Result<string, Er
|
|
|
58
62
|
|
|
59
63
|
export async function deleteEmptyFolders(dir: string): Promise<Result<string, Error>> {
|
|
60
64
|
try {
|
|
61
|
-
if (!fs.existsSync(dir))
|
|
65
|
+
if (!fs.existsSync(dir))
|
|
66
|
+
return ok(`Path ${dir} does not exist`)
|
|
62
67
|
|
|
63
68
|
const files = fs.readdirSync(dir)
|
|
64
69
|
for (const file of files) {
|
|
65
70
|
const p = join(dir, file)
|
|
66
71
|
if (isFolder(p)) {
|
|
67
|
-
if (fs.readdirSync(p).length === 0)
|
|
72
|
+
if (fs.readdirSync(p).length === 0)
|
|
73
|
+
fs.rmSync(p, { recursive: true, force: true })
|
|
68
74
|
else await deleteEmptyFolders(p)
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
return ok(`Deleted empty folders located in ${dir}`)
|
|
73
|
-
}
|
|
79
|
+
}
|
|
80
|
+
catch (error: any) {
|
|
74
81
|
return err(error)
|
|
75
82
|
}
|
|
76
83
|
}
|
|
@@ -84,14 +91,16 @@ export function deleteFile(path: string): Promise<Result<string, Error>> {
|
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
return resolve(ok(`Path ${path} was not a file`))
|
|
87
|
-
}
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
88
96
|
return reject(err(error))
|
|
89
97
|
}
|
|
90
98
|
})
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
export async function deleteGlob(path: string): Promise<Result<string, Error>> {
|
|
94
|
-
if (!path.includes('*'))
|
|
102
|
+
if (!path.includes('*'))
|
|
103
|
+
return err(handleError(`Path ${path} does not contain a glob`))
|
|
95
104
|
|
|
96
105
|
const directories = await glob([path], { onlyDirectories: true })
|
|
97
106
|
|
|
@@ -109,11 +118,14 @@ export async function deleteGlob(path: string): Promise<Result<string, Error>> {
|
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
export async function del(path: string): Promise<Result<string, Error>> {
|
|
112
|
-
if (isFile(path))
|
|
121
|
+
if (isFile(path))
|
|
122
|
+
return await deleteFile(path)
|
|
113
123
|
|
|
114
|
-
if (isFolder(path))
|
|
124
|
+
if (isFolder(path))
|
|
125
|
+
return await deleteFolder(path)
|
|
115
126
|
|
|
116
|
-
if (path.includes('*'))
|
|
127
|
+
if (path.includes('*'))
|
|
128
|
+
return await deleteGlob(path)
|
|
117
129
|
|
|
118
130
|
return err(handleError(`Path ${path} cannot be deleted due to an unhandled condition. Please report this issue.`))
|
|
119
131
|
}
|
package/src/files.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { JsonFile, PackageJson, TextFile } from '@stacksjs/types'
|
|
1
2
|
import { contains } from '@stacksjs/arrays'
|
|
3
|
+
import { log } from '@stacksjs/logging'
|
|
2
4
|
import { dirname, join, path as p } from '@stacksjs/path'
|
|
3
|
-
import { createFolder, isFolder } from '@stacksjs/storage'
|
|
4
5
|
import { detectIndent, detectNewline } from '@stacksjs/strings'
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
6
|
+
import { createFolder, isFolder } from './'
|
|
7
|
+
import { existsSync, fs } from './fs'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Reads a JSON file and returns the parsed data.
|
|
@@ -20,7 +21,7 @@ export async function readJsonFile(name: string, cwd?: string): Promise<JsonFile
|
|
|
20
21
|
/**
|
|
21
22
|
* Reads a package.json file and returns the parsed data.
|
|
22
23
|
*/
|
|
23
|
-
export async function readPackageJson(name: string, cwd?: string) {
|
|
24
|
+
export async function readPackageJson(name: string, cwd?: string): Promise<PackageJson> {
|
|
24
25
|
const file = await readJsonFile(name, cwd)
|
|
25
26
|
return file.data as PackageJson
|
|
26
27
|
}
|
|
@@ -32,7 +33,8 @@ export async function writeFile(path: string, data: any): Promise<number> {
|
|
|
32
33
|
// export async function writeFile(path: Path, data: Data): Promise<number> {
|
|
33
34
|
if (typeof path === 'string') {
|
|
34
35
|
const dirPath = dirname(path)
|
|
35
|
-
if (!(await existsSync(dirPath)))
|
|
36
|
+
if (!(await existsSync(dirPath)))
|
|
37
|
+
await createFolder(dirPath)
|
|
36
38
|
|
|
37
39
|
return await Bun.write(Bun.file(path), data)
|
|
38
40
|
}
|
|
@@ -46,7 +48,8 @@ export async function writeFile(path: string, data: any): Promise<number> {
|
|
|
46
48
|
export async function writeJsonFile(file: JsonFile): Promise<number> {
|
|
47
49
|
let json = JSON.stringify(file.data, undefined, file.indent)
|
|
48
50
|
|
|
49
|
-
if (file.newline)
|
|
51
|
+
if (file.newline)
|
|
52
|
+
json += file.newline
|
|
50
53
|
|
|
51
54
|
return writeTextFile({ ...file, data: json })
|
|
52
55
|
}
|
|
@@ -58,13 +61,15 @@ export function readTextFile(name: string, cwd?: string): Promise<TextFile> {
|
|
|
58
61
|
return new Promise((resolve, reject) => {
|
|
59
62
|
let filePath: string
|
|
60
63
|
|
|
61
|
-
if (cwd)
|
|
64
|
+
if (cwd)
|
|
65
|
+
filePath = join(cwd, name)
|
|
62
66
|
else filePath = name
|
|
63
67
|
|
|
64
68
|
fs.readFile(filePath, 'utf8', (err, text) => {
|
|
65
69
|
if (err) {
|
|
66
70
|
reject(err)
|
|
67
|
-
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
68
73
|
resolve({
|
|
69
74
|
path: filePath,
|
|
70
75
|
data: text,
|
|
@@ -102,7 +107,9 @@ export function doesNotExist(path: string): boolean {
|
|
|
102
107
|
export function hasFiles(folder: string): boolean {
|
|
103
108
|
try {
|
|
104
109
|
return fs.readdirSync(folder).length > 0
|
|
105
|
-
}
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
log.debug(`Error reading folder: ${folder}`, err)
|
|
106
113
|
return false
|
|
107
114
|
}
|
|
108
115
|
}
|
|
@@ -115,14 +122,16 @@ export function hasFunctions(): boolean {
|
|
|
115
122
|
return hasFiles(p.functionsPath())
|
|
116
123
|
}
|
|
117
124
|
|
|
118
|
-
export function deleteFiles(dir: string, exclude: string[] = []) {
|
|
125
|
+
export function deleteFiles(dir: string, exclude: string[] = []): void {
|
|
119
126
|
if (fs.existsSync(dir)) {
|
|
120
127
|
fs.readdirSync(dir).forEach((file) => {
|
|
121
128
|
const p = join(dir, file)
|
|
122
129
|
if (fs.statSync(p).isDirectory()) {
|
|
123
|
-
if (fs.readdirSync(p).length === 0)
|
|
130
|
+
if (fs.readdirSync(p).length === 0)
|
|
131
|
+
fs.rmSync(p, { recursive: true, force: true })
|
|
124
132
|
else deleteFiles(p, exclude)
|
|
125
|
-
}
|
|
133
|
+
}
|
|
134
|
+
else if (!contains(p, exclude)) {
|
|
126
135
|
fs.rmSync(p)
|
|
127
136
|
}
|
|
128
137
|
})
|
|
@@ -137,17 +146,20 @@ export function getFiles(dir: string, exclude: string[] = []): string[] {
|
|
|
137
146
|
file = join(dir, file)
|
|
138
147
|
const stat = fs.statSync(file)
|
|
139
148
|
|
|
140
|
-
if (stat.isDirectory())
|
|
141
|
-
|
|
149
|
+
if (stat.isDirectory())
|
|
150
|
+
results = results.concat(getFiles(file, exclude))
|
|
151
|
+
else if (!contains(file, exclude))
|
|
152
|
+
results.push(file)
|
|
142
153
|
})
|
|
143
154
|
|
|
144
155
|
return results
|
|
145
156
|
}
|
|
146
157
|
|
|
147
|
-
export function put(path: string, contents: string) {
|
|
158
|
+
export function put(path: string, contents: string): void {
|
|
148
159
|
const dirPath = dirname(path)
|
|
149
160
|
|
|
150
|
-
if (!fs.existsSync(dirPath))
|
|
161
|
+
if (!fs.existsSync(dirPath))
|
|
162
|
+
fs.mkdirSync(dirPath, { recursive: true })
|
|
151
163
|
|
|
152
164
|
fs.writeFileSync(path, contents, 'utf-8')
|
|
153
165
|
}
|
|
@@ -156,7 +168,23 @@ export async function get(path: string): Promise<string> {
|
|
|
156
168
|
return Bun.file(path).text()
|
|
157
169
|
}
|
|
158
170
|
|
|
159
|
-
export
|
|
171
|
+
export interface Files {
|
|
172
|
+
readJsonFile: typeof readJsonFile
|
|
173
|
+
readPackageJson: typeof readPackageJson
|
|
174
|
+
readTextFile: typeof readTextFile
|
|
175
|
+
writeJsonFile: typeof writeJsonFile
|
|
176
|
+
writeTextFile: typeof writeTextFile
|
|
177
|
+
isFile: typeof isFile
|
|
178
|
+
hasFiles: typeof hasFiles
|
|
179
|
+
hasComponents: typeof hasComponents
|
|
180
|
+
hasFunctions: typeof hasFunctions
|
|
181
|
+
deleteFiles: typeof deleteFiles
|
|
182
|
+
getFiles: typeof getFiles
|
|
183
|
+
put: typeof put
|
|
184
|
+
get: typeof get
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export const files: Files = {
|
|
160
188
|
readJsonFile,
|
|
161
189
|
readPackageJson,
|
|
162
190
|
readTextFile,
|
package/src/folders.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { fs } from './fs'
|
|
|
7
7
|
export function isFolder(path: string): boolean {
|
|
8
8
|
try {
|
|
9
9
|
return fs.statSync(path).isDirectory()
|
|
10
|
-
}
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
11
12
|
return false
|
|
12
13
|
}
|
|
13
14
|
}
|
|
@@ -20,14 +21,15 @@ export function isDir(path: string): boolean {
|
|
|
20
21
|
return isFolder(path)
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
export function doesFolderExist(path: string) {
|
|
24
|
+
export function doesFolderExist(path: string): boolean {
|
|
24
25
|
return fs.existsSync(path)
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
export function createFolder(dir: string): Promise<void> {
|
|
28
29
|
return new Promise((resolve, reject) => {
|
|
29
30
|
fs.mkdirs(dir, (err: any) => {
|
|
30
|
-
if (err)
|
|
31
|
+
if (err)
|
|
32
|
+
reject(err)
|
|
31
33
|
else resolve()
|
|
32
34
|
})
|
|
33
35
|
})
|
|
@@ -39,7 +41,14 @@ export function getFolders(dir: string): string[] {
|
|
|
39
41
|
})
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
export
|
|
44
|
+
export interface Folders {
|
|
45
|
+
isFolder: (path: string) => boolean
|
|
46
|
+
doesFolderExist: (path: string) => boolean
|
|
47
|
+
createFolder: (dir: string) => Promise<void>
|
|
48
|
+
getFolders: (dir: string) => string[]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const folders: Folders = {
|
|
43
52
|
isFolder,
|
|
44
53
|
doesFolderExist,
|
|
45
54
|
createFolder,
|
package/src/fs.ts
CHANGED
|
@@ -5,4 +5,4 @@ export async function exists(path: string): Promise<boolean> {
|
|
|
5
5
|
return await existsSync(path)
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export { fs,
|
|
8
|
+
export { existsSync, fs, fsWatch, mkdirSync, readFileSync, watchFile, writeFileSync }
|
package/src/glob.ts
CHANGED
|
@@ -1 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import { globSync as gs } from 'tinyglobby'
|
|
2
|
+
|
|
3
|
+
export { glob } from 'tinyglobby'
|
|
4
|
+
|
|
5
|
+
export interface GlobOptions {
|
|
6
|
+
absolute?: boolean
|
|
7
|
+
cwd?: string
|
|
8
|
+
patterns?: string[]
|
|
9
|
+
ignore?: string[]
|
|
10
|
+
dot?: boolean
|
|
11
|
+
deep?: number
|
|
12
|
+
expandDirectories?: boolean
|
|
13
|
+
onlyDirectories?: boolean
|
|
14
|
+
onlyFiles?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function globSync(patterns: string | string[], options?: Omit<GlobOptions, 'patterns'>): string[] {
|
|
18
|
+
if (typeof patterns === 'string') {
|
|
19
|
+
return gs([patterns], options)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return gs(patterns, options)
|
|
23
|
+
}
|
package/src/hash.ts
CHANGED
package/src/helpers.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url'
|
|
|
2
2
|
import { dirname } from '@stacksjs/path'
|
|
3
3
|
import { fs } from './fs'
|
|
4
4
|
|
|
5
|
-
export const _dirname = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url))
|
|
5
|
+
export const _dirname: string = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url))
|
|
6
6
|
|
|
7
7
|
export function updateConfigFile(filePath: string, newConfig: Record<string, unknown>): Promise<void> {
|
|
8
8
|
return new Promise((resolve, reject) => {
|
|
@@ -13,13 +13,19 @@ export function updateConfigFile(filePath: string, newConfig: Record<string, unk
|
|
|
13
13
|
try {
|
|
14
14
|
fs.writeFileSync(filePath, JSON.stringify(config, null, 2))
|
|
15
15
|
resolve()
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
17
18
|
reject(error)
|
|
18
19
|
}
|
|
19
20
|
})
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
interface Helpers {
|
|
24
|
+
_dirname: string
|
|
25
|
+
updateConfigFile: (filePath: string, newConfig: Record<string, unknown>) => Promise<void>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const helpers: Helpers = {
|
|
23
29
|
_dirname,
|
|
24
30
|
updateConfigFile,
|
|
25
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
export * from './copy'
|
|
2
|
+
export * from './delete'
|
|
1
3
|
export * from './files'
|
|
2
4
|
export * from './folders'
|
|
3
|
-
export * from './hash'
|
|
4
|
-
export * from './helpers'
|
|
5
5
|
export * from './fs'
|
|
6
|
-
export * from './copy'
|
|
7
6
|
export * from './glob'
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './
|
|
10
|
-
|
|
7
|
+
export * from './hash'
|
|
8
|
+
export * from './helpers'
|
|
11
9
|
export * as storage from './storage'
|
|
10
|
+
|
|
11
|
+
export * from './zip'
|
package/src/move.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { err, handleError, ok, type Result } from '@stacksjs/error-handling'
|
|
2
2
|
import { log } from '@stacksjs/logging'
|
|
3
3
|
import { path } from '@stacksjs/path'
|
|
4
4
|
import { fs } from './fs'
|
|
@@ -20,7 +20,8 @@ export async function move(
|
|
|
20
20
|
const to = path.resolve(dest, path.basename(file))
|
|
21
21
|
const result = await rename(from, to, options)
|
|
22
22
|
|
|
23
|
-
if (result.isErr())
|
|
23
|
+
if (result.isErr())
|
|
24
|
+
return log.error(result.error)
|
|
24
25
|
})
|
|
25
26
|
|
|
26
27
|
await Promise.all(operations)
|
|
@@ -38,7 +39,8 @@ export async function move(
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
return ok({ message: 'File moved successfully' })
|
|
41
|
-
}
|
|
42
|
+
}
|
|
43
|
+
catch (error: any) {
|
|
42
44
|
return err(handleError(error))
|
|
43
45
|
}
|
|
44
46
|
}
|
|
@@ -52,14 +54,17 @@ export async function rename(
|
|
|
52
54
|
try {
|
|
53
55
|
// Check if the "to" directory exists
|
|
54
56
|
const dir = path.dirname(to)
|
|
55
|
-
if (!fs.existsSync(dir))
|
|
57
|
+
if (!fs.existsSync(dir))
|
|
58
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
56
59
|
|
|
57
60
|
// Ensure the "from" file exists
|
|
58
|
-
if (!fs.existsSync(from))
|
|
61
|
+
if (!fs.existsSync(from))
|
|
62
|
+
return reject(err(new Error(`File or directory does not exist: ${from}`)))
|
|
59
63
|
|
|
60
64
|
// Ensure the "to" file does not exist
|
|
61
65
|
if (fs.existsSync(to)) {
|
|
62
|
-
if (!options?.overwrite)
|
|
66
|
+
if (!options?.overwrite)
|
|
67
|
+
return reject(err(new Error(`File or directory already exists: ${to}`)))
|
|
63
68
|
|
|
64
69
|
fs.unlinkSync(to)
|
|
65
70
|
}
|
|
@@ -68,8 +73,10 @@ export async function rename(
|
|
|
68
73
|
fs.renameSync(from, to)
|
|
69
74
|
|
|
70
75
|
return resolve(ok({ message: 'File moved successfully' }))
|
|
71
|
-
}
|
|
72
|
-
|
|
76
|
+
}
|
|
77
|
+
catch (error: any) {
|
|
78
|
+
if (error.code === 'ENOENT')
|
|
79
|
+
log.error('File or directory does not exist\n\n', error)
|
|
73
80
|
else log.error(error)
|
|
74
81
|
|
|
75
82
|
return reject(err(new Error(error)))
|
package/src/storage.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
export * from './copy'
|
|
2
|
+
export * from './delete'
|
|
1
3
|
export * from './files'
|
|
2
4
|
export * from './folders'
|
|
3
|
-
export * from './helpers'
|
|
4
|
-
export * from './fs'
|
|
5
5
|
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
6
|
+
export * from './fs'
|
|
7
|
+
export * from './helpers'
|
|
8
8
|
export * from './move'
|
|
9
|
-
export * from './zip'
|
|
10
9
|
export * from './visibility'
|
|
10
|
+
export * from './zip'
|
package/src/visibility.ts
CHANGED
package/src/zip.ts
CHANGED
|
@@ -1,53 +1,61 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Result } from '@stacksjs/error-handling'
|
|
2
|
+
import type { CommandError, Subprocess } from '@stacksjs/types'
|
|
2
3
|
import type { ZlibCompressionOptions } from 'bun'
|
|
4
|
+
import { runCommand } from '@stacksjs/cli'
|
|
3
5
|
|
|
4
6
|
interface ZipOptions {
|
|
5
7
|
cwd?: string
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
export async function zip(
|
|
10
|
+
export async function zip(
|
|
11
|
+
from: string | string[],
|
|
12
|
+
to?: string,
|
|
13
|
+
options?: ZipOptions,
|
|
14
|
+
): Promise<Result<Subprocess, CommandError>> {
|
|
9
15
|
const toPath = to || 'archive.zip'
|
|
10
16
|
const fromPath = Array.isArray(from) ? from.join(' ') : from
|
|
11
17
|
|
|
12
|
-
if (Array.isArray(from))
|
|
18
|
+
if (Array.isArray(from))
|
|
19
|
+
return runCommand(`zip -r ${toPath} ${fromPath}`, options)
|
|
13
20
|
|
|
14
21
|
return runCommand(`zip -r ${to} ${from}`, options)
|
|
15
22
|
}
|
|
16
23
|
|
|
17
|
-
export async function unzip(paths: string | string[]) {
|
|
18
|
-
if (Array.isArray(paths))
|
|
24
|
+
export async function unzip(paths: string | string[]): Promise<Result<Subprocess, CommandError>> {
|
|
25
|
+
if (Array.isArray(paths))
|
|
26
|
+
return runCommand(`unzip ${paths.join(' ')}`)
|
|
19
27
|
|
|
20
28
|
return runCommand(`unzip ${paths}`)
|
|
21
29
|
}
|
|
22
30
|
|
|
23
|
-
export function archive(paths: string | string[]) {
|
|
31
|
+
export function archive(paths: string | string[]): Promise<Result<Subprocess, CommandError>> {
|
|
24
32
|
return zip(paths)
|
|
25
33
|
}
|
|
26
34
|
|
|
27
|
-
export function unarchive(paths: string | string[]) {
|
|
35
|
+
export function unarchive(paths: string | string[]): Promise<Result<Subprocess, CommandError>> {
|
|
28
36
|
return unzip(paths)
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
export function compress(paths: string[]) {
|
|
39
|
+
export function compress(paths: string[]): Promise<Result<Subprocess, CommandError>> {
|
|
32
40
|
return zip(paths)
|
|
33
41
|
}
|
|
34
42
|
|
|
35
|
-
export function decompress(paths: string | string[]) {
|
|
43
|
+
export function decompress(paths: string | string[]): Promise<Result<Subprocess, CommandError>> {
|
|
36
44
|
return unzip(paths)
|
|
37
45
|
}
|
|
38
46
|
|
|
39
|
-
export function gzipSync(data: Uint8Array, options?: ZlibCompressionOptions) {
|
|
47
|
+
export function gzipSync(data: Uint8Array, options?: ZlibCompressionOptions): Uint8Array {
|
|
40
48
|
return Bun.gzipSync(data, options) // buffer extends Uint8Array
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
export function gunzipSync(data: Uint8Array) {
|
|
51
|
+
export function gunzipSync(data: Uint8Array): Uint8Array {
|
|
44
52
|
return Bun.gunzipSync(data) // decompressed
|
|
45
53
|
}
|
|
46
54
|
|
|
47
|
-
export function deflateSync(data: Uint8Array, options?: ZlibCompressionOptions) {
|
|
55
|
+
export function deflateSync(data: Uint8Array, options?: ZlibCompressionOptions): Uint8Array {
|
|
48
56
|
return Bun.deflateSync(data, options) // compressed
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
export function inflateSync(data: Uint8Array) {
|
|
59
|
+
export function inflateSync(data: Uint8Array): Uint8Array {
|
|
52
60
|
return Bun.inflateSync(data) // decompressed
|
|
53
61
|
}
|