Skip to content

Commit

Permalink
Merge pull request #1036 from bertdeblock/dont-use-ember-cli-project-…
Browse files Browse the repository at this point in the history
…instance-outside-of-ember-cli-commands

Don't use ember-cli `project` instance outside of ember-cli commands
  • Loading branch information
bertdeblock authored Jan 25, 2025
2 parents 8618294 + 951a41e commit e0d196e
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 74 deletions.
3 changes: 2 additions & 1 deletion lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ module.exports = {
async run(commandOptions) {
debug('Options:\n', commandOptions);

let cwd = this.project.root;
let config = await require('../utils/config')({
project: this.project,
configPath: commandOptions.configPath,
cwd,
});

log(JSON.stringify(config, null, 2));
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ module.exports = {
works: 'insideProject',

async run() {
let config = await require('../utils/config')({ project: this.project });
let cwd = this.project.root;
let config = await require('../utils/config')({ cwd });
let ResetTask = require('../tasks/reset');

let resetTask = new ResetTask({
project: this.project,
config,
cwd,
});

return await resetTask.run();
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/try-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ module.exports = {
async run(commandOptions) {
debug('Options:\n', commandOptions);

let cwd = this.project.root;
let config = await this._getConfig({
project: this.project,
configPath: commandOptions.configPath,
cwd,
});

debug('Config: %s', JSON.stringify(config));

let tryEachTask = new this._TryEachTask({
project: this.project,
config,
cwd,
});

return await tryEachTask.run(config.scenarios, { skipCleanup: commandOptions.skipCleanup });
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/try-ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ module.exports = {
debug('Options:\n', commandOptions);
debug('Ember semver statement', emberVersion);

let cwd = this.project.root;
let config = await this._getConfig({
project: this.project,
configPath: commandOptions.configPath,
cwd,
versionCompatibility: {
ember: emberVersion,
},
Expand All @@ -35,8 +36,8 @@ module.exports = {
debug('Config: %s', JSON.stringify(config));

let tryEachTask = new this._TryEachTask({
project: this.project,
config,
cwd,
});

return await tryEachTask.run(config.scenarios, { skipCleanup: commandOptions.skipCleanup });
Expand Down
7 changes: 4 additions & 3 deletions lib/commands/try-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ module.exports = {
throw new Error('The `ember try:one` command requires a ' + 'scenario name to be specified.');
}

let cwd = this.project.root;
let config = await this._getConfig({
project: this.project,
configPath: commandOptions.configPath,
cwd,
});

debug('Config: %s', JSON.stringify(config));
Expand All @@ -57,9 +58,9 @@ module.exports = {
}

let tryEachTask = new this._TryEachTask({
project: this.project,
config,
commandArgs,
config,
cwd,
});

return await tryEachTask.run([scenario], { skipCleanup: commandOptions.skipCleanup });
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const DependencyManagerAdapterFactory = require('./../utils/dependency-manager-a
module.exports = class ResetTask {
constructor(options) {
this.config = options.config;
this.project = options.project;
this.cwd = options.cwd;
}

run() {
let dependencyAdapters = DependencyManagerAdapterFactory.generateFromConfig(
this.config,
this.project.root,
this.cwd,
);
debug(
'DependencyManagerAdapters: %s',
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/try-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = class TryEachTask {
this.commandArgs = options.commandArgs;
this.commandOptions = options.commandOptions;
this.config = options.config;
this.cwd = options.cwd;
this.dependencyManagerAdapters = options.dependencyManagerAdapters;
this.project = options.project;
}

async run(scenarios, options) {
Expand All @@ -21,7 +21,7 @@ module.exports = class TryEachTask {

let dependencyManagerAdapters =
this.dependencyManagerAdapters ||
DependencyManagerAdapterFactory.generateFromConfig(this.config, this.project.root);
DependencyManagerAdapterFactory.generateFromConfig(this.config, this.cwd);
debug(
'DependencyManagerAdapters: %s',
dependencyManagerAdapters.map((item) => {
Expand Down Expand Up @@ -134,7 +134,7 @@ module.exports = class TryEachTask {
}

_runCommand(options) {
return runCommand(this.project.root, options.commandArgs, options.commandOptions);
return runCommand(this.cwd, options.commandArgs, options.commandOptions);
}

_commandOptions(env) {
Expand Down
32 changes: 19 additions & 13 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const fs = require('fs');
const { prefix, warn } = require('./console');
const debug = require('debug')('ember-try:utils:config');

function getConfigPath(project) {
function getConfigPath(cwd) {
let packageFile = readPackageFile(cwd);
let possibleConfigPath;
if (project.pkg && project.pkg['ember-addon'] && project.pkg['ember-addon']['configPath']) {
let configDir = project.pkg['ember-addon']['configPath'];

possibleConfigPath = path.join(configDir, 'ember-try.js');
if (packageFile['ember-addon']?.['configPath']) {
possibleConfigPath = path.join(packageFile['ember-addon']['configPath'], 'ember-try.js');
}

if (fs.existsSync(possibleConfigPath)) {
Expand All @@ -25,8 +25,8 @@ function getConfigPath(project) {
}

async function getBaseConfig(options) {
let relativeConfigPath = options.configPath || getConfigPath(options.project);
let configPath = path.join(options.project.root, relativeConfigPath);
let relativeConfigPath = options.configPath || getConfigPath(options.cwd);
let configPath = path.join(options.cwd, relativeConfigPath);
let data;

if (fs.existsSync(configPath)) {
Expand All @@ -42,14 +42,13 @@ async function getBaseConfig(options) {
}

let versionCompatibility =
options.versionCompatibility || versionCompatibilityFromPackageJSON(options.project.root);
options.versionCompatibility || versionCompatibilityFromPackageJSON(options.cwd);
if (versionCompatibility) {
// Required lazily to improve startup speed.
let autoScenarioConfigForEmber = require('ember-try-config');

let autoConfig = await autoScenarioConfigForEmber({
versionCompatibility,
project: options.project,
});
return await mergeAutoConfigAndConfigFileData(autoConfig, data);
} else {
Expand Down Expand Up @@ -103,11 +102,18 @@ function mergeAutoConfigAndConfigFileData(autoConfig, configData) {
return conf;
}

function versionCompatibilityFromPackageJSON(root) {
let packageJSONFile = path.join(root, 'package.json');
if (fs.existsSync(packageJSONFile)) {
let packageJSON = JSON.parse(fs.readFileSync(packageJSONFile));
function versionCompatibilityFromPackageJSON(cwd) {
let packageFile = readPackageFile(cwd);

return packageJSON['ember-addon'] ? packageJSON['ember-addon'].versionCompatibility : null;
return packageFile['ember-addon']?.versionCompatibility ?? null;
}

function readPackageFile(cwd) {
let packageFile = path.join(cwd, 'package.json');

if (fs.existsSync(packageFile)) {
return JSON.parse(fs.readFileSync(packageFile));
} else {
return {};
}
}
10 changes: 5 additions & 5 deletions lib/utils/dependency-manager-adapter-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const WorkspaceAdapter = require('../dependency-manager-adapters/workspace');
const YarnAdapter = require('../dependency-manager-adapters/yarn');

module.exports = {
generateFromConfig(config, root) {
generateFromConfig(config, cwd) {
let hasNpm = false;
let adapters = [];
if (!config || !config.scenarios) {
Expand All @@ -22,7 +22,7 @@ module.exports = {
if (config.useWorkspaces) {
adapters.push(
new WorkspaceAdapter({
cwd: root,
cwd,
managerOptions: config.npmOptions,
packageManager: config.packageManager,
buildManagerOptions: config.buildManagerOptions,
Expand All @@ -31,23 +31,23 @@ module.exports = {
} else if (config.packageManager === 'pnpm') {
adapters.push(
new PnpmAdapter({
cwd: root,
cwd,
managerOptions: config.npmOptions,
buildManagerOptions: config.buildManagerOptions,
}),
);
} else if (config.packageManager === 'yarn') {
adapters.push(
new YarnAdapter({
cwd: root,
cwd,
managerOptions: config.npmOptions,
buildManagerOptions: config.buildManagerOptions,
}),
);
} else if (hasNpm) {
adapters.push(
new NpmAdapter({
cwd: root,
cwd,
managerOptions: config.npmOptions,
buildManagerOptions: config.buildManagerOptions,
}),
Expand Down
4 changes: 4 additions & 0 deletions test/commands/try-each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('commands/try-each', () => {
MockTryEachTask.prototype.run = function () {};

beforeEach(() => {
TryEachCommand.project = { root: '' };

TryEachCommand._getConfig = function () {
return Promise.resolve(mockConfig || { scenarios: [] });
};
Expand All @@ -20,6 +22,8 @@ describe('commands/try-each', () => {
});

afterEach(() => {
delete TryEachCommand.project;

TryEachCommand._TryEachTask = origTryEachTask;
TryEachCommand._getConfig = origGetConfig;
mockConfig = null;
Expand Down
4 changes: 4 additions & 0 deletions test/commands/try-ember-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('commands/try-ember', () => {
MockTryEachTask.prototype.run = function () {};

beforeEach(() => {
TryEmberCommand.project = { root: '' };

TryEmberCommand._getConfig = function () {
return Promise.resolve(mockConfig || { scenarios: [] });
};
Expand All @@ -20,6 +22,8 @@ describe('commands/try-ember', () => {
});

afterEach(() => {
delete TryEmberCommand.project;

TryEmberCommand._TryEachTask = origTryEachTask;
TryEmberCommand._getConfig = origGetConfig;
mockConfig = null;
Expand Down
4 changes: 4 additions & 0 deletions test/commands/try-one-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ describe('commands/try-one', () => {
MockTryEachTask.prototype.run = function () {};

beforeEach(() => {
TryOneCommand.project = { root: '' };

TryOneCommand._getConfig = function () {
return Promise.resolve(mockConfig || { scenarios: [] });
};
Expand All @@ -45,6 +47,8 @@ describe('commands/try-one', () => {
});

afterEach(() => {
delete TryOneCommand.project;

TryOneCommand._TryEachTask = origTryEachTask;
TryOneCommand._getConfig = origGetConfig;
mockConfig = null;
Expand Down
2 changes: 1 addition & 1 deletion test/tasks/reset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('reset', () => {
};

let resetTask = new ResetTask({
project: { root: tmpdir },
config,
cwd: tmpdir,
});

writeJSONFile('package.json', fixturePackageJson);
Expand Down
Loading

0 comments on commit e0d196e

Please sign in to comment.