Skip to content

Commit

Permalink
feat: Introduce interactive update
Browse files Browse the repository at this point in the history
Use `--interactive` or `-i` to select the dependencies you want to update.
  • Loading branch information
KamiKillertO authored Jul 3, 2024
1 parent cb5cdda commit d3975e2
Show file tree
Hide file tree
Showing 9 changed files with 13,503 additions and 9,568 deletions.
22,986 changes: 13,419 additions & 9,567 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"pify": "^4.0.1",
"semver": "^7.3.5",
"string-width": "^4.2.0",
"inquirer": "7.0.0",
"unicons": "0.0.3",
"yargs": "^15.1.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/Updtr.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default class Updtr extends EventEmitter {
registry,
updateTo,
save,
interactive: config.interactive,
};
this.cmds = cmds[packageManager];
this.parse = parse[packageManager];
Expand Down
5 changes: 5 additions & 0 deletions src/bin/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default yargs
array: true,
alias: "ex",
})
.option("interactive", {
describe: "Let you select the packages you want to update",
alias: "i",
default: false,
})
.option("update-to", {
describe: "Specify which updates you want to install",
choices: UPDATE_TO_OPTIONS,
Expand Down
53 changes: 52 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import inquirer from "inquirer";
import chalk from "chalk";
import init from "./tasks/init";
import sequentialUpdate from "./tasks/sequentialUpdate";
import splitUpdateTasks from "./tasks/util/splitUpdateTasks";
Expand Down Expand Up @@ -46,14 +48,62 @@ async function runUpdateTasks(updtr, updateTasks) {
return finish(updtr, results);
}

function generateChoices(tasks) {
let dims = [
"name".length,
"from".length,
// "to".length,
];

dims = tasks.reduce((acc, {name, rollbackTo, updateTo}) => ([
name.length > acc[0] ? name.length : acc[0],
rollbackTo.length > acc[1] ? rollbackTo.length : acc[1],
// updateTo.length > acc[2] ? updateTo.length : acc[2],
]), dims);

let choices = [
new inquirer.Separator(" "),
new inquirer.Separator(chalk` {green.bold.underline name}${"".padEnd((dims[0] + 3) - "name".length, " ")}{green.bold.underline from}${"".padEnd((dims[1] + 5) - "from".length, " ")}{green.bold.underline to}`),
];

choices = choices.concat(tasks.map(task => ({
name: chalk`{green ${task.name.padEnd(dims[0] + 3)}}{blue ${task.rollbackTo.padEnd(dims[1])}} ❯ {blue ${task.updateTo}}`,
value: task,
short: `${task.name}@${task.updateTo}`,
})));

return choices;
}

async function selectUpdateTasks(tasks) {
const prompt = inquirer.createPromptModule();
const choices = generateChoices(tasks);
const {packagesToUpdate} = await prompt([
{
name: "packagesToUpdate",
type: "checkbox",
message: chalk`{white.bold Choose which packages to update.}`,
choices,
pageSize: choices.length,
validate: answer => Boolean(answer.length) || "You must choose at least one package.",
},
]);

return packagesToUpdate;
}

export default (async function run(updtr) {
const results = [];

updtr.emit("start", {
config: updtr.config,
});

const {updateTasks} = await init(updtr);
let {updateTasks} = await init(updtr);

if (updtr.config.interactive === true) {
updateTasks = await selectUpdateTasks(updateTasks);
}

if (updateTasks.length > 0) {
results.push(...(await runUpdateTasks(updtr, updateTasks)));
Expand All @@ -67,3 +117,4 @@ export default (async function run(updtr) {

return results;
});

1 change: 1 addition & 0 deletions test/__snapshots__/Updtr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`new Updtr() .config should match the default shape 1`] = `
Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/run.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -24,6 +25,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -55,6 +57,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -73,6 +76,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -104,6 +108,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -135,6 +140,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down
2 changes: 2 additions & 0 deletions test/bin/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Object {
"b",
"c",
],
"interactive": false,
"save": "exact",
"test": "jest -b",
"updateTo": "non-breaking",
Expand All @@ -32,6 +33,7 @@ Object {
"updtrConfig": Object {
"cwd": "/test/bin",
"exclude": Array [],
"interactive": false,
"save": "smart",
"updateTo": "latest",
"use": "npm",
Expand Down
16 changes: 16 additions & 0 deletions test/tasks/__snapshots__/init.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Array [
"updtr-test-module-1",
"updtr-test-module-2",
],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -40,6 +41,7 @@ Array [
"updtr-test-module-1",
"updtr-test-module-2",
],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -58,6 +60,7 @@ Array [
"updtr-test-module-1",
"updtr-test-module-2",
],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -75,6 +78,7 @@ Array [
"updtr-test-module-1",
"updtr-test-module-2",
],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -120,6 +124,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -135,6 +140,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -150,6 +156,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -164,6 +171,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -196,6 +204,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -211,6 +220,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -226,6 +236,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -240,6 +251,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down Expand Up @@ -283,6 +295,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -298,6 +311,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -313,6 +327,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand All @@ -327,6 +342,7 @@ Array [
"config": Object {
"cwd": "/updtr/test/cwd",
"exclude": Array [],
"interactive": undefined,
"registry": undefined,
"save": "smart",
"test": undefined,
Expand Down

0 comments on commit d3975e2

Please sign in to comment.