UNPKG

2.23 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const fse = require('fs-extra')
5const junk = require('junk')
6const mm = require('micromatch')
7const globEscape = require('glob-escape')
8const log = require('./log')
9
10/**
11 * Get a list of files which should not be copied.
12 * @param {Array} routes - Array of route configurations.
13 * @param {Array} customFiles - Array of user-defined globs.
14 * @param {String} srcPath - Path to the source folder.
15 * @returns {Array} ignoredFiles
16 */
17const getIgnoredFiles = function(routes, customFiles, srcPath) {
18
19 // Always ignore the following files
20 const ignoredFiles = [
21 '**/CVS',
22 '**/.git',
23 '**/.svn',
24 '**/.hg',
25 '**/.lock-wscript',
26 '**/.wafpickle-N'
27 ]
28
29 // Escape glob patterns. srcPath should not glob.
30 const saveSrcPath = globEscape(srcPath)
31
32 // Make route paths absolute and ignore them
33 const ignoredRoutes = routes.map((route) => path.join(saveSrcPath, route.path))
34
35 // Return all ignored files
36 return [
37 ...ignoredFiles,
38 ...ignoredRoutes,
39 ...customFiles
40 ]
41
42}
43
44/**
45 * Copy an entire directory with all its files and folders. Specified files will be ignored.
46 * @public
47 * @param {Array} routes - Array of route configurations.
48 * @param {String} srcPath - Path to the source folder.
49 * @param {String} distPath - Path to the destination folder.
50 * @param {Object} opts - Additional optional options.
51 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
52 */
53module.exports = function(routes, srcPath, distPath, opts, next) {
54
55 const ignoredFiles = getIgnoredFiles(routes, opts.ignore, srcPath)
56
57 const fseOpts = {
58 filter: (filePath) => {
59
60 const fileName = path.parse(filePath).base
61
62 const isIgnored = mm.any(filePath, ignoredFiles)
63 const isJunk = junk.is(fileName)
64
65 // Copy file when it's not ignored or not junk
66 const copy = isIgnored===false && isJunk===false
67
68 if (opts.verbose===true) {
69
70 if (copy===false) log(`{cyan:Skipping file: {grey:${ filePath }`)
71 if (copy===true) log(`{cyan:Copying file: {grey:${ filePath }`)
72
73 }
74
75 // Return true to include, false to exclude
76 return copy
77
78 }
79 }
80
81 fse.copy(srcPath, distPath, fseOpts, next)
82
83}
\No newline at end of file