Skip to content

Commit

Permalink
refactor: move classes into separate files
Browse files Browse the repository at this point in the history
Part of #31
  • Loading branch information
php-coder committed Mar 9, 2024
1 parent 0d485ec commit 4c4bcf2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 75 deletions.
77 changes: 2 additions & 75 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/generator/Generator.js
Original file line number Diff line number Diff line change
@@ -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}`)
}
}

}
14 changes: 14 additions & 0 deletions src/generator/GoGenerator.js
Original file line number Diff line number Diff line change
@@ -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`
}

}
12 changes: 12 additions & 0 deletions src/generator/JsGenerator.js
Original file line number Diff line number Diff line change
@@ -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`
}

}
12 changes: 12 additions & 0 deletions src/generator/PyGenerator.js
Original file line number Diff line number Diff line change
@@ -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`
}

}
14 changes: 14 additions & 0 deletions src/generator/TsGenerator.js
Original file line number Diff line number Diff line change
@@ -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`
}

}

0 comments on commit 4c4bcf2

Please sign in to comment.