Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 6b2516d

Browse files
author
Vishwac Sena Kannan
committed
Merge branch 'master' of https://github.com/microsoft/botframework-cli into vishwac/newEntityAndFeatures
2 parents 6c44b9b + 959b607 commit 6b2516d

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

packages/luis/src/commands/luis/generate/cs.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Command, flags} from '@microsoft/bf-cli-command'
1+
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
22
import {camelCase, upperFirst} from 'lodash'
33
import * as path from 'path'
44

@@ -14,6 +14,7 @@ export default class LuisGenerateCs extends Command {
1414
in: flags.string({description: 'Source .lu file(s) or LUIS application JSON model'}),
1515
out: flags.string({description: 'Output file or folder name. If not specified stdout will be used as output', default: ''}),
1616
className: flags.string({description: 'Name of the class'}),
17+
force: flags.boolean({description: 'If --in flag provided with the path to an existing file, overwrites it', default: false}),
1718
}
1819

1920
reorderEntities(app: any, name: string): void {
@@ -28,7 +29,12 @@ export default class LuisGenerateCs extends Command {
2829
let stdInput = await this.readStdin()
2930

3031
const pathPrefix = path.isAbsolute(flags.in) ? '' : process.cwd()
31-
const app = stdInput ? JSON.parse(stdInput as string) : await fs.readJSON(path.join(pathPrefix, flags.in))
32+
let app: any
33+
try {
34+
app = stdInput ? JSON.parse(stdInput as string) : await fs.readJSON(path.join(pathPrefix, flags.in))
35+
} catch (err) {
36+
throw new CLIError(err)
37+
}
3238

3339
flags.className = flags.className || app.name
3440
flags.className = upperFirst(camelCase(flags.className))
@@ -46,7 +52,7 @@ export default class LuisGenerateCs extends Command {
4652
this.reorderEntities(app, 'patternAnyEntities')
4753
this.reorderEntities(app, 'composites')
4854

49-
const outputPath = Utils.validatePath(flags.out, process.cwd(), flags.className + '.cs')
55+
const outputPath = Utils.validatePath(flags.out, process.cwd(), flags.className + '.cs', flags.force)
5056

5157
this.log(
5258
`Generating file at ${outputPath || ''} that contains class ${space}.${flags.className}.`

packages/luis/src/commands/luis/generate/ts.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Command, flags} from '@microsoft/bf-cli-command'
1+
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
22
import {camelCase, kebabCase, upperFirst} from 'lodash'
33
import * as path from 'path'
44

@@ -14,6 +14,7 @@ export default class LuisGenerateTs extends Command {
1414
in: flags.string({description: 'Source .lu file(s) or LUIS application JSON model'}),
1515
out: flags.string({description: 'Output file or folder name. If not specified stdout will be used as output', default: ''}),
1616
className: flags.string({description: 'Name of the class'}),
17+
force: flags.boolean({description: 'If --in flag provided with the path to an existing file, overwrites it', default: false}),
1718
}
1819

1920
reorderEntities(app: any, name: string): void {
@@ -27,7 +28,12 @@ export default class LuisGenerateTs extends Command {
2728
let stdInput = await this.readStdin()
2829

2930
const pathPrefix = path.isAbsolute(flags.in) ? '' : process.cwd()
30-
const app = stdInput ? JSON.parse(stdInput as string) : await fs.readJSON(path.join(pathPrefix, flags.in))
31+
let app: any
32+
try {
33+
app = stdInput ? JSON.parse(stdInput as string) : await fs.readJSON(path.join(pathPrefix, flags.in))
34+
} catch (err) {
35+
throw new CLIError(err)
36+
}
3137

3238
flags.className = flags.className || app.name
3339
flags.className = upperFirst(camelCase(flags.className))
@@ -39,7 +45,7 @@ export default class LuisGenerateTs extends Command {
3945
this.reorderEntities(app, 'patternAnyEntities')
4046
this.reorderEntities(app, 'composites')
4147

42-
const outputPath = Utils.validatePath(flags.out, process.cwd(), kebabCase(flags.className) + '.ts')
48+
const outputPath = Utils.validatePath(flags.out, process.cwd(), kebabCase(flags.className) + '.ts', flags.force)
4349

4450
this.log(
4551
`Generating file at ${outputPath || ''} that contains class ${flags.className}.`

packages/luis/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {existsSync} from 'fs'
33
import {basename, dirname, extname, isAbsolute, join} from 'path'
44

55
export namespace Utils {
6-
export function validatePath(outputPath: string, workingDirectory: string, defaultFileName: string): string {
6+
export function validatePath(outputPath: string, workingDirectory: string, defaultFileName: string, forceWrite = false): string {
77
let completePath = isAbsolute(outputPath) ? outputPath : join(workingDirectory, outputPath)
88
const containingDir = dirname(completePath)
99

@@ -15,13 +15,13 @@ export namespace Utils {
1515

1616
// If the last element in the path is a file
1717
if (baseElement.includes('.')) {
18-
return pathAlreadyExist ? enumerateFileName(completePath) : completePath
18+
return pathAlreadyExist && !forceWrite ? enumerateFileName(completePath) : completePath
1919
}
2020

2121
// If the last element in the path is a folder
2222
if (!pathAlreadyExist) throw new CLIError(`Target directory path doesn't exist: ${completePath}`)
2323
completePath = join(completePath, defaultFileName)
24-
return existsSync(completePath) ? enumerateFileName(completePath) : completePath
24+
return existsSync(completePath) && !forceWrite ? enumerateFileName(completePath) : completePath
2525
}
2626

2727
function enumerateFileName(filePath: string): string {
Binary file not shown.

0 commit comments

Comments
 (0)