-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboilerplate.js
172 lines (128 loc) · 4.01 KB
/
boilerplate.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
167
168
169
170
171
172
const fs = require('fs')
, path = require('path')
, after = require('after')
, cpr = require('cpr')
function adjustForLang (orig, lang, callback) {
if (!lang) {
process.nextTick(function () {
callback(null, orig)
})
return
}
var extName = path.extname(orig)
, langAware = orig.slice(0, -extName.length) + '.' + lang + extName
fs.open(langAware, 'r', function (err, fd) {
if (!err && fd)
fs.close(fd)
callback(null, err ? orig : langAware)
})
}
// find a destination for this file, if it already exists then add a number to
// the end of the filename (before extension) and keep incrementing that number
// until we find a free filename
function findDestination (file, lang, callback) {
if (lang)
file = file.replace('.' + lang, '')
file = path.basename(file)
var f = path.join(process.cwd(), file)
fs.exists(f, function (exists) {
if (!exists)
return callback(null, f)
var ext = path.extname(file)
, pfx = file.substring(0, file.length - ext.length)
;(function next (i) {
f = path.join(process.cwd(), pfx + i + ext)
fs.exists(f, function (exists) {
if (!exists)
return callback(null, f)
next(i + 1)
})
}(1))
})
}
// copy the boilerplate files to CWD with names that aren't going to
// overwrite existing files
function prepare (callback) {
if (!this._boilerplate.length)
return process.nextTick(callback)
var done = after(this._boilerplate.length, callback)
, out = this.boilerplateOut = {}
, self = this
this._boilerplate.forEach(function (src, index) {
adjustForLang(src, self.lang, function(err, src) {
// This will never happen, actually, but I wouldn't
// want you to fear it might :-)
if (err)
return callback(err)
self._boilerplate[index] = src
findDestination(src, self.lang, function (err, dst) {
if (err)
return done(err)
out[src] = out[path.basename(src)] = path.basename(dst)
copy(src, dst, done)
})
})
})
}
function copy (src, dst, callback) {
function copyFile () {
fs.createReadStream(src)
.on('error', function (err) {
callback && callback(err)
})
.pipe(fs.createWriteStream(dst))
.on('error', function (err) {
callback && callback(err)
})
.on('close', function () {
callback && callback()
})
}
fs.stat(src, function (err, stat) {
if (err)
return callback(err)
if (stat.isFile())
return copyFile()
if (stat.isDirectory())
return cpr(src, dst, { overwrite: true }, callback)
return callback(new Error('Boilerplate source must be a regular file or directory'))
})
}
function fix (exercise) {
exercise._boilerplate = []
exercise.addPrepare(prepare)
exercise.addBoilerplate = function (file) {
if (Array.isArray(file)) {
return file.forEach(function (f) {
exercise.addBoilerplate(f)
})
}
if (typeof file != 'string')
throw new TypeError('addBoilerplate must be provided with a path to a file or an array of paths')
exercise._boilerplate.push(file)
}
// augment getExerciseText() such that the string {boilerplate:filename} will be replaced
// in the problem.md with the name of the copy of that file written to CWD
var getExerciseText = exercise.getExerciseText
exercise.getExerciseText = function (callback) {
var boilerplateOut = this.boilerplateOut
getExerciseText.call(this, function (err, type, contents) {
if (err)
return callback(err)
// proper path resolution
contents = contents.replace(
/\{boilerplate:([^}]+)\}/gi
, function (match, boilerplateFile) {
return boilerplateOut[boilerplateFile] || '(ERROR: Unknown boilerplate file)'
}
)
callback(null, type, contents)
})
}
}
function boilerplate (exercise) {
if (typeof exercise.addBoilerplate != 'function')
fix(exercise)
return exercise
}
module.exports = boilerplate