Skip to content

Commit

Permalink
Merge pull request #1030 from bertdeblock/remove-use-of-ember-cli-ui-…
Browse files Browse the repository at this point in the history
…instance

Remove use of ember-cli `ui` instance
  • Loading branch information
bertdeblock authored Dec 18, 2024
2 parents 98ec8fe + 89aa730 commit 8f8be97
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 67 deletions.
3 changes: 2 additions & 1 deletion lib/commands/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const debug = require('debug')('ember-try:commands:config');
const { log } = require('../utils/console');

module.exports = {
name: 'try:config',
Expand All @@ -16,6 +17,6 @@ module.exports = {
configPath: commandOptions.configPath,
});

this.ui.writeLine(JSON.stringify(config, null, 2));
log(JSON.stringify(config, null, 2));
},
};
1 change: 0 additions & 1 deletion lib/commands/try-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module.exports = {
debug('Config: %s', JSON.stringify(config));

let tryEachTask = new this._TryEachTask({
ui: this.ui,
project: this.project,
config,
});
Expand Down
1 change: 0 additions & 1 deletion lib/commands/try-ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ module.exports = {
debug('Config: %s', JSON.stringify(config));

let tryEachTask = new this._TryEachTask({
ui: this.ui,
project: this.project,
config,
});
Expand Down
1 change: 0 additions & 1 deletion lib/commands/try-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ module.exports = {
}

let tryEachTask = new this._TryEachTask({
ui: this.ui,
project: this.project,
config,
commandArgs,
Expand Down
14 changes: 6 additions & 8 deletions lib/dependency-manager-adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const chalk = require('chalk');
const debug = require('debug');
const { get, set } = require('es-toolkit/compat');
const fs = require('fs-extra');
const path = require('node:path');
const semver = require('semver');
const Backup = require('../utils/backup');
const { warn } = require('../utils/console');
const { LOCKFILE, PACKAGE_JSON } = require('../utils/package-managers');

class BaseAdapter {
Expand Down Expand Up @@ -35,8 +35,8 @@ class BaseAdapter {
this.debugFunction(...args);
}

async setup(options = {}) {
this._checkForDifferentLockfiles(options.ui);
async setup() {
this._checkForDifferentLockfiles();

await this.backup.addFiles([PACKAGE_JSON, this.lockfile]);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ class BaseAdapter {
}
}

_checkForDifferentLockfiles(ui) {
_checkForDifferentLockfiles() {
for (const packageManager in LOCKFILE) {
const lockfile = LOCKFILE[packageManager];

Expand All @@ -97,10 +97,8 @@ class BaseAdapter {

try {
if (fs.statSync(path.join(this.cwd, lockfile)).isFile()) {
ui.writeLine(
chalk.yellow(
`Detected a \`${lockfile}\` file. Add \`packageManager: '${packageManager}'\` to your \`config/ember-try.js\` configuration file if you want to use ${packageManager} to install dependencies.`,
),
warn(
`Detected a \`${lockfile}\` file. Add \`packageManager: '${packageManager}'\` to your \`config/ember-try.js\` configuration file if you want to use ${packageManager} to install dependencies.`,
);
}
} catch {
Expand Down
4 changes: 2 additions & 2 deletions lib/dependency-manager-adapters/pnpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = class PnpmAdapter extends BaseAdapter {
name = 'pnpm';
overridesKey = 'pnpm.overrides';

async setup(options) {
async setup() {
await this._throwOnResolutionMode();
await super.setup(options);
await super.setup();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/dependency-manager-adapters/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ module.exports = class {
});
}

setup(options) {
return Promise.all(this._packageAdapters.map((adapter) => adapter.setup(options)));
setup() {
return Promise.all(this._packageAdapters.map((adapter) => adapter.setup()));
}

async changeToDependencySet(depSet) {
Expand Down
22 changes: 10 additions & 12 deletions lib/tasks/try-each.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const chalk = require('chalk');
const debug = require('debug')('ember-try:task:try-each');
const runCommand = require('./../utils/run-command');
const { error, info, log } = require('../utils/console');
const runCommand = require('../utils/run-command');

module.exports = class TryEachTask {
constructor(options) {
Expand All @@ -11,7 +11,6 @@ module.exports = class TryEachTask {
this.config = options.config;
this.dependencyManagerAdapters = options.dependencyManagerAdapters;
this.project = options.project;
this.ui = options.ui;
}

async run(scenarios, options) {
Expand All @@ -30,14 +29,13 @@ module.exports = class TryEachTask {
}),
);
this.ScenarioManager = new ScenarioManager({
ui: this.ui,
dependencyManagerAdapters,
});

this._canceling = false;
this._on('SIGINT', () => {
this._canceling = true;
this.ui.writeLine('\nGracefully shutting down from SIGINT (Ctrl-C)');
log('\nGracefully shutting down from SIGINT (Ctrl-C)');
return this.ScenarioManager.cleanup();
});

Expand All @@ -57,11 +55,11 @@ module.exports = class TryEachTask {

return this._exitAsAppropriate(results);
} catch (err) {
this.ui.writeLine(chalk.red('Error!'));
error('Error!');

if (err) {
this.ui.writeLine(chalk.red(err));
this.ui.writeLine(chalk.red(err.stack));
error(err);
error(err.stack);
}

return 1; // Signifies exit code
Expand Down Expand Up @@ -111,12 +109,12 @@ module.exports = class TryEachTask {
_writeHeader(text) {
let count = 75 - text.length;
let separator = new Array(count + 1).join('=');
this.ui.writeLine(chalk.blue(`\n=== ${text} ${separator}\n`));
info(`\n=== ${text} ${separator}\n`);
}

_writeFooter(text) {
this.ui.writeLine(chalk.blue(`\n${text}`));
this.ui.writeLine(chalk.blue('---\n'));
info(`\n${text}`);
info('---\n');
}

_determineCommandFor(scenario) {
Expand Down Expand Up @@ -152,7 +150,7 @@ module.exports = class TryEachTask {
}

_printResults(results) {
new this.ResultSummary({ ui: this.ui, results }).print();
new this.ResultSummary({ results }).print();
}

_exitAsAppropriate(results) {
Expand Down
8 changes: 3 additions & 5 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const { prefix, warn } = require('./console');
const findByName = require('./find-by-name');
const debug = require('debug')('ember-try:utils:config');

Expand Down Expand Up @@ -70,10 +70,8 @@ async function config(options) {
const [name, oldOption] = packageManager;

if (typeof configData[oldOption] === 'boolean') {
console.warn(
chalk.yellow(
`${chalk.inverse(' ember-try DEPRECATION ')} The \`${oldOption}\` config option is deprecated. Please use \`packageManager: '${name}'\` instead.`,
),
warn(
`${prefix('ember-try DEPRECATION')} The \`${oldOption}\` config option is deprecated. Please use \`packageManager: '${name}'\` instead.`,
);

delete configData[oldOption];
Expand Down
51 changes: 51 additions & 0 deletions lib/utils/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const chalk = require('chalk');

let logFunction = originalLogFunction;

function error(message) {
logFunction(message, console.error, chalk.red);
}

function info(message) {
logFunction(message, console.info, chalk.blue);
}

function log(message) {
logFunction(message, console.log);
}

function prefix(string) {
return chalk.inverse(` ${string} `);
}

function success(message) {
logFunction(message, console.log, chalk.green);
}

function warn(message) {
logFunction(message, console.warn, chalk.yellow);
}

function originalLogFunction(message, consoleLogFunction, chalkColorFunction) {
consoleLogFunction(chalkColorFunction ? chalkColorFunction(message) : message);
}

function mockLog(mockedLogFunction) {
logFunction = mockedLogFunction;
}

function restoreLog() {
logFunction = originalLogFunction;
}

module.exports = {
error,
info,
log,
prefix,
success,
warn,

_mockLog: mockLog,
_restoreLog: restoreLog,
};
31 changes: 14 additions & 17 deletions lib/utils/result-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const chalk = require('chalk');
const Table = require('cli-table3');
const { error, info, log, success } = require('./console');

module.exports = class ResultSummary {
constructor(options) {
this.results = options.results;
this.ui = options.ui;
}

print() {
Expand All @@ -33,30 +33,28 @@ module.exports = class ResultSummary {
colorAndMessage = chalk.red(failMessage);
countFailed++;
}
task.ui.writeLine(colorAndMessage);
task.ui.writeLine(`Command run: ${scenario.command}`);
log(colorAndMessage);
log(`Command run: ${scenario.command}`);
if (scenario.envState) {
task.ui.writeLine(`with env: ${JSON.stringify(scenario.envState, null, 2)}`);
log(`with env: ${JSON.stringify(scenario.envState, null, 2)}`);
}
task._printDependencyTable(scenario.dependencyState);
});

task.ui.writeLine('');
log('');
task._printResultsSummary(countFailed, countPassed, allowedFailCount, this.results.length);
}

_printResultHeader() {
let task = this;
task.ui.writeLine('');
task.ui.writeLine('------ RESULTS ------');
task.ui.writeLine('');
log('');
log('------ RESULTS ------');
log('');
}

_printDependencyTable(dependencyStatus) {
if (!dependencyStatus.length) {
return;
}
let task = this;
let colorForDepFn;
let tableRow;
let table = new Table({
Expand Down Expand Up @@ -84,22 +82,21 @@ module.exports = class ResultSummary {
});
table.push(tableRow);
});
task.ui.writeLine(table);
task.ui.writeLine('');
log(table);
log('');
}

_printResultsSummary(countFailed, countPassed, allowedFailCount, total) {
let task = this;
if (countFailed) {
let failMessage = `${countFailed} scenarios failed`;
if (allowedFailCount) {
failMessage = `${failMessage} (${allowedFailCount} allowed)`;
}
task.ui.writeLine(chalk.red(failMessage));
task.ui.writeLine(chalk.green(`${countPassed} scenarios succeeded`));
task.ui.writeLine(chalk.gray(`${total} scenarios run`));
error(failMessage);
success(`${countPassed} scenarios succeeded`);
info(`${total} scenarios run`);
} else {
task.ui.writeLine(chalk.green(`All ${countPassed} scenarios succeeded`));
success(`All ${countPassed} scenarios succeeded`);
}
}
};
6 changes: 2 additions & 4 deletions lib/utils/scenario-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
module.exports = class ScenarioManager {
constructor(options) {
this.dependencyManagerAdapters = options.dependencyManagerAdapters;
this.ui = options.ui;
}

async setup() {
let { ui } = this;

for (let depManager of this.dependencyManagerAdapters) {
await depManager.setup({ ui });
await depManager.setup();
}
}

async changeTo(scenario) {
let results = [];

for (let depManager of this.dependencyManagerAdapters) {
if (scenario[depManager.configKey]) {
let depManagerResults = await depManager.changeToDependencySet(
Expand Down
Loading

0 comments on commit 8f8be97

Please sign in to comment.