Skip to content

Commit

Permalink
feat: adapt lift for multitask; change lift mode names and default
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 9, 2021
1 parent d9154a3 commit afb6efd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/cli/commands/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function lift(params: CLI.Extension.Params): Promise<Task> {
Options:
--purge Purge all non-${params.options.bin} scripts
--defaults Lift default tasks and subtasks by their own
--mode <value> Lift mode of operation (default, confirm, dry, audit)
--mode <value> Lift mode of operation (confirm, fix, dry, audit)
-h, --help Show help
`;

Expand Down Expand Up @@ -44,9 +44,9 @@ export async function lift(params: CLI.Extension.Params): Promise<Task> {
}

if (
!['default', 'confirm', 'dry', 'audit'].includes(cmd['--mode'] || 'default')
!['confirm', 'fix', 'dry', 'audit'].includes(cmd['--mode'] || 'confirm')
) {
return raises(Error(`Lift mode must be default, confirm, dry, or audit`));
return raises(Error(`Lift mode must be confirm, fix, dry, or audit`));
}

const tasks = await fetch({
Expand All @@ -61,7 +61,8 @@ export async function lift(params: CLI.Extension.Params): Promise<Task> {
purge: cmd['--purge'],
defaults: cmd['--defaults'],
mode: cmd['--mode'] as any,
bin: params.options.bin
bin: params.options.bin,
multitask: params.options.multitask
})
);
}
35 changes: 19 additions & 16 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export interface LiftOptions {
purge?: boolean;
/**
* Lift mode of operation:
* * `'default'`: produces an immediate write.
* * `'confirm'`: prints the changes and waits for confirmation before a write.
* * `'fix'`: produces an immediate write.
* * `'dry'`: prints the expected changes.
* * `'audit'`: prints the expected changes and fails if there are pending changes.
*/
mode?: 'default' | 'confirm' | 'dry' | 'audit';
mode?: 'confirm' | 'fix' | 'dry' | 'audit';
/**
* Lift default tasks and subtasks by their own
*/
Expand All @@ -35,6 +35,10 @@ export interface LiftOptions {
* Name of kpo's executable
*/
bin?: string;
/**
* Whether kpo's executable allows running multiple tasks.
*/
multitask?: boolean;
}

/**
Expand All @@ -53,9 +57,10 @@ export function lift(
const opts = shallow(
{
purge: false,
mode: 'default',
mode: 'confirm',
defaults: false,
bin: constants.cli.bin
bin: constants.cli.bin,
multitask: constants.cli.multitask
},
options || undefined
);
Expand Down Expand Up @@ -83,7 +88,7 @@ export function lift(
return keys.reduce(
(acc: Members<string>, name) => ({
...acc,
[name]: `${opts.bin} ${name} --`
[name]: opts.bin + ' ' + name + (opts.multitask ? ' --' : '')
}),
{}
);
Expand All @@ -92,29 +97,27 @@ export function lift(

pkg.scripts = opts.purge ? taskScripts : { ...pkg.scripts, ...taskScripts };

const isDefault = !['confirm', 'dry', 'audit'].includes(opts.mode);
const areChangesPending = await evaluateChanges(
pkgScripts,
taskScripts,
ctx,
{ post: isDefault, purge: opts.purge }
{ post: opts.mode === 'fix', purge: opts.purge }
);

if (!areChangesPending || (await isCancelled(ctx))) return;

if (isDefault) {
return write(pkgPath, pkg, { exists: 'overwrite' });
}
if (opts.mode === 'dry') return;
if (opts.mode === 'audit') {
throw Error(`There are pending scripts changes`);
}
if (opts.mode === 'confirm') {
return confirm(
{ default: true, message: 'Continue?' },
write(pkgPath, pkg, { exists: 'overwrite' }),
null
);
if (opts.mode === 'fix') {
return write(pkgPath, pkg, { exists: 'overwrite' });
}
return confirm(
{ default: true, message: 'Continue?' },
write(pkgPath, pkg, { exists: 'overwrite' }),
null
);
});
}

Expand Down

0 comments on commit afb6efd

Please sign in to comment.