Skip to content

Commit

Permalink
refactor: generator tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Mar 5, 2021
1 parent 8e6bc29 commit b12ae2d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
54 changes: 43 additions & 11 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Octokit } from '@octokit/rest' // eslint-disable-line import/no-extraneous-dependencies
import { series } from 'gulp' // eslint-disable-line import/no-extraneous-dependencies
import { flatten } from 'lodash' // eslint-disable-line import/no-extraneous-dependencies
import path from 'path'
import { array, Codec, GetType, nullType, oneOf, string } from 'purify-ts/Codec'

Expand All @@ -12,6 +13,10 @@ const GithubObject = Codec.interface({
})
type GithubObject = GetType<typeof GithubObject>

interface APIModel extends GithubObject {
command: string
}

// Using authentication to increases Github API rate limit.
const octokit = new Octokit()
const owner = 'amzn'
Expand All @@ -31,23 +36,50 @@ async function fetchContentsByPath(repoPath = 'models'): Promise<GithubObject[]>
)
}

function generateModel(model: GithubObject) {
function generateAPIModel(model: GithubObject): APIModel {
const outputFolder = path.basename(path.dirname(model.path))
const out = `src/api-models/${outputFolder}`
return `openapi-generator-cli generate -g typescript-axios --additional-properties=supportsES6=true -o ${out} -i ${model.download_url}`
// TODO: run scripts to generate models
const command = `openapi-generator-cli generate -g typescript-axios --additional-properties=supportsES6=true -o ${out} -i ${model.download_url}`

return {
...model,
command,
}
}

async function generateModels() {
const modelFolders = await fetchContentsByPath()
const modelPromises = modelFolders.map((modelFolder) =>
fetchContentsByPath(modelFolder.path).then((models) =>
models.map((model) => generateModel(model)),
),
)
await Promise.all(modelPromises)
function executeGeneratorCLI(model: APIModel): APIModel {
// TODO: generate model from API model

return model
}

function removeRedundantObjects(model: APIModel): APIModel {
/**
* TODO: clean up:
*- .openapi-generator
*- .gitignore
*- .openapi-generator-ignore
*- git_push.sh
*/

return model
}

function exportAPIModel(model: APIModel): APIModel {
// TODO: export models into src/index.ts and commit files

return model
}

async function generateModels() {
const githubFolders = await fetchContentsByPath()
const githubFilePromises = githubFolders.map((folder) => fetchContentsByPath(folder.path))

return flatten(await Promise.all(githubFilePromises))
.map(generateAPIModel)
.map(executeGeneratorCLI)
.map(removeRedundantObjects)
.map(exportAPIModel)
}

export default series(generateModels)
12 changes: 9 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
"@scaleleap/utils": "1.9.10",
"@types/gulp": "4.0.8",
"@types/jest": "26.0.20",
"@types/lodash": "4.14.168",
"@types/node": "13.13.45",
"danger": "10.6.3",
"gulp": "4.0.2",
"jest": "26.6.3",
"lodash": "4.17.21",
"rimraf": "3.0.2",
"ts-jest": "26.5.0",
"ts-node": "9.1.1",
Expand Down

0 comments on commit b12ae2d

Please sign in to comment.