Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: promisify methods instead of a single function #222

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/preProcessPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ export default function preProcessPattern(globalRef, pattern) {

debug(`determined '${pattern.from}' to be read from '${pattern.absoluteFrom}'`);

return pify(inputFileSystem).stat(pattern.absoluteFrom)
.catch(() => {
// If from doesn't appear to be a glob, then log a warning
if (isGlob(pattern.from) || pattern.from.indexOf('*') !== -1) {
pattern.fromType = 'glob';
pattern.absoluteFrom = escape(pattern.context, pattern.from);
} else {
const msg = `unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`;
warning(msg);
compilation.errors.push(`[copy-webpack-plugin] ${msg}`);
pattern.fromType = 'nonexistent';


return pify(inputFileSystem).stat(pattern.absoluteFrom, (err, stat) => {
if (err) {
// If from doesn't appear to be a glob, then log a warning
if (isGlob(pattern.from) || pattern.from.indexOf('*') !== -1) {
pattern.fromType = 'glob';
pattern.absoluteFrom = escape(pattern.context, pattern.from);
} else {
const msg = `unable to locate '${pattern.from}' at '${pattern.absoluteFrom}'`;
warning(msg);
compilation.errors.push(`[copy-webpack-plugin] ${msg}`);
pattern.fromType = 'nonexistent';
}

return;
}
})
.then((stat) => {

if (!stat) {
return pattern;
}
Expand All @@ -95,6 +99,7 @@ export default function preProcessPattern(globalRef, pattern) {
} else if(!pattern.fromType) {
info(`Unrecognized file type for ${pattern.from}`);
}

return pattern;
});
})
}
27 changes: 12 additions & 15 deletions src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import findCacheDir from 'find-cache-dir';
export default function writeFile(globalRef, pattern, file) {
const {info, debug, compilation, fileDependencies, written, inputFileSystem, copyUnmodified} = globalRef;

return pify(inputFileSystem).stat(file.absoluteFrom)
.then((stat) => {
return pify(inputFileSystem).stat(file.absoluteFrom, (err, stat) => {
// We don't write empty directories
if (stat.isDirectory()) {
return;
Expand All @@ -22,8 +21,8 @@ export default function writeFile(globalRef, pattern, file) {
}

info(`reading ${file.absoluteFrom} to write to assets`);
return pify(inputFileSystem).readFile(file.absoluteFrom)
.then((content) => {

return pify(inputFileSystem).readFile(file.absoluteFrom, (err, content) => {
if (pattern.transform) {
const transform = (content, absoluteFrom) => {
return pattern.transform(content, absoluteFrom);
Expand All @@ -46,22 +45,20 @@ export default function writeFile(globalRef, pattern, file) {
return cacache
.get(globalRef.cacheDir, cacheKey)
.then(
(result) => result.data,
() => {
return Promise
.resolve()
.then(() => transform(content, file.absoluteFrom))
.then((content) => cacache.put(globalRef.cacheDir, cacheKey, content)
.then(() => content));
}
);
(result) => result.data,
() => {
return Promise
.resolve()
.then(() => transform(content, file.absoluteFrom))
.then((content) => cacache.put(globalRef.cacheDir, cacheKey, content)
.then(() => content));
}
);
}

content = transform(content, file.absoluteFrom);
}

return content;
}).then((content) => {
const hash = loaderUtils.getHashDigest(content);

if (pattern.toType === 'template') {
Expand Down