Skip to content

Commit

Permalink
fix: use given callback for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMalch committed Oct 27, 2020
1 parent fb1de75 commit 1827714
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 56 deletions.
12 changes: 9 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,20 @@ const prettyPrintSrcDest = (src, dest) => {
);
};

const errorHandler = (e) => {
if (e != null) {
console.error('error:', e.message);
process.exit(1);
}
}

try {
globZip({
...program,
outFile: output,
globPatterns,
fileInfoCallback: process.env.VERBOSE ? prettyPrintSrcDest : undefined,
});
}, errorHandler);
} catch (e) {
console.error('error:', e.message);
process.exit(1);
errorHandler(e);
}
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface GlobZipOptions {
fileInfoCallback?: (src: string, dest: string) => void;
}

export declare function globZip(options: GlobZipOptions, callback?: () => void);
export declare function globZip(options: GlobZipOptions, callback?: (error?: any) => void);

110 changes: 58 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,71 @@ module.exports.globZip = function (
},
callback = () => {}
) {
let globPatterns;
if (_globPatterns instanceof Set) {
globPatterns = _globPatterns;
} else if (typeof _globPatterns === 'string') {
globPatterns = new Set([_globPatterns]);
} else if (Array.isArray(_globPatterns)) {
globPatterns = new Set(_globPatterns);
} else {
throw new TypeError(`cannot use given globPatterns`);
}
try {
let globPatterns;
if (_globPatterns instanceof Set) {
globPatterns = _globPatterns;
} else if (typeof _globPatterns === 'string') {
globPatterns = new Set([_globPatterns]);
} else if (Array.isArray(_globPatterns)) {
globPatterns = new Set(_globPatterns);
} else {
callback(new TypeError(`cannot use given globPatterns`));
return;
}

const files = Array.from(globPatterns.values()).reduce(
(acc, p) => acc.concat(glob.sync(p, { dot: true })),
[]
);
const files = Array.from(globPatterns.values()).reduce(
(acc, p) => acc.concat(glob.sync(p, { dot: true })),
[]
);

if (!dryRun && fs.existsSync(outFile) && append !== true) {
fs.unlinkSync(outFile);
}
if (!dryRun && fs.existsSync(outFile) && append !== true) {
fs.unlinkSync(outFile);
}

const zip =
append === true && fs.existsSync(outFile)
? new AdmZip(outFile)
: new AdmZip();
const zip =
append === true && fs.existsSync(outFile)
? new AdmZip(outFile)
: new AdmZip();

const prefix =
wrap == null ? '' : wrap.endsWith(path.sep) ? wrap : wrap + path.sep;
const prefix =
wrap == null ? '' : wrap.endsWith(path.sep) ? wrap : wrap + path.sep;

files.forEach((file) => {
const segments = path.normalize(file).split(path.sep);
if (segments.length <= lift) {
throw new Error(
`cannot lift '${file}' ${lift} directories (${
lift + 1 - segments.length
} too many)`
);
}
const srcPath = path.resolve(process.cwd(), file);
const destPath = prefix + segments.slice(lift, -1).join(path.sep);
const destPathWithName =
destPath + path.sep + segments[segments.length - 1];
if (fs.lstatSync(srcPath).isDirectory()) {
if (includeEmptyDirectories && fs.readdirSync(srcPath).length === 0) {
zip.addFile(destPathWithName + path.sep, Buffer.alloc(0));
fileInfoCallback(srcPath + path.sep, destPathWithName + path.sep);
files.forEach((file) => {
const segments = path.normalize(file).split(path.sep);
if (segments.length <= lift) {
throw new Error(
`cannot lift '${file}' ${lift} directories (${
lift + 1 - segments.length
} too many)`
);
}
} else {
zip.addLocalFile(srcPath, destPath);
fileInfoCallback(srcPath, destPathWithName);
}
});
const srcPath = path.resolve(process.cwd(), file);
const destPath = prefix + segments.slice(lift, -1).join(path.sep);
const destPathWithName =
destPath + path.sep + segments[segments.length - 1];
if (fs.lstatSync(srcPath).isDirectory()) {
if (includeEmptyDirectories && fs.readdirSync(srcPath).length === 0) {
zip.addFile(destPathWithName + path.sep, Buffer.alloc(0));
fileInfoCallback(srcPath + path.sep, destPathWithName + path.sep);
}
} else {
zip.addLocalFile(srcPath, destPath);
fileInfoCallback(srcPath, destPathWithName);
}
});

if (failIfZipEmpty && zip.getEntries().length === 0) {
throw new Error('no files found');
}
if (failIfZipEmpty && zip.getEntries().length === 0) {
callback(new Error('no files found'));
return;
}

if (!dryRun) {
zip.writeZip(outFile, callback);
} else {
callback();
if (!dryRun) {
zip.writeZip(outFile, callback);
} else {
callback();
}
} catch (e) {
callback(e);
}
};

0 comments on commit 1827714

Please sign in to comment.