+15
-7
@@ -8,2 +8,4 @@ 'use strict'; | ||
| return new Promise((resolve, reject) => { | ||
| let desiredMajor = null; | ||
| if (!name) { | ||
@@ -13,2 +15,6 @@ return reject(new Error(`Please specify the name of the library`)); | ||
| if (name.indexOf('@') > -1) { | ||
| [name, desiredMajor] = name.split('@'); | ||
| } | ||
| got(`https://api.cdnjs.com/libraries?search=${name}&fields=version,assets`, {json: true}) | ||
@@ -20,12 +26,14 @@ .then(res => { | ||
| const versionAssets = library.assets.filter(files => { | ||
| if (desiredMajor) { | ||
| return new RegExp(`^${desiredMajor}`).test(files.version) === true; | ||
| } | ||
| return files.version === library.version; | ||
| }); | ||
| // https://cdnjs.cloudflare.com/ajax/libs/[library]/[version]/[files] | ||
| const rootDirectory = library.latest.substring( | ||
| 0, | ||
| (library.latest.indexOf(library.version) + | ||
| library.version.length) | ||
| ).concat('/'); | ||
| // Return only the latest version | ||
| let files = versionAssets[0].files.map(item => urlResolve(rootDirectory, item)); | ||
| let files = versionAssets[0].files.map(item => urlResolve( | ||
| `https://cdnjs.cloudflare.com/ajax/libs/${library.name}/${versionAssets[0].version}/`, | ||
| item) | ||
| ); | ||
@@ -32,0 +40,0 @@ if (opts && opts.only) { |
+1
-1
| { | ||
| "name": "latest-lib", | ||
| "version": "0.1.2", | ||
| "version": "0.2.0", | ||
| "description": "Get the latest version of a CSS or JavaScript library hosted on CDNJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+20
-1
@@ -18,3 +18,3 @@ # latest-lib [](https://travis-ci.org/GabrielMangiurea/latest-lib) | ||
| // search for bootstrap | ||
| // search for the latest version of bootstrap | ||
| latestLib('bootstrap') | ||
@@ -24,2 +24,8 @@ .then(library => console.log(library)) | ||
| .catch(err => console.log(err)); | ||
| // search for the latest version of bootstrap 3 | ||
| latestLib('bootstrap@3') | ||
| .then(library => console.log(library)) | ||
| //=> {name: 'twitter-bootstrap', version: '3.3.7', files: [...]} | ||
| .catch(err => console.log(err)); | ||
| ``` | ||
@@ -32,2 +38,13 @@ | ||
| Returns a `Promise` containing an object with the following properties: | ||
| - On success: | ||
| - **name** `String` | ||
| - **version** `String` | ||
| - **files** `Array` | ||
| - On error: | ||
| - an `Error` object | ||
| #### name | ||
@@ -39,2 +56,4 @@ | ||
| Suffixing the name with `@[number]` will fetch the latest version of `[number]` major release | ||
| #### opts | ||
@@ -41,0 +60,0 @@ |
+7
-0
@@ -21,2 +21,9 @@ 'use strict'; | ||
| test('success: should return the correct major version when specified', async t => { | ||
| const f = await m('bootstrap@3'); | ||
| t.is(f.version, '3.3.7'); | ||
| // Resource URLs should also include the correct version number | ||
| t.true(f.files[0].indexOf('3.3.7') > -1); | ||
| }); | ||
| test('failure: should return an error object', async t => { | ||
@@ -23,0 +30,0 @@ await m() |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
5844
17.14%76
15.15%74
34.55%