Created
March 21, 2018 06:19
-
-
Save brainysmurf/373c1924efe8ef51043acdfb68f1a94a to your computer and use it in GitHub Desktop.
AppsScriptsRequests
This file contains 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
(function(global,name,Package,helpers){var ref=function wrapper(args){var wrapped=function(){return Package.apply(global.Import&&Import.module?Import._import(name):global[name],[global].concat(Array.prototype.slice.call(arguments)))};for(i in args){wrapped[i]=args[i]}return wrapped}(helpers);if(global.Import&&Import.module){Import.register(name,ref)}else{Object.defineProperty(global,name,{value:ref});global.Import=global.Import||function(lib){return global[lib]};Import.module=false}})(this, | |
'AppsScriptsRequests', | |
function AppsScriptsRequests_ (_, _config) { | |
_config = _config || {}; | |
_config.scriptId = _config.scriptId || null; | |
if (_config.scriptId === 'me') _config.scriptId = ScriptApp.getScriptId(); | |
var self, requests, keepAppsscriptsFile, requiresScriptId; | |
self = this; | |
requests = function (met) { | |
/* TODO: cache these */ | |
return Import("Requests")({ | |
oauth: 'me', | |
discovery: { | |
name: 'script', | |
version: 'v1', | |
category: 'projects', | |
method: met, | |
}, | |
}); | |
}; | |
/* | |
keepResourcesBasedOnExisting: | |
Files with the same names as current content keep their createTime date | |
appsscripts.json file is kept | |
*/ | |
keepResourcesBasedOnExisting = function (resources, commands) { | |
resources = resources || {}; | |
resources.files = resources.files || []; | |
commands = commands || {}; | |
commands.keepAppscript = commands.keepAppscript || true; | |
commands.keepMissing = commands.keepMissing || false; | |
commands.rememberCreateTime = commands.rememberCreateTime || false; | |
var hash; | |
hash = self.utils.filesListToHash(resources.files); | |
var data = content().json(); | |
var existingHash = self.utils.filesListToHash(data.files); | |
if (!commands.keepAppscript && !hash.appscript) { | |
throw Error("No appscript file and setting to not include it"); | |
} | |
if (commands.keepAppscript && !hash.appsscript) { | |
hash['appsscript'] = existingHash.appsscript; // assuming guaranteed to be present | |
} | |
if (commands.rememberCreatedAt) { | |
Object.keys(hash).reduce(function (acc, key) { | |
if (existingHash[key]) { | |
hash[key].createTime = existingHash[key].createTime; | |
} | |
}); | |
} | |
if (commands.keepMissing) { | |
Object.keys(existingHash).reduce(function (acc, key) { | |
if (!hash[key]) acc.push(key); | |
return acc; | |
}, []).forEach(function (missingKey) { | |
hash[missingKey] = existingHash[missingKey]; | |
}); | |
} | |
Logger.log(Object.keys(hash)); | |
resources.files = self.utils.hashToFilesList(hash); | |
return resources; | |
}; | |
requiresScriptId = function () { | |
if (!_config.scriptId) throw Error("scriptId is not defined"); | |
}; | |
var create = function (title, options) { | |
options = options || {}; | |
options.body = options.body || {}; | |
options.body.title = title || "New Unnamed Project"; | |
return requests('create').post({/*empty*/}, options); | |
}; | |
var new_ = function (title, options, commands) { | |
var resp, json; | |
options = options || {}; | |
options.body = options.body || {}; | |
options.body.files = options.body.files || []; | |
resp = create(title /* don't pass options here */); | |
json = resp.json(); | |
if (json.error) { | |
throw Error("An error occurred: " + json.error.message); | |
} | |
_config.scriptId = json.scriptId; | |
if (!options.body.files || options.body.files.length == 0) return resp; | |
return update(options, commands); | |
}; | |
var update = function (options, commands) { | |
requiresScriptId(); | |
options = options || {}; | |
commands = commands || {}; | |
if (!options.body || !options.body.files) throw Error("No body.files key in update call"); | |
if (!Array.isArray(options.body.files)) throw Error("files needs to be an array"); | |
options.body.files.forEach(function (file) { | |
var split, ext; | |
if (!file.type && file.name) { | |
split = file.name.split('.'); | |
ext = split[split.length-1].toLowerCase(); | |
file.type = {'gs': 'SERVER_JS', 'js': 'SERVER_JS', 'json': 'JSON', 'html': 'HTML'}[ext] || 'ENUM_TYPE_UNSPECIFIED'; | |
} | |
}); | |
if (commands) { | |
options.body = keepResourcesBasedOnExisting(options.body, commands); | |
} | |
return requests('updateContent').put({scriptId: _config.scriptId}, options); | |
}; | |
var content = function (options) { | |
requiresScriptId(); | |
return requests('getContent').get({scriptId: _config.scriptId}, options); | |
}; | |
var metadata = function (options) { | |
requiresScriptId(); | |
return requests('get').get({scriptId: _config.scriptId}, options); | |
}; | |
return { | |
create: create, | |
new_: new_, | |
update: update, | |
content: content, | |
metadata: metadata | |
}; | |
}, | |
{ /* helpers */ | |
utils: { | |
filesListToHash: function (files) { | |
return files.reduce(function (acc, item) { | |
acc[item.name] = item; | |
return acc; | |
}, {}); | |
}, | |
hashToFilesList: function (hash) { | |
return Object.keys(hash).reduce(function (acc, key) { | |
acc.push(hash[key]); | |
return acc; | |
}, []); | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment