Skip to content

Commit 0d485ec

Browse files
committed
refactor: split logic for showing usage example into classes
Part of #31
1 parent 848a7ad commit 0d485ec

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

src/cli.js

+57-15
Original file line numberDiff line numberDiff line change
@@ -335,41 +335,79 @@ const createTypeScriptConfig = async (destDir, lang) => {
335335
return fsPromises.writeFile(resultFile, tsConfigJson)
336336
}
337337

338-
const showInstructions = (lang) => {
339-
console.info('The application has been generated!')
340-
if (lang === 'js') {
341-
console.info(`Use
338+
class JsGenerator {
339+
340+
usageExampleAsText() {
341+
return `Use
342342
npm install
343343
to install its dependencies and
344344
export DB_NAME=db DB_USER=user DB_PASSWORD=secret
345345
npm start
346-
afteward to run`)
347-
} else if (lang === 'ts') {
348-
console.info(`Use
346+
afteward to run`
347+
}
348+
349+
}
350+
351+
class TsGenerator {
352+
353+
usageExampleAsText() {
354+
return `Use
349355
npm install
350356
to install its dependencies,
351357
npm run build
352358
to build the application, and
353359
export DB_NAME=db DB_USER=user DB_PASSWORD=secret
354360
npm start
355-
afteward to run`)
356-
} else if (lang === 'go') {
357-
console.info(`Use
361+
afteward to run`
362+
}
363+
364+
}
365+
366+
class GoGenerator {
367+
368+
usageExampleAsText() {
369+
return `Use
358370
export DB_NAME=db DB_USER=user DB_PASSWORD=secret
359371
go run *.go
360372
or
361373
go build -o app
362374
export DB_NAME=db DB_USER=user DB_PASSWORD=secret
363375
./app
364-
to build and run it`)
365-
} else if (lang === 'python') {
366-
console.info(`Use
376+
to build and run it`
377+
}
378+
379+
}
380+
381+
class PyGenerator {
382+
383+
usageExampleAsText() {
384+
return `Use
367385
pip install -r requirements.txt
368386
to install its dependencies and
369387
export DB_NAME=db DB_USER=user DB_PASSWORD=secret
370388
uvicorn app:app
371-
afteward to run`)
389+
afteward to run`
390+
}
391+
392+
}
393+
394+
class Generator {
395+
396+
static for(lang) {
397+
switch (lang) {
398+
case 'js':
399+
return new JsGenerator()
400+
case 'ts':
401+
return new TsGenerator()
402+
case 'go':
403+
return new GoGenerator()
404+
case 'python':
405+
return new PyGenerator()
406+
default:
407+
throw new Error(`Unsupported language: ${lang}`)
408+
}
372409
}
410+
373411
}
374412

375413
const absolutePathToDestDir = (argv) => {
@@ -388,12 +426,16 @@ const main = async (argv) => {
388426
fs.mkdirSync(destDir, {recursive: true})
389427
}
390428

429+
const generator = Generator.for(argv.lang)
430+
391431
await createApp(destDir, argv)
392432
await createDb(destDir, argv)
393433
await createEndpoints(destDir, argv, config)
394434
await createDependenciesDescriptor(destDir, argv)
395435
await createTypeScriptConfig(destDir, argv.lang)
396-
showInstructions(argv.lang)
436+
437+
console.info('The application has been generated!')
438+
console.info(generator.usageExampleAsText())
397439
}
398440

399441

0 commit comments

Comments
 (0)