This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from yahoo/commandLineOptions
Make extractify command line friendly
- Loading branch information
Showing
6 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function handleUnderScore(obj, prop) { | ||
if (!Array.isArray(obj[prop]) && obj[prop]._ && Array.isArray(obj[prop]._)) { | ||
if (obj[prop]._.length === 0) { | ||
obj[prop] = [obj[prop]]; | ||
} else { | ||
obj[prop] = obj[prop]._; | ||
} | ||
delete obj[prop]._; | ||
} | ||
return obj; | ||
} | ||
|
||
module.exports = function processOptions(opts) { | ||
if (!(opts && opts.lazy)) { | ||
throw new Error('Please provide lazy option. Refer to extractify documentation for available options'); | ||
} | ||
|
||
if (opts._) { | ||
//command line | ||
if (!opts._.length) { | ||
delete opts._; | ||
} | ||
if (opts.bundleMapOption && opts.bundleMapOption._) { | ||
delete opts.bundleMapOption._; | ||
} | ||
|
||
} else { | ||
// api or grunt | ||
return opts; | ||
} | ||
|
||
// process commandline options | ||
opts = handleUnderScore(opts, 'lazy'); | ||
for (var i = 0; i < opts.lazy.length; i++) { | ||
if (opts.lazy[i].entries && typeof opts.lazy[i].entries === 'string') { | ||
// handle single entry | ||
opts.lazy[i].entries = [opts.lazy[i].entries]; | ||
} else { | ||
// handle multiple entries | ||
opts.lazy[i] = handleUnderScore(opts.lazy[i], 'entries'); | ||
} | ||
|
||
delete opts.lazy[i]._; | ||
} | ||
|
||
return opts; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* Copyright 2016, Yahoo Inc. | ||
Copyrights licensed under the MIT License. | ||
See the accompanying LICENSE file for terms. */ | ||
|
||
var test = require('tap').test; | ||
|
||
test('commandline options', function(t) { | ||
var processOptions = require('../lib/process_options'); | ||
var opt1 = { | ||
'_': [], | ||
'lazy': { | ||
'_': [{ | ||
'_': [], | ||
'entries': { | ||
'_': ['foo1', 'bar1'] | ||
}, | ||
'outfile': 'oo1' | ||
}, { | ||
'_': [], | ||
'entries': { | ||
'_': ['foo12', 'bar2'] | ||
}, | ||
'outfile': 'oo2' | ||
}] | ||
}, | ||
'bundleMapOption': { | ||
'_': [], | ||
'dest': 'boo', | ||
'basedir': 'moo' | ||
} | ||
}; | ||
var expected1 = { | ||
'lazy': [{ | ||
'entries': ['foo1', 'bar1'], | ||
'outfile': 'oo1' | ||
}, { | ||
'entries': ['foo12', 'bar2'], | ||
'outfile': 'oo2' | ||
}], | ||
'bundleMapOption': { | ||
'dest': 'boo', | ||
'basedir': 'moo' | ||
} | ||
}; | ||
var opt2 = { | ||
'_': [], | ||
'lazy': { | ||
'_': [{ | ||
'_': [], | ||
'entries': { | ||
'_': ['foo1', 'bar1'] | ||
}, | ||
'outfile': 'oo1' | ||
}] | ||
} | ||
}; | ||
var expected2 = { | ||
'lazy': [{ | ||
'entries': ['foo1', 'bar1'], | ||
'outfile': 'oo1' | ||
}] | ||
}; | ||
var opt3 = { | ||
'_': [], | ||
'lazy': { | ||
'_': [{ | ||
'_': [], | ||
'entries': { | ||
'_': ['foo1', 'bar1'] | ||
}, | ||
'outfile': 'oo1' | ||
}] | ||
} | ||
}; | ||
var expected3 = { | ||
'lazy': [{ | ||
'entries': ['foo1', 'bar1'], | ||
'outfile': 'oo1' | ||
}] | ||
}; | ||
var opt4 = { | ||
'_': [], | ||
'lazy': { | ||
'_': [], | ||
'entries': 'foo1', | ||
'output': 'oo1' | ||
} | ||
}; | ||
var expected4 = { | ||
'lazy': [{ | ||
'entries': ['foo1'], | ||
'output': 'oo1' | ||
}] | ||
}; | ||
|
||
t.plan(5); | ||
t.same(processOptions(opt1), expected1); | ||
t.same(processOptions(opt2), expected2); | ||
t.same(processOptions(opt3), expected3); | ||
t.same(processOptions(opt4), expected4); | ||
t.throws(function() { | ||
processOptions({}); | ||
}); | ||
}); |