Skip to content

Commit

Permalink
fix: default options assignment for undefined values -uses merge-stra…
Browse files Browse the repository at this point in the history
…tegies
  • Loading branch information
rafamel committed Apr 3, 2021
1 parent 1d6ebe6 commit b3f430c
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 34 deletions.
94 changes: 80 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"find-up": "^5.0.0",
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
"merge-strategies": "^0.2.0",
"pipettes": "^0.1.3",
"prefix-stream": "^1.0.1",
"type-core": "^0.8.0"
Expand Down
5 changes: 3 additions & 2 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { log } from '../tasks/stdio/log';
import { constants } from '../constants';
import main from './main';
import { NullaryFn } from 'type-core';
import { shallow } from 'merge-strategies';
import { attach, options as _options, resolver, add } from 'exits';

export interface BinOptions {
Expand All @@ -22,14 +23,14 @@ export interface BinOptions {
*/
export async function bin(options?: BinOptions): Promise<void> {
try {
const opts = Object.assign(
const opts = shallow(
{
bin: constants.bin,
file: constants.file,
description: constants.description,
version: constants.version
},
options
options || undefined
);

const task = await main({ argv: process.argv.slice(2) }, opts);
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/filesystem/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Task, Context } from '../../definitions';
import { getPathPairs, usePair } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { log } from '../stdio/log';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand Down Expand Up @@ -29,9 +30,9 @@ export function copy(
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Copy', paths, 'to', destination));

const opts = Object.assign(
const opts = shallow(
{ glob: false, single: false, strict: false, exists: 'error' },
options
options || undefined
);
const pairs = await getPathPairs(paths, destination, ctx, {
glob: opts.glob,
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/filesystem/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getPaths, useSource } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { log } from '../stdio/log';
import { Serial } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand Down Expand Up @@ -33,7 +34,7 @@ export function edit(
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Edit:', paths));

const opts = Object.assign({ glob: false, strict: false }, options);
const opts = shallow({ glob: false, strict: false }, options || undefined);
const sources = await getPaths(paths, ctx, {
glob: opts.glob,
strict: opts.strict
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/filesystem/mkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Task, Context } from '../../definitions';
import { getPaths } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { log } from '../stdio/log';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand All @@ -21,7 +22,7 @@ export function mkdir(
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Create directories:', paths));

const opts = Object.assign({ ensure: false }, options);
const opts = shallow({ ensure: false }, options || undefined);

const dirs = await getPaths(paths, ctx, { glob: false, strict: false });

Expand Down
5 changes: 3 additions & 2 deletions src/tasks/filesystem/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Task, Context } from '../../definitions';
import { getPathPairs, usePair } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { log } from '../stdio/log';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand Down Expand Up @@ -29,9 +30,9 @@ export function move(
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Move', paths, 'to', destination));

const opts = Object.assign(
const opts = shallow(
{ glob: false, single: false, strict: false, exists: 'error' },
options
options || undefined
);
const pairs = await getPathPairs(paths, destination, ctx, {
glob: opts.glob,
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/filesystem/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Task, Context } from '../../definitions';
import { getPaths, useSource } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { log } from '../stdio/log';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand All @@ -25,9 +26,9 @@ export function remove(
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Remove:', paths));

const opts = Object.assign(
const opts = shallow(
{ glob: false, strict: false, exists: 'error' },
options
options || undefined
);
const sources = await getPaths(paths, ctx, {
glob: opts.glob,
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/filesystem/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { clear } from '../stdio/clear';
import { series } from '../aggregate/series';
import { NullaryFn, UnaryFn, Empty } from 'type-core';
import { into, combine } from 'pipettes';
import { shallow } from 'merge-strategies';
import chokidar from 'chokidar';
import debounce from 'debounce';

Expand Down Expand Up @@ -51,7 +52,7 @@ export function watch(options: WatchOptions | Empty, task: Task): Task.Async {
poll: -1,
symlinks: false
},
(defaults): Required<WatchOptions> => Object.assign(defaults, options),
(defaults) => shallow(defaults, options || undefined),
({ include, exclude, ...options }) => ({
...options,
include: Array.isArray(include) ? include : [include],
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/filesystem/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Task, Context } from '../../definitions';
import { useDestination } from '../../helpers/paths';
import { log } from '../stdio/log';
import { Serial } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';

Expand Down Expand Up @@ -31,7 +32,7 @@ export function write(
await useDestination(
path,
ctx,
Object.assign({ exists: 'error' }, options),
shallow({ exists: 'error' }, options || undefined),
(dest) => fs.writeFile(dest, data)
);
};
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/process/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Task, Context } from '../../definitions';
import { getPrefix } from '../../helpers/prefix';
import { log } from '../stdio/log';
import { Empty } from 'type-core';
import { shallow } from 'merge-strategies';
import transform from 'prefix-stream';
import { WriteStream } from 'tty';
import { into } from 'pipettes';
import transform from 'prefix-stream';
import execa from 'execa';

export interface ExecOptions extends execa.Options {
Expand All @@ -24,7 +25,7 @@ export function exec(
options?: ExecOptions | Empty,
cb?: (ps: execa.ExecaChildProcess) => void
): Task.Async {
const opts = Object.assign({ extendEnv: true }, options || undefined);
const opts = shallow({ extendEnv: true }, options || undefined);
return async (ctx: Context): Promise<void> => {
const fullArgs = (args || []).concat(ctx.args || []);

Expand Down
5 changes: 3 additions & 2 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { constants } from '../../constants';
import { select } from '../transform/select';
import { write } from '../filesystem/write';
import { print } from '../stdio/print';
import { shallow } from 'merge-strategies';
import { Members } from 'type-core';
import { into } from 'pipettes';
import chalk from 'chalk';
Expand Down Expand Up @@ -38,9 +39,9 @@ export interface LiftOptions {
*/
export function lift(tasks: Task.Record, options?: LiftOptions): Task.Async {
return async (ctx: Context): Promise<void> => {
const opts = Object.assign(
const opts = shallow(
{ purge: false, mode: 'default', bin: constants.bin },
options
options || undefined
);

const pkgPath = getAbsolutePath('package.json', ctx);
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseToArray } from '../../helpers/parse';
import { constants } from '../../constants';
import { print } from '../stdio/print';
import { Empty } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import table from 'as-table';
import chalk from 'chalk';
Expand All @@ -25,7 +26,7 @@ export function list(
map?: (name: string, route: string[]) => string[]
): Task.Sync {
return (ctx: Context): void => {
const opts = Object.assign({ bin: constants.bin }, options);
const opts = shallow({ bin: constants.bin }, options || undefined);
const items = parseToArray(tasks);
const maxRouteLength = items.reduce(
(acc, item) => (acc > item.route.length ? acc : item.route.length),
Expand Down
Loading

0 comments on commit b3f430c

Please sign in to comment.