@jpillora/take 0.11.0 → 0.12.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/package.json +1 -1
- package/take.d.mts +1 -0
- package/take.mjs +69 -2
package/package.json
CHANGED
package/take.d.mts
CHANGED
package/take.mjs
CHANGED
|
@@ -3,10 +3,20 @@
|
|
|
3
3
|
// IMPORTANT: All code must stay in 1 file.
|
|
4
4
|
// deno-lint-ignore-file no-explicit-any
|
|
5
5
|
import { Buffer } from "node:buffer";
|
|
6
|
-
import { readFile } from "node:fs/promises";
|
|
7
|
-
import { basename } from "node:path";
|
|
6
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
7
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
8
8
|
import { spawn as nodeSpawn, } from "node:child_process";
|
|
9
9
|
import process from "node:process";
|
|
10
|
+
let _insertHelpPath = null;
|
|
11
|
+
export function insertHelp(path) {
|
|
12
|
+
if (isAbsolute(path)) {
|
|
13
|
+
_insertHelpPath = path;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const scriptDir = dirname(process.argv[1] || ".");
|
|
17
|
+
_insertHelpPath = join(scriptDir, path);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
10
20
|
export function newFlags(flags) {
|
|
11
21
|
return flags;
|
|
12
22
|
}
|
|
@@ -193,6 +203,62 @@ async function loadEnvFile(path) {
|
|
|
193
203
|
return false;
|
|
194
204
|
}
|
|
195
205
|
}
|
|
206
|
+
const MARKER_START = "<!-- take:start -->";
|
|
207
|
+
const MARKER_END = "<!-- take:end -->";
|
|
208
|
+
async function _writeInsertHelp(commands) {
|
|
209
|
+
if (!_insertHelpPath)
|
|
210
|
+
return;
|
|
211
|
+
const path = _insertHelpPath;
|
|
212
|
+
// Build markdown list with flags as sub-bullets
|
|
213
|
+
const lines = [];
|
|
214
|
+
for (const cmd of commands) {
|
|
215
|
+
if (cmd.name === "debug")
|
|
216
|
+
continue;
|
|
217
|
+
const desc = cmd.description ? ` - ${cmd.description}` : "";
|
|
218
|
+
lines.push(`- \`${cmd.name}\`${desc}`);
|
|
219
|
+
// Add flags as sub-bullets
|
|
220
|
+
const shorts = new Set();
|
|
221
|
+
for (const [name, flag] of Object.entries(cmd.flags)) {
|
|
222
|
+
const letter = name[0];
|
|
223
|
+
const shortStr = shorts.has(letter) ? "" : ` \`-${letter}\``;
|
|
224
|
+
shorts.add(letter);
|
|
225
|
+
const typeStr = typeof flag.initial === "boolean"
|
|
226
|
+
? ""
|
|
227
|
+
: ` <${typeof flag.initial}>`;
|
|
228
|
+
const parts = [];
|
|
229
|
+
if (flag.env)
|
|
230
|
+
parts.push(`env=${flag.env}`);
|
|
231
|
+
if (flag.initial)
|
|
232
|
+
parts.push(`default=${flag.initial}`);
|
|
233
|
+
const extras = parts.length ? ` (${parts.join(" ")})` : "";
|
|
234
|
+
lines.push(` - \`--${name}\`${shortStr}${typeStr} - ${flag.description}${extras}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const content = MARKER_START + "\n" + lines.join("\n") + "\n" + MARKER_END;
|
|
238
|
+
// Read existing file
|
|
239
|
+
let fileContent;
|
|
240
|
+
try {
|
|
241
|
+
fileContent = await readFile(path, "utf-8");
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
return; // file doesn't exist, nothing to insert into
|
|
245
|
+
}
|
|
246
|
+
// Find markers and replace or append
|
|
247
|
+
const startIdx = fileContent.indexOf(MARKER_START);
|
|
248
|
+
const endIdx = fileContent.indexOf(MARKER_END);
|
|
249
|
+
let newContent;
|
|
250
|
+
if (startIdx !== -1 && endIdx !== -1 && endIdx > startIdx) {
|
|
251
|
+
newContent = fileContent.substring(0, startIdx) + content +
|
|
252
|
+
fileContent.substring(endIdx + MARKER_END.length);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
const sep = fileContent.endsWith("\n") ? "\n" : "\n\n";
|
|
256
|
+
newContent = fileContent + sep + content + "\n";
|
|
257
|
+
}
|
|
258
|
+
if (newContent !== fileContent) {
|
|
259
|
+
await writeFile(path, newContent, "utf-8");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
196
262
|
export async function Register(...commands) {
|
|
197
263
|
// Load .env by default
|
|
198
264
|
await loadEnvFile(".env");
|
|
@@ -238,6 +304,7 @@ export async function Register(...commands) {
|
|
|
238
304
|
}
|
|
239
305
|
}
|
|
240
306
|
commands.sort((a, b) => (a.name < b.name ? -1 : 1));
|
|
307
|
+
await _writeInsertHelp(commands);
|
|
241
308
|
const joinColumns = (table) => {
|
|
242
309
|
const max = table.reduce((m, { left }) => Math.max(m, left.length), 0);
|
|
243
310
|
return table
|