Skip to content

Commit 89aa730

Browse files
committed
Remove use of Ember CLI's ui instance
1 parent 98ec8fe commit 89aa730

13 files changed

Lines changed: 120 additions & 67 deletions

File tree

lib/commands/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

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

56
module.exports = {
67
name: 'try:config',
@@ -16,6 +17,6 @@ module.exports = {
1617
configPath: commandOptions.configPath,
1718
});
1819

19-
this.ui.writeLine(JSON.stringify(config, null, 2));
20+
log(JSON.stringify(config, null, 2));
2021
},
2122
};

lib/commands/try-each.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ module.exports = {
2626
debug('Config: %s', JSON.stringify(config));
2727

2828
let tryEachTask = new this._TryEachTask({
29-
ui: this.ui,
3029
project: this.project,
3130
config,
3231
});

lib/commands/try-ember.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module.exports = {
3535
debug('Config: %s', JSON.stringify(config));
3636

3737
let tryEachTask = new this._TryEachTask({
38-
ui: this.ui,
3938
project: this.project,
4039
config,
4140
});

lib/commands/try-one.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ module.exports = {
5858
}
5959

6060
let tryEachTask = new this._TryEachTask({
61-
ui: this.ui,
6261
project: this.project,
6362
config,
6463
commandArgs,

lib/dependency-manager-adapters/base.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
const chalk = require('chalk');
43
const debug = require('debug');
54
const { get, set } = require('es-toolkit/compat');
65
const fs = require('fs-extra');
76
const path = require('node:path');
87
const semver = require('semver');
98
const Backup = require('../utils/backup');
9+
const { warn } = require('../utils/console');
1010
const { LOCKFILE, PACKAGE_JSON } = require('../utils/package-managers');
1111

1212
class BaseAdapter {
@@ -35,8 +35,8 @@ class BaseAdapter {
3535
this.debugFunction(...args);
3636
}
3737

38-
async setup(options = {}) {
39-
this._checkForDifferentLockfiles(options.ui);
38+
async setup() {
39+
this._checkForDifferentLockfiles();
4040

4141
await this.backup.addFiles([PACKAGE_JSON, this.lockfile]);
4242
}
@@ -87,7 +87,7 @@ class BaseAdapter {
8787
}
8888
}
8989

90-
_checkForDifferentLockfiles(ui) {
90+
_checkForDifferentLockfiles() {
9191
for (const packageManager in LOCKFILE) {
9292
const lockfile = LOCKFILE[packageManager];
9393

@@ -97,10 +97,8 @@ class BaseAdapter {
9797

9898
try {
9999
if (fs.statSync(path.join(this.cwd, lockfile)).isFile()) {
100-
ui.writeLine(
101-
chalk.yellow(
102-
`Detected a \`${lockfile}\` file. Add \`packageManager: '${packageManager}'\` to your \`config/ember-try.js\` configuration file if you want to use ${packageManager} to install dependencies.`,
103-
),
100+
warn(
101+
`Detected a \`${lockfile}\` file. Add \`packageManager: '${packageManager}'\` to your \`config/ember-try.js\` configuration file if you want to use ${packageManager} to install dependencies.`,
104102
);
105103
}
106104
} catch {

lib/dependency-manager-adapters/pnpm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ module.exports = class PnpmAdapter extends BaseAdapter {
1010
name = 'pnpm';
1111
overridesKey = 'pnpm.overrides';
1212

13-
async setup(options) {
13+
async setup() {
1414
await this._throwOnResolutionMode();
15-
await super.setup(options);
15+
await super.setup();
1616
}
1717

1818
/**

lib/dependency-manager-adapters/workspace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ module.exports = class {
5454
});
5555
}
5656

57-
setup(options) {
58-
return Promise.all(this._packageAdapters.map((adapter) => adapter.setup(options)));
57+
setup() {
58+
return Promise.all(this._packageAdapters.map((adapter) => adapter.setup()));
5959
}
6060

6161
async changeToDependencySet(depSet) {

lib/tasks/try-each.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

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

77
module.exports = class TryEachTask {
88
constructor(options) {
@@ -11,7 +11,6 @@ module.exports = class TryEachTask {
1111
this.config = options.config;
1212
this.dependencyManagerAdapters = options.dependencyManagerAdapters;
1313
this.project = options.project;
14-
this.ui = options.ui;
1514
}
1615

1716
async run(scenarios, options) {
@@ -30,14 +29,13 @@ module.exports = class TryEachTask {
3029
}),
3130
);
3231
this.ScenarioManager = new ScenarioManager({
33-
ui: this.ui,
3432
dependencyManagerAdapters,
3533
});
3634

3735
this._canceling = false;
3836
this._on('SIGINT', () => {
3937
this._canceling = true;
40-
this.ui.writeLine('\nGracefully shutting down from SIGINT (Ctrl-C)');
38+
log('\nGracefully shutting down from SIGINT (Ctrl-C)');
4139
return this.ScenarioManager.cleanup();
4240
});
4341

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

5856
return this._exitAsAppropriate(results);
5957
} catch (err) {
60-
this.ui.writeLine(chalk.red('Error!'));
58+
error('Error!');
6159

6260
if (err) {
63-
this.ui.writeLine(chalk.red(err));
64-
this.ui.writeLine(chalk.red(err.stack));
61+
error(err);
62+
error(err.stack);
6563
}
6664

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

117115
_writeFooter(text) {
118-
this.ui.writeLine(chalk.blue(`\n${text}`));
119-
this.ui.writeLine(chalk.blue('---\n'));
116+
info(`\n${text}`);
117+
info('---\n');
120118
}
121119

122120
_determineCommandFor(scenario) {
@@ -152,7 +150,7 @@ module.exports = class TryEachTask {
152150
}
153151

154152
_printResults(results) {
155-
new this.ResultSummary({ ui: this.ui, results }).print();
153+
new this.ResultSummary({ results }).print();
156154
}
157155

158156
_exitAsAppropriate(results) {

lib/utils/config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const chalk = require('chalk');
43
const path = require('path');
54
const fs = require('fs');
5+
const { prefix, warn } = require('./console');
66
const findByName = require('./find-by-name');
77
const debug = require('debug')('ember-try:utils:config');
88

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

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

7977
delete configData[oldOption];

lib/utils/console.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const chalk = require('chalk');
2+
3+
let logFunction = originalLogFunction;
4+
5+
function error(message) {
6+
logFunction(message, console.error, chalk.red);
7+
}
8+
9+
function info(message) {
10+
logFunction(message, console.info, chalk.blue);
11+
}
12+
13+
function log(message) {
14+
logFunction(message, console.log);
15+
}
16+
17+
function prefix(string) {
18+
return chalk.inverse(` ${string} `);
19+
}
20+
21+
function success(message) {
22+
logFunction(message, console.log, chalk.green);
23+
}
24+
25+
function warn(message) {
26+
logFunction(message, console.warn, chalk.yellow);
27+
}
28+
29+
function originalLogFunction(message, consoleLogFunction, chalkColorFunction) {
30+
consoleLogFunction(chalkColorFunction ? chalkColorFunction(message) : message);
31+
}
32+
33+
function mockLog(mockedLogFunction) {
34+
logFunction = mockedLogFunction;
35+
}
36+
37+
function restoreLog() {
38+
logFunction = originalLogFunction;
39+
}
40+
41+
module.exports = {
42+
error,
43+
info,
44+
log,
45+
prefix,
46+
success,
47+
warn,
48+
49+
_mockLog: mockLog,
50+
_restoreLog: restoreLog,
51+
};

0 commit comments

Comments
 (0)