Skip to content

Commit

Permalink
fix(tasks): fix lift confirm mode when there are no pending changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 4, 2021
1 parent 2079576 commit cc7576d
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ export function lift(
pkg.scripts = opts.purge ? taskScripts : { ...pkg.scripts, ...taskScripts };

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

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

if (isDefault) {
return run(write(pkgPath, pkg, { exists: 'overwrite' }), ctx);
}

if (await isCancelled(ctx)) return;
printChanges(pkgScripts, taskScripts, ctx, {
purge: opts.purge,
audit: opts.mode === 'audit'
});

if (opts.mode === 'audit') {
throw Error(`There are pending scripts changes`);
}
if (opts.mode === 'confirm') {
return run(
select(
Expand All @@ -113,12 +115,12 @@ export function lift(
};
}

function printChanges(
function evaluateChanges(
pkgScripts: Members<string>,
taskScripts: Members<string>,
context: Context,
options: { purge: boolean; audit: boolean }
): void {
options: { print: boolean; purge: boolean }
): boolean {
const pkgScriptNames = Object.keys(pkgScripts);
const taskScriptNames = Object.keys(taskScripts);

Expand Down Expand Up @@ -151,32 +153,32 @@ function printChanges(
removeScriptNames.length
);

if (areChangesPending) {
if (addScriptNames.length) {
strArr.push(
styleString('Scripts to add', { bold: true, color: 'green' }),
addScriptNames.join(', ') + '\n'
);
}
if (replaceScriptNames.length) {
strArr.push(
styleString('Scripts to replace', { bold: true, color: 'yellow' }),
replaceScriptNames.join(', ') + '\n'
);
}
if (removeScriptNames.length) {
strArr.push(
styleString('Scripts to remove', { bold: true, color: 'red' }),
removeScriptNames.join(', ') + '\n'
);
if (options.print) {
if (areChangesPending) {
if (addScriptNames.length) {
strArr.push(
styleString('Scripts to add', { bold: true, color: 'green' }),
addScriptNames.join(', ') + '\n'
);
}
if (replaceScriptNames.length) {
strArr.push(
styleString('Scripts to replace', { bold: true, color: 'yellow' }),
replaceScriptNames.join(', ') + '\n'
);
}
if (removeScriptNames.length) {
strArr.push(
styleString('Scripts to remove', { bold: true, color: 'red' }),
removeScriptNames.join(', ') + '\n'
);
}
} else {
strArr.push(styleString('No pending scripts changes', { bold: true }));
}
} else {
strArr.push(styleString('No pending scripts changes', { bold: true }));
}

into(context, print(strArr.join('\n')));

if (options.audit && areChangesPending) {
throw Error(`There are pending scripts changes`);
into(context, print(strArr.join('\n')));
}

return areChangesPending;
}

0 comments on commit cc7576d

Please sign in to comment.