Better args handling
This commit is contained in:
parent
b6b5f784c8
commit
90d317795b
11
.vscode/tasks.json
vendored
11
.vscode/tasks.json
vendored
@ -26,7 +26,10 @@
|
|||||||
"command": "node",
|
"command": "node",
|
||||||
"args": [
|
"args": [
|
||||||
"${workspaceFolder}/create-collection.js",
|
"${workspaceFolder}/create-collection.js",
|
||||||
"${input:collectionId}"
|
"--id",
|
||||||
|
"${input:collectionId}",
|
||||||
|
"--tags",
|
||||||
|
"${input:tags}"
|
||||||
],
|
],
|
||||||
"problemMatcher": [],
|
"problemMatcher": [],
|
||||||
"group": "build",
|
"group": "build",
|
||||||
@ -39,6 +42,12 @@
|
|||||||
"description": "Collection ID (lowercase, hyphen-separated)",
|
"description": "Collection ID (lowercase, hyphen-separated)",
|
||||||
"default": "my-collection",
|
"default": "my-collection",
|
||||||
"type": "promptString"
|
"type": "promptString"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "tags",
|
||||||
|
"description": "Comma separated list of tags",
|
||||||
|
"default": "tag1,tag2",
|
||||||
|
"type": "promptString"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,16 +15,50 @@ function prompt(question) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseArgs() {
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
const out = { id: undefined, tags: undefined };
|
||||||
|
|
||||||
|
// simple long/short option parsing
|
||||||
|
for (let i = 0; i < args.length; i++) {
|
||||||
|
const a = args[i];
|
||||||
|
if (a === '--id' || a === '-i') {
|
||||||
|
out.id = args[i + 1];
|
||||||
|
i++;
|
||||||
|
} else if (a.startsWith('--id=')) {
|
||||||
|
out.id = a.split('=')[1];
|
||||||
|
} else if (a === '--tags' || a === '-t') {
|
||||||
|
out.tags = args[i + 1];
|
||||||
|
i++;
|
||||||
|
} else if (a.startsWith('--tags=')) {
|
||||||
|
out.tags = a.split('=')[1];
|
||||||
|
} else if (!a.startsWith('-') && !out.id) {
|
||||||
|
// first positional -> id
|
||||||
|
out.id = a;
|
||||||
|
} else if (!a.startsWith('-') && out.id && !out.tags) {
|
||||||
|
// second positional -> tags
|
||||||
|
out.tags = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalize tags to string (comma separated) or undefined
|
||||||
|
if (Array.isArray(out.tags)) {
|
||||||
|
out.tags = out.tags.join(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
async function createCollectionTemplate() {
|
async function createCollectionTemplate() {
|
||||||
try {
|
try {
|
||||||
console.log("🎯 Collection Creator");
|
console.log("🎯 Collection Creator");
|
||||||
console.log("This tool will help you create a new collection manifest.\n");
|
console.log("This tool will help you create a new collection manifest.\n");
|
||||||
|
|
||||||
|
// Parse CLI args and fall back to interactive prompts when missing
|
||||||
|
const parsed = parseArgs();
|
||||||
// Get collection ID
|
// Get collection ID
|
||||||
let collectionId;
|
let collectionId = parsed.id;
|
||||||
if (process.argv[2]) {
|
if (!collectionId) {
|
||||||
collectionId = process.argv[2];
|
|
||||||
} else {
|
|
||||||
collectionId = await prompt("Collection ID (lowercase, hyphens only): ");
|
collectionId = await prompt("Collection ID (lowercase, hyphens only): ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +93,7 @@ async function createCollectionTemplate() {
|
|||||||
.split("-")
|
.split("-")
|
||||||
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
||||||
.join(" ");
|
.join(" ");
|
||||||
|
|
||||||
let collectionName = await prompt(`Collection name (default: ${defaultName}): `);
|
let collectionName = await prompt(`Collection name (default: ${defaultName}): `);
|
||||||
if (!collectionName.trim()) {
|
if (!collectionName.trim()) {
|
||||||
collectionName = defaultName;
|
collectionName = defaultName;
|
||||||
@ -72,11 +106,15 @@ async function createCollectionTemplate() {
|
|||||||
description = defaultDescription;
|
description = defaultDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tags
|
// Get tags (from CLI or prompt)
|
||||||
let tags = [];
|
let tags = [];
|
||||||
const tagInput = await prompt("Tags (comma-separated, or press Enter for defaults): ");
|
let tagInput = parsed.tags;
|
||||||
if (tagInput.trim()) {
|
if (!tagInput) {
|
||||||
tags = tagInput.split(",").map(tag => tag.trim()).filter(tag => tag);
|
tagInput = await prompt("Tags (comma-separated, or press Enter for defaults): ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tagInput && tagInput.toString().trim()) {
|
||||||
|
tags = tagInput.toString().split(",").map(tag => tag.trim()).filter(tag => tag);
|
||||||
} else {
|
} else {
|
||||||
// Generate some default tags from the collection ID
|
// Generate some default tags from the collection ID
|
||||||
tags = collectionId.split("-").slice(0, 3);
|
tags = collectionId.split("-").slice(0, 3);
|
||||||
@ -120,4 +158,4 @@ display:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run the interactive creation process
|
// Run the interactive creation process
|
||||||
createCollectionTemplate();
|
createCollectionTemplate();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user