-
Notifications
You must be signed in to change notification settings - Fork 388
Support for CLI configuration in an external JSON file #80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,40 +22,75 @@ var meow = require('meow'); | |
var path = require('path'); | ||
var swPrecache = require('./'); | ||
|
||
function setDefaults(cli) { | ||
cli.flags.root = cli.flags.root || './'; | ||
if (cli.flags.root.lastIndexOf('/') !== cli.flags.root.length - 1) { | ||
cli.flags.root += '/'; | ||
function setDefaults(cli, configFileFlags) { | ||
var compositeFlags = cli.flags; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be easier to follow. Perhaps something like:
(probably have to polyfill Object.assign if you still want to support older node). Fewer characters to parse on each line is usually better for readability, and it's much easier to see what the default values actually are. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still officially support I'd rather rewrite all this code and then run it through Babel and publish that than do ad hoc polyfilling. I can track that in a different bug so as not to delay this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
compositeFlags.root = compositeFlags.root || configFileFlags.root || './'; | ||
if (compositeFlags.root.lastIndexOf('/') !== compositeFlags.root.length - 1) { | ||
compositeFlags.root += '/'; | ||
} | ||
cli.flags.stripPrefix = cli.flags.stripPrefix || cli.flags.root; | ||
cli.flags.swFile = cli.flags.swFile || 'service-worker.js'; | ||
cli.flags.swFilePath = path.join(cli.flags.root, cli.flags.swFile); | ||
cli.flags.staticFileGlobs = cli.flags.staticFileGlobs ? | ||
[cli.flags.staticFileGlobs] : [cli.flags.root + '/**/*.*']; | ||
cli.flags.cacheId = cli.flags.cacheId || cli.pkg.name; | ||
if (cli.flags.ignoreUrlParametersMatching) { | ||
cli.flags.ignoreUrlParametersMatching = | ||
cli.flags.ignoreUrlParametersMatching.split(',').map( | ||
function(s) { | ||
return new RegExp(s); | ||
} | ||
); | ||
|
||
compositeFlags.stripPrefix = compositeFlags.stripPrefix || | ||
configFileFlags.stripPrefix || compositeFlags.root; | ||
|
||
compositeFlags.swFile = compositeFlags.swFile || configFileFlags.swFile || | ||
'service-worker.js'; | ||
compositeFlags.swFilePath = path.join(compositeFlags.root, | ||
compositeFlags.swFile); | ||
|
||
compositeFlags.cacheId = compositeFlags.cacheId || | ||
configFileFlags.cacheId || cli.pkg.name; | ||
|
||
compositeFlags.staticFileGlobs = compositeFlags.staticFileGlobs || | ||
configFileFlags.staticFileGlobs; | ||
if (compositeFlags.staticFileGlobs) { | ||
if (typeof compositeFlags.staticFileGlobs === 'string') { | ||
compositeFlags.staticFileGlobs = [compositeFlags.staticFileGlobs]; | ||
} | ||
} else { | ||
compositeFlags.staticFileGlobs = [compositeFlags.root + '/**/*.*']; | ||
} | ||
if (cli.flags.importScripts) { | ||
cli.flags.importScripts = cli.flags.importScripts.split(','); | ||
|
||
compositeFlags.ignoreUrlParametersMatching = | ||
compositeFlags.ignoreUrlParametersMatching || | ||
configFileFlags.ignoreUrlParametersMatching; | ||
if (compositeFlags.ignoreUrlParametersMatching && | ||
typeof compositeFlags.ignoreUrlParametersMatching === 'string') { | ||
compositeFlags.ignoreUrlParametersMatching = | ||
compositeFlags.ignoreUrlParametersMatching.split(',').map(function(s) { | ||
return new RegExp(s); | ||
}); | ||
} | ||
|
||
compositeFlags.importScripts = compositeFlags.importScripts || | ||
configFileFlags.importScripts; | ||
if (compositeFlags.importScripts && | ||
typeof compositeFlags.importScripts === 'string') { | ||
compositeFlags.importScripts = compositeFlags.importScripts.split(','); | ||
} | ||
|
||
return cli.flags; | ||
return compositeFlags; | ||
} | ||
|
||
var cli = meow({ | ||
help: 'Options from https://github.com/GoogleChrome/sw-precache#options are accepted as flags.' | ||
help: 'Options from https://github.com/GoogleChrome/sw-precache#options ' + | ||
'are accepted as flags.\nAlternatively, use --config <file>, where ' + | ||
'<file> is the path to the JSON data representing the same options.\n' + | ||
'When both a config file and command line option is given, the ' + | ||
'command line option takes precedence.' | ||
}); | ||
var options = setDefaults(cli); | ||
|
||
// If the --config option is used, then read the options from an external | ||
// JSON configuration file. Options from the --config file can be overwritten | ||
// by any command line options. | ||
var configFileFlags = cli.flags.config ? | ||
require(path.resolve(cli.flags.config)) : {}; | ||
var options = setDefaults(cli, configFileFlags); | ||
|
||
swPrecache.write(options.swFilePath, options, function(error) { | ||
if (error) { | ||
throw error; | ||
console.error(error.stack); | ||
process.exit(1); | ||
} | ||
|
||
console.log(options.swFilePath, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"dynamicUrlToDependencies": { | ||
"dynamic/page1": [ | ||
"app/views/layout.jade", | ||
"app/views/page1.jade" | ||
], | ||
"dynamic/page2": [ | ||
"app/views/layout.jade", | ||
"app/views/page2.jade" | ||
] | ||
}, | ||
"staticFileGlobs": [ | ||
"app/css/**.css", | ||
"app/**.html", | ||
"app/images/**.*", | ||
"app/js/**.js" | ||
], | ||
"stripPrefix": "app/", | ||
"verbose": true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps include a very basic snippet of what the config file would look like. I just find it easier to grok what it means with an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.