infra-cli
Version:
86 lines (75 loc) • 2.7 kB
JavaScript
const chalk = require('chalk')
const inquirer = require('inquirer')
const download = require('download-git-repo')
const ora = require('ora')
const util = require('util')
const { runInstall } = require('./install')
const fetch = require('./util/fetch')
const { editPackageJson, getUserName } = require('./util/utils')
// const replaceTemplate = require('./dealWithTemplate')
// const wrapLoading = require('./wrapLoading')
async function fetchTemplateList () {
const url = 'https://git.garena.com/api/v4/groups/11154/projects'
try {
const result = await fetch.get(`${url}?private_token=gfKzCGRJzwoD1AByyt_A`)
return result
} catch (error) {
console.error(chalk.redBright(error))
process.exit(1)
}
}
function fetchGitRepo (templateName) {
return fetchTemplateList().then(async (templateList) => {
const templateNames = templateList.map((item) => item.name)
let gitUrl = ''
if (templateName === true) {
await inquirer
.prompt([
{
type: 'list',
name: 'templateName',
message: 'Which template do you want to download?',
choices: templateList
}
])
.then((answers) => {
const index = templateNames.indexOf(answers.templateName)
gitUrl = templateList[index].http_url_to_repo
})
} else {
if (!templateNames.includes(templateName)) {
console.log(`There is no template named ${chalk.green(templateName)}`)
process.exit(1)
} else {
const index = templateNames.indexOf(templateName)
gitUrl = templateList[index].http_url_to_repo
}
}
return gitUrl
})
}
async function generatorProjectFromTemplate (projectName, root, templateName) {
fetchGitRepo(templateName).then(async (gitUrl) => {
const fetchSpinner = ora('Fetching remote preset ...').start()
try {
await util.promisify(download)(`direct:${gitUrl}`, root, { clone: true })
fetchSpinner.succeed('Fetching remote preset')
} catch (err) {
fetchSpinner.fail(`Fetching remote prese ${err}`)
}
// try {
// await wrapLoading(replaceTemplate, chalk.greenBright('replace template...\n'), { projectName }, root)
// } catch (error) {
// console.error(chalk.redBright(error))
// }
editPackageJson(root, { name: projectName, author: getUserName(), version: '0.1.0' })
const installSpinner = ora('Installing dependencies ...').start()
try {
await runInstall(root)
installSpinner.succeed('Installed dependencies')
} catch (err) {
installSpinner.fail(`Installing dependencies ${err}`)
}
})
}
module.exports = generatorProjectFromTemplate