Skip to content

Commit

Permalink
feat: add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Mar 23, 2021
1 parent d4f32bd commit 130c9ec
Show file tree
Hide file tree
Showing 5 changed files with 598 additions and 6 deletions.
40 changes: 40 additions & 0 deletions compiler/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import inquirer from 'inquirer'
import Logger, { Level } from './log'
import fs from 'fs'
import { getBoilerplateSpecContent, SOURCE_FOLDER_NAME } from './constants'
import path from 'path'

async function createNewSpecCLI() {
try {
const { cliName } = await inquirer.prompt<{ cliName: string }>([
{
type: 'input',
message: "What's the name of the CLI you want to create the Spec for?",
name: 'cliName',
},
])

const specFileName = `${cliName.toLowerCase()}.ts`
const specPath = path.join(process.cwd(), SOURCE_FOLDER_NAME, specFileName)
const hasSpec = fs.existsSync(specPath)

// We don't want to overwrite the spec if it already exists
if (hasSpec) {
Logger.log(`The spec "${specFileName}" already exists.`, Level.ERROR)
return
}

fs.writeFileSync(specPath, getBoilerplateSpecContent(cliName), {
encoding: 'utf-8',
})

Logger.log(
`Successfully created the new Spec "${specFileName}"!`,
Level.SUCCESS
)
} catch (e) {
Logger.log("Couldn't create Spec! Please try again.", Level.ERROR)
}
}

createNewSpecCLI()
5 changes: 1 addition & 4 deletions compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import { specTransformer } from './transformer'
import SpecLogger, { Level } from './log'
import ProgressBar from 'progress'
import { DESTINATION_FOLDER_NAME, SOURCE_FOLDER_NAME } from './constants'

// The options for the TypeScript compiler
const options: ts.TranspileOptions = {
Expand All @@ -15,10 +16,6 @@ const options: ts.TranspileOptions = {
},
}

// Folder names
const SOURCE_FOLDER_NAME = 'dev'
const DESTINATION_FOLDER_NAME = 'specs'

/**
* Process a spec by transpiling it with the TypeScript
* compiler.
Expand Down
9 changes: 9 additions & 0 deletions compiler/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Folder names
export const SOURCE_FOLDER_NAME = 'dev'
export const DESTINATION_FOLDER_NAME = 'specs'


export const getBoilerplateSpecContent = (specName: string) => `
export const completion: Fig.Spec = {
name: "${specName}"
}`
Loading

0 comments on commit 130c9ec

Please sign in to comment.