-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
91 lines (77 loc) · 2.86 KB
/
utils.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
'use strict'
var fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
q = require('q'),
SRC_DIR = path.join( __dirname, 'src' ),
EXERCISES_DIR = path.join( SRC_DIR, 'exercises' ),
CONF_FILE = 'conf.js'
module.exports = {
getExercises: function() {
return q.nfcall( fs.readdir, EXERCISES_DIR )
.then( function( exercises ) {
var configs = {},
readFileDeferreds = exercises.map( function() {
return q.defer()
}),
readFilePromises = readFileDeferreds.map( function( deferred ) {
return deferred.promise
})
exercises.map( function( exercise, i ) {
var exerciseConfFile = path.join( EXERCISES_DIR, exercise, CONF_FILE )
q.nfcall( fs.readFile, exerciseConfFile )
.done( function( data ) {
configs[ exercise ] = { path: exerciseConfFile, data: data.toString() }
readFileDeferreds[ i ].resolve()
})
})
return q.all( readFilePromises )
.then( function() {
return configs
})
})
},
/**
* Executes a git command in a specified repo
* @param {String} repo the path to the repository in which to execute the command
* @param {String} cmd the git command to run
* @param {String|Array} args the arguments to pass to the command
* @return {Promise} a promise on the completion of the command
*/
git: function( repo, cmd, args ) {
var done = q.defer()
fs.stat( repo, function( err ) {
if ( err ) { return done.reject( new Error( err ) ) }
var cmdArgs = ( args instanceof Array ? args : args.trim().split(' ') ),
git = spawn( 'git', [ cmd ].concat( cmdArgs ), { cwd: repo } ),
output = '',
errOutput = ''
git.stderr.on( 'data', function( data ) { errOutput += data.toString() })
git.stdout.on( 'data', function( data ) { output += data.toString() })
git.on( 'close', function( code ) {
if ( code !== 0 ) {
done.reject( new Error( errOutput ) )
} else {
done.resolve( output )
}
})
})
return done.promise
},
cp: function( from, to ) {
var done = q.defer(),
cp = spawn( 'cp', [ '-rf', from, to ] ),
cpErr = ''
cp.stderr.on( 'data', function( data ) {
cpErr += data.toString()
})
cp.on( 'close', function( cpRet ) {
if ( cpRet !== 0 ) {
done.reject( new Error( cpErr ) )
} else {
done.resolve()
}
})
return done.promise
}
}