Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listr2 vibes #211

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading