@micropue/drop-cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +307 -2
- package/package.json +52 -4
- package/src/cli.js +95 -0
- package/src/commands/auth.js +71 -0
- package/src/commands/deploy.js +81 -0
- package/src/commands/init.js +50 -0
- package/src/commands/status.js +38 -0
- package/src/commands/watch.js +380 -0
- package/src/index.js +9 -0
- package/src/lib/api.js +206 -0
- package/src/lib/auth.js +88 -0
- package/src/lib/config.js +159 -0
- package/src/lib/constants.js +22 -0
- package/src/lib/deployer.js +248 -0
- package/src/lib/discovery.js +329 -0
- package/src/lib/errors.js +59 -0
- package/src/lib/ignore.js +48 -0
- package/src/lib/prompt.js +58 -0
- package/src/lib/runtime.js +130 -0
- package/src/lib/snapshot.js +38 -0
- package/src/lib/terminal.js +45 -0
- package/src/lib/utils.js +189 -0
package/src/lib/utils.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { LIMITS, SUPPORTED_EXTENSIONS } from "./constants.js";
|
|
5
|
+
|
|
6
|
+
export function toPosixPath(value) {
|
|
7
|
+
return value.split(path.sep).join("/");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function sha256(value) {
|
|
11
|
+
return createHash("sha256").update(value).digest("hex");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function formatBytes(bytes) {
|
|
15
|
+
if (bytes < 1024) {
|
|
16
|
+
return `${bytes} B`;
|
|
17
|
+
}
|
|
18
|
+
if (bytes < 1024 * 1024) {
|
|
19
|
+
return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KiB`;
|
|
20
|
+
}
|
|
21
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function snapshotHash(files, folders = []) {
|
|
25
|
+
const manifest = [
|
|
26
|
+
...files.map((file) => `${file.path}\0${file.sha256}\n`),
|
|
27
|
+
...folders.map((folder) => `${folder}/\0\n`),
|
|
28
|
+
].join("");
|
|
29
|
+
return sha256(manifest);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function foldersFromFiles(files) {
|
|
33
|
+
const folders = new Set();
|
|
34
|
+
for (const file of files) {
|
|
35
|
+
const segments = file.path.split("/");
|
|
36
|
+
segments.pop();
|
|
37
|
+
let current = "";
|
|
38
|
+
for (const segment of segments) {
|
|
39
|
+
current = current ? `${current}/${segment}` : segment;
|
|
40
|
+
folders.add(current);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [...folders].sort();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isSupportedPath(filePath) {
|
|
47
|
+
return SUPPORTED_EXTENSIONS.has(path.extname(filePath));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function hasWrongCaseSupportedExtension(filePath) {
|
|
51
|
+
const extension = path.extname(filePath);
|
|
52
|
+
return (
|
|
53
|
+
!SUPPORTED_EXTENSIONS.has(extension) &&
|
|
54
|
+
SUPPORTED_EXTENSIONS.has(extension.toLowerCase())
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function isHiddenRelativePath(relativePath) {
|
|
59
|
+
return relativePath.split("/").some((segment) => segment.startsWith("."));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function isInside(parent, candidate) {
|
|
63
|
+
const relative = path.relative(parent, candidate);
|
|
64
|
+
return (
|
|
65
|
+
relative === "" ||
|
|
66
|
+
(!relative.startsWith(`..${path.sep}`) &&
|
|
67
|
+
relative !== ".." &&
|
|
68
|
+
!path.isAbsolute(relative))
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function resolveInside(parent, relativePath) {
|
|
73
|
+
const normalized = relativePath.split("/").join(path.sep);
|
|
74
|
+
const resolved = path.resolve(parent, normalized);
|
|
75
|
+
if (!isInside(parent, resolved) || resolved === parent) {
|
|
76
|
+
throw new Error(`Unsafe relative path: ${relativePath}`);
|
|
77
|
+
}
|
|
78
|
+
return resolved;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function validateRelativeFilePath(filePath) {
|
|
82
|
+
if (typeof filePath !== "string" || filePath.length === 0) {
|
|
83
|
+
return "File path must be a non-empty string.";
|
|
84
|
+
}
|
|
85
|
+
if (filePath.length > LIMITS.pathLength) {
|
|
86
|
+
return `File path exceeds ${LIMITS.pathLength} characters: ${filePath}`;
|
|
87
|
+
}
|
|
88
|
+
if (filePath.startsWith("/") || filePath.endsWith("/")) {
|
|
89
|
+
return `File path must not start or end with a slash: ${filePath}`;
|
|
90
|
+
}
|
|
91
|
+
if (filePath.includes("\\")) {
|
|
92
|
+
return `File path must use forward slashes: ${filePath}`;
|
|
93
|
+
}
|
|
94
|
+
const segments = filePath.split("/");
|
|
95
|
+
if (
|
|
96
|
+
segments.some(
|
|
97
|
+
(segment) =>
|
|
98
|
+
segment === "" ||
|
|
99
|
+
segment === "." ||
|
|
100
|
+
segment === ".." ||
|
|
101
|
+
segment.includes("\0"),
|
|
102
|
+
)
|
|
103
|
+
) {
|
|
104
|
+
return `File path contains an invalid segment: ${filePath}`;
|
|
105
|
+
}
|
|
106
|
+
if (hasWrongCaseSupportedExtension(filePath)) {
|
|
107
|
+
return `File extensions must be lowercase: ${filePath}`;
|
|
108
|
+
}
|
|
109
|
+
if (!isSupportedPath(filePath)) {
|
|
110
|
+
return `Unsupported file extension: ${filePath}`;
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function normalizeSlug(input) {
|
|
116
|
+
return String(input)
|
|
117
|
+
.normalize("NFKD")
|
|
118
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
119
|
+
.toLowerCase()
|
|
120
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
121
|
+
.replace(/^-+|-+$/g, "")
|
|
122
|
+
.slice(0, 40)
|
|
123
|
+
.replace(/-+$/g, "");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function isValidSlug(slug) {
|
|
127
|
+
return /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/.test(slug);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function deriveSlug(root) {
|
|
131
|
+
const base = path.basename(root);
|
|
132
|
+
const normalized = normalizeSlug(base);
|
|
133
|
+
if (isValidSlug(normalized)) {
|
|
134
|
+
return normalized;
|
|
135
|
+
}
|
|
136
|
+
return `site-${randomUUID().slice(0, 6)}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function normalizeVisibility(value) {
|
|
140
|
+
if (value === undefined || value === null || value === "") {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const normalized = String(value).trim().toLowerCase();
|
|
144
|
+
if (normalized === "private") {
|
|
145
|
+
return "password";
|
|
146
|
+
}
|
|
147
|
+
if (normalized === "public" || normalized === "password") {
|
|
148
|
+
return normalized;
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function parsePositiveInteger(value, name, { min = 1, max } = {}) {
|
|
154
|
+
const number = Number(value);
|
|
155
|
+
if (!Number.isSafeInteger(number) || number < min || (max && number > max)) {
|
|
156
|
+
const suffix = max ? ` between ${min} and ${max}` : ` of at least ${min}`;
|
|
157
|
+
throw new Error(`${name} must be an integer${suffix}.`);
|
|
158
|
+
}
|
|
159
|
+
return number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function sleep(milliseconds, signal) {
|
|
163
|
+
if (milliseconds <= 0) {
|
|
164
|
+
return Promise.resolve();
|
|
165
|
+
}
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
const timer = setTimeout(resolve, milliseconds);
|
|
168
|
+
const onAbort = () => {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
reject(signal.reason ?? new Error("Aborted"));
|
|
171
|
+
};
|
|
172
|
+
if (signal?.aborted) {
|
|
173
|
+
onAbort();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function humanTimestamp(value) {
|
|
181
|
+
if (!value) {
|
|
182
|
+
return "never";
|
|
183
|
+
}
|
|
184
|
+
const date = new Date(value);
|
|
185
|
+
if (Number.isNaN(date.getTime())) {
|
|
186
|
+
return String(value);
|
|
187
|
+
}
|
|
188
|
+
return date.toLocaleString();
|
|
189
|
+
}
|