Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
allow pre-answering questions
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block committed Aug 30, 2024
1 parent b8864e0 commit e941171
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
4 changes: 3 additions & 1 deletion bin/create-web5-app.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/usr/bin/env node
import '../dist/index.js';
import main from '../dist/index.js';

main('https://github.com/TBD54566975/tbd-examples.git', 'app-creator');
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "create-web5-app",
"version": "0.1.1",
"version": "0.2.0",
"description": "",
"main": "index.js",
"type": "module",
"bin": {
"create-web5-app": "bin/create-web5-app.js"
},
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "tsc && node dist/index.js"
"create-web5-app": "tsc && bin/create-web5-app.js"
},
"keywords": [],
"author": "",
Expand Down
54 changes: 38 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ class TemplateVariable {
}[];
}

async function updateCache(gitDir: string) {
function getAnswer(question: string): string | null {
const env = question.toUpperCase().replaceAll("-", "_");
const answer = process.env[env];
if (answer) {
return answer;
}

return null;
}

async function updateCache(gitDir: string, repo: string, branch?: string) {
const s = spinner();
if (fs.existsSync(gitDir)) {
s.start("updating template library");
await git.pull({
fs,
http: http,
url: "https://github.com/TBD54566975/tbd-examples",
url: repo,
dir: gitDir,
author: { name: "user", email: "user@host" },
singleBranch: true,
Expand All @@ -49,21 +59,24 @@ async function updateCache(gitDir: string) {
await git.clone({
fs,
http: http,
url: "https://github.com/TBD54566975/tbd-examples",
url: repo,
dir: gitDir,
singleBranch: true,
});
s.stop("downloaded template library");
}

await git.checkout({
fs,
dir: gitDir,
ref: "app-creator",
});
if (branch) {
await git.checkout({ fs, dir: gitDir, ref: branch });
}
}

async function pickLanguage(gitDir: string): Promise<string | symbol> {
const answer = getAnswer("language");
if (answer) {
return answer;
}

let languages: { value: string; label: string; hint?: string }[] = [];
for (let lang of fs.readdirSync(gitDir)) {
if (lang.startsWith(".")) {
Expand Down Expand Up @@ -92,6 +105,8 @@ class TemplateData {
async function pickTemplate(
langDir: string
): Promise<TemplateData | Symbol | null> {
const preSelectedAnswer = getAnswer("template");

let templates: { value: TemplateData; label: string; hint?: string }[] = [];
for (let slug of fs.readdirSync(langDir)) {
if (slug.startsWith(".")) {
Expand All @@ -118,8 +133,14 @@ async function pickTemplate(
fs.readFileSync(templateConfig, "utf-8")
);

const value: TemplateData = { slug, location, config };

if (preSelectedAnswer != null && preSelectedAnswer == slug) {
return value;
}

templates.push({
value: { slug, location, config },
value: value,
label: config.name || slug,
hint: config.description,
});
Expand Down Expand Up @@ -222,9 +243,12 @@ async function renderTemplate(
question.name = name;
}

const answer = await templateQuestion(question);
if (isCancel(answer)) {
return answer;
let answer = getAnswer(question);
if (answer == null) {
answer = await templateQuestion(question);
if (isCancel(answer)) {
return answer;
}
}

answers[name] = answer;
Expand Down Expand Up @@ -281,13 +305,13 @@ async function renderTemplateRecursive(
}
}

async function main() {
export default async function main(repo: string, branch?: string) {
try {
intro("Web5 App Creator");
const cacheDir = await globalCacheDir("web5-app-creator");
const gitDir = cacheDir + "/tbd-examples";

await updateCache(gitDir);
await updateCache(gitDir, repo, branch);

const language = await pickLanguage(gitDir);
if (isCancel(language)) {
Expand Down Expand Up @@ -334,5 +358,3 @@ async function main() {
console.log("\n", e.stack || e);
}
}

main();

0 comments on commit e941171

Please sign in to comment.