2021-07-07 12:58:24 +02:00
const path = require ( 'path' ) ;
const fs = require ( 'fs' ) ;
const linter = require ( './linter' ) ;
2021-07-07 13:51:44 +02:00
const prompt = require ( './prompt' ) ;
2021-07-07 14:16:19 +02:00
const os = require ( 'os' )
const it _root _questions = [
{ question : 'Service name' , name : 'name' , validator : linter . regex . NAME _REGEX , error : "Name must only contain lowercase letters, numbers or '-_'!" } ,
{ question : 'Service description' , name : 'description' , validator : linter . regex . DESCRIPTION _REGEX , error : "Description must be a descriptive sentence starting with a uppercase letter and ending with a '.'!" } ,
{ question : 'Template' , name : 'template' , default : defaultTemplatePath } ,
{ question : 'Output' , name : 'output' , default : results => defaultOutputPath ( results . name ) } ,
{ question : 'Namespace' , name : 'namespace' } ,
{ question : 'Base URL' , name : 'base_url' }
] ;
function userTemplatePath ( name ) {
let templatePath = path . join ( os . homedir ( ) , '.api' , 'templates' , name )
if ( fs . existsSync ( templatePath ) ) {
return templatePath ;
}
return false ;
}
2021-07-07 13:51:44 +02:00
function defaultTemplatePath ( ) {
2021-07-07 14:16:19 +02:00
return userTemplatePath ( 'default.json' ) || path . join ( _ _dirname , '..' , 'templates' , 'default.json' )
2021-07-07 13:51:44 +02:00
}
function defaultOutputPath ( name ) {
return path . join ( process . cwd ( ) , ` ${ name } .json ` )
}
2021-07-07 12:58:24 +02:00
2021-07-07 14:16:19 +02:00
2021-07-07 12:58:24 +02:00
function doInteractiveGeneration ( args ) {
2021-07-07 13:51:44 +02:00
prompt . start ( [
2021-07-07 14:16:19 +02:00
... it _root _questions
2021-07-07 13:51:44 +02:00
] , results => {
generateTemplate ( results ) ;
} ) ;
2021-07-07 12:58:24 +02:00
}
function doSilentGeneration ( args ) {
let config = { } ;
2021-07-07 13:51:44 +02:00
config . template = args . template || defaultTemplatePath ( ) ;
2021-07-07 12:58:24 +02:00
config . name = args . name ;
config . description = args . description ;
2021-07-07 13:51:44 +02:00
config . output = args . output || defaultOutputPath ( args . name )
2021-07-07 13:54:21 +02:00
config . namespace = args . namespace || undefined
config . base _url = args . base _url || undefined
2021-07-07 14:16:19 +02:00
config . info = userTemplatePath ( 'info.json' ) || undefined
2021-07-07 13:54:21 +02:00
2021-07-07 12:58:24 +02:00
generateTemplate ( config ) ;
}
function generateTemplate ( config ) {
console . log ( ` Generate Service Spec for service ' ${ config . name } ' to ${ config . output } ` )
let template = require ( config . template ) ;
template . name = config . name
template . description = config . description
2021-07-07 13:54:21 +02:00
template . namespace = config . namespace
template . base _url = config . base _url
2021-07-07 12:58:24 +02:00
2021-07-07 14:16:19 +02:00
if ( config . info ) {
template . info = require ( config . info )
}
2021-07-07 12:58:24 +02:00
let messages = linter . lint ( template ) ;
if ( messages . length ) {
console . error ( "Failed to generate service spec:" ) ;
messages . forEach ( message => console . log ( ` \t ${ message } ` ) ) ;
process . exit ( - 1 )
}
let output = JSON . stringify ( template , null , '\t' ) ;
fs . writeFileSync ( config . output , output ) ;
}
module . exports = function ( args ) {
if ( args . silent ) {
return doSilentGeneration ( args ) ;
}
return doInteractiveGeneration ( args ) ;
}