|
1 | | -const fs = require('fs') |
2 | | -const path = require('path') |
3 | | -const async = require('async') |
4 | | -const glob = require('glob') |
5 | | -const minimatch = require('minimatch') |
6 | | - |
7 | | -function gathererFromSourceDirectory(source, pattern, { keepConcatenated }) { |
8 | | - return done => { |
9 | | - // We loop over all the files Metalsmith knows of and return an array of all |
10 | | - // the files contents matching the given pattern |
11 | | - return done( |
12 | | - null, |
13 | | - Object.keys(source).reduce((acc, filepath) => { |
14 | | - if (minimatch(filepath, pattern)) { |
15 | | - acc.push(source[filepath].contents) |
16 | | - if (!keepConcatenated) { |
17 | | - delete source[filepath] |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - return acc |
22 | | - }, []) |
23 | | - ) |
24 | | - } |
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const async = require('async'); |
| 4 | +const glob = require('glob'); |
| 5 | +const minimatch = require('minimatch'); |
| 6 | + |
| 7 | +function gathererFromSourceDirectory(source, pattern, {keepConcatenated}) { |
| 8 | + return done => { |
| 9 | + // We loop over all the files Metalsmith knows of and return an array of all |
| 10 | + // the files contents matching the given pattern |
| 11 | + return done( |
| 12 | + null, |
| 13 | + Object.keys(source).reduce((acc, filepath) => { |
| 14 | + if (minimatch(filepath, pattern)) { |
| 15 | + acc.push(source[filepath].contents); |
| 16 | + if (!keepConcatenated) { |
| 17 | + delete source[filepath]; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return acc; |
| 22 | + }, []) |
| 23 | + ); |
| 24 | + }; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | function gathererFromSearchPaths(rootPath, searchPaths, pattern) { |
28 | | - return done => { |
29 | | - // We loop over the search paths and return an array of all the files |
30 | | - // contents matching the given pattern |
31 | | - async.map( |
32 | | - searchPaths, |
33 | | - (searchPath, callback) => { |
34 | | - const globPattern = path.resolve(rootPath, searchPath, pattern) |
35 | | - glob.glob( |
36 | | - globPattern, |
37 | | - { |
38 | | - ignore: path.resolve(rootPath, 'src/**/*'), |
39 | | - minimatch, |
40 | | - nodir: true, |
41 | | - }, |
42 | | - (error, filepaths) => { |
43 | | - if (error) { |
44 | | - return callback(error) |
45 | | - } |
46 | | - |
47 | | - async.map( |
48 | | - filepaths.map(filepath => path.resolve(rootPath, filepath)), |
49 | | - fs.readFile, |
50 | | - callback |
51 | | - ) |
52 | | - } |
53 | | - ) |
54 | | - }, |
55 | | - (error, filesContents) => { |
56 | | - if (error) { |
57 | | - return done(error) |
58 | | - } |
59 | | - |
60 | | - return done(null, [].concat(...filesContents)) // Shallow flatten |
61 | | - } |
62 | | - ) |
63 | | - } |
| 28 | + return done => { |
| 29 | + // We loop over the search paths and return an array of all the files |
| 30 | + // contents matching the given pattern |
| 31 | + async.map( |
| 32 | + searchPaths, |
| 33 | + (searchPath, callback) => { |
| 34 | + const globPattern = path.resolve(rootPath, searchPath, pattern); |
| 35 | + glob.glob( |
| 36 | + globPattern, |
| 37 | + { |
| 38 | + ignore: path.resolve(rootPath, 'src/**/*'), |
| 39 | + minimatch, |
| 40 | + nodir: true |
| 41 | + }, |
| 42 | + (error, filepaths) => { |
| 43 | + if (error) { |
| 44 | + return callback(error); |
| 45 | + } |
| 46 | + |
| 47 | + async.map( |
| 48 | + filepaths.map(filepath => path.resolve(rootPath, filepath)), |
| 49 | + fs.readFile, |
| 50 | + callback |
| 51 | + ); |
| 52 | + } |
| 53 | + ); |
| 54 | + }, |
| 55 | + (error, filesContents) => { |
| 56 | + if (error) { |
| 57 | + return done(error); |
| 58 | + } |
| 59 | + |
| 60 | + return done(null, [].concat(...filesContents)); // Shallow flatten |
| 61 | + } |
| 62 | + ); |
| 63 | + }; |
64 | 64 | } |
65 | 65 |
|
66 | 66 | function metalsmithifyPath(path) { |
67 | | - return path.replace(/[/\\]+/g, '/') |
| 67 | + return path.replace(/[/\\]+/g, '/'); |
68 | 68 | } |
69 | 69 |
|
70 | 70 | module.exports = (options = {}) => { |
71 | | - if (!(typeof options.output === 'string' && options.output.length > 0)) { |
72 | | - throw new Error( |
73 | | - '`options.output` is mandatory and has to be a non-empty string' |
74 | | - ) |
75 | | - } |
76 | | - |
77 | | - const output = metalsmithifyPath(options.output) |
78 | | - |
79 | | - const patterns = (Array.isArray(options.files) |
80 | | - ? options.files |
81 | | - : typeof options.files === 'string' |
82 | | - ? [options.files] |
83 | | - : ['**/*'] |
84 | | - ).map(metalsmithifyPath) |
85 | | - |
86 | | - const EOL = |
87 | | - typeof options.insertNewline === 'string' |
88 | | - ? options.insertNewline |
89 | | - : options.insertNewline === false |
90 | | - ? '' |
91 | | - : '\n' |
92 | | - |
93 | | - const searchPaths = Array.isArray(options.searchPaths) |
94 | | - ? options.searchPaths |
95 | | - : typeof options.searchPaths === 'string' |
96 | | - ? [options.searchPaths] |
97 | | - : [] |
98 | | - |
99 | | - const { forceOutput = false, keepConcatenated = false } = options |
100 | | - |
101 | | - return (files, metalsmith, done) => { |
102 | | - if (output in files && !forceOutput) { |
103 | | - return done( |
104 | | - new Error( |
105 | | - `The file "${output}" already exists, see the documentation for 'options.forceOutput'` |
106 | | - ) |
107 | | - ) |
108 | | - } |
109 | | - |
110 | | - // We reduce the array of patterns into an array of gatherers |
111 | | - const gatherers = patterns.reduce( |
112 | | - (acc, pattern) => [ |
113 | | - ...acc, |
114 | | - gathererFromSourceDirectory(files, pattern, { keepConcatenated }), |
115 | | - gathererFromSearchPaths(metalsmith._directory, searchPaths, pattern), |
116 | | - ], |
117 | | - [] |
118 | | - ) |
119 | | - |
120 | | - // Gather all the files contents and generate the final concatenated file |
121 | | - async.parallel(gatherers, (error, gatherersResults) => { |
122 | | - if (error) { |
123 | | - return done(error) |
124 | | - } |
125 | | - |
126 | | - const filesContents = [ |
127 | | - ...[].concat(...gatherersResults), // Shallow flatten the results from each gatherers [[a], [b]] -> [a, b] |
128 | | - '', // Append an empty string so that the final join result includes a trailing new line |
129 | | - ] |
130 | | - |
131 | | - files[output] = { contents: Buffer.from(filesContents.join(EOL)) } |
132 | | - return done() |
133 | | - }) |
134 | | - } |
135 | | -} |
| 71 | + if (!(typeof options.output === 'string' && options.output.length > 0)) { |
| 72 | + throw new Error( |
| 73 | + '`options.output` is mandatory and has to be a non-empty string' |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + const output = metalsmithifyPath(options.output); |
| 78 | + |
| 79 | + const patterns = (Array.isArray(options.files) ? |
| 80 | + options.files : |
| 81 | + (typeof options.files === 'string' ? |
| 82 | + [options.files] : |
| 83 | + ['**/*']) |
| 84 | + ).map(p => metalsmithifyPath(p)); |
| 85 | + |
| 86 | + const EOL = typeof options.insertNewline === 'string' ? |
| 87 | + options.insertNewline : |
| 88 | + (options.insertNewline === false ? |
| 89 | + '' : |
| 90 | + '\n'); |
| 91 | + |
| 92 | + const searchPaths = Array.isArray(options.searchPaths) ? |
| 93 | + options.searchPaths : |
| 94 | + (typeof options.searchPaths === 'string' ? |
| 95 | + [options.searchPaths] : |
| 96 | + []); |
| 97 | + |
| 98 | + const {forceOutput = false, keepConcatenated = false} = options; |
| 99 | + |
| 100 | + return (files, metalsmith, done) => { |
| 101 | + if (output in files && !forceOutput) { |
| 102 | + return done( |
| 103 | + new Error( |
| 104 | + `The file "${output}" already exists, see the documentation for 'options.forceOutput'` |
| 105 | + ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + // We reduce the array of patterns into an array of gatherers |
| 110 | + const gatherers = patterns.reduce( |
| 111 | + (acc, pattern) => [ |
| 112 | + ...acc, |
| 113 | + gathererFromSourceDirectory(files, pattern, {keepConcatenated}), |
| 114 | + gathererFromSearchPaths(metalsmith._directory, searchPaths, pattern) |
| 115 | + ], |
| 116 | + [] |
| 117 | + ); |
| 118 | + |
| 119 | + // Gather all the files contents and generate the final concatenated file |
| 120 | + async.parallel(gatherers, (error, gatherersResults) => { |
| 121 | + if (error) { |
| 122 | + return done(error); |
| 123 | + } |
| 124 | + |
| 125 | + const filesContents = [ |
| 126 | + ...[].concat(...gatherersResults), // Shallow flatten the results from each gatherers [[a], [b]] -> [a, b] |
| 127 | + '' // Append an empty string so that the final join result includes a trailing new line |
| 128 | + ]; |
| 129 | + |
| 130 | + files[output] = {contents: Buffer.from(filesContents.join(EOL))}; |
| 131 | + return done(); |
| 132 | + }); |
| 133 | + }; |
| 134 | +}; |
0 commit comments