forked from gideonsenku/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Env.scriptable
12 lines (11 loc) · 2.84 KB
/
Env.scriptable
1
2
3
4
5
6
7
8
9
10
11
12
{
"always_run_in_app" : false,
"icon" : {
"color" : "brown",
"glyph" : "terminal"
},
"name" : "Env",
"script" : "\/**\n * Author: GideonSenku\n * Github: https:\/\/github.com\/GideonSenku\n *\/\nmodule.exports = () => {\n return new(class {\n constructor() {\n this.request = new Request('')\n this.documentDirectory = FileManager.iCloud().documentsDirectory()\n this.defaultHeaders = {\n \"Accept\": \"application\/json\",\n \"Content-Type\": \"application\/json\"\n }\n }\n\n async get({ url, headers = {} }) {\n this.request.url = url\n this.request.method = 'GET'\n this.request.headers = {\n ...headers,\n ...this.defaultHeaders\n }\n return await this.request.loadJSON()\n }\n\n async getStr({ url, headers = {} }, callback = () => {}) {\n this.request.url = url\n this.request.method = 'GET'\n this.request.headers = {\n ...headers,\n ...this.defaultHeaders\n }\n const res = await this.request.loadString()\n callback(this.request.response)\n return res\n }\n\n async post({ url, body, headers = {} }) {\n this.request.url = url\n this.request.body = body ? JSON.stringify(body) : `{}`\n this.request.method = 'POST'\n this.request.headers = {\n ...headers,\n ...this.defaultHeaders\n }\n return await this.request.loadJSON()\n }\n \n async getFile({moduleName, url}) {\n const header = `\/\/ Variables used by Scriptable.\n\/\/ These must be at the very top of the file. Do not edit.\n\/\/ icon-color: deep-gray; icon-glyph: file-code;\\n`;\n const content = await this.getStr({ url })\n const fileHeader = content.includes('icon-color') ? `` : header\n this.writeFile(`${moduleName}`, `${fileHeader}${content}`)\n }\n \n async require({ moduleName, url = '', forceDownload = false }) {\n if (this.isFileExists(moduleName) && !forceDownload) {\n return importModule(moduleName)\n } else {\n await this.getFile({moduleName, url})\n return importModule(moduleName)\n }\n }\n\n writeFile(fileName, content) {\n let file = this.initFile(fileName)\n const filePath = `${this.documentDirectory}\/${file}`\n FileManager.iCloud().writeString(filePath, content)\n return true\n }\n \n isFileExists(fileName) {\n let file = this.initFile(fileName)\n return FileManager.iCloud().fileExists(`${this.documentDirectory}\/${file}`)\n }\n\n initFile(fileName) {\n const hasSuffix = fileName.lastIndexOf('.') + 1\n return !hasSuffix ? `${fileName}.js` : fileName\n }\n readFile(fileName) {\n const file = this.initFile(fileName)\n return FileManager.iCloud().readString(`${this.documentDirectory}\/${file}`)\n }\n\n })()\n}",
"share_sheet_inputs" : [
]
}