-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers-io.js
89 lines (83 loc) · 3.43 KB
/
helpers-io.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
/*!
* customize <https://github.com/bootprint/customize>
*
* Copyright (c) 2017 Nils Knappmeier.
* Released under the MIT license.
*/
'use strict'
var lazy = require('./lib/lazy')
var path = require('path')
var leaf = require('./lib/leaf')
var fs = require('fs')
var util = require('./lib/util')
var glob = require('glob')
module.exports = {
files: files,
readFiles: readFiles
}
/**
* An overridable directory which resolves to the contents of all its files (recursively).
* Returns an undefined value if the directory path is undefined.
* @param {string|null|undefined} directoryPath the path to the directory
* @param {object=} options
* @param {string=} options.glob an optional glob pattern for filtering files
* @param {boolean=} options.stream if set to true, the contents of a file will be a readable stream
* instead of the actual data.
* @param {string=} options.encoding the file is expected to be encoded. This means that the
* instead of a Buffer, a string is returned. If the 'stream' option is set, the stream's encoding
* will be set via [readable.setEncoding(encoding)](https://nodejs.org/api/stream.html#stream_readable_setencoding_encoding)
* @return {Promise<object<string,Promise<{path:string,contents:string}>>>} an object containing
* the relative file-path from the directoryPath as key and the file-path and the file-contents as value
*/
function readFiles (directoryPath, options) {
if (directoryPath == null) {
return undefined
}
var _options = options || {}
// Collect all files
var result = util.asPromise((cb) => glob(_options.glob || '**', { cwd: directoryPath, mark: true }, cb))
.then(function (relativePaths) {
var set = relativePaths
// Ignore directories
.filter((relativePath) => !relativePath.match(/\/$/))
// Convert to a set based on relative paths
// (i.e. {'dir/file.txt': 'dir/file.txt'}
.reduce((set, relativePath) => {
set[relativePath] = relativePath
return set
}, {})
// Create lazy promises (only resolve when .then() is called) acting
// as leafs (do not dive inside when merging)
return util.mapValues(set, (relativePath) => {
var fullPath = path.resolve(directoryPath, relativePath)
return leaf(lazy(() => {
return {
path: path.relative(process.cwd(), fullPath),
contents: _options.stream
? fs.createReadStream(fullPath, { encoding: _options.encoding })
: util.asPromise((cb) => fs.readFile(fullPath, { encoding: _options.encoding }, cb))
}
}))
})
})
result.watch = directoryPath
return result
}
/**
* An overridable directory which resolves to the contents of all its files (recursively).
* Returns an undefined value if the directory path is undefined.
* The contents of each file is a UTF-8 encoded string.
* @param {string|null|undefined} directoryPath the path to the directory
* @param {object=} options
* @param {string=} options.glob an optional glob pattern for filtering files
* @return {Promise<object<string,Promise<{path:string,contents:string}>>>} an object containing
* the relative file-path from the directoryPath as key and the file-path and the file-contents as value
* @deprecated Use {@link #readFiles} instead
*/
function files (directoryPath, options) {
return readFiles(directoryPath, {
glob: options && options.glob,
stream: false,
encoding: 'utf-8'
})
}