Skip to content

Commit d001716

Browse files
committed
CmdName type, use tempate literals
1 parent ba8680e commit d001716

10 files changed

Lines changed: 31 additions & 29 deletions

File tree

src/commands/ajv.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export default function (argv: ParsedArgs): AjvCore {
3030
try {
3131
ajv[method](schema)
3232
} catch (err) {
33-
console.error(fileType, file, "is invalid")
34-
console.error("error:", (err as Error).message)
33+
console.error(`${fileType} ${file} is invalid`)
34+
console.error(`error: ${(err as Error).message}`)
3535
invalid = true
3636
}
3737
})
@@ -45,8 +45,8 @@ export default function (argv: ParsedArgs): AjvCore {
4545
try {
4646
require(file)(ajv)
4747
} catch (err) {
48-
console.error("module", file, "is invalid; it should export function")
49-
console.error("error:", (err as Error).message)
48+
console.error(`module ${file} is invalid; it should export function`)
49+
console.error(`error: ${(err as Error).message}`)
5050
invalid = true
5151
}
5252
})

src/commands/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function execute(argv: ParsedArgs): boolean {
3434
function compileMultiExportModule(files: string[]): boolean {
3535
const allValid = all(files, (file) => !!compileSchema(file))
3636
if (allValid) return saveStandaloneCode()
37-
console.error("module not generated")
37+
console.error("module not saved")
3838
return false
3939
}
4040

src/commands/help.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Command} from "./types"
1+
import type {Command, CmdName} from "./types"
22
import type {ParsedArgs} from "minimist"
33
import usage from "./usage"
44

@@ -14,23 +14,23 @@ const cmd: Command = {
1414

1515
export default cmd
1616

17-
const commands: {[Name in string]?: () => void} = {
18-
validate: helpValidate,
17+
const commands: {[Name in CmdName]: () => void} = {
18+
help: mainHelp,
1919
compile: helpCompile,
20+
validate: helpValidate,
2021
migrate: helpMigrate,
2122
test: helpTest,
2223
}
2324

2425
function execute(argv: ParsedArgs): boolean {
2526
const command = argv._[1]
26-
if (!command || command === "help") {
27+
if (!command) {
2728
mainHelp()
2829
return true
2930
}
3031

31-
const cmdHelp = commands[command]
32-
if (cmdHelp) {
33-
cmdHelp()
32+
if (command in commands) {
33+
commands[command as CmdName]()
3434
return true
3535
}
3636

src/commands/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type {Command} from "./types"
1+
import type {Command, CmdName} from "./types"
22
import compile from "./compile"
33
import help from "./help"
44
import validate from "./validate"
55
import migrate from "./migrate"
66
import test from "./test"
77

8-
const commands: {[Cmd in string]?: Command} = {
9-
compile,
8+
const commands: {[Name in CmdName]: Command} = {
109
help,
10+
compile,
1111
validate,
1212
migrate,
1313
test,

src/commands/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ function execute(argv: ParsedArgs): boolean {
3636
return all(getFiles(argv.d), testDataFile)
3737

3838
function testDataFile(file: string): boolean {
39-
const data = openFile(file, "data file " + file)
39+
const data = openFile(file, `data file ${file}`)
4040
const validData = validate(data)
4141
let errors
4242
if (!validData) errors = logJSON(argv.errors, validate.errors, ajv)
4343

4444
if (validData === shouldBeValid) {
45-
console.log(file, "passed test")
45+
console.log(`${file} passed test`)
4646
if (errors) console.log(errors)
4747
return true
4848
}
49-
console.error(file, "failed test")
49+
console.error(`${file} failed test`)
5050
if (errors) console.error(errors)
5151
return false
5252
}

src/commands/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type {SchemaObject} from "ajv"
22
import type {ParsedArgs} from "minimist"
33

4+
export type CmdName = "compile" | "help" | "validate" | "migrate" | "test"
5+
46
export interface Command {
57
execute: (argv: ParsedArgs) => boolean
68
schema: SchemaObject

src/commands/util.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function decodeFile(contents: string, format: string): any {
3838
case "yaml":
3939
return yaml.safeLoad(contents)
4040
default:
41-
throw new Error("unsupported format " + format)
41+
throw new Error(`unsupported file format ${format}`)
4242
}
4343
}
4444

@@ -82,8 +82,8 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction {
8282
try {
8383
return ajv.compile(schema)
8484
} catch (err) {
85-
console.error("schema", schemaFile, "is invalid")
86-
console.error("error:", err.message)
85+
console.error(`schema ${schemaFile} is invalid`)
86+
console.error(`error: ${err.message}`)
8787
process.exit(1)
8888
}
8989
}
@@ -92,6 +92,7 @@ export function getSpec(argv: ParsedArgs): SchemaSpec {
9292
return argv.spec === "draft2019" ? "draft2019" : "draft7"
9393
}
9494

95-
export function all(xs: string[], f: (x: string) => boolean): boolean {
96-
return xs.reduce((res: boolean, x: string) => f(x) && res, true)
95+
// Like Array.prototype.every but without short-circuiting (for side-effects)
96+
export function all<T>(xs: T[], f: (x: T) => boolean): boolean {
97+
return xs.reduce((res: boolean, x: T) => f(x) && res, true)
9798
}

src/commands/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function execute(argv: ParsedArgs): boolean {
3434
return all(getFiles(argv.d), validateDataFile)
3535

3636
function validateDataFile(file: string): boolean {
37-
const data = openFile(file, "data file " + file)
37+
const data = openFile(file, `data file ${file}`)
3838
let original
3939
if (argv.changes) original = JSON.parse(JSON.stringify(data))
4040
const validData = validate(data) as boolean

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import minimist = require("minimist")
44
import commands from "./commands"
55
import {checkOptions} from "./commands/options"
66
import usage from "./commands/usage"
7+
import type {CmdName} from "./commands/types"
78

89
const argv = minimist(process.argv.slice(2))
910
const command = argv._[0] || "validate"
10-
const cmd = commands[command]
11-
12-
if (cmd) {
11+
if (command in commands) {
12+
const cmd = commands[command as CmdName]
1313
const errors = checkOptions(cmd.schema, argv)
1414
if (errors) {
1515
console.error(errors)
@@ -20,7 +20,7 @@ if (cmd) {
2020
process.exit(ok ? 0 : 1)
2121
}
2222
} else {
23-
console.error("Unknown command", command)
23+
console.error(`Unknown command ${command}`)
2424
usage()
2525
process.exit(2)
2626
}

test/migrate.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ describe("migrate", function () {
9696

9797
it("should fail on invalid schema", (done) => {
9898
cli("migrate -s test/migrate/schema_invalid.json", (error, stdout, stderr) => {
99-
console.log(error, stdout, stderr)
10099
assert(error instanceof Error)
101100
assert.strictEqual(stdout, "")
102101
assertError(stderr)

0 commit comments

Comments
 (0)