Skip to content

Commit

Permalink
fix: make sure we can compile the examples in the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Aug 22, 2023
1 parent 67bf7ee commit 65b0e3c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/03-simple-commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MyCommand extends Command {
})
}

args = (parser: Args<unknown>) =>
args = (parser: Args<{}>) =>
parser.arg(['--cmd-arg'], a.string())

run = this.runner(async args => {
Expand Down
4 changes: 2 additions & 2 deletions examples/04-package-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Query extends Command {
})
}

args = (parser: Args<unknown>) => parser
args = (parser: Args<{}>) => parser
.arg(['--search', '-s'],
a.string()
.array()
Expand Down Expand Up @@ -58,7 +58,7 @@ class Sync extends Command {
})
}

args = (parser: Args<unknown>) => parser
args = (parser: Args<{}>) => parser
.arg(['--search', '-s'],
a.string()
.array()
Expand Down
6 changes: 3 additions & 3 deletions examples/05-application-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

import { Args, ParserOpts, a, util, Middleware } from 'args.ts'
import { Args, ParserOpts, a, util, Resolver } from 'args.ts'

export const parserOpts: ParserOpts = {
programName: '05-application-config',
programDescription: 'description'
}

class UserConfigMiddleware extends Middleware {
class UserConfigResolver extends Resolver {
private data: { [k: string]: string | undefined } = {}

async load (): Promise<this> {
Expand All @@ -33,7 +33,7 @@ async function main (): Promise<void> {
.arg(['--username'], a.string())
.arg(['--password'], a.string())
// The string here is used for internal identification of the middleware
.middleware(await (new UserConfigMiddleware('user-config')).load())
.resolver(await (new UserConfigResolver('user-config')).load())

const result = util.exitOnFailure(await parser.parse(util.makeArgs(), true))

Expand Down
12 changes: 6 additions & 6 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Args<TArgTypes extends DefaultArgTypes = DefaultArgTypes> {
public commands: PrefixTree<InternalCommand> = new PrefixTree()
public commandsList: InternalCommand[] = []

public middlewares: Resolver[] = []
public resolvers: Resolver[] = []
public footerLines: string[] = []
public headerLines: string[] = []

Expand All @@ -56,8 +56,8 @@ export class Args<TArgTypes extends DefaultArgTypes = DefaultArgTypes> {
}
}

public middleware (middleware: Resolver): Args<TArgTypes> {
this.middlewares.push(middleware)
public resolver (resolver: Resolver): Args<TArgTypes> {
this.resolvers.push(resolver)
return this
}

Expand Down Expand Up @@ -293,15 +293,15 @@ export class Args<TArgTypes extends DefaultArgTypes = DefaultArgTypes> {
commandParser.opts,
commandParser.arguments,
commandParser.argumentsList,
[...commandParser.opts.resolvers, ...commandParser.middlewares]
[...commandParser.opts.resolvers, ...commandParser.resolvers]
)
} else {
coercionResult = await coerce(
parseResult.val,
this.opts,
this.arguments,
this.argumentsList,
[...this.opts.resolvers, ...this.middlewares]
[...this.opts.resolvers, ...this.resolvers]
)
}

Expand Down Expand Up @@ -346,6 +346,6 @@ export class Args<TArgTypes extends DefaultArgTypes = DefaultArgTypes> {
public reset (): void {
this.arguments = new PrefixTree()
this.commands = new PrefixTree()
this.middlewares = []
this.resolvers = []
}
}
20 changes: 20 additions & 0 deletions test/ensure-examples-compile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fsp from 'fs/promises'
import path from 'path'
import { execSync } from 'child_process'

describe('Example compilation', () => {
it('can compile the examples', async () => {
if (!process.env.GITHUB_ACTIONS) {
return
}

const dir = path.join(__dirname, '..', 'examples')
const examples = await fsp.readdir(dir)

for (const example of examples) {
console.time(example)
execSync(`npm run build --prefix ${path.join(dir, example)}`)
console.timeEnd(example)
}
})
})
4 changes: 3 additions & 1 deletion test/integrations/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Args, a } from '../../src'
import { parserOpts } from '../shared'
import { readFile } from 'fs/promises'
import { CoercionError, ParseError } from '../../src/error'
import path from 'path'

describe('Edge cases', () => {
const parser = new Args(parserOpts)
.arg(['--non-latin'], a.string())
Expand All @@ -10,7 +12,7 @@ describe('Edge cases', () => {
it('can parse the naughty strings', async () => {
const strings: string[] = JSON.parse(
await readFile(
'./test/integrations/naughty-strings.json',
path.join(__dirname, 'naughty-strings.json'),
{ encoding: 'utf-8' }
)
)
Expand Down
4 changes: 2 additions & 2 deletions test/integrations/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Resolver tests', () => {

const parser = new Args(parserOpts)
.arg(['--ware'], a.string())
.middleware(new MockResolver(existsFn, valueFn))
.resolver(new MockResolver(existsFn, valueFn))

const result = await runArgsExecution(parser, '')
expect(result.ware).toBe('value')
Expand All @@ -34,7 +34,7 @@ describe('Resolver tests', () => {

const parser = new Args(parserOpts)
.arg(['--ware'], a.decimal())
.middleware(new MockResolver(existsFn, valueFn))
.resolver(new MockResolver(existsFn, valueFn))

const result = expect(async () => await runArgsExecution(parser, ''))
await result.rejects.toMatchInlineSnapshot(`[Error: parser 'decimal' failed: 'value' is not a number, expected 'decimal' received 'value']`)
Expand Down

0 comments on commit 65b0e3c

Please sign in to comment.