| 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 |
27×
27×
1×
1×
1×
4×
1×
4×
4×
3×
3×
2×
3×
1×
3×
3×
2×
1×
2×
2×
1×
2×
1×
1×
1×
1×
2×
1×
2×
1×
2×
1×
1×
1×
1×
2×
2×
2×
2×
1×
1×
1×
1×
2×
5×
3×
2×
5×
1×
1×
1×
1×
1×
2×
1×
10×
20×
20×
2×
10×
162×
10×
1×
8×
8×
4×
4×
4×
4×
1×
2×
2×
1×
2×
1×
1×
5×
4×
4×
5×
5×
3×
3×
3×
3×
4×
1×
3×
3×
2×
2×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
27×
| 'use strict'
var fs = require('fs')
, vfs = require('vinyl-fs')
, JSZip = require('jszip')
, concat = require('concat-stream')
, toposort = require('toposort')
, async = require('async')
, _ = require('lodash')
, path = require('path')
, slash = require('slash')
, git = require('gulp-git')
, util = require('./util.js')
var include = [
'*',
'docs/**/*',
'src/**/*',
'lib/**/*',
'rest-api/ext/**/*',
'rest-api/transform/**/*',
'rest-api/service/**/*'
]
, ignore = [
'mlpm.json',
'mlpm_modules',
'.*.swp',
'._*',
'.DS_Store',
'.git',
'.hg',
'.lock-wscript',
'.svn',
'.wafpickle-*',
'CVS',
'npm-debug.log',
'test',
'src/test',
'xray',
'src/xray'
]
, symbols = {
pkg: '├── ',
pipe: '│ ',
last: '└── '
}
function getConfig(cb) {
util.readJson('mlpm.json', cb)
}
function saveConfig(data, cb) {
util.writeJson('mlpm.json', data, cb)
}
function getRepository(cb) {
git.exec({ args: 'config --get remote.origin.url', quiet: true }, function(err, stdout) {
if (err) return cb(err)
var origin = stdout.toString().trim()
if ( origin && /^git@github.com:/.test( origin ) ) {
origin = origin.replace(/^git@github.com:/, 'https://github.com/')
}
cb(null, origin)
})
}
function getHeadCommit(cb) {
git.revParse({ args: 'HEAD', quiet: true }, function(err, stdout) {
if (err) return cb(err)
cb( null, stdout.toString().trim() )
})
}
function getRepoStatus(cb) {
git.status({ args: '--porcelain', quiet: true }, function (err, stdout) {
if (err) return cb(err)
var changes = _.chain( stdout.trim().split('\n') )
.filter(function (line) {
return line.trim() && !line.match(/^\?\? /)
}).map(function (line) {
return line.trim()
})
.value()
cb(null, changes)
})
}
function commitConfig(msg, cb) {
vfs.src('mlpm.json')
.pipe(git.add({ quiet: true }))
.pipe(git.commit( msg, { quiet: true } ))
.on('error', function(err) {
cb(err)
})
.on('end', function() {
cb(null)
})
}
function tagRepo(tag, cb) {
if ( !/^v/.test(tag) ) {
tag = 'v' + tag
}
git.tag(tag, tag, { quiet: true }, function (err) {
cb(err)
})
}
function getDefaultConfig(pkgConfig, cb) {
if (pkgConfig) {
// cthulu
cb(null, _.pick(pkgConfig, 'name', 'version', 'description', 'repository'))
} else {
getRepository(function(err, repository) {
// ignore err
cb(null, {
name: path.basename( process.cwd() ),
version: '1.0.0',
repository: repository
})
})
}
}
function saveDependency(pkgConfig, name, version, cb) {
pkgConfig.dependencies = pkgConfig.dependencies || {}
// save semver any patch version
version = /^\d+\.\d+\.\d+$/.test( version ) ?
version.replace(/\d+$/, '*') :
version
pkgConfig.dependencies[ name ] = version
saveConfig(pkgConfig, cb)
}
function deleteDependency(pkgConfig, name, cb) {
// TODO: check if exists?
delete pkgConfig.dependencies[name]
saveConfig(pkgConfig, cb)
}
/* inverse topological sort */
function sortPackages(packages) {
var sortedNames = _.chain(packages)
.filter(function(pkg) { return pkg.dependencies })
.map(function(pkg) {
return _.map(pkg.dependencies, function(_, dep) { return [dep, pkg.name] })
})
.flatten(false)
.thru(toposort)
.value()
return _.sortBy(packages, function(pkg) {
return sortedNames.indexOf(pkg.name)
})
}
function getPackages(cb) {
var src = vfs.src('./mlpm_modules/*/mlpm.json')
src.on('error', cb)
src.pipe(concat(function(files) {
cb(null,
sortPackages(
_.map(files, function(file) {
// TODO try/catch?
return JSON.parse(file.contents)
})
)
)
}))
}
//TODO: replace with byline and concat
function getIgnoreGlobs(cb) {
async.map(['.mlpmignore', '.gitignore'], function(file, cb) {
fs.readFile(file, function(err, data) {
if (err) return cb(null, [])
cb( null, data.toString().split(/\r?\n/) )
})
}, function(err, globs) {
var ignoreGlobs = _.chain([ignore, globs])
.flatten(true)
.compact()
.uniq()
.map(function(line) { return '!' + line; })
.value()
cb( null, ignoreGlobs )
})
}
function getGlobs(pkgConfig, cb) {
getIgnoreGlobs(function(__, ignoreGlobs) {
if ( !(pkgConfig.files && pkgConfig.files.length) ) {
return cb( null, include.concat(ignoreGlobs) )
}
// TODO: sync with defaultExcludes in package.getFiles()
var globs = [
'README',
'README.{md,mdown}',
'LICENSE',
'license.txt'
]
async.map(pkgConfig.files, fs.stat, function(err, stats) {
if (err) return cb(err)
_.each(stats, function(file, index) {
var path = pkgConfig.files[ index ].replace(/\/$/, '')
if ( file.isDirectory() ) {
path += '/**/*'
}
globs.push(path)
})
cb( null, globs.concat(ignoreGlobs) )
})
})
}
function getFiles(pkgConfig, readContents, cb) {
if ( !cb ) {
cb = readContents
readContents = true
}
getGlobs(pkgConfig, function(err, globs) {
if (err) return cb(err)
var src = vfs.src(globs, { read: readContents })
src.on('error', cb)
src.pipe(concat(function(files) {
cb(null, _.filter(files, function(file) {
return file.stat.isFile()
}))
}))
})
}
function createZip(pkgConfig, cb) {
getFiles(pkgConfig, function(err, files) {
if (err) return cb(err)
getHeadCommit(function(err, gitHead) {
var zip = new JSZip()
function normalizePath(file) {
var relative = file.path.replace( process.cwd(), '' )
return slash( relative ).replace(/^\//, '')
}
if ( !err ) pkgConfig.gitHead = gitHead
zip.file( 'mlpm.json', util.formatJson(pkgConfig) )
_.each(files, function(file) {
var filePath = normalizePath(file)
// in case it was explicitly included in files array, or otherwise snuck through ...
Iif ( filePath === 'mlpm.json' ) return
zip.file( filePath, file.contents )
})
Iif ( _.keys( zip.files ).length === 0 ) {
return cb(new Error('aborting, empty zip'))
}
cb( null, zip )
})
})
}
module.exports = {
getConfig: getConfig,
saveConfig: saveConfig,
getRepository: getRepository,
getHeadCommit: getHeadCommit,
getRepoStatus: getRepoStatus,
commitConfig: commitConfig,
tagRepo: tagRepo,
getDefaultConfig: getDefaultConfig,
saveDependency: saveDependency,
deleteDependency: deleteDependency,
sortPackages: sortPackages,
getPackages: getPackages,
getIgnoreGlobs: getIgnoreGlobs,
getGlobs: getGlobs,
getFiles: getFiles,
createZip: createZip,
symbols: symbols
}
|