UNPKG

nuevo-cli

Version:
71 lines (60 loc) 1.82 kB
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 schemaTemplate = require('../../../templates/schemaTemplate') // Inquirer Questions const questions = [ { type: 'input', name: 'schemaName', message: 'Enter the name for the new Schema' }, { type: 'confirm', name: 'createResolver', message: 'Do you want to create a matching Resolver?', default: true } ] class SchemaCommand extends Command { async run () { const { args: { schemaName } } = this.parse(SchemaCommand) if (schemaName) { await this.createNewSchema({ schemaName }) this.exit() } const answers = await inquirer.prompt(questions) await this.createNewSchema(answers) this.exit() } get currentProjectPath () { return pkgUp().then(filepath => { return filepath.replace('/package.json', '') }) } async createNewSchema ({ schemaName, createResolver = false }) { const projectPath = await this.currentProjectPath || './' const schemaFile = `${projectPath}/src/Schemas/${schemaName}.graphql` try { const fileExists = await fs.pathExists(schemaFile) if (fileExists) throw new Error('Whoops - seems this schema file already exists...') await fs.outputFile(schemaFile, schemaTemplate(schemaName)) const data = await fs.readFile(schemaFile, 'utf8') this.log(colors.green(data)) } catch (err) { throw new CLIError(err.message) } } } SchemaCommand.description = `Create a new Schema ... Create a new GraphQL Schema for some types ` SchemaCommand.args = [ { name: 'schemaName' } ] module.exports = SchemaCommand