@arela/uploader 0.0.1 → 0.0.2
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/package.json +1 -1
- package/src/index.js +38 -11
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -39,30 +39,57 @@ const checkCredentials = async () => {
|
|
|
39
39
|
|
|
40
40
|
await checkCredentials();
|
|
41
41
|
|
|
42
|
+
function getCommonPathPrefix(paths) {
|
|
43
|
+
if (paths.length === 0) return '';
|
|
44
|
+
|
|
45
|
+
const splitPaths = paths.map((p) => path.resolve(p).split(path.sep));
|
|
46
|
+
|
|
47
|
+
const commonParts = [];
|
|
48
|
+
for (let i = 0; i < splitPaths[0].length; i++) {
|
|
49
|
+
const segment = splitPaths[0][i];
|
|
50
|
+
if (splitPaths.every((parts) => parts[i] === segment)) {
|
|
51
|
+
commonParts.push(segment);
|
|
52
|
+
} else {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return commonParts.length > 0 ? commonParts.join(path.sep) : '';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// const sleep = () => new Promise((resolve) => setTimeout(resolve, 5000));
|
|
61
|
+
|
|
42
62
|
program
|
|
43
63
|
.name('supabase-uploader')
|
|
44
|
-
.description('CLI to upload files
|
|
45
|
-
.argument('<source>', '
|
|
64
|
+
.description('CLI to upload files using glob patterns to Supabase Storage')
|
|
65
|
+
.argument('<source>', 'Glob pattern of files to upload')
|
|
46
66
|
.option('-p, --prefix <prefix>', 'Prefix path in bucket', '')
|
|
47
67
|
.action(async (source, options) => {
|
|
48
68
|
try {
|
|
49
|
-
|
|
50
|
-
const files =
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
// await sleep();
|
|
70
|
+
const files = await globby([`${source}/**/*`], { onlyFiles: true });
|
|
71
|
+
|
|
72
|
+
if (files.length === 0) {
|
|
73
|
+
console.error('⚠️ No matching files found for pattern:', source);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const baseDir = getCommonPathPrefix(
|
|
78
|
+
files.map((file) => path.dirname(file)),
|
|
79
|
+
);
|
|
53
80
|
|
|
54
81
|
for (const file of files) {
|
|
55
82
|
const content = fs.readFileSync(file);
|
|
56
|
-
const relativePath = path.relative(
|
|
83
|
+
const relativePath = path.relative(baseDir, file);
|
|
57
84
|
const uploadPath = options.prefix
|
|
58
|
-
? path.join(options.prefix, relativePath)
|
|
59
|
-
: relativePath;
|
|
85
|
+
? path.posix.join(options.prefix, relativePath).replace(/\\/g, '/')
|
|
86
|
+
: relativePath.replace(/\\/g, '/');
|
|
60
87
|
|
|
61
88
|
const contentType = mime.lookup(file) || 'application/octet-stream';
|
|
62
89
|
|
|
63
|
-
const {
|
|
90
|
+
const { error } = await supabase.storage
|
|
64
91
|
.from(bucket)
|
|
65
|
-
.upload(uploadPath
|
|
92
|
+
.upload(uploadPath, content, {
|
|
66
93
|
upsert: true,
|
|
67
94
|
contentType,
|
|
68
95
|
});
|