badmfck-api-server 5.0.5 → 5.0.6

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.
@@ -9,6 +9,7 @@ const path_1 = __importDefault(require("path"));
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const __1 = require("../..");
11
11
  const crypto_1 = __importDefault(require("crypto"));
12
+ const os_1 = __importDefault(require("os"));
12
13
  const undici_1 = require("undici");
13
14
  const resolveConfig_1 = require("./resolveConfig");
14
15
  async function Deploy(opt) {
@@ -74,21 +75,33 @@ async function Deploy(opt) {
74
75
  spaMode = true;
75
76
  console.log("No ./bin found — using entire ./dist contents as distribution");
76
77
  }
78
+ const staged = [];
77
79
  if (Array.isArray(opt.includes)) {
78
- for (const inc of opt.includes) {
79
- if (typeof inc !== "string" || inc.length === 0)
80
+ for (const raw of opt.includes) {
81
+ const inc = typeof raw === "string" ? { src: raw, dest: raw } : raw;
82
+ if (!inc || typeof inc.src !== "string" || inc.src.length === 0)
80
83
  continue;
81
- if (inc.startsWith("-")) {
82
- console.warn(`Skipping include that starts with '-': ${inc}`);
84
+ const src = inc.src;
85
+ const dest = (inc.dest && inc.dest.length > 0 ? inc.dest : src).replace(/^\.\//, "");
86
+ if (src.startsWith("-") || dest.startsWith("-"))
87
+ throw new Error(`Include path must not start with '-': ${src} -> ${dest}`);
88
+ if (!fs_1.default.existsSync(src))
89
+ throw new Error(`Include source does not exist: ${src}`);
90
+ if (path_1.default.isAbsolute(dest) || dest.split(/[\\/]/).includes(".."))
91
+ throw new Error(`Include 'dest' must be a relative path inside the slot, without '..': ${dest}`);
92
+ if (dest === src.replace(/^\.\//, "")) {
93
+ items.push(src);
83
94
  continue;
84
95
  }
85
- if (!fs_1.default.existsSync(inc)) {
86
- console.warn(`Include path does not exist (tar will error): ${inc}`);
87
- }
88
- items.push(inc);
96
+ staged.push({ src, dest });
89
97
  }
90
98
  }
91
- if (items.length === 0 && !spaMode) {
99
+ for (const s of staged) {
100
+ const clash = items.some(i => i.replace(/^\.\//, "") === s.dest);
101
+ if (clash)
102
+ console.warn(`Include dest "${s.dest}" collides with an already-archived path — the later entry wins on extraction`);
103
+ }
104
+ if (items.length === 0 && !spaMode && staged.length === 0) {
92
105
  throw new Error("Nothing to archive (no ./bin, no ./dist/index.html, no valid includes)");
93
106
  }
94
107
  const excludes = new Set();
@@ -127,6 +140,17 @@ async function Deploy(opt) {
127
140
  else {
128
141
  tarArgs.push(...items);
129
142
  }
143
+ let stageDir = null;
144
+ if (staged.length > 0) {
145
+ stageDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "badmfck-deploy-"));
146
+ for (const { src, dest } of staged) {
147
+ const target = path_1.default.join(stageDir, dest);
148
+ fs_1.default.mkdirSync(path_1.default.dirname(target), { recursive: true });
149
+ fs_1.default.cpSync(path_1.default.resolve(src), target, { recursive: true });
150
+ console.log(`Include: ${src} -> ${dest}`);
151
+ }
152
+ tarArgs.push("-C", stageDir, ".");
153
+ }
130
154
  if (spaMode) {
131
155
  console.log("Archiving: contents of ./dist (recursively, at archive root)");
132
156
  if (items.length > 0)
@@ -154,6 +178,16 @@ async function Deploy(opt) {
154
178
  console.error(errOut);
155
179
  process.exit(typeof err.status === "number" ? err.status : 1);
156
180
  }
181
+ finally {
182
+ if (stageDir) {
183
+ try {
184
+ fs_1.default.rmSync(stageDir, { recursive: true, force: true });
185
+ }
186
+ catch (e) {
187
+ console.warn(`Could not remove staging dir ${stageDir}: ${e?.message ?? e}`);
188
+ }
189
+ }
190
+ }
157
191
  if (!fs_1.default.existsSync(path_1.default.resolve(archiveName))) {
158
192
  console.error("ARCHIVE NOT CREATED!");
159
193
  return "";
@@ -4,7 +4,10 @@ export interface IDeployerConfig {
4
4
  host: string;
5
5
  username: string;
6
6
  password: string;
7
- includes?: string[];
7
+ includes?: (string | {
8
+ src: string;
9
+ dest?: string;
10
+ })[];
8
11
  excludes?: string[];
9
12
  switch_host?: string;
10
13
  }
@@ -14,8 +14,20 @@ function validateConfig(cfg) {
14
14
  return "not an object";
15
15
  if (!cfg.name || !cfg.token || !cfg.host || !cfg.username || !cfg.password)
16
16
  return "missing one of: name, token, host, username, password";
17
- if (cfg.includes && !Array.isArray(cfg.includes))
18
- return "includes must be an array";
17
+ if (cfg.includes) {
18
+ if (!Array.isArray(cfg.includes))
19
+ return "includes must be an array";
20
+ for (const inc of cfg.includes) {
21
+ if (typeof inc === "string")
22
+ continue;
23
+ if (!inc || typeof inc !== "object")
24
+ return "includes entries must be a string or { src, dest? }";
25
+ if (typeof inc.src !== "string" || inc.src.length === 0)
26
+ return "includes entry is missing a non-empty 'src'";
27
+ if (inc.dest !== undefined && (typeof inc.dest !== "string" || inc.dest.length === 0))
28
+ return `includes entry "${inc.src}" has an invalid 'dest'`;
29
+ }
30
+ }
19
31
  if (cfg.excludes && !Array.isArray(cfg.excludes))
20
32
  return "excludes must be an array";
21
33
  if (cfg.switch_host && typeof cfg.switch_host !== "string")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "5.0.5",
3
+ "version": "5.0.6",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",