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

Support for CLI configuration in an external JSON file #80

Merged
merged 2 commits into from
Feb 10, 2016
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ $ sw-precache --root=dist --static-file-globs='dist/**/*.html'
to your shell (such as the `*` characters in the sample command line above,
for example).

Finally, there's support for storing a complex configuration in an external
JSON file, using `--config <file>`. Any of the options from the file can be
overridden via a command-line flag. For example,

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

```sh
$ sw-precache --config=path/to/sw-precache-config.json --verbose --no-handle-fetch
```

will generate a service worker file using the options provided in the
`path/to/sw-precache-config.json` file, but with the `verbose` option set to
`true` and the `handleFetch` option set to `false`.

`sw-precache-config.json` might look like:

```json
{
"staticFileGlobs": [
"app/css/**.css",
"app/**.html",
"app/images/**.*",
"app/js/**.js"
],
"stripPrefix": "app/"
}
```

## API

### Methods
Expand Down
81 changes: 58 additions & 23 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be easier to follow.

Perhaps something like:

// Merge command-line and config file flags
var flags = Object.assign(cli.flags, configFileFlags);
var defaults = {
  root: './',
  swFile: 'service-worker.js',
  cacheId: cli.pkg.name;
};

Object.assign(flags, defaults);

if (flags.root.lastIndexOf('/') !== flags.root.length - 1) {
  flags.root += '/';
}
flags.stripPrefix = flags.stripPrefix || flags.root;
flags.swFilePath = path.join(flags.root, flags.swFile);

(etc.)

(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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still officially support node >= 0.1.0 as a runtime.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#82


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,
Expand Down
20 changes: 20 additions & 0 deletions demo/sw-precache-config.json
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
}