Skip to content

Adds directory recursion and fixes a webpack v4 deprecation #4

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 45 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,62 @@ class WebpackSfdcDeployPlugin {

const dir = this.options.filesFolderPath;

compiler.plugin('done', stats => {
const applyFn = stats => {
if (stats.hasErrors()) {
this.printErrors(stats.compilation.errors);
return;
}
this.prepareFileValidation();

fs.readdir(dir, (err, files) => {
const resourceZip = new Zip();

this.addDirectoryToZipResource(resourceZip, dir).then(() => {
if (this.options.deploy) {
this.deploy(resourceZip);
}
}).catch(e => { throw(e) });

};

if ('hooks' in compiler) {
// v4 approach:
compiler.hooks.done.tap('SfdcDeployPlugin', applyFn.bind(this));
} else {
// legacy approach:
compiler.plugin('done', applyFn.bind(this));
}


}

addDirectoryToZipResource(resourceZip, dirName) {
return new Promise((resolve, reject) => {
fs.readdir(dirName, async (err, filesOrDirectories) => {
if (err) {
this.printErrors(err);
reject(err);
}

const resourceZip = new Zip();
files.forEach(fileName => {
if(this.validateFile(fileName)) {
const data = fs.readFileSync(path.resolve(dir, fileName), 'utf8');
resourceZip.file(fileName, data);
}
});

if (this.options.deploy) {
this.deploy(resourceZip);
}
const zipDirName = dirName.slice(this.options.filesFolderPath.length + 1);

resolve(Promise.all(
filesOrDirectories.map(async fileOrDirName => {
let fullPathName = path.resolve(dirName, fileOrDirName);
let zipPathName = zipDirName + '/' + fileOrDirName;

if(fs.lstatSync(fullPathName).isDirectory()) {
resourceZip.folder(zipPathName)
await this.addDirectoryToZipResource(
resourceZip, dirName + '/' + fileOrDirName
);

} else if(this.validateFile(fileOrDirName)) {
const data = fs.readFileSync(fullPathName, 'utf8');
resourceZip.file(zipPathName, data);
}
})
));
});

});
}

Expand Down