This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathprepare-file.js
106 lines (89 loc) · 2.36 KB
/
prepare-file.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
'use strict'
const isNode = require('detect-node')
const flatmap = require('flatmap')
function loadPaths (opts, file) {
const path = require('path')
const fs = require('fs')
const glob = require('glob')
const followSymlinks = opts.followSymlinks != null ? opts.followSymlinks : true
file = path.resolve(file)
const stats = fs.statSync(file)
if (stats.isDirectory() && !opts.recursive) {
throw new Error('Can only add directories using --recursive')
}
if (stats.isDirectory() && opts.recursive) {
// glob requires a POSIX filename
file = file.split(path.sep).join('/')
const fullDir = file + (file.endsWith('/') ? '' : '/')
let dirName = fullDir.split('/')
dirName = dirName[dirName.length - 2] + '/'
const mg = new glob.sync.GlobSync('**/*', {
cwd: file,
follow: followSymlinks,
dot: opts.hidden,
ignore: opts.ignore
})
return mg.found
.map((name) => {
const fqn = fullDir + name
// symlinks
if (mg.symlinks[fqn] === true) {
return {
path: dirName + name,
symlink: true,
dir: false,
content: fs.readlinkSync(fqn)
}
}
// files
if (mg.cache[fqn] === 'FILE') {
return {
path: dirName + name,
symlink: false,
dir: false,
content: fs.createReadStream(fqn)
}
}
// directories
if (mg.cache[fqn] === 'DIR' || mg.cache[fqn] instanceof Array) {
return {
path: dirName + name,
symlink: false,
dir: true
}
}
// files inside symlinks and others
})
// filter out null files
.filter(Boolean)
}
return {
path: path.basename(file),
content: fs.createReadStream(file)
}
}
function prepareFile (file, opts) {
let files = [].concat(file)
return flatmap(files, (file) => {
if (typeof file === 'string') {
if (!isNode) {
throw new Error('Can only add file paths in node')
}
return loadPaths(opts, file)
}
if (file.path && !file.content) {
file.dir = true
return file
}
if (file.content || file.dir) {
return file
}
return {
path: '',
symlink: false,
dir: false,
content: file
}
})
}
exports = module.exports = prepareFile