@promte/app-upload 0.1.1 → 0.1.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/README.md +1 -0
- package/bin/promte-app-upload.js +64 -8
- package/package.json +1 -1
package/README.md
CHANGED
package/bin/promte-app-upload.js
CHANGED
|
@@ -3,9 +3,11 @@ import fs from "node:fs";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import AdmZip from "adm-zip";
|
|
6
|
-
import
|
|
6
|
+
import undici from "undici";
|
|
7
7
|
import readline from "node:readline/promises";
|
|
8
8
|
|
|
9
|
+
const { FormData, File } = undici;
|
|
10
|
+
|
|
9
11
|
const DEFAULT_HOST = "https://app.promte.dk";
|
|
10
12
|
const DEFAULT_DIST = "./dist";
|
|
11
13
|
const CONFIG_PATH = path.join(os.homedir(), ".promte-app-upload.json");
|
|
@@ -15,7 +17,7 @@ function usage() {
|
|
|
15
17
|
[
|
|
16
18
|
"Usage:",
|
|
17
19
|
" promte-app-upload config",
|
|
18
|
-
" promte-app-upload push [--host <url>] [--workspaceId <id>] [--dist <path>] [--serviceKey <key>]",
|
|
20
|
+
" promte-app-upload push [--host <url>] [--workspaceId <id>] [--dist <path>] [--zip <file>] [--serviceKey <key>]",
|
|
19
21
|
"",
|
|
20
22
|
"Defaults:",
|
|
21
23
|
` host: ${DEFAULT_HOST}`,
|
|
@@ -119,32 +121,86 @@ function ensureDist(distPath) {
|
|
|
119
121
|
const indexPath = path.join(abs, "index.html");
|
|
120
122
|
assert(
|
|
121
123
|
fs.existsSync(indexPath),
|
|
122
|
-
`index.html not found in dist folder: ${indexPath}
|
|
124
|
+
`index.html not found in dist folder: ${indexPath}. Make sure you're pointing at the built output.`,
|
|
123
125
|
);
|
|
124
126
|
return abs;
|
|
125
127
|
}
|
|
126
128
|
|
|
129
|
+
function normalizeZipPath(p) {
|
|
130
|
+
return p.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function detectSingleRoot(entryNames) {
|
|
134
|
+
const roots = new Set(
|
|
135
|
+
entryNames
|
|
136
|
+
.map(normalizeZipPath)
|
|
137
|
+
.filter(Boolean)
|
|
138
|
+
.map((p) => p.split("/")[0]),
|
|
139
|
+
);
|
|
140
|
+
return roots.size === 1 ? [...roots][0] : null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function ensureZipHasIndex(zipPath) {
|
|
144
|
+
const abs = path.resolve(process.cwd(), zipPath);
|
|
145
|
+
assert(fs.existsSync(abs), `Zip file not found: ${abs}`);
|
|
146
|
+
assert(fs.statSync(abs).isFile(), `Zip path is not a file: ${abs}`);
|
|
147
|
+
assert(/\.zip$/i.test(abs), `File must be a .zip: ${abs}`);
|
|
148
|
+
|
|
149
|
+
const zip = new AdmZip(abs);
|
|
150
|
+
const entries = zip.getEntries().filter((e) => !e.isDirectory);
|
|
151
|
+
assert(entries.length > 0, "Zip contains no files.");
|
|
152
|
+
|
|
153
|
+
const entryNames = entries.map((e) => e.entryName);
|
|
154
|
+
const singleRoot = detectSingleRoot(entryNames);
|
|
155
|
+
|
|
156
|
+
const normalized = entryNames
|
|
157
|
+
.map(normalizeZipPath)
|
|
158
|
+
.map((name) => {
|
|
159
|
+
if (singleRoot && name.startsWith(singleRoot + "/")) {
|
|
160
|
+
return name.slice(singleRoot.length + 1);
|
|
161
|
+
}
|
|
162
|
+
return name;
|
|
163
|
+
})
|
|
164
|
+
.filter(Boolean);
|
|
165
|
+
|
|
166
|
+
assert(
|
|
167
|
+
normalized.includes("index.html"),
|
|
168
|
+
"Zip is missing index.html at the root (after unzipping).",
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return { abs, name: path.basename(abs) };
|
|
172
|
+
}
|
|
173
|
+
|
|
127
174
|
async function runPush(args) {
|
|
128
175
|
const config = loadConfig();
|
|
129
176
|
const host = args.host || config.host || DEFAULT_HOST;
|
|
130
177
|
const workspaceId = args.workspaceId || config.workspaceId;
|
|
131
178
|
const dist = args.dist || config.dist || DEFAULT_DIST;
|
|
179
|
+
const zipPath = args.zip;
|
|
132
180
|
let serviceKey = args.serviceKey;
|
|
133
181
|
|
|
134
182
|
assert(workspaceId, "workspaceId is required (config or --workspaceId)");
|
|
135
|
-
const absDist = ensureDist(dist);
|
|
183
|
+
const absDist = zipPath ? null : ensureDist(dist);
|
|
136
184
|
|
|
137
185
|
if (!serviceKey) {
|
|
138
186
|
serviceKey = await promptSecret("Service key");
|
|
139
187
|
}
|
|
140
188
|
assert(serviceKey, "serviceKey is required");
|
|
141
189
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
190
|
+
let zipBuffer;
|
|
191
|
+
let zipName = "dist.zip";
|
|
192
|
+
if (zipPath) {
|
|
193
|
+
const { abs, name } = ensureZipHasIndex(zipPath);
|
|
194
|
+
zipBuffer = fs.readFileSync(abs);
|
|
195
|
+
zipName = name;
|
|
196
|
+
} else {
|
|
197
|
+
const zip = new AdmZip();
|
|
198
|
+
zip.addLocalFolder(absDist, "");
|
|
199
|
+
zipBuffer = zip.toBuffer();
|
|
200
|
+
}
|
|
145
201
|
|
|
146
202
|
const form = new FormData();
|
|
147
|
-
const file = new File([zipBuffer],
|
|
203
|
+
const file = new File([zipBuffer], zipName, {
|
|
148
204
|
type: "application/zip",
|
|
149
205
|
});
|
|
150
206
|
form.append("app", file);
|