Skip to content

Commit

Permalink
cli: extract option splitter and use it for --compiler (#958)
Browse files Browse the repository at this point in the history
resolves #746
  • Loading branch information
darrinholst authored and charlierudolph committed Nov 5, 2017
1 parent 184faba commit 8b5fcdd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/cli/configuration_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ArgvParser from './argv_parser'
import fs from 'mz/fs'
import path from 'path'
import PathExpander from './path_expander'
import OptionSplitter from './option_splitter'
import Promise from 'bluebird'

export default class ConfigurationBuilder {
Expand Down Expand Up @@ -95,17 +96,9 @@ export default class ConfigurationBuilder {

getFormats() {
const mapping = { '': 'progress' }
this.options.format.forEach(function(format) {
let type = format
let outputTo = ''
const parts = format.split(/([^A-Z]):([^\\])/)

if (parts.length > 1) {
type = parts.slice(0, 2).join('')
outputTo = parts.slice(2).join('')
}

mapping[outputTo] = type
this.options.format.forEach(format => {
const [type, outputTo] = OptionSplitter.split(format)
mapping[outputTo || ''] = type
})
return _.map(mapping, function(type, outputTo) {
return { outputTo, type }
Expand Down Expand Up @@ -139,9 +132,9 @@ export default class ConfigurationBuilder {
async expandSupportCodePaths(supportCodePaths) {
const extensions = ['js']
this.options.compiler.forEach(compiler => {
const parts = compiler.split(':')
extensions.push(parts[0])
require(parts[1])
const [extension, module] = OptionSplitter.split(compiler)
extensions.push(extension)
require(module)
})
return await this.pathExpander.expandPathsWithExtensions(
supportCodePaths,
Expand Down
17 changes: 17 additions & 0 deletions src/cli/option_splitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default class OptionSplitter {
static split(option) {
const parts = option.split(/([^A-Z]):(?!\\)/)

return parts.reduce((memo, part, i) => {
if (partNeedsRecombined(i)) {
memo.push(parts.slice(i, i + 2).join(''))
}

return memo
}, [])
}
}

function partNeedsRecombined(i) {
return i % 2 === 0
}
52 changes: 52 additions & 0 deletions src/cli/option_splitter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import OptionSplitter from './option_splitter'

describe('OptionSplitter', function() {
const examples = [
{
description: "doesn't split when nothing to split on",
input: '../custom/formatter',
output: ['../custom/formatter']
},
{
description: 'splits relative unix paths',
input: '../custom/formatter:../formatter/output.txt',
output: ['../custom/formatter', '../formatter/output.txt']
},
{
description: 'splits absolute unix paths',
input: '/custom/formatter:/formatter/output.txt',
output: ['/custom/formatter', '/formatter/output.txt']
},
{
description: 'splits absolute windows paths',
input: 'C:\\custom\\formatter:C:\\formatter\\output.txt',
output: ['C:\\custom\\formatter', 'C:\\formatter\\output.txt']
},
{
description: 'does not split a single absolute windows paths',
input: 'C:\\custom\\formatter',
output: ['C:\\custom\\formatter']
},
{
description: 'splits extensions and modules',
input: 'ts:typescript',
output: ['ts', 'typescript']
},
{
description: 'splits extension and module with an absolute windows path',
input: 'ts:C:/typescript',
output: ['ts', 'C:/typescript']
},
{
description: 'splits more than 2 parts',
input: 'part1:part2:part3',
output: ['part1', 'part2', 'part3']
}
]

examples.forEach(({ description, input, output }) => {
it(description, function() {
expect(OptionSplitter.split(input)).to.eql(output)
})
})
})

0 comments on commit 8b5fcdd

Please sign in to comment.