Skip to content

Commit

Permalink
feat(public/fs): options take a logger key to disable logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 20, 2019
1 parent 8369346 commit 33209be
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/public/fs/copy/copy.ts
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs-extra';
import { absolute, exists } from '~/utils/file';
import { IFsUpdateOptions, TCopyFilterFn, TDestination } from '../types';
import confirm from '~/utils/confirm';
import logger from '~/utils/logger';
import log from '../log';
import { open } from '~/utils/errors';

export default async function copy(
Expand Down Expand Up @@ -73,7 +73,9 @@ export async function each(

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

Expand All @@ -91,5 +93,5 @@ export async function each(
}
}
});
logger.info(`Copied: "${relatives.src}" to "${relatives.dest}"`);
log(options, 'info')(`Copied: "${relatives.src}" to "${relatives.dest}"`);
}
12 changes: 12 additions & 0 deletions src/public/fs/log.ts
@@ -0,0 +1,12 @@
import logger from '~/utils/logger';
import { IFsReadOptions } from './types';

export default function log(
options: IFsReadOptions,
level: 'trace' | 'debug' | 'info' | 'warn' | 'error'
): (...args: any[]) => void {
return (...args) => {
if (!options.hasOwnProperty('logger')) return logger[level](...args);
if (options.logger) logger[level](...args);
};
}
10 changes: 5 additions & 5 deletions src/public/fs/mkdir/mkdir.ts
Expand Up @@ -3,9 +3,9 @@ import fs from 'fs-extra';
import { absolute, exists } from '~/utils/file';
import confirm from '~/utils/confirm';
import { parallel } from 'promist';
import logger from '~/utils/logger';
import chalk from 'chalk';
import { IFsCreateDeleteOptions } from '../types';
import log from '../log';

export default async function mkdir(
paths: string | string[],
Expand All @@ -29,7 +29,7 @@ export default async function mkdir(
}

// eslint-disable-next-line no-console
(options.confirm ? console.log : logger.debug)(
(options.confirm ? console.log : log(options, 'debug'))(
chalk.bold.yellow(
relatives.existing.length
? 'Directories to create'
Expand All @@ -44,7 +44,7 @@ export default async function mkdir(
);

if (!nonExistingPaths.length) {
logger.info(
log(options, 'info')(
`Create skipped: "${relatives.existing
.concat(relatives.nonExisting)
.join('", "')}"`
Expand All @@ -57,7 +57,7 @@ export default async function mkdir(
await fs.ensureDir(absolute);

const relative = relatives.nonExisting[i];
logger.debug(`Created: ${relative}`);
log(options, 'debug')(`Created: ${relative}`);
});
logger.info(`Created: "${relatives.nonExisting.join('", "')}"`);
log(options, 'info')(`Created: "${relatives.nonExisting.join('", "')}"`);
}
12 changes: 8 additions & 4 deletions src/public/fs/move/move.ts
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs-extra';
import { absolute, exists } from '~/utils/file';
import { IFsUpdateOptions, TDestination } from '../types';
import confirm from '~/utils/confirm';
import logger from '~/utils/logger';
import log from '../log';

export default async function move(
src: string | string[],
Expand Down Expand Up @@ -69,7 +69,9 @@ export async function each(

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

Expand All @@ -79,7 +81,9 @@ export async function each(
throw Error(`Destination already exists: ${relatives.dest}`);
}
if (!options.overwrite) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
log(options, 'info')(
`Move skipped: "${relatives.src}" to "${relatives.dest}"`
);
return;
}
}
Expand All @@ -88,5 +92,5 @@ export async function each(
if (!(await confirm(msg, options))) return;

await fs.move(src, dest, { overwrite: options.overwrite });
logger.info(`Moved: "${relatives.src}" to "${relatives.dest}"`);
log(options, 'info')(`Moved: "${relatives.src}" to "${relatives.dest}"`);
}
10 changes: 5 additions & 5 deletions src/public/fs/remove/remove.ts
Expand Up @@ -3,9 +3,9 @@ import fs from 'fs-extra';
import { absolute, exists } from '~/utils/file';
import confirm from '~/utils/confirm';
import { parallel } from 'promist';
import logger from '~/utils/logger';
import chalk from 'chalk';
import { IFsCreateDeleteOptions } from '../types';
import log from '../log';

export default async function remove(
paths: string | string[],
Expand All @@ -29,7 +29,7 @@ export default async function remove(
}

// eslint-disable-next-line no-console
(options.confirm ? console.log : logger.debug)(
(options.confirm ? console.log : log(options, 'debug'))(
chalk.bold.yellow(
relatives.existing.length ? 'Paths to remove' : 'No paths to remove'
) +
Expand All @@ -42,7 +42,7 @@ export default async function remove(
);

if (!existingPaths.length) {
logger.info(
log(options, 'info')(
`Remove skipped: "${relatives.existing
.concat(relatives.nonExisting)
.join('", "')}"`
Expand All @@ -55,7 +55,7 @@ export default async function remove(
await fs.remove(absolute);

const relative = relatives.existing[i];
logger.debug(`Removed: ${relative}`);
log(options, 'debug')(`Removed: ${relative}`);
});
logger.info(`Removed: "${relatives.existing.join('", "')}"`);
log(options, 'info')(`Removed: "${relatives.existing.join('", "')}"`);
}
6 changes: 3 additions & 3 deletions src/public/fs/rw/rw.ts
Expand Up @@ -3,8 +3,8 @@ import fs from 'fs-extra';
import { exists, absolute } from '~/utils/file';
import confirm from '~/utils/confirm';
import { IFsUpdateOptions, TContentFn } from '../types';
import logger from '~/utils/logger';
import { open } from '~/utils/errors';
import log from '../log';

export default async function rw(
file: string | string[],
Expand Down Expand Up @@ -39,13 +39,13 @@ export async function each(
}

if (response === undefined || (doesExist && !options.overwrite)) {
logger.info(`Write skipped: ${relative}`);
log(options, 'info')(`Write skipped: ${relative}`);
return;
}

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

await fs.ensureDir(path.parse(file).dir);
await fs.writeFile(file, String(response));
logger.info(`Written: ${relative}`);
log(options, 'info')(`Written: ${relative}`);
}
10 changes: 7 additions & 3 deletions src/public/fs/types.ts
Expand Up @@ -20,7 +20,11 @@ export type TContentFn = (
*/
export interface IFsReadOptions {
/**
* If `false`, it won't fail if a path doesn't exist for a read, or if it already exists for a write. Defaults to `false`.
* Whether to enable logging. Default: `true`.
*/
logger?: boolean;
/**
* If `false`, it won't fail if a path doesn't exist for a read, or if it already exists for a write. Default: `false`.
*/
fail?: boolean;
}
Expand All @@ -30,7 +34,7 @@ export interface IFsReadOptions {
*/
export interface IFsCreateDeleteOptions extends IFsReadOptions {
/**
* If `true`, it will require user confirmation for removal. Defaults to `false`.
* If `true`, it will require user confirmation for removal. Default: `false`.
*/
confirm?: boolean;
}
Expand All @@ -40,7 +44,7 @@ export interface IFsCreateDeleteOptions extends IFsReadOptions {
*/
export interface IFsUpdateOptions extends IFsCreateDeleteOptions {
/**
* Overwrites files if they already exist. Defaults to `true`.
* Overwrites files if they already exist. Default: `true`.
*/
overwrite?: boolean;
}
6 changes: 3 additions & 3 deletions src/public/fs/write/write.ts
Expand Up @@ -3,8 +3,8 @@ import path from 'path';
import { IFsUpdateOptions, TContentFn } from '../types';
import { exists, absolute } from '~/utils/file';
import confirm from '~/utils/confirm';
import logger from '~/utils/logger';
import { open } from '~/utils/errors';
import log from '../log';

export default async function write(
file: string | string[],
Expand Down Expand Up @@ -41,13 +41,13 @@ export async function each(
}

if (doesExist && !options.overwrite) {
logger.info(`Write skipped: ${relative}`);
log(options, 'info')(`Write skipped: ${relative}`);
return;
}

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

await fs.ensureDir(path.parse(file).dir);
await fs.writeFile(file, raw ? String(raw) : '');
logger.info(`Written: ${relative}`);
log(options, 'info')(`Written: ${relative}`);
}

0 comments on commit 33209be

Please sign in to comment.