|
| 1 | +"use strict"; |
| 2 | +const _ = require('lodash'); |
| 3 | +const path = require('path'); |
| 4 | +const process = require('process'); |
| 5 | + |
| 6 | +let copy = require('recursive-copy'); // eslint-disable-line prefer-const |
| 7 | +let chokidar = require('chokidar'); // eslint-disable-line prefer-const |
| 8 | + |
| 9 | +const asset_copier = () => { |
| 10 | + |
| 11 | + const transform_paths = (directories) => { |
| 12 | + //create array with all source keys minus our blacklist |
| 13 | + const dirs = {}; |
| 14 | + const blackList = ['root', 'patterns', 'data', 'meta', 'annotations', 'patternlabFiles']; |
| 15 | + _.each(directories.source, (dir, key) => { |
| 16 | + |
| 17 | + if (blackList.includes(key)) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + if (!dirs.key) { |
| 22 | + dirs[key] = {}; |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + // loop through all source keys |
| 27 | + _.each(dirs, (dir, key) => { |
| 28 | + // add source key path |
| 29 | + dirs[key].source = directories.source[key]; |
| 30 | + |
| 31 | + // add public key path |
| 32 | + dirs[key].public = directories.public[key]; |
| 33 | + }); |
| 34 | + return dirs; |
| 35 | + }; |
| 36 | + |
| 37 | + const copyFile = (p, dest, options) => { |
| 38 | + copy( |
| 39 | + p, |
| 40 | + dest, |
| 41 | + options |
| 42 | + ).on(copy.events.COPY_FILE_COMPLETE, () => { |
| 43 | + if (options.debug) { |
| 44 | + console.log(`Moved ${p} to ${dest}`); |
| 45 | + } |
| 46 | + options.emitter.emit('patternlab-asset-change', { |
| 47 | + file: p, |
| 48 | + dest: dest |
| 49 | + }); |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + const asset_copy = (assetDirectories, patternlab, options) => { |
| 54 | + |
| 55 | + //take our configured paths and sanitize best we can to only the assets |
| 56 | + const dirs = transform_paths(assetDirectories); |
| 57 | + |
| 58 | + //find out where we are |
| 59 | + const basePath = path.resolve(process.cwd()); |
| 60 | + |
| 61 | + const copyOptions = |
| 62 | + { |
| 63 | + overwrite: true, |
| 64 | + emitter: patternlab.events, |
| 65 | + debug: patternlab.config.debug |
| 66 | + }; |
| 67 | + |
| 68 | + //loop through each directory asset object (source / public pairing) |
| 69 | + _.each(dirs, (dir, key) => { |
| 70 | + |
| 71 | + //if we want to watch files, do so, otherwise just copy each file |
| 72 | + if (options.watch) { |
| 73 | + if (patternlab.config.debug) { |
| 74 | + console.log(`Pattern Lab is watching ${path.resolve(basePath, dir.source)} for changes`); |
| 75 | + } |
| 76 | + |
| 77 | + if (patternlab.watchers[key]) { |
| 78 | + patternlab.watchers[key].close(); |
| 79 | + } |
| 80 | + |
| 81 | + const assetWatcher = chokidar.watch( |
| 82 | + path.resolve(basePath, dir.source), |
| 83 | + { |
| 84 | + ignored: /(^|[\/\\])\../, |
| 85 | + ignoreInitial: true, |
| 86 | + awaitWriteFinish : { |
| 87 | + stabilityThreshold: 200, |
| 88 | + pollInterval: 100 |
| 89 | + } |
| 90 | + } |
| 91 | + ); |
| 92 | + |
| 93 | + //watch for changes and copy |
| 94 | + assetWatcher.on('addDir', (p) => { |
| 95 | + const destination = path.resolve(basePath, dir.public + '/' + path.basename(p)); |
| 96 | + copyFile(p, destination, copyOptions); |
| 97 | + }).on('add', (p) => { |
| 98 | + const destination = path.resolve(basePath, dir.public + '/' + path.basename(p)); |
| 99 | + copyFile(p, destination, copyOptions); |
| 100 | + }).on('change', (p) => { |
| 101 | + const destination = path.resolve(basePath, dir.public + '/' + path.basename(p)); |
| 102 | + copyFile(p, destination, copyOptions); |
| 103 | + }); |
| 104 | + |
| 105 | + patternlab.watchers[key] = assetWatcher; |
| 106 | + |
| 107 | + } else { |
| 108 | + //just copy |
| 109 | + const destination = path.resolve(basePath, dir.public); |
| 110 | + copyFile(dir.source, destination, copyOptions); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + // copy the styleguide |
| 115 | + copyFile(assetDirectories.source.styleguide, assetDirectories.public.root, copyOptions); |
| 116 | + |
| 117 | + // copy the favicon |
| 118 | + copyFile(`${assetDirectories.source.root}/favicon.ico`, `${assetDirectories.public.root}/favicon.ico`, copyOptions); |
| 119 | + |
| 120 | + //we need to special case patterns/**/*.md|.json|.pattern-extensions as well as the global structures |
| 121 | + if (options.watch) { |
| 122 | + |
| 123 | + // watch global structures, such as _data/* and _meta/ |
| 124 | + const globalSources = [assetDirectories.source.data, assetDirectories.source.meta]; |
| 125 | + const globalPaths = globalSources.map(globalSource => path.join( |
| 126 | + basePath, |
| 127 | + globalSource, |
| 128 | + '*' |
| 129 | + )); |
| 130 | + |
| 131 | + _.each(globalPaths, (globalPath) => { |
| 132 | + |
| 133 | + if (patternlab.config.debug) { |
| 134 | + console.log(`Pattern Lab is watching ${globalPath} for changes`); |
| 135 | + } |
| 136 | + |
| 137 | + if (patternlab.watchers[globalPath]) { |
| 138 | + patternlab.watchers[globalPath].close(); |
| 139 | + } |
| 140 | + |
| 141 | + const globalWatcher = chokidar.watch( |
| 142 | + path.resolve(globalPath), |
| 143 | + { |
| 144 | + ignored: /(^|[\/\\])\../, |
| 145 | + ignoreInitial: true, |
| 146 | + awaitWriteFinish : { |
| 147 | + stabilityThreshold: 200, |
| 148 | + pollInterval: 100 |
| 149 | + } |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + //watch for changes and rebuild |
| 154 | + globalWatcher.on('addDir', (p) => { |
| 155 | + patternlab.events.emit('patternlab-global-change', { |
| 156 | + file: p |
| 157 | + }); |
| 158 | + }) |
| 159 | + .on('add', (p) => { |
| 160 | + patternlab.events.emit('patternlab-global-change', { |
| 161 | + file: p |
| 162 | + }); |
| 163 | + }).on('change', (p) => { |
| 164 | + patternlab.events.emit('patternlab-global-change', { |
| 165 | + file: p |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + patternlab.watchers[globalPath] = globalWatcher; |
| 170 | + |
| 171 | + }); |
| 172 | + |
| 173 | + // watch patterns |
| 174 | + const baseFileExtensions = ['.json', '.yml', '.yaml', '.md']; |
| 175 | + const patternWatches = baseFileExtensions.concat(patternlab.engines.getSupportedFileExtensions()).map( |
| 176 | + dotExtension => path.join( |
| 177 | + basePath, |
| 178 | + assetDirectories.source.patterns, |
| 179 | + `/**/*${dotExtension}` |
| 180 | + ) |
| 181 | + ); |
| 182 | + _.each(patternWatches, (patternWatchPath) => { |
| 183 | + if (patternlab.config.debug) { |
| 184 | + console.log(`Pattern Lab is watching ${patternWatchPath} for changes`); |
| 185 | + } |
| 186 | + |
| 187 | + const patternWatcher = chokidar.watch( |
| 188 | + path.resolve(patternWatchPath), |
| 189 | + { |
| 190 | + ignored: /(^|[\/\\])\../, |
| 191 | + ignoreInitial: true, |
| 192 | + awaitWriteFinish : { |
| 193 | + stabilityThreshold: 200, |
| 194 | + pollInterval: 100 |
| 195 | + } |
| 196 | + } |
| 197 | + ); |
| 198 | + |
| 199 | + //watch for changes and rebuild |
| 200 | + patternWatcher.on('addDir', (p) => { |
| 201 | + patternlab.events.emit('patternlab-pattern-change', { |
| 202 | + file: p |
| 203 | + }); |
| 204 | + }).on('add', (p) => { |
| 205 | + patternlab.events.emit('patternlab-pattern-change', { |
| 206 | + file: p |
| 207 | + }); |
| 208 | + }).on('change', (p) => { |
| 209 | + patternlab.events.emit('patternlab-pattern-change', { |
| 210 | + file: p |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | + } |
| 215 | + }; |
| 216 | + |
| 217 | + return { |
| 218 | + copyAssets: (assetDirectories, patternlab, options) => { |
| 219 | + asset_copy(assetDirectories, patternlab, options); |
| 220 | + }, |
| 221 | + transformConfigPaths: (paths) => { |
| 222 | + return transform_paths(paths); |
| 223 | + } |
| 224 | + }; |
| 225 | + |
| 226 | +}; |
| 227 | + |
| 228 | +module.exports = asset_copier; |
0 commit comments