Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Break out option parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreedm committed Feb 25, 2014
1 parent 83dfa1a commit df22471
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 34 deletions.
72 changes: 72 additions & 0 deletions lib/optparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/

var fs = require('fs');
var path = require('path');

var ABS_URL = require('./contants.js').ABS_URL;
var DEFAULT = 'vulcanized.html';

// validate options with boolean return
function processOptions(optHash, callback) {
excludes = {
imports: [ABS_URL],
scripts: [ABS_URL],
styles: [ABS_URL]
};

var options = {
csp: '',
input: '',
excludes: excludes,
output: '',
outputDir: ''
};

if (!optHash.input) {
return callback('No input file given!');
}

options.input = optHash.input;

if (optHash.excludes) {
var e = optHash.excludes;
try {
if (e.imports) {
e.imports.forEach(function(r) {
excludes.imports.push(new RegExp(r));
});
}
if (e.scripts) {
e.scripts.forEach(function(r) {
excludes.scripts.push(new RegExp(r));
});
}
if (e.styles) {
e.styles.forEach(function(r) {
excludes.styles.push(new RegExp(r));
});
}
} catch(_) {
return callback('Malformed import exclude config');
}
}

if (!optHash.output) {
options.output = path.resolve(path.dirname(optHash.input), DEFAULT);
options.outputDir = path.dirname(options.output);
} else {
options.output = optHash.output;
}

if (optHash.csp) {
options.csp = options.output.replace(/\.html$/, '.js');
}

callback(null, options);
}

exports.processOptions = processOptions;
38 changes: 4 additions & 34 deletions lib/vulcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,18 @@ var fs = require('fs');
var path = require('path');
var uglify = require('uglify-js');

var excludes = {};
var constants = require('./constants.js');
var optparser = require('./optparser.js');

var import_buffer = [];
var imports_before_polymer = [];
var read = {};
var options = {};

// validate options with boolean return
function setOptions(optHash) {
if (!optHash.input || !fs.existsSync(optHash.input)) {
console.error('No input file given!');
return;
}

excludes = {
imports: [ABS_URL],
scripts: [ABS_URL],
styles: [ABS_URL]
};

if (optHash.excludes) {
var e = optHash.excludes;
if (e.imports && Array.isArray(e.imports)) {
e.imports.forEach(function(r) {
excludes.imports.push(new RegExp(r));
});
} else {
console.error('Malformed import exclude config');
return;
}
}

if (!optHash.output) {
console.warn('Default output to vulcanized.html' + (optHash.csp ? ' and vulcanized.js' : '') + ' in the input directory.');
optHash.output = path.resolve(path.dirname(optHash.input), DEFAULT_OUTPUT);
}

optHash.outputDir = path.dirname(optHash.output);
options = optHash;

return true;
// validate options with boolean return
function setOptions(optHash, callback) {
optparser.processOptions(optHash, callback);
}

function exclude(regexes, href) {
Expand Down

0 comments on commit df22471

Please sign in to comment.