-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (70 loc) · 1.91 KB
/
index.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
'use strict'
var fs = require('fs')
var path = require('path')
var gutil = require('gulp-util')
var through = require('through2')
var assign = require('object-assign')
var mk = require('mkdirp').sync
var nar = require('nar')
var NAME = 'gulp-nar'
exports = module.exports = narTask('create')
exports.createExecutable = narTask('createExec')
exports.extract = narTask('extract')
exports.create = exports
function narTask(action) {
return function task(output, options) {
var fileStream, archivePath
return through.obj(onWrite, onEnd)
function onWrite(file, enc, done) {
if (file.isNull()) { return done() }
if (!fileStream) { fileStream = file }
try {
doTask()
} catch (err) {
throw new gutil.PluginError(NAME, err);
}
function doTask() {
if (!fs.existsSync(output)) {
mk(output)
}
nar[action](assign({ dest: output, path: file.path }, (options || {})))
.on('error', function (err) {
throw err
})
.on('end', function (dest) {
archivePath = dest
done()
})
}
}
function onEnd(done) {
var outputPath
if (!fileStream) {
return done()
}
if (archivePath) {
if (typeof archivePath === 'object') {
outputPath = archivePath.path
} else {
outputPath = path.join(fileStream.base, archivePath)
}
} else {
outputPath = fileStream.base
}
this.push(new gutil.File({
cwd: fileStream.cwd,
base: fileStream.base,
path: outputPath,
contents: fs.createReadStream(outputPath)
}))
done()
}
}
}
//function narError() {
// throw new gutil.PluginError(NAME, [
// '', 'Fatal error:', 'nar is not installed as global package',
// '', 'You must install it. Run:',
// 'npm install -g nar', ''
// ].join('\n'))
//}