unpkg.js
· 1.3 KiB · JavaScript
Bruto
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: archive;
// unpkg: like NPM but not as good
// and available in Scriptable
//
// Examples:
// - const _ = await unpkg('lodash')
// - const CryptoJS = await unpkg('crypto-js')
// - const OAuth = await unpkg('oauth-1.0a')
const unpkg = (package, file, version) => {
return new Promise((callback) => {
const jsFile = file || package
const pkgVersion = version ? `@${version}` : ''
const fm = FileManager.iCloud()
const modulesPath = fm.joinPath(fm.documentsDirectory(), 'modules/')
const modulePath = fm.joinPath(modulesPath, `${package}${pkgVersion}/`)
const filePath = fm.joinPath(modulePath, `${jsFile.split('/')[jsFile.split('/').length-1]}.js`)
if (!fm.fileExists(modulePath)) {
fm.createDirectory(modulePath, true)
}
if (!fm.fileExists(filePath) ) {
const req = new Request(`https://unpkg.com/${package}${pkgVersion}/${jsFile}.js`)
req.loadString().then(res => {
fm.writeString(filePath, `${res}`).then(() => {
callback(importModule(filePath))
})
})
} else {
fm.downloadFileFromiCloud(filePath).then(() => {
callback(importModule(filePath))
})
}
})
}
module.exports = unpkg
| 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 |
| 40 |