-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
executable file
·166 lines (143 loc) · 3.9 KB
/
bin.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#! /usr/bin/env node
'use strict'
// Node core
const fs = require('fs')
const path = require('path')
const child = require('child_process')
// npm
const minimist = require('minimist')
const which = require('which').sync
const q = require('workq')()
const chalk = require('chalk')
// utils
const Logger = require('./log')
// Plugins to test
const plugins = Object.keys(require('./package.json').dependencies)
const skipPlugins = [
'@fastify/mongodb',
'@fastify/redis',
'@fastify/postgres',
'@fastify/leveldb',
'@fastify/elasticsearch'
]
const args = minimist(process.argv.slice(2), {
boolean: ['npm-logs', 'verbose', 'log-errors', 'help'],
string: ['fastify'],
alias: {
'npm-logs': 'N',
verbose: 'V',
'log-errors': 'L',
fastify: 'F',
help: 'H'
},
default: {
'npm-logs': false,
verbose: false,
'log-errors': false,
fastify: 'fastify/fastify#next',
help: false
}
})
if (args.help) {
try {
console.log(chalk.cyan(fs.readFileSync(path.resolve(__dirname, 'help.txt'), 'utf8')))
} catch (err) {
console.log(chalk.red(err))
}
process.exit()
}
plugins.forEach(plugin => {
if (skipPlugins.indexOf(plugin) > -1) return
q.add(worker, plugin)
})
q.drain(done => {
console.log(chalk.green('Finish'))
done()
})
function worker (q, plugin, done) {
const log = Logger()
log.text(`Testing plugin ${plugin}`)
// get the plugin path
const pluginPath = path.join(__dirname, 'node_modules', plugin)
// use main branch of fastify
const success = updatePluginFastify.call(this, pluginPath)
if (!success) {
log.warn(`Cannot update plugin ${plugin}`)
done()
return
}
// get node and npm binaries
const nodeBin = which('node', { path: process.env.PATH })
const npmBin = which('npm', { path: process.env.PATH })
// install plugin dependencies
log.text(`Installing dependencies ${plugin}`)
installDeps(nodeBin, npmBin, pluginPath, err => {
if (err) {
log.warn(`${plugin} install deps failed!`)
return done()
}
// run plugin tests
log.text(`Running test ${plugin}`)
runTest(nodeBin, npmBin, pluginPath, err => {
if (err) {
log.fail(`${plugin} test not ok!`)
} else {
log.succeed(`${plugin} test ok!`)
}
done()
})
})
}
function installDeps (nodeBin, npmBin, pluginPath, cb) {
const install = child.spawn(nodeBin, [npmBin, 'install'], { cwd: pluginPath })
let log = ''
install.stdout.on('data', data => {
log += data
})
install.stderr.on('data', (data) => {
if (args['npm-logs'] && args.verbose) {
console.log(chalk.red(data.toString()))
}
})
install.on('close', code => {
if (args['npm-logs'] && args.verbose) {
console.log(chalk.magenta(log.toString()))
}
cb(code > 0 ? new Error('Install failed') : null)
})
}
function runTest (nodeBin, npmBin, pluginPath, cb) {
const test = child.spawn(nodeBin, [npmBin, 'test'], { cwd: pluginPath })
let log = ''
test.stdout.on('data', data => {
log += data
})
test.stderr.on('data', (data) => {
if (args['npm-logs'] && args.verbose) {
console.log(chalk.red(data.toString()))
}
})
test.on('close', code => {
if (args.verbose || (args['log-errors'] && code > 0)) {
console.log(chalk.yellow(log.toString()))
}
cb(code > 0 ? new Error('Test failed') : null)
})
}
function updatePluginFastify (pluginPath) {
const packageJsonPath = path.join(pluginPath, 'package.json')
const packageJson = require(packageJsonPath)
try {
packageJson.devDependencies.fastify = args.fastify
} catch (err) {
console.log(chalk.red(`Cannot open or update package.json for plugin ${this.plugin}`), err)
return false
}
try {
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8')
} catch (err) {
console.log(chalk.red(`Cannot overwrite package.json for plugin ${this.plugin}`), err)
return false
}
return true
}