melanie revised this gist . Go to revision
1 file changed, 39 insertions
unpkg.js(file created)
@@ -0,0 +1,39 @@ | |||
1 | + | // Variables used by Scriptable. | |
2 | + | // These must be at the very top of the file. Do not edit. | |
3 | + | // icon-color: green; icon-glyph: archive; | |
4 | + | // unpkg: like NPM but not as good | |
5 | + | // and available in Scriptable | |
6 | + | // | |
7 | + | // Examples: | |
8 | + | // - const _ = await unpkg('lodash') | |
9 | + | // - const CryptoJS = await unpkg('crypto-js') | |
10 | + | // - const OAuth = await unpkg('oauth-1.0a') | |
11 | + | ||
12 | + | const unpkg = (package, file, version) => { | |
13 | + | return new Promise((callback) => { | |
14 | + | const jsFile = file || package | |
15 | + | const pkgVersion = version ? `@${version}` : '' | |
16 | + | const fm = FileManager.iCloud() | |
17 | + | const modulesPath = fm.joinPath(fm.documentsDirectory(), 'modules/') | |
18 | + | const modulePath = fm.joinPath(modulesPath, `${package}${pkgVersion}/`) | |
19 | + | const filePath = fm.joinPath(modulePath, `${jsFile.split('/')[jsFile.split('/').length-1]}.js`) | |
20 | + | ||
21 | + | if (!fm.fileExists(modulePath)) { | |
22 | + | fm.createDirectory(modulePath, true) | |
23 | + | } | |
24 | + | if (!fm.fileExists(filePath) ) { | |
25 | + | const req = new Request(`https://unpkg.com/${package}${pkgVersion}/${jsFile}.js`) | |
26 | + | req.loadString().then(res => { | |
27 | + | fm.writeString(filePath, `${res}`).then(() => { | |
28 | + | callback(importModule(filePath)) | |
29 | + | }) | |
30 | + | }) | |
31 | + | } else { | |
32 | + | fm.downloadFileFromiCloud(filePath).then(() => { | |
33 | + | callback(importModule(filePath)) | |
34 | + | }) | |
35 | + | } | |
36 | + | }) | |
37 | + | } | |
38 | + | ||
39 | + | module.exports = unpkg |
Newer
Older