@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
107 lines • 4.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
class ScriptUpdaterImpl {
pkg;
latestScripts;
shouldConfirmIfScriptExistsButIsDifferent;
ui;
settings;
static Class;
constructor(options) {
this.pkg = options.pkg;
this.latestScripts = options.latestScripts;
this.shouldConfirmIfScriptExistsButIsDifferent =
options.shouldConfirmIfScriptExistsButIsDifferent ?? true;
this.ui = options.ui;
this.settings = options.settings;
}
static FromFeature(feature, options) {
const cwd = options?.cwd ?? feature.cwd;
const updater = new (this.Class ?? ScriptUpdaterImpl)({
pkg: feature.Service('pkg', cwd),
latestScripts: feature.scripts ?? [],
//@ts-ignore
ui: feature.ui,
settings: feature.Service('settings', cwd),
...options,
});
return updater;
}
async update(options) {
this.shouldConfirmIfScriptExistsButIsDifferent =
options?.shouldConfirmIfScriptExistsButIsDifferent ??
this.shouldConfirmIfScriptExistsButIsDifferent;
const scripts = this.pkg.get('scripts') ?? {};
const all = this.latestScripts;
const oldScripts = this.pkg.get('scripts') ?? {};
let shouldConfirm = this.shouldConfirmIfScriptExistsButIsDifferent;
let shouldSkipAll = false;
const updaterSettings = {
skipped: [],
...this.settings.get('scriptUpdater'),
};
for (const name in this.latestScripts) {
const script = this.latestScripts[name];
const oldScript = oldScripts[name];
const shouldAlwaysSkip = (updaterSettings.skipped ?? []).indexOf(name) > -1;
let shouldWrite = !shouldSkipAll && !shouldAlwaysSkip;
if (shouldConfirm &&
!shouldSkipAll &&
oldScript &&
script !== oldScript &&
!shouldAlwaysSkip) {
this.ui.clear();
this.ui.renderSection({
headline: `Change to \`${name}\` detected in ${this.pkg.getSkillNamespace()}!`,
object: {
Current: oldScript,
' New': script,
},
});
const desiredAction = await this.ui.prompt({
type: 'select',
label: 'What should I do?',
options: {
choices: [
{
label: 'Overwrite',
value: constants_1.FILE_ACTION_OVERWRITE,
},
{
label: 'Skip',
value: constants_1.FILE_ACTION_SKIP,
},
{
label: 'Always skip',
value: constants_1.FILE_ACTION_ALWAYS_SKIP,
},
{
label: 'Skip all',
value: 'skipAll',
},
],
},
});
if (desiredAction === 'alwaysSkip') {
updaterSettings.skipped.push(name);
shouldWrite = false;
}
else if (desiredAction === 'skipAll') {
shouldSkipAll = true;
shouldWrite = false;
}
else if (desiredAction === 'skip') {
shouldWrite = false;
}
}
if (shouldWrite) {
scripts[name] = script;
}
}
this.settings.set('scriptUpdater', updaterSettings);
this.pkg.set({ path: 'scripts', value: scripts });
}
}
exports.default = ScriptUpdaterImpl;
//# sourceMappingURL=ScriptUpdater.js.map