Created
October 11, 2017 11:53
-
-
Save watilde/6a67c6bcb6152fa98765a11740968bf5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const https = require('https') | |
| const pkgJSON = require('./package.json') | |
| const deps = Object.assign({}, pkgJSON.dependencies, pkgJSON.devDependencies) | |
| const list = Object.keys(deps).map((key) => { | |
| return new Promise((resolve, reject) => { | |
| const pkg = encodeURIComponent(key) | |
| https.get(`https://registry.npmjs.com/${pkg}/latest`, (res) => { | |
| const { statusCode } = res | |
| const contentType = res.headers['content-type'] | |
| let error | |
| if (statusCode !== 200) { | |
| error = new Error('Request Failed.\n' + | |
| `Status Code: ${statusCode}`) | |
| } else if (!/^application\/json/.test(contentType)) { | |
| error = new Error('Invalid content-type.\n' + | |
| `Expected application/json but received ${contentType}`) | |
| } | |
| if (error) { | |
| res.resume() | |
| reject(error) | |
| } | |
| res.setEncoding('utf8') | |
| let rawData = '' | |
| res.on('data', (chunk) => { rawData += chunk }) | |
| res.on('end', () => { | |
| try { | |
| const parsedData = JSON.parse(rawData) | |
| resolve([key, deps[key], parsedData.version]) | |
| } catch (e) { | |
| reject(e) | |
| } | |
| }) | |
| }).on('error', reject) | |
| }) | |
| }) | |
| Promise.all(list).then((result) => { | |
| // result | |
| // => [ | |
| // 'package name', 'current version', 'latest version' | |
| // ] | |
| }).catch((e) => { | |
| throw new Error(e) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment