forked from Strider-CD/strider-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
160 lines (151 loc) · 5.81 KB
/
worker.js
File metadata and controls
160 lines (151 loc) · 5.81 KB
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
var path = require('path')
, fs = require('fs-extra')
, spawn = require('child_process').spawn
, async = require('async')
, md5 = require('MD5')
, npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
function cpDir(from, to, done) {
fs.remove(to, function () {
fs.mkdirp(path.dirname(to), function () {
fs.copy(from, to, function (err) {
return done(err && new Error('Failed to copy directory: ' + err))
})
})
})
}
function packageHash(dir, done) {
fs.readFile(path.join(dir, 'package.json'), 'utf8', function (err, packagejson) {
if (err) return done(err)
var hash = md5(packagejson)
done(null, hash)
})
}
function installPackages(context, cachier, datadir, policy, update, done) {
function install() {
context.cmd({command: npm, args: ['install', '--color=always'], screen: 'npm install'}, done)
}
if (policy !== 'strict' && policy !== 'loose') return install()
// hash the package.json
packageHash(datadir, function (err, hash) {
if (err) return done()
var dest = path.join(datadir, 'node_modules')
// try for an exact match
cachier.get(hash, dest, function (err) {
if (!err) {
context.comment('restored node_modules from cache')
if (!update) return done(null, true)
return context.cmd({command: npm, args: ['update', '--color=always'], screen: 'npm update'}, done)
}
if (policy === 'strict') return install()
// otherwise restore from the latest branch, prune and update
cachier.get(context.branch, dest, function (err) {
if (err) return install()
context.comment('restored node_modules from cache')
context.cmd({command: npm, args: ['prune', '--color=always'], screen: 'npm prune'}, function (err) {
if (err) return done(err)
context.cmd({command: npm, args: ['update', '--color=always'], screen: 'npm update'}, done)
})
})
})
})
}
function updateCache(context, cachier, datadir, done) {
packageHash(datadir, function (err, hash) {
if (err) return done()
var dest = path.join(datadir, 'node_modules')
context.comment('saved node_modules to cache')
async.series([
cachier.update.bind(null, hash, dest),
cachier.update.bind(null, context.branch, dest),
], done)
})
}
function updateGlobalCache(globals, context, cachier, datadir, done) {
var dest = path.join(datadir, 'node_modules')
context.comment('saved global modules to cache')
cachier.update(md5(globals.join(' ')), dest, done)
}
function installGlobals(globals, context, cachier, globalDir, policy, done) {
function install() {
fs.mkdirp(path.join(globalDir, 'node_modules'), function () {
context.cmd({
cmd: {command: npm, args: ['install', '--color=always'].concat(globals), screen: 'npm install -g'},
cwd: globalDir
}, function (err) {
return done(err)
})
})
}
if (policy !== 'strict' && policy !== 'loose') return install()
var dest = path.join(globalDir, 'node_modules')
cachier.get(md5(globals.join(' ')), dest, function (err) {
if (err) return install()
context.comment('restored global modules from cache')
return done(null, true)
})
}
module.exports = {
// Initialize the plugin for a job
// config: taken from DB config extended by flat file config
// job & repo: see strider-runner-core
// cb(err, initialized plugin)
init: function (config, job, context, cb) {
config = config || {}
var ret = {
env: {
MOCHA_COLORS: 1
},
path: [path.join(__dirname, 'node_modules/.bin'), path.join(context.dataDir, '.globals/node_modules/.bin')],
prepare: function (context, done) {
var npmInstall = fs.existsSync(path.join(context.dataDir, 'package.json'))
, global = config.globals && config.globals.length
if (config.test && config.test !== '<none>') context.data({doTest: true}, 'extend')
if (!npmInstall && !global) return done(null, false)
var tasks = []
var nocache = config.caching !== 'strict' && config.caching !== 'loose'
if (npmInstall) {
tasks.push(function (next) {
installPackages(context, context.cachier('modules'), context.dataDir, config.caching, config.update_cache, function (err, exact) {
if (err || exact === true || nocache) return next(err)
updateCache(context, context.cachier('modules'), context.dataDir, next)
})
})
}
if (global) {
tasks.push(function (next) {
var globalDir = path.join(context.dataDir, '.globals')
installGlobals(config.globals, context, context.cachier('globals'), globalDir, config.caching, function (err, cached) {
if (err || nocache || cached) return next(err)
updateGlobalCache(config.globals, context, context.cachier('globals'), globalDir, next)
})
})
}
async.series(tasks, done)
},
// cleanup: 'rm -rf node_modules'
}
if (config.test && config.test !== '<none>') {
ret.test = typeof(config.test) !== 'string' ? 'npm test' : config.test
if (ret.test === 'npm test')
ret.test = {command: npm, args: ['test', '--color=always'], screen: 'npm test'}
}
if (config.runtime && config.runtime !== 'whatever') {
ret.env.N_PREFIX = path.join(context.baseDir, '.n')
// string or list - to be prefixed to the PATH
ret.path = ret.path.concat([path.join(__dirname, 'node_modules/n/bin'), ret.env.N_PREFIX + '/bin'])
ret.environment = {
cmd: 'n ' + config.runtime,
silent: true
}
}
cb(null, ret)
},
// if provided, autodetect is run if the project has *no* plugin
// configuration at all.
autodetect: {
filename: 'package.json',
exists: true,
language: 'node.js',
framework: null
}
}