From 4c4bcf26cc42ad4019710a9d166ba9d770d36007 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 9 Mar 2024 12:59:51 +0700 Subject: [PATCH] refactor: move classes into separate files Part of #31 --- src/cli.js | 77 +----------------------------------- src/generator/Generator.js | 23 +++++++++++ src/generator/GoGenerator.js | 14 +++++++ src/generator/JsGenerator.js | 12 ++++++ src/generator/PyGenerator.js | 12 ++++++ src/generator/TsGenerator.js | 14 +++++++ 6 files changed, 77 insertions(+), 75 deletions(-) create mode 100644 src/generator/Generator.js create mode 100644 src/generator/GoGenerator.js create mode 100644 src/generator/JsGenerator.js create mode 100644 src/generator/PyGenerator.js create mode 100644 src/generator/TsGenerator.js diff --git a/src/cli.js b/src/cli.js index 0f69179..fb87062 100755 --- a/src/cli.js +++ b/src/cli.js @@ -10,6 +10,8 @@ const parseArgs = require('minimist') const { Parser } = require('node-sql-parser') +const Generator = require('./generator/Generator') + const endpointsFile = 'endpoints.yaml' const parseCommandLineArgs = (args) => { @@ -335,81 +337,6 @@ const createTypeScriptConfig = async (destDir, lang) => { return fsPromises.writeFile(resultFile, tsConfigJson) } -class JsGenerator { - - usageExampleAsText() { - return `Use - npm install -to install its dependencies and - export DB_NAME=db DB_USER=user DB_PASSWORD=secret - npm start -afteward to run` - } - -} - -class TsGenerator { - - usageExampleAsText() { - return `Use - npm install -to install its dependencies, - npm run build -to build the application, and - export DB_NAME=db DB_USER=user DB_PASSWORD=secret - npm start -afteward to run` - } - -} - -class GoGenerator { - - usageExampleAsText() { - return `Use - export DB_NAME=db DB_USER=user DB_PASSWORD=secret - go run *.go -or - go build -o app - export DB_NAME=db DB_USER=user DB_PASSWORD=secret - ./app -to build and run it` - } - -} - -class PyGenerator { - - usageExampleAsText() { - return `Use - pip install -r requirements.txt -to install its dependencies and - export DB_NAME=db DB_USER=user DB_PASSWORD=secret - uvicorn app:app -afteward to run` - } - -} - -class Generator { - - static for(lang) { - switch (lang) { - case 'js': - return new JsGenerator() - case 'ts': - return new TsGenerator() - case 'go': - return new GoGenerator() - case 'python': - return new PyGenerator() - default: - throw new Error(`Unsupported language: ${lang}`) - } - } - -} - const absolutePathToDestDir = (argv) => { const relativeDestDir = argv._.length > 0 ? argv._[0] : argv['dest-dir'] return path.resolve(process.cwd(), relativeDestDir) diff --git a/src/generator/Generator.js b/src/generator/Generator.js new file mode 100644 index 0000000..436253c --- /dev/null +++ b/src/generator/Generator.js @@ -0,0 +1,23 @@ +const JsGenerator = require('./JsGenerator') +const TsGenerator = require('./TsGenerator') +const GoGenerator = require('./GoGenerator') +const PyGenerator = require('./PyGenerator') + +module.exports = class Generator { + + static for(lang) { + switch (lang) { + case 'js': + return new JsGenerator() + case 'ts': + return new TsGenerator() + case 'go': + return new GoGenerator() + case 'python': + return new PyGenerator() + default: + throw new Error(`Unsupported language: ${lang}`) + } + } + +} diff --git a/src/generator/GoGenerator.js b/src/generator/GoGenerator.js new file mode 100644 index 0000000..1d34590 --- /dev/null +++ b/src/generator/GoGenerator.js @@ -0,0 +1,14 @@ +module.exports = class GoGenerator { + + usageExampleAsText() { + return `Use + export DB_NAME=db DB_USER=user DB_PASSWORD=secret + go run *.go +or + go build -o app + export DB_NAME=db DB_USER=user DB_PASSWORD=secret + ./app +to build and run it` + } + +} diff --git a/src/generator/JsGenerator.js b/src/generator/JsGenerator.js new file mode 100644 index 0000000..13f658c --- /dev/null +++ b/src/generator/JsGenerator.js @@ -0,0 +1,12 @@ +module.exports = class JsGenerator { + + usageExampleAsText() { + return `Use + npm install +to install its dependencies and + export DB_NAME=db DB_USER=user DB_PASSWORD=secret + npm start +afteward to run` + } + +} diff --git a/src/generator/PyGenerator.js b/src/generator/PyGenerator.js new file mode 100644 index 0000000..1d871e0 --- /dev/null +++ b/src/generator/PyGenerator.js @@ -0,0 +1,12 @@ +module.exports = class PyGenerator { + + usageExampleAsText() { + return `Use + pip install -r requirements.txt +to install its dependencies and + export DB_NAME=db DB_USER=user DB_PASSWORD=secret + uvicorn app:app +afteward to run` + } + +} diff --git a/src/generator/TsGenerator.js b/src/generator/TsGenerator.js new file mode 100644 index 0000000..0fa46cc --- /dev/null +++ b/src/generator/TsGenerator.js @@ -0,0 +1,14 @@ +module.exports = class TsGenerator { + + usageExampleAsText() { + return `Use + npm install +to install its dependencies, + npm run build +to build the application, and + export DB_NAME=db DB_USER=user DB_PASSWORD=secret + npm start +afteward to run` + } + +}