Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: extract option splitting and use it for --compiler (#746) #958

Merged
merged 1 commit into from
Nov 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})