diff --git a/lib/commands/config.js b/lib/commands/config.js index 9415bec5..df31f173 100644 --- a/lib/commands/config.js +++ b/lib/commands/config.js @@ -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)); diff --git a/lib/commands/reset.js b/lib/commands/reset.js index 892c0f64..e1c2fa3b 100644 --- a/lib/commands/reset.js +++ b/lib/commands/reset.js @@ -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(); diff --git a/lib/commands/try-each.js b/lib/commands/try-each.js index b58b7a08..6b7646f9 100644 --- a/lib/commands/try-each.js +++ b/lib/commands/try-each.js @@ -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 }); diff --git a/lib/commands/try-ember.js b/lib/commands/try-ember.js index 7ddd9556..ff3b3f2f 100644 --- a/lib/commands/try-ember.js +++ b/lib/commands/try-ember.js @@ -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, }, @@ -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 }); diff --git a/lib/commands/try-one.js b/lib/commands/try-one.js index 29e7e3ec..1c0e1532 100644 --- a/lib/commands/try-one.js +++ b/lib/commands/try-one.js @@ -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)); @@ -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 }); diff --git a/lib/tasks/reset.js b/lib/tasks/reset.js index c258da88..5679ec85 100644 --- a/lib/tasks/reset.js +++ b/lib/tasks/reset.js @@ -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', diff --git a/lib/tasks/try-each.js b/lib/tasks/try-each.js index 97eb577b..4a29619e 100644 --- a/lib/tasks/try-each.js +++ b/lib/tasks/try-each.js @@ -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) { @@ -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) => { @@ -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) { diff --git a/lib/utils/config.js b/lib/utils/config.js index d37630f4..b1e2ee45 100644 --- a/lib/utils/config.js +++ b/lib/utils/config.js @@ -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)) { @@ -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)) { @@ -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 { @@ -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 {}; } } diff --git a/lib/utils/dependency-manager-adapter-factory.js b/lib/utils/dependency-manager-adapter-factory.js index f4b57543..0f5b675d 100644 --- a/lib/utils/dependency-manager-adapter-factory.js +++ b/lib/utils/dependency-manager-adapter-factory.js @@ -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) { @@ -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, @@ -31,7 +31,7 @@ module.exports = { } else if (config.packageManager === 'pnpm') { adapters.push( new PnpmAdapter({ - cwd: root, + cwd, managerOptions: config.npmOptions, buildManagerOptions: config.buildManagerOptions, }), @@ -39,7 +39,7 @@ module.exports = { } else if (config.packageManager === 'yarn') { adapters.push( new YarnAdapter({ - cwd: root, + cwd, managerOptions: config.npmOptions, buildManagerOptions: config.buildManagerOptions, }), @@ -47,7 +47,7 @@ module.exports = { } else if (hasNpm) { adapters.push( new NpmAdapter({ - cwd: root, + cwd, managerOptions: config.npmOptions, buildManagerOptions: config.buildManagerOptions, }), diff --git a/test/commands/try-each-test.js b/test/commands/try-each-test.js index 1abc7af5..bbfa13e3 100644 --- a/test/commands/try-each-test.js +++ b/test/commands/try-each-test.js @@ -12,6 +12,8 @@ describe('commands/try-each', () => { MockTryEachTask.prototype.run = function () {}; beforeEach(() => { + TryEachCommand.project = { root: '' }; + TryEachCommand._getConfig = function () { return Promise.resolve(mockConfig || { scenarios: [] }); }; @@ -20,6 +22,8 @@ describe('commands/try-each', () => { }); afterEach(() => { + delete TryEachCommand.project; + TryEachCommand._TryEachTask = origTryEachTask; TryEachCommand._getConfig = origGetConfig; mockConfig = null; diff --git a/test/commands/try-ember-test.js b/test/commands/try-ember-test.js index 275a8114..356a62ae 100644 --- a/test/commands/try-ember-test.js +++ b/test/commands/try-ember-test.js @@ -12,6 +12,8 @@ describe('commands/try-ember', () => { MockTryEachTask.prototype.run = function () {}; beforeEach(() => { + TryEmberCommand.project = { root: '' }; + TryEmberCommand._getConfig = function () { return Promise.resolve(mockConfig || { scenarios: [] }); }; @@ -20,6 +22,8 @@ describe('commands/try-ember', () => { }); afterEach(() => { + delete TryEmberCommand.project; + TryEmberCommand._TryEachTask = origTryEachTask; TryEmberCommand._getConfig = origGetConfig; mockConfig = null; diff --git a/test/commands/try-one-test.js b/test/commands/try-one-test.js index 02c8fc0c..80b5605a 100644 --- a/test/commands/try-one-test.js +++ b/test/commands/try-one-test.js @@ -37,6 +37,8 @@ describe('commands/try-one', () => { MockTryEachTask.prototype.run = function () {}; beforeEach(() => { + TryOneCommand.project = { root: '' }; + TryOneCommand._getConfig = function () { return Promise.resolve(mockConfig || { scenarios: [] }); }; @@ -45,6 +47,8 @@ describe('commands/try-one', () => { }); afterEach(() => { + delete TryOneCommand.project; + TryOneCommand._TryEachTask = origTryEachTask; TryOneCommand._getConfig = origGetConfig; mockConfig = null; diff --git a/test/tasks/reset-test.js b/test/tasks/reset-test.js index 53c3f872..007cafa5 100644 --- a/test/tasks/reset-test.js +++ b/test/tasks/reset-test.js @@ -37,8 +37,8 @@ describe('reset', () => { }; let resetTask = new ResetTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, }); writeJSONFile('package.json', fixturePackageJson); diff --git a/test/tasks/try-each-test.js b/test/tasks/try-each-test.js index 35a391f0..60cc2f3c 100644 --- a/test/tasks/try-each-test.js +++ b/test/tasks/try-each-test.js @@ -83,8 +83,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, }); tryEachTask._on = () => {}; @@ -138,8 +138,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, }); tryEachTask._on = () => {}; @@ -203,8 +203,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [], }); @@ -251,8 +251,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, commandArgs: ['ember', 'serve'], commandOptions: { timeout: { length: 20000, isSuccess: true } }, dependencyManagerAdapters: [new StubDependencyAdapter()], @@ -305,8 +305,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -356,8 +356,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -408,8 +408,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -463,8 +463,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, commandArgs: [], dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -511,8 +511,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, commandArgs: ['ember', 'serve'], dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -584,8 +584,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -632,8 +632,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, commandArgs: ['ember', 'version', '--verbose', 'true'], dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -688,8 +688,8 @@ describe('tryEach', () => { _mockLog(outputFn); let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); @@ -742,8 +742,8 @@ describe('tryEach', () => { }; let tryEachTask = new TryEachTask({ - project: { root: tmpdir }, config, + cwd: tmpdir, dependencyManagerAdapters: [new StubDependencyAdapter()], }); diff --git a/test/utils/config-test.js b/test/utils/config-test.js index eabdac13..276d73e5 100644 --- a/test/utils/config-test.js +++ b/test/utils/config-test.js @@ -11,13 +11,11 @@ const root = process.cwd(); const tmproot = path.join(root, 'tmp'); describe('utils/config', () => { - let project; - let tmpdir; + let cwd; beforeEach(() => { - tmpdir = tmp.in(tmproot); - process.chdir(tmpdir); - project = { root: tmpdir, pkg: {} }; + cwd = tmp.in(tmproot); + process.chdir(cwd); }); afterEach(() => { @@ -38,7 +36,7 @@ describe('utils/config', () => { 'config/non-default.js', ); - return getConfig({ project, configPath: 'config/non-default.js' }).then((config) => { + return getConfig({ configPath: 'config/non-default.js', cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].qux).to.equal('baz'); }); @@ -50,11 +48,9 @@ describe('utils/config', () => { 'other-path/ember-try.js', ); - project.pkg['ember-addon'] = { - configPath: 'other-path', - }; + fs.writeJsonSync('package.json', { 'ember-addon': { configPath: 'other-path' } }); - let config = await getConfig({ project }); + let config = await getConfig({ cwd }); expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].foo).to.equal('bar'); @@ -63,11 +59,9 @@ describe('utils/config', () => { it('falls back to config/ember-try.js if projects configured configPath is not present', async () => { generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); - project.pkg['ember-addon'] = { - configPath: 'other-path', - }; + fs.writeJsonSync('package.json', { 'ember-addon': { configPath: 'other-path' } }); - let config = await getConfig({ project }); + let config = await getConfig({ cwd }); expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].foo).to.equal('bar'); @@ -76,7 +70,7 @@ describe('utils/config', () => { it('uses projects config/ember-try.js if present', () => { generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].foo).to.equal('bar'); }); @@ -85,7 +79,7 @@ describe('utils/config', () => { it('config file can export a function', () => { generateConfigFile('module.exports = function() { return { scenarios: [ { foo: "bar" }] } };'); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].foo).to.equal('bar'); }); @@ -102,14 +96,14 @@ describe('utils/config', () => { ' });' + '};'; generateConfigFile(configFile); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].bar).to.equal('baz'); }); }); it('throws error if project.root/config/ember-try.js is not present and no versionCompatibility', () => { - return getConfig({ project }).catch((error) => { + return getConfig({ cwd }).catch((error) => { expect(error).to.match( /No ember-try configuration found\. Please see the README for configuration options/, ); @@ -123,7 +117,7 @@ describe('utils/config', () => { ); generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); // Should not be used - return getConfig({ project, configPath: 'config/non-default.js' }).then((config) => { + return getConfig({ configPath: 'config/non-default.js', cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].qux).to.equal('baz'); }); @@ -135,7 +129,7 @@ describe('utils/config', () => { }); it('is used if there is no config file', () => { - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { let scenarios = config.scenarios; expect(scenarios.length).to.equal(5); expect(scenarios).to.include.deep.members([ @@ -155,7 +149,7 @@ describe('utils/config', () => { it('is always used if passed in and behaves as if config file has "useVersionCompatibility: true"', () => { generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); - return getConfig({ project, versionCompatibility: { ember: '2.18.0' } }).then((config) => { + return getConfig({ cwd, versionCompatibility: { ember: '2.18.0' } }).then((config) => { let scenarios = config.scenarios; expect(scenarios.length).to.equal(6); expect(scenarios).to.include.deep.members([ @@ -170,7 +164,7 @@ describe('utils/config', () => { }); it('can be overridden by passed in versionCompatibility', () => { - return getConfig({ project, versionCompatibility: { ember: '2.18.0' } }).then((config) => { + return getConfig({ cwd, versionCompatibility: { ember: '2.18.0' } }).then((config) => { let scenarios = config.scenarios; expect(scenarios.length).to.equal(5); expect(scenarios).to.include.deep.members([ @@ -186,7 +180,7 @@ describe('utils/config', () => { it('is ignored if config file has scenarios', () => { generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.scenarios).to.have.lengthOf(1); expect(config.scenarios[0].foo).to.equal('bar'); }); @@ -194,7 +188,7 @@ describe('utils/config', () => { it('is merged with config if config does not have scenarios', () => { generateConfigFile('module.exports = { npmOptions: ["--some-thing=true"] };'); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.npmOptions).to.eql(['--some-thing=true']); expect(config.scenarios.length).to.equal(5); }); @@ -204,7 +198,7 @@ describe('utils/config', () => { generateConfigFile( 'module.exports = { useVersionCompatibility: true, npmOptions: ["--whatever=true"], scenarios: [ { name: "bar" }, { name: "ember-beta", allowedToFail: false } ] };', ); - return getConfig({ project }).then((config) => { + return getConfig({ cwd }).then((config) => { expect(config.useVersionCompatibility).to.equal(true); expect(config.npmOptions).to.eql(['--whatever=true']); expect(config.scenarios.length).to.equal(6); @@ -235,7 +229,7 @@ describe('utils/config', () => { 'config/use-pnpm.js', ); - return getConfig({ configPath: 'config/use-pnpm.js', project }).then((config) => { + return getConfig({ configPath: 'config/use-pnpm.js', cwd }).then((config) => { expect(config.usePnpm).to.be.undefined; expect(config.packageManager).to.be.equal('pnpm'); }); @@ -247,7 +241,7 @@ describe('utils/config', () => { 'config/use-yarn.js', ); - return getConfig({ configPath: 'config/use-yarn.js', project }).then((config) => { + return getConfig({ configPath: 'config/use-yarn.js', cwd }).then((config) => { expect(config.useYarn).to.be.undefined; expect(config.packageManager).to.be.equal('yarn'); });