-
Notifications
You must be signed in to change notification settings - Fork 8
/
upgrade-ready.js
executable file
·111 lines (96 loc) · 2.44 KB
/
upgrade-ready.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
/*!
* upgrade-ready
* Copyright(c) 2015 NodeSource, Inc.
* MIT Licensed
*/
/**
* Module dependencies
*/
var path = require('path')
var utils = require('util')
var fs = require('fs')
var rc = require('rc')
var request = require('client-request')
var extend = utils._extend
var gatherDependencies = require('gather-dependencies')
var urlJoin = require('url-join')
/**
* Configuration
*/
var config = rc('upgrade-ready', {
endpoint: 'http://upgrade-ready.nodesource.com/api/',
verbose: false,
targetVersion: '6.10.3',
path: path.resolve('.')
})
// Use the first parameter as targetVersion
if (config._[0]) {
config.targetVersion = config._[0]
}
/*
* Exports
*/
if (module.parent) {
module.exports = run
} else {
run()
}
/**
* Upgrade ready
* @param {object} options Options
* @param {function} callback callback
*/
function upgradeReady (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = extend(config, options)
gatherDependencies(options.path, function cb (err, data) {
if (err) {
console.error(err.message)
process.exit(1)
}
if (options.v || options.verbose) {
console.log('Gathered dependencies:')
console.log(utils.inspect(data, { showHidden: true, colors: true }))
}
var postOpts = {
uri: urlJoin(options.endpoint, options.targetVersion),
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/json'
}
}
request(postOpts, function callback (err, response, body) {
if (err) return console.error(err.message)
var msgParsed
try {
msgParsed = JSON.parse(body.toString()).message
} catch (e) {
msgParsed = body.toString()
}
console.log(msgParsed)
})
})
}
function run () {
if (config.h || config.help) {
var helpFile = path.join(__dirname, 'help.txt')
var helpText = fs.readFileSync(helpFile)
console.log(String(helpText))
process.exit(0)
}
fs.stat(path.resolve(config.path, 'package.json'), function (err, stats) {
if (err || !stats.isFile()) {
console.error('ERROR: package.json file not found.')
console.error('Provide a valid path to look using --path')
console.error('A package.json file is needed to check dependencies list')
console.error('Use --help to get more information')
process.exit(1)
}
upgradeReady()
})
}