Last active
August 29, 2015 14:10
-
-
Save jsdf/94cdaf9830d8de318441 to your computer and use it in GitHub Desktop.
Drupal Services JSON export download in Coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
merge = require 'xtend' | |
Promise = require 'bluebird' | |
request = Promise.promisify require 'request' | |
fs = require 'fs' | |
Promise.promisifyAll(fs) | |
servicesEndpointUrl = process.argv[2] or 'http://example.com/api' | |
contentType = process.argv[3] or 'node' | |
idType = process.argv[4] or 'nid' | |
MAX_CONCURRENCY = 4 | |
clientError = (e) -> | |
e.code >= 400 and e.code < 500 | |
fs.mkdirAsync(contentType) | |
.catch Promise.OperationalError, (e) -> | |
if e.cause.code is 'EEXIST' | |
Promise.resolve() | |
else | |
Promise.reject(e) | |
.then -> | |
console.log servicesEndpointUrl + "/#{contentType}.json" | |
request(servicesEndpointUrl + "/#{contentType}.json") | |
.catch clientError, (e) -> | |
console.error e | |
.spread (res, body) -> | |
JSON.parse(body) | |
.map (value) -> | |
console.log "downloading #{value.uri}" | |
request("#{value.uri}.json") | |
.catch clientError, (e) -> | |
console.error e | |
.spread (res, body) -> | |
JSON.parse(body) | |
, concurrency: MAX_CONCURRENCY | |
.map (value) -> | |
fs.writeFileAsync("#{contentType}/#{value[idType]}.json", JSON.stringify(value, null, 2)) | |
.then -> console.log 'done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment