diff --git a/.gitignore b/.gitignore index 5466f24..a0908fe 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ results node_modules npm-debug.log + +pagespeed.json diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 7300000..bc36c1f 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -26,6 +26,13 @@ module.exports = (grunt) -> locale: "en_GB" strategy: "mobile" threshold: 62 + toFile: + options: + paths: ["/speed/docs/insights/about", "/speed/docs/insights/v1/getting_started"] + locale: "en_GB" + strategy: "desktop" + file: "pagespeed" + filepath: "./" grunt.loadTasks 'tasks' diff --git a/README.md b/README.md index 1e8e004..0364559 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ _This task is a [multi task][] so any targets, files and options should be speci [multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks -###Usage Example - +### Usage Example +```js pagespeed: { options: { nokey: true, @@ -73,7 +73,19 @@ _This task is a [multi task][] so any targets, files and options should be speci threshold: 80 } } + saveOutputToFile: { + options: { + paths: ["/speed/docs/insights/v1/getting_started", "/speed/docs/about"], + locale: "en_GB", + strategy: "desktop", + threshold: 80, + format: "json", + file: "pagespeed", + filepath: "results/" + } + } } +``` ### Options @@ -85,6 +97,17 @@ Use the nokey option to test-drive PageSpeed Insights before acquiring a key for #### key Type: `String` +#### format +Type: `String`, values(json|cli|tap) + +#### file +Type: `String`, filename for output + +If file is set, format is set to json. + +#### filepath +Type: `String` path for output + [Google API Key](https://code.google.com/apis/console/) #### url diff --git a/tasks/lib/config.coffee b/tasks/lib/config.coffee index dd52a1b..9695e6e 100644 --- a/tasks/lib/config.coffee +++ b/tasks/lib/config.coffee @@ -13,10 +13,15 @@ exports.init = (grunt) -> exports = {} config = {} DEFAULT_THRESHOLD = 70 + DEFAULT_FORMAT = 'cli' key = -> config["key"] if config["key"] + format = -> + return config["format"] if config["format"] + return DEFAULT_FORMAT unless config["format"] + nokey = -> config["nokey"] if config["nokey"] @@ -38,6 +43,20 @@ exports.init = (grunt) -> strategy = -> config["strategy"] if config["strategy"] + timeout = -> + config["timeout"] if config["timeout"] + + file = -> + config["file"] + ".json" if config["file"] + + filepath = -> + config["filepath"] if config["filepath"] + + format = -> + return "json" if config["file"] + return config["format"] if config["format"] && config['format'].match(/json|cli|tap/) + 'cli' unless config["format"] && config['format'].match(/json|cli|tap/) + threshold = -> return DEFAULT_THRESHOLD unless config["threshold"] config["threshold"] @@ -56,6 +75,9 @@ exports.init = (grunt) -> param["locale"] = locale() param["strategy"] = strategy() param["threshold"] = threshold() + param["filepath"] = filepath() + param["file"] = file() + param["format"] = format() if config["format"] param params diff --git a/tasks/pagespeed.coffee b/tasks/pagespeed.coffee index c13ab50..064a65a 100644 --- a/tasks/pagespeed.coffee +++ b/tasks/pagespeed.coffee @@ -19,9 +19,31 @@ module.exports = (grunt) -> numOfTests = params.length current = 0 + output = [] + for index, options of params - psi.output(options.url, options).then (response) -> - current++ - done() if numOfTests == current - .catch (err) -> - done(err) if numOfTests == current + # safe output to file + if options.file + psi(options.url, options).then (data) -> + current++ + grunt.log.write 'url:' + data.id + ' ' + grunt.log.ok 'score:' + data.ruleGroups.SPEED.score + # full output + # output.push {data} + output.push {'url': data.id, 'score': data.ruleGroups.SPEED.score, 'pageStats': data.pageStats } + if numOfTests == current + grunt.file.write options.filepath + options.file, JSON.stringify(output, false, 4) + grunt.log.writeln(' ') + grunt.log.ok 'Wrote output to ' + options.filepath + options.file + done() + .catch (err) -> + current++ + done(err) if numOfTests == current + # print output to cli + else + psi.output(options.url, options).then (response) -> + current++ + done() if numOfTests == current + .catch (err) -> + current++ + done(err) if numOfTests == current