Skip to content

Commit

Permalink
Rework LISTR2 support (#211)
Browse files Browse the repository at this point in the history
* update listr2 and add a processTasks option to CLI

* remove checksToListr and push up processTasks

* add a place on the CLI to collect listr tasks

* rework listr task running

* rework listr task running part 2

* Bump to dockerfile-build branch of @lando/core for testing purposes
  • Loading branch information
pirog authored Sep 21, 2023
1 parent 5b69c64 commit 21ab463
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 172 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## v3.20.0 - [TBD](https://github.com/lando/cli/releases/tag/3.20.0)
## v3.20.0 - [September 22, 2023](https://github.com/lando/cli/releases/tag/3.20.0)

### CLI

Expand Down
2 changes: 1 addition & 1 deletion lib/art.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ exports.appStart = ({name, phase = 'pre', warnings = {}} = {}) => {
'',
chalk.yellow(niceFont('Warning!', 'Small Slant')),
'',
`Your app started up but we detected some things you ma${italicize('may')}y wish to investigate.`,
`Your app started up but we detected some things you ${italicize('may')} wish to investigate.`,
'',
];

Expand Down
1 change: 1 addition & 0 deletions lib/cli-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ module.exports = class Cli {
const data = {options: argv, inquiry: require('./formatters').getInteractive(task.options, argv)};

// run our pre command hook
// @TODO: add command names and such?
await this.runHook('prerun', {id: argv._[0], data, cli: this, task});

// queue up an extended debugger
Expand Down
62 changes: 53 additions & 9 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ module.exports = class Cli {
return formatters.formatOptions(omit);
}

// @TODO: throw error instead of false?
getRenderer(type = 'default') {
try {
return require(path.resolve(__dirname, '..', 'renderers', type));
} catch (error) {
throw new Error(`Could not find ${type} renderer`);
}
}

/**
* Cli wrapper for error handler
*
Expand Down Expand Up @@ -496,6 +487,59 @@ module.exports = class Cli {
this.init(yargs, tasks, config, userConfig);
}

// run a listr tasks list
async runTaskList(tasks, {
ctx = {},
renderer = 'default',
rendererDebug = 'debug',
rendererForce = false,
rendererOptions = {},
rendererDebugOptions = {},
listrOptions = {},
} = {}) {
// get the bossman
const {Manager} = require('listr2');

// if rendererForce === false then reset the renderer if we are in debug mode
if (rendererForce === false
&& (this.logLevel > 1
|| this.logLevel === 'info'
|| this.logLevel === 'verbose'
|| this.logLevel === 'debug'
|| this.logLevel === 'silly'
)) {
renderer = rendererDebug;
rendererOptions = rendererDebugOptions;
}

// attempt to reset the renderer if its a string and has a renderer we can load
if (typeof renderer === 'string' && fs.existsSync(path.resolve(__dirname, '..', 'renderers', `${renderer}.js`))) {
renderer = require(path.resolve(__dirname, '..', 'renderers', renderer));
}

const defaults = {
ctx,
renderer,
collectErrors: true,
concurrent: true,
showErrorMessage: false,
exitOnError: false,
rendererOptions: {
log: require('@lando/core-next/debug')('lando').extend('cli'),
collapseSubtasks: false,
suffixRetries: false,
showErrorMessage: false,
},
};

// construct the runner
const runner = new Manager(_.merge({}, defaults, {...listrOptions, rendererOptions}));
// add the tasks
runner.add(tasks);
// run
return runner.runAll();
}

/*
* Toggle a toggle
*/
Expand Down
46 changes: 0 additions & 46 deletions lib/listr.js

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"@lando/argv": "^1.1.0",
"@lando/backdrop": "0.8.0",
"@lando/compose": "0.7.0",
"@lando/core": "3.17.2",
"@lando/core": "github:lando/core#dockerfile-build",
"@lando/core-next": "github:lando/core-next#183d75d",
"@lando/dotnet": "^0.7.2",
"@lando/drupal": "0.10.0",
Expand Down Expand Up @@ -146,13 +146,12 @@
"cli-table": "^0.3.1",
"cli-table3": "^0.5.1",
"debug": "^4.3.4",
"delay": "^5.0.0",
"figlet": "^1.1.1",
"inquirer": "^6.2.1",
"inquirer-autocomplete-prompt": "^1.0.1",
"is-docker": "^2",
"is-root": "^2",
"listr2": "^5.0.7",
"listr2": "^6.6.1",
"lodash": "^4.17.21",
"mkdirp": "^0.5.1",
"sudo-block": "^2.0.0",
Expand Down
31 changes: 31 additions & 0 deletions renderers/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const {Listr} = require('listr2');

// we do this to coax out the default renderer class so we can extend it
const listr = new Listr([], {renderer: 'verbose', fallbackRenderer: 'verbose'});

class LandoDebugRenderer extends listr.rendererClass {
static debug = require('debug')('lando:debug-renderer');

constructor(tasks, options, $renderHook) {
super(tasks, options, $renderHook);
this.options.level = 0;
const debug = options.log || LandoDebugRenderer.debug;

// update the logger with our debug wrapper
this.logger.log = (level, message, options) => {
const output = debug(this.logger.format(level, message, options));

if (output && this.logger.options.toStderr.includes(level)) {
this.logger.process.toStderr(output);
return;
}

if (output) this.logger.process.toStdout(output);
};
}
}

module.exports = LandoDebugRenderer;

42 changes: 0 additions & 42 deletions renderers/default.js

This file was deleted.

46 changes: 46 additions & 0 deletions renderers/lando.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const {EOL} = require('os');
const {Listr} = require('listr2');

// we do this to coax out the default renderer class so we can extend it
const listr = new Listr([], {renderer: 'default', fallbackRenderer: 'default'});

class LandoRenderer extends listr.rendererClass {
constructor(tasks, options, $renderHook) {
super(tasks, options, $renderHook);
this.options.level = options.level || 0;
}

create(options) {
options = {
tasks: true,
bottomBar: true,
prompt: true,
...options,
};

const render = [];

const renderTasks = this.renderer(this.tasks, this.options.level);
const renderBottomBar = this.renderBottomBar();
const renderPrompt = this.renderPrompt();

if (options.tasks && renderTasks.length > 0) render.push(...renderTasks);

if (options.bottomBar && renderBottomBar.length > 0) {
if (render.length > 0) render.push('');
render.push(...renderBottomBar);
}

if (options.prompt && renderPrompt.length > 0) {
if (render.length > 0) render.push('');
render.push(...renderPrompt);
}

return render.join(EOL);
}
}

module.exports = LandoRenderer;

Loading

0 comments on commit 21ab463

Please sign in to comment.