-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote with debug mode and warn on missing file or dir (#74)
- Loading branch information
Showing
11 changed files
with
372 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Promise from 'bluebird'; | ||
import path from 'path'; | ||
import _ from 'lodash'; | ||
|
||
const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs | ||
|
||
var isGlobLike = /\*/; | ||
var isTemplateLike = new RegExp('templateRegExp'); | ||
|
||
export default function preProcessPattern(globalRef, pattern) { | ||
const {info, debug, warning, context, | ||
fileDependencies, contextDependencies, compilation} = globalRef; | ||
|
||
pattern = _.cloneDeep(pattern); | ||
pattern.to = pattern.to || ''; | ||
pattern.context = pattern.context || context; | ||
if (!path.isAbsolute(pattern.context)) { | ||
pattern.context = path.join(context, pattern.context); | ||
} | ||
pattern.ignore = globalRef.ignore.concat(pattern.ignore || []); | ||
|
||
info(`processing from: '${pattern.from}' to: '${pattern.to}'`); | ||
|
||
switch(true) { | ||
case !!pattern.toType: // if toType already exists | ||
break; | ||
case isTemplateLike.test(pattern.to): | ||
pattern.toType = 'template'; | ||
break; | ||
case path.extname(pattern.to) === '' || pattern.to.slice(-1) === '/': | ||
pattern.toType = 'dir'; | ||
break; | ||
default: | ||
pattern.toType = 'file'; | ||
} | ||
|
||
debug(`determined '${pattern.to}' is a '${pattern.toType}'`); | ||
|
||
if (path.isAbsolute(pattern.from)) { | ||
pattern.absoluteFrom = pattern.from; | ||
} else { | ||
pattern.absoluteFrom = path.resolve(pattern.context, pattern.from); | ||
} | ||
|
||
debug(`determined '${pattern.from}' to be read from '${pattern.absoluteFrom}'`); | ||
|
||
return fs | ||
.statAsync(pattern.absoluteFrom) | ||
.catch(() => { | ||
// If from doesn't appear to be a glob, then log a warning | ||
if (isGlobLike.test(pattern.from)) { | ||
pattern.fromType = 'glob'; | ||
} else { | ||
const msg = `unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`; | ||
warning(msg); | ||
compilation.errors.push(`[copy-webpack-plugin] ${msg}`); | ||
pattern.fromType = 'nonexistent'; | ||
} | ||
}) | ||
.then((stat) => { | ||
if (!stat) { | ||
return pattern; | ||
} | ||
|
||
if (stat.isDirectory()) { | ||
pattern.fromType = 'dir'; | ||
pattern.context = pattern.absoluteFrom; | ||
contextDependencies.push(pattern.absoluteFrom); | ||
pattern.absoluteFrom = path.join(pattern.absoluteFrom, '**/*'); | ||
pattern.fromArgs = { | ||
dot: true | ||
}; | ||
} else if(stat.isFile()) { | ||
pattern.fromType = 'file'; | ||
pattern.context = path.dirname(pattern.absoluteFrom); | ||
pattern.fromArgs = { | ||
dot: true | ||
}; | ||
fileDependencies.push(pattern.absoluteFrom); | ||
} else if(!pattern.fromType) { | ||
info(`Unrecognized file type for ${pattern.from}`); | ||
} | ||
return pattern; | ||
}); | ||
} |
Oops, something went wrong.