nuevo-cli
Version:
71 lines (60 loc) • 1.84 kB
JavaScript
const { Command } = require('@oclif/command')
const { CLIError } = require('@oclif/errors')
const pkgUp = require('pkg-up')
const inquirer = require('inquirer')
const colors = require('colors')
const fs = require('fs-extra')
// Templates
const resolverTemplate = require('../../../templates/resolverTemplate')
// Inquirer Questions
const questions = [
{
type: 'input',
name: 'resolverName',
message: 'Enter the name for the new Resolver'
},
{
type: 'confirm',
name: 'createSchema',
message: 'Do you want to create a matching Schema?',
default: true
}
]
class ResolverCommand extends Command {
async run () {
const { args: { resolverName } } = this.parse(ResolverCommand)
if (resolverName) {
await this.createNewSchema({ resolverName })
this.exit()
}
const answers = await inquirer.prompt(questions)
await this.createNewResolver(answers)
this.exit()
}
get currentProjectPath () {
return pkgUp().then(filepath => {
return filepath.replace('/package.json', '')
})
}
async createNewResolver ({ resolverName, createSchema = false }) {
const projectPath = await this.currentProjectPath || './'
const resolverFile = `${projectPath}/src/Resolvers/${resolverName}.js`
try {
const fileExists = await fs.pathExists(resolverFile)
if (fileExists) throw new Error('Whoops - seems this schema file already exists...')
await fs.outputFile(resolverFile, resolverTemplate(resolverName))
const data = await fs.readFile(resolverFile, 'utf8')
this.log(colors.green(data))
} catch (err) {
throw new CLIError(err.message)
}
}
}
ResolverCommand.description = `Create a new Resolver
...
Create a new GraphQL Resolver
`
ResolverCommand.args = [
{ name: 'resolverName' }
]
module.exports = ResolverCommand