Skip to content

Commit

Permalink
fix: refactor args formatting (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
warapitiya authored Jul 18, 2023
1 parent f39b981 commit 9f1d12f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
9 changes: 1 addition & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ export const PLUGIN_NAME = 'gulpYarn'
* @param args string
*/
export const formatArgument = (args: string): string => {
const arg = args.trim().replace(/\s\s+/g, ' ').split(/\s/)
return arg.map((l) => {
let result = l
while (!/--.*/.test(result))
result = `-${result}`

return result
}).join(' ')
return args.trim()
}

/**
Expand Down
33 changes: 16 additions & 17 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ import { formatArgument, formatArguments, resolveYarnOptions } from '../src/util

tap.test('formatArgument() returns formatted argument', (t) => {
t.plan(8)
t.equal(formatArgument('production'), '--production')
t.equal(formatArgument('production'), 'production')
t.equal(formatArgument('--production'), '--production')
t.equal(formatArgument('-production'), '--production')
t.equal(formatArgument('production --nice-work'), '--production --nice-work')
t.equal(formatArgument('--production nice-work'), '--production --nice-work')
t.equal(formatArgument('--production -nice-work'), '--production --nice-work')
t.equal(formatArgument('--production --nice-work'), '--production --nice-work')
t.equal(formatArgument('--production -nice-work'), '--production --nice-work')
t.equal(formatArgument('-production'), '-production')
t.equal(formatArgument('production --nice-work'), 'production --nice-work')
t.equal(formatArgument('--production nice-work'), '--production nice-work')
t.equal(formatArgument('--production -nice-work'), '--production -nice-work')
t.equal(formatArgument(' --production --nice-work '), '--production --nice-work')
t.end()
})

tap.test('formatArguments() returns formatted arguments', (t) => {
t.plan(9)
t.same(formatArguments(['production']), ['--production'])
t.same(formatArguments(['production']), ['production'])
t.same(formatArguments(['--production']), ['--production'])
t.same(formatArguments(['-production']), ['--production'])
t.same(formatArguments(['production', '--nice-work']), ['--production', '--nice-work'])
t.same(formatArguments(['--production', 'nice-work']), ['--production', '--nice-work'])
t.same(formatArguments(['--production', '-nice-work']), ['--production', '--nice-work'])
t.same(formatArguments(['-production']), ['-production'])
t.same(formatArguments(['production', '--nice-work']), ['production', '--nice-work'])
t.same(formatArguments(['--production', 'nice-work']), ['--production', 'nice-work'])
t.same(formatArguments(['--production', '-nice-work']), ['--production', '-nice-work'])
t.same(formatArguments(['--production', ' --nice-work']), ['--production', '--nice-work'])
t.same(formatArguments(['--production', ' -nice-work']), ['--production', '--nice-work'])
t.same(formatArguments('--production -nice-work'), ['--production --nice-work'])
t.same(formatArguments(['--production', ' -nice-work ']), ['--production', '-nice-work'])
t.same(formatArguments('--production -nice-work'), ['--production -nice-work'])
t.end()
})

Expand All @@ -44,11 +43,11 @@ tap.test('resolveYarnOptions() can resolve passed options', (t) => {

[arg1, arg2] = resolveYarnOptions({ args: ['done'] })
t.equal(arg1, null)
t.same(arg2, ['--done']);
t.same(arg2, ['done']);

[arg1, arg2] = resolveYarnOptions({ force: true, args: ['done'] })
[arg1, arg2] = resolveYarnOptions({ force: true, args: '--registry https://www.google.com' })
t.equal(arg1, null)
t.same(arg2, ['--force', '--done']);
t.same(arg2, ['--force', '--registry https://www.google.com']);

[arg1, arg2] = resolveYarnOptions()
t.equal(arg1, null)
Expand Down

0 comments on commit 9f1d12f

Please sign in to comment.