Skip to content

Commit

Permalink
refactor: extract magic const, clarify args, asserts first
Browse files Browse the repository at this point in the history
  • Loading branch information
atao60 committed Sep 19, 2020
1 parent 6c172e4 commit b4b9621
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { fetchOptionsFrom } from './config';
import { doit } from './wrapper';

export async function cli(args: string[]) {
const options = await fetchOptionsFrom(args);
await doit(options);
const { jobTag, options } = await fetchOptionsFrom(args);
await doit(jobTag, options);
}
35 changes: 19 additions & 16 deletions src/tasks/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const copyDef = {
questions.push({
type: 'confirm',
name: 'errorOnExist',
when: (answers: {keepExisting: boolean}) => !answers.keepExisting,
when: (answers: { keepExisting: boolean }) => !answers.keepExisting,
message: 'When the destination exists, throw an error?',
default: copyDef.default.errorOnExist
});
Expand Down Expand Up @@ -99,7 +99,7 @@ interface CliCopyOptions extends CopyOptions {
* Wrapper for node-fs-exta copy function.
* https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md
*/
export function job ({ src, dest, ...copyOptions }:
export function job({ src, dest, ...copyOptions }:
{ src: string; dest: string; copyOptions: { [_: string]: unknown } }): void {

const otherOptions = copyOptions as CliCopyOptions;
Expand All @@ -118,28 +118,31 @@ export function job ({ src, dest, ...copyOptions }:
});
}

function mainMessageFromError (error: Error | string | { code: string; syscall: string; path: string }): string {
function mainMessageFromError(error: Error | string | { code: string; syscall: string; path: string }): string {
const msg = error.toString();
const groups = /^\s*Error\s*:\s*(.*?\s+already\s+exists\s*)$/.exec(msg);
if (groups) {
return groups[1];
}
// only if under Linux
// TODO what about other os?
const linuxError = error as { code: string; syscall: string; path: string };
if (linuxError.code === 'EISDIR' && linuxError.syscall === 'unlink') {
return `it seems your're trying to copy a file on the directory '${linuxError.path}'`
+ ', which is not allowed';
if (!groups) {
// only if under Linux
// TODO what about other os?
const linuxError = error as { code: string; syscall: string; path: string };
if (linuxError.code === 'EISDIR' && linuxError.syscall === 'unlink') {
return `it seems your're trying to copy a file to the directory '${linuxError.path}'`
+ ', which is not allowed';
}
return;
}

return groups[1];
}

copy(src, dest, otherOptions, error => {
if (!error) {
console.info('Copy complete...');
if (error) {
const mainMsg = mainMessageFromError(error) || error;
console.error(`${red.bold('ERROR')} thrown while copying file or directory: `, mainMsg);
return;
}
const mainMsg = mainMessageFromError(error) || error;
return console.error(`${red.bold('ERROR')} thrown while copying file or directory: `, mainMsg);
console.info('Copy complete...');
return;
});

}
3 changes: 2 additions & 1 deletion src/tasks/emptyDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function job ({ directory }: { directory: string }): void {

emptyDir(directory, error => {
if (error) {
return console.error(`${red.bold('ERROR')} thrown while emptying directory: `, error);
console.error(`${red.bold('ERROR')} thrown while emptying directory: `, error);
return;
}
console.info(`Directory ${directory} emptied.`);
});
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/ensureDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export function job ({ dir: directory, mode }:

ensureDir(directory, mode, error => {
if (error) {
return console.error(`${red.bold('ERROR')} thrown while creating directory: `, error);
console.error(`${red.bold('ERROR')} thrown while creating directory: `, error);
return;
}
console.info(`Directory ${directory} exists.`);
});
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/ensureFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export function job ({ file }: { file: string }): void {

ensureFile(file, error => {
if (error) {
return console.error(`${red.bold('ERROR')} thrown while creating file: `, error);
console.error(`${red.bold('ERROR')} thrown while creating file: `, error);
return;
}
console.info(`File ${file} exists`);
});
Expand Down
10 changes: 5 additions & 5 deletions src/tasks/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ export function job ({ src, dest, ...moveOptions }:
}

move(src, dest, otherOptions, error => {
if (!error) {
console.info('Move complete...');
return;
if (error) {
const mainMsg = mainMessageFromError(error) || error;
return console.error(`${red.bold('ERROR')} thrown while moving file or directory: `, mainMsg);
}
const mainMsg = mainMessageFromError(error) || error;
return console.error(`${red.bold('ERROR')} thrown while moving file or directory: `, mainMsg);
console.info('Move complete...');
return;
});

}
3 changes: 2 additions & 1 deletion src/tasks/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function job ({ dir }: { dir: string }): void {

remove(dir, error => {
if (error) {
return console.error(`${red.bold('ERROR')} thrown while removing file or directory: `, error);
console.error(`${red.bold('ERROR')} thrown while removing file or directory: `, error);
return;
}
console.info(`File or directory ${dir} gone.`);
});
Expand Down
6 changes: 4 additions & 2 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { join } from 'path';

export async function doit ({ jobTag, options }: { jobTag: string, options: {} }) {
const tasksSubDir = 'tasks';

const modulePath = join(__dirname, 'tasks', jobTag);
export async function doit (jobTag: string, options: {} ) {

const modulePath = join(__dirname, tasksSubDir, jobTag);
const module = await import(modulePath);

module.job(options);
Expand Down

0 comments on commit b4b9621

Please sign in to comment.