UNPKG

2.05 kBJavaScriptView Raw
1'use strict'
2function loadPaths (dir,opts, prefix="") {
3 const path = require('path')
4 const fs = require('fs')
5 const glob = require('glob')
6
7 const followSymlinks = dir.followSymlinks != null ? dir.followSymlinks : true
8
9 let file = path.resolve(dir.path)
10 const stats = fs.statSync(file)
11 if(!prefix.endsWith('/')) {
12 prefix += '/'
13 }
14
15 if (stats.isDirectory() && !opts.recursive) {
16 throw new Error('Can only add directories using --recursive')
17 }
18
19 if (stats.isDirectory()) {
20 // glob requires a POSIX filename
21 file = file.split(path.sep).join('/')
22 const fullDir = file + (file.endsWith('/') ? '' : '/')
23 let dirName = fullDir.split('/')
24
25 dirName = prefix + dirName[dirName.length - 2] + '/'
26 const mg = new glob.sync.GlobSync('**/*', {
27 cwd: file,
28 follow: followSymlinks,
29 dot: dir.hidden,
30 ignore: dir.ignore
31 })
32
33 return mg.found
34 .map((name) => {
35 const fqn = fullDir + name
36 // symlinks
37 if (mg.symlinks[fqn] === true) {
38 return {
39 path: dirName + name,
40 //symlink: true,
41 //dir: false,
42 content: fs.readlinkSync(fqn)
43 }
44 }
45
46 // files
47 if (mg.cache[fqn] === 'FILE') {
48 return {
49 path: dirName + name,
50 //symlink: false,
51 //dir: false,
52 content: fs.createReadStream(fqn)
53 }
54 }
55
56 // directories
57 if (mg.cache[fqn] === 'DIR' || mg.cache[fqn] instanceof Array) {
58 return {
59 path: dirName + name,
60 //symlink: false,
61 //dir: true
62 }
63 }
64 // files inside symlinks and others
65 })
66 // filter out null files
67 .filter(Boolean)
68 }
69
70 return {
71 path: prefix + path.basename(file),
72 content: fs.createReadStream(file)
73 }
74 }
75
76 exports = module.exports = loadPaths
\No newline at end of file