@stacksjs/storage 0.38.3
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/LICENSE.md +21 -0
- package/README.md +74 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.mjs +139 -0
- package/package.json +60 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Stacks Storage
|
|
2
|
+
|
|
3
|
+
This package contains the Stacks File System source.
|
|
4
|
+
|
|
5
|
+
## โ๏ธ Features
|
|
6
|
+
|
|
7
|
+
- Easily create your own cloud storage
|
|
8
|
+
- Driver-based architecture
|
|
9
|
+
- Laravel-like Storage API
|
|
10
|
+
- Drop-in Node.js module replacement
|
|
11
|
+
|
|
12
|
+
## ๐ค Usage
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm i -D @stacksjs/storage
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Now, you can use it in your project:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import {
|
|
22
|
+
_dirname,
|
|
23
|
+
readJsonFile,
|
|
24
|
+
writeJsonFile,
|
|
25
|
+
readTextFile,
|
|
26
|
+
writeTextFile,
|
|
27
|
+
isFolder,
|
|
28
|
+
isFile,
|
|
29
|
+
hasFiles,
|
|
30
|
+
hasComponents,
|
|
31
|
+
hasFunctions,
|
|
32
|
+
copyFolder,
|
|
33
|
+
deleteFolder,
|
|
34
|
+
deleteFiles,
|
|
35
|
+
deleteEmptyFolders,
|
|
36
|
+
doesFolderExist,
|
|
37
|
+
fs, // fs-extra Node module
|
|
38
|
+
fileURLToPath,
|
|
39
|
+
} from '@stacksjs/storage'
|
|
40
|
+
|
|
41
|
+
// wip
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To view the full documentation, please visit [https://stacksjs.dev/storage](https://stacksjs.dev/storage).
|
|
45
|
+
|
|
46
|
+
## ๐งช Testing
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pnpm test
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## ๐ Changelog
|
|
53
|
+
|
|
54
|
+
Please see our [releases](https://github.com/stacksjs/stacks/releases) page for more information on what has changed recently.
|
|
55
|
+
|
|
56
|
+
## ๐ช๐ผ Contributing
|
|
57
|
+
|
|
58
|
+
Please see [CONTRIBUTING](../../.github/CONTRIBUTING.md) for details.
|
|
59
|
+
|
|
60
|
+
## ๐ Community
|
|
61
|
+
|
|
62
|
+
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
|
|
63
|
+
|
|
64
|
+
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)
|
|
65
|
+
|
|
66
|
+
For casual chit-chat with others using this package:
|
|
67
|
+
|
|
68
|
+
[Join the Open Web Discord Server](https://discord.ow3.org)
|
|
69
|
+
|
|
70
|
+
## ๐ License
|
|
71
|
+
|
|
72
|
+
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
73
|
+
|
|
74
|
+
Made with โค๏ธ
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import type { JsonFile, TextFile } from '@stacksjs/types';
|
|
4
|
+
export declare const _dirname: any;
|
|
5
|
+
/**
|
|
6
|
+
* Reads a JSON file and returns the parsed data.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readJsonFile(name: string, cwd?: string): Promise<JsonFile>;
|
|
9
|
+
/**
|
|
10
|
+
* Writes the given data to the specified JSON file.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writeJsonFile(file: JsonFile): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Reads a text file and returns its contents.
|
|
15
|
+
*/
|
|
16
|
+
export declare function readTextFile(name: string, cwd?: string): Promise<TextFile>;
|
|
17
|
+
/**
|
|
18
|
+
* Writes the given text to the specified file.
|
|
19
|
+
*/
|
|
20
|
+
export declare function writeTextFile(file: TextFile): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Determine whether a path is a folder.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isFolder(path: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Determine whether a path is a file.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isFile(path: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Determine whether a folder has any files in it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function hasFiles(folder: string): boolean;
|
|
33
|
+
export declare function hasComponents(): boolean;
|
|
34
|
+
export declare function hasFunctions(): boolean;
|
|
35
|
+
export declare function copyFolder(src: string, dest: string, exclude?: string[]): void;
|
|
36
|
+
export declare function deleteFolder(path: string): Promise<void>;
|
|
37
|
+
export declare function deleteFiles(dir: string, exclude?: string[]): void;
|
|
38
|
+
export declare function deleteEmptyFolders(dir: string): void;
|
|
39
|
+
export declare function doesFolderExist(path: string): any;
|
|
40
|
+
export declare const filesystem: {
|
|
41
|
+
_dirname: any;
|
|
42
|
+
readJsonFile: typeof readJsonFile;
|
|
43
|
+
writeJsonFile: typeof writeJsonFile;
|
|
44
|
+
readTextFile: typeof readTextFile;
|
|
45
|
+
writeTextFile: typeof writeTextFile;
|
|
46
|
+
isFolder: typeof isFolder;
|
|
47
|
+
isFile: typeof isFile;
|
|
48
|
+
hasFiles: typeof hasFiles;
|
|
49
|
+
hasComponents: typeof hasComponents;
|
|
50
|
+
hasFunctions: typeof hasFunctions;
|
|
51
|
+
copyFolder: typeof copyFolder;
|
|
52
|
+
deleteFolder: typeof deleteFolder;
|
|
53
|
+
deleteFiles: typeof deleteFiles;
|
|
54
|
+
deleteEmptyFolders: typeof deleteEmptyFolders;
|
|
55
|
+
doesFolderExist: typeof doesFolderExist;
|
|
56
|
+
fs: any;
|
|
57
|
+
fileURLToPath: typeof fileURLToPath;
|
|
58
|
+
};
|
|
59
|
+
export default filesystem;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import detectIndent from "detect-indent";
|
|
4
|
+
import { detectNewline } from "detect-newline";
|
|
5
|
+
import { componentsPath, dirname, functionsPath, join, projectPath } from "@stacksjs/path";
|
|
6
|
+
import { contains } from "@stacksjs/arrays";
|
|
7
|
+
export const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
export async function readJsonFile(name, cwd) {
|
|
9
|
+
const file = await readTextFile(name, cwd);
|
|
10
|
+
const data = JSON.parse(file.data);
|
|
11
|
+
const indent = detectIndent(file.data).indent;
|
|
12
|
+
const newline = detectNewline(file.data);
|
|
13
|
+
return { ...file, data, indent, newline };
|
|
14
|
+
}
|
|
15
|
+
export async function writeJsonFile(file) {
|
|
16
|
+
let json = JSON.stringify(file.data, void 0, file.indent);
|
|
17
|
+
if (file.newline)
|
|
18
|
+
json += file.newline;
|
|
19
|
+
return writeTextFile({ ...file, data: json });
|
|
20
|
+
}
|
|
21
|
+
export function readTextFile(name, cwd) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
let filePath;
|
|
24
|
+
if (cwd)
|
|
25
|
+
filePath = join(cwd, name);
|
|
26
|
+
else
|
|
27
|
+
filePath = name;
|
|
28
|
+
fs.readFile(filePath, "utf8", (err, text) => {
|
|
29
|
+
if (err) {
|
|
30
|
+
reject(err);
|
|
31
|
+
} else {
|
|
32
|
+
resolve({
|
|
33
|
+
path: filePath,
|
|
34
|
+
data: text
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export function writeTextFile(file) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
fs.writeFile(file.path, file.data, (err) => {
|
|
43
|
+
if (err)
|
|
44
|
+
reject(err);
|
|
45
|
+
else
|
|
46
|
+
resolve();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export function isFolder(path) {
|
|
51
|
+
try {
|
|
52
|
+
return fs.statSync(path).isDirectory();
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function isFile(path) {
|
|
58
|
+
return fs.existsSync(projectPath(path));
|
|
59
|
+
}
|
|
60
|
+
export function hasFiles(folder) {
|
|
61
|
+
return fs.readdirSync(folder).length > 0;
|
|
62
|
+
}
|
|
63
|
+
export function hasComponents() {
|
|
64
|
+
return hasFiles(componentsPath());
|
|
65
|
+
}
|
|
66
|
+
export function hasFunctions() {
|
|
67
|
+
return hasFiles(functionsPath());
|
|
68
|
+
}
|
|
69
|
+
export function copyFolder(src, dest, exclude = []) {
|
|
70
|
+
if (!fs.existsSync(dest))
|
|
71
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
72
|
+
if (fs.existsSync(src)) {
|
|
73
|
+
fs.readdirSync(src).forEach((file) => {
|
|
74
|
+
if (!contains(join(src, file), exclude)) {
|
|
75
|
+
const srcPath = join(src, file);
|
|
76
|
+
const destPath = join(dest, file);
|
|
77
|
+
if (fs.statSync(srcPath).isDirectory())
|
|
78
|
+
copyFolder(srcPath, destPath, exclude);
|
|
79
|
+
else
|
|
80
|
+
fs.copyFileSync(srcPath, destPath);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export async function deleteFolder(path) {
|
|
86
|
+
if (fs.statSync(path).isDirectory())
|
|
87
|
+
await fs.rmSync(path, { recursive: true, force: true });
|
|
88
|
+
}
|
|
89
|
+
export function deleteFiles(dir, exclude = []) {
|
|
90
|
+
if (fs.existsSync(dir)) {
|
|
91
|
+
fs.readdirSync(dir).forEach((file) => {
|
|
92
|
+
const p = join(dir, file);
|
|
93
|
+
if (fs.statSync(p).isDirectory()) {
|
|
94
|
+
if (fs.readdirSync(p).length === 0)
|
|
95
|
+
fs.rmSync(p, { recursive: true, force: true });
|
|
96
|
+
else
|
|
97
|
+
deleteFiles(p, exclude);
|
|
98
|
+
} else if (!contains(p, exclude)) {
|
|
99
|
+
fs.rmSync(p);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function deleteEmptyFolders(dir) {
|
|
105
|
+
if (fs.existsSync(dir)) {
|
|
106
|
+
fs.readdirSync(dir).forEach((file) => {
|
|
107
|
+
const p = join(dir, file);
|
|
108
|
+
if (fs.statSync(p).isDirectory()) {
|
|
109
|
+
if (fs.readdirSync(p).length === 0)
|
|
110
|
+
fs.rmSync(p, { recursive: true, force: true });
|
|
111
|
+
else
|
|
112
|
+
deleteEmptyFolders(p);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function doesFolderExist(path) {
|
|
118
|
+
return fs.existsSync(path);
|
|
119
|
+
}
|
|
120
|
+
export const filesystem = {
|
|
121
|
+
_dirname,
|
|
122
|
+
readJsonFile,
|
|
123
|
+
writeJsonFile,
|
|
124
|
+
readTextFile,
|
|
125
|
+
writeTextFile,
|
|
126
|
+
isFolder,
|
|
127
|
+
isFile,
|
|
128
|
+
hasFiles,
|
|
129
|
+
hasComponents,
|
|
130
|
+
hasFunctions,
|
|
131
|
+
copyFolder,
|
|
132
|
+
deleteFolder,
|
|
133
|
+
deleteFiles,
|
|
134
|
+
deleteEmptyFolders,
|
|
135
|
+
doesFolderExist,
|
|
136
|
+
fs,
|
|
137
|
+
fileURLToPath
|
|
138
|
+
};
|
|
139
|
+
export default filesystem;
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacksjs/storage",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.38.3",
|
|
5
|
+
"packageManager": "pnpm@7.15.0",
|
|
6
|
+
"description": "The Stacks file system.",
|
|
7
|
+
"author": "Chris Breuer",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/.stacks/core/storage#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
+
"directory": "./.stacks/core/storage"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/stacksjs/stacks/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"storage",
|
|
21
|
+
"fs",
|
|
22
|
+
"file system",
|
|
23
|
+
"api",
|
|
24
|
+
"collections",
|
|
25
|
+
"utilities",
|
|
26
|
+
"functions",
|
|
27
|
+
"stacks"
|
|
28
|
+
],
|
|
29
|
+
"main": "dist/index.mjs",
|
|
30
|
+
"module": "dist/index.mjs",
|
|
31
|
+
"types": "dist/index.d.ts",
|
|
32
|
+
"contributors": [
|
|
33
|
+
"Chris Breuer <chris@ow3.org>"
|
|
34
|
+
],
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=v18.12.1",
|
|
41
|
+
"pnpm": ">=7.15.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@stacksjs/arrays": "0.38.3",
|
|
45
|
+
"@stacksjs/types": "0.38.3",
|
|
46
|
+
"@stacksjs/utils": "0.38.3",
|
|
47
|
+
"fs-extra": "^10.1.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@stacksjs/path": "0.38.3",
|
|
51
|
+
"mkdist": "^0.4.0",
|
|
52
|
+
"typescript": "^4.8.4",
|
|
53
|
+
"unstorage": "^0.6.0"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "mkdist -d",
|
|
57
|
+
"dev": "mkdist -d",
|
|
58
|
+
"typecheck": "tsc --noEmit"
|
|
59
|
+
}
|
|
60
|
+
}
|