Skip to content

Commit

Permalink
feat(exposed/fs): improves logger messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 29, 2019
1 parent 714835c commit 98d3474
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 34 deletions.
15 changes: 6 additions & 9 deletions src/exposed/fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,21 @@ function copy(src: string, dest: string, ...args: any[]): () => Promise<void> {
};

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) return;

if (
!(await confirm(
`Copy "${relatives.src}" to "${relatives.dest}"?`,
options
))
) {
if (!srcExist) {
logger.info(`Copy skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}

const msg = `Copy "${relatives.src}" to "${relatives.dest}"?`;
if (!(await confirm(msg, options))) return;

await fs
.copy(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.fail,
filter
})
.catch(rejects);
logger.info(`Copied "${relatives.src}" to "${relatives.dest}"`);
logger.info(`Copied: "${relatives.src}" to "${relatives.dest}"`);
};
}
11 changes: 8 additions & 3 deletions src/exposed/fs/mkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ function mkdir(
: '')
);

if (!nonExistingPaths.length) return;

if (!nonExistingPaths.length) {
logger.info(
`Create skipped: "${relatives.existing
.concat(relatives.nonExisting)
.join('", "')}"`
);
return;
}
if (!(await confirm('Create?', options))) return;

await parallel.each(nonExistingPaths, async (absolute, i) => {
Expand All @@ -65,7 +71,6 @@ function mkdir(
const relative = relatives.nonExisting[i];
logger.debug(`Created: ${relative}`);
});

logger.info(`Created: "${relatives.nonExisting.join('", "')}"`);
};
}
22 changes: 11 additions & 11 deletions src/exposed/fs/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ function move(
};

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) return;
if (!srcExist) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}

const destExists = await exists(dest);
if (destExists) {
if (options.fail) {
throw Error(`Destination already exists: ${relatives.dest}`);
}
if (!options.overwrite) return;
if (!options.overwrite) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}
}

if (
!(await confirm(
`Move "${relatives.src}" to "${relatives.dest}"?`,
options
))
) {
return;
}
const msg = `Move "${relatives.src}" to "${relatives.dest}"?`;
if (!(await confirm(msg, options))) return;

await fs.move(src, dest, { overwrite: options.overwrite }).catch(rejects);
logger.info(`Moved "${relatives.src}" to "${relatives.dest}"`);
logger.info(`Moved: "${relatives.src}" to "${relatives.dest}"`);
};
}
10 changes: 8 additions & 2 deletions src/exposed/fs/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ function remove(
: '')
);

if (!existingPaths.length) return;
if (!existingPaths.length) {
logger.info(
`Remove skipped: "${relatives.existing
.concat(relatives.nonExisting)
.join('", "')}"`
);
return;
}
if (!(await confirm('Remove?', options))) return;

await parallel.each(existingPaths, async (absolute, i) => {
Expand All @@ -62,7 +69,6 @@ function remove(
const relative = relatives.existing[i];
logger.debug(`Removed: ${relative}`);
});

logger.info(`Removed: "${relatives.existing.join('", "')}"`);
};
}
12 changes: 9 additions & 3 deletions src/exposed/fs/rw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import expose from '~/utils/expose';
import confirm from './utils/confirm';
import { IFsOptions } from './types';
import write from './utils/write';
import logger from '~/utils/logger';

export default expose(rw);

Expand Down Expand Up @@ -35,9 +36,14 @@ function rw(
: undefined;

const response = await fn(raw);
if (response !== undefined) {
if (!(await confirm(`Write "${relative}"?`, options))) return;
await write(file, relative, response);
if (response === undefined) {
logger.info(`Write skipped: ${relative}`);
return;
}

if (!(await confirm(`Write "${relative}"?`, options))) return;

await write(file, response);
logger.info(`Written: ${relative}`);
};
}
3 changes: 0 additions & 3 deletions src/exposed/fs/utils/write.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import path from 'path';
import fs from 'fs-extra';
import logger from '~/utils/logger';
import ensure from '../../tags/ensure';
import { rejects } from 'errorish';

export default async function write(
file: string,
relative: string,
content: string
): Promise<void> {
await ensure.fn(path.parse(file).dir);
await fs.writeFile(file, String(content)).catch(rejects);
logger.info(`Written: ${relative}`);
}
12 changes: 9 additions & 3 deletions src/exposed/fs/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IFsWriteOptions } from './types';
import { exists, absolute } from '~/utils/file';
import confirm from './utils/confirm';
import _write from './utils/write';
import logger from '~/utils/logger';

export default expose(write) as TExposedOverload<
typeof write,
Expand Down Expand Up @@ -43,9 +44,14 @@ function write(file: string, ...args: any[]): () => Promise<void> {
throw Error(`File already exists: ${relative}`);
}

if (!doesExist || options.overwrite) {
if (!(await confirm(`Write "${relative}"?`, options))) return;
await _write(file, relative, raw);
if (doesExist && !options.overwrite) {
logger.info(`Write skipped: ${relative}`);
return;
}

if (!(await confirm(`Write "${relative}"?`, options))) return;

await _write(file, raw);
logger.info(`Written: ${relative}`);
};
}

0 comments on commit 98d3474

Please sign in to comment.