Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ results
node_modules

npm-debug.log

pagespeed.json
7 changes: 7 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tasks/lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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"]
Expand All @@ -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
Expand Down
32 changes: 27 additions & 5 deletions tasks/pagespeed.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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