Skip to content

Commit

Permalink
Merge pull request #1020 from bertdeblock/remove-use-of-core-object-f…
Browse files Browse the repository at this point in the history
…or-adapters

Remove use of `core-object` for adapters
  • Loading branch information
bertdeblock authored Dec 11, 2024
2 parents ced2d08 + 15b5f65 commit b4bc68f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 90 deletions.
50 changes: 27 additions & 23 deletions lib/dependency-manager-adapters/npm.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
'use strict';

const CoreObject = require('core-object');
const fs = require('fs-extra');
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:npm');
const chalk = require('chalk');
const semver = require('semver');
const Backup = require('../utils/backup');

module.exports = CoreObject.extend({
init() {
this._super.apply(this, arguments);
module.exports = class {
configKey = 'npm';
packageJSON = 'package.json';
packageLock = 'package-lock.json';
useYarnCommand = false;
yarnLock = 'yarn.lock';

constructor(options) {
this.buildManagerOptions = options.buildManagerOptions;
this.cwd = options.cwd;
this.managerOptions = options.managerOptions;
this.run = options.run || require('../utils/run');
this.useYarnCommand = options.useYarnCommand ?? false;

this.backup = new Backup({ cwd: this.cwd });
this.run = this.run || require('../utils/run');
},
useYarnCommand: false,
yarnLock: 'yarn.lock',
configKey: 'npm',
packageJSON: 'package.json',
packageLock: 'package-lock.json',
}

async setup(options) {
if (!options) {
Expand All @@ -28,7 +32,7 @@ module.exports = CoreObject.extend({
this._runYarnCheck(options.ui);

return await this._backupOriginalDependencies();
},
}

async changeToDependencySet(depSet) {
this.applyDependencySet(depSet);
Expand All @@ -48,15 +52,15 @@ module.exports = CoreObject.extend({
debug('Switched to dependencies: \n', currentDeps);

return currentDeps;
},
}

async cleanup() {
try {
await this._restoreOriginalDependencies();
} catch (e) {
console.log('Error cleaning up npm scenario:', e); // eslint-disable-line no-console
}
},
}

_runYarnCheck(ui) {
if (!this.useYarnCommand) {
Expand All @@ -72,7 +76,7 @@ module.exports = CoreObject.extend({
// If no yarn.lock is found, no need to warn.
}
}
},
}

_findCurrentVersionOf(packageName) {
let filename = path.join(this.cwd, 'node_modules', packageName, this.packageJSON);
Expand All @@ -81,7 +85,7 @@ module.exports = CoreObject.extend({
} else {
return null;
}
},
}

async _install(depSet) {
let mgrOptions = this.managerOptions || [];
Expand Down Expand Up @@ -112,7 +116,7 @@ module.exports = CoreObject.extend({
debug('Run npm/yarn install with options %s', mgrOptions);

await this.run(cmd, [].concat(['install'], mgrOptions), { cwd: this.cwd });
},
}

applyDependencySet(depSet) {
debug('Changing to dependency set: %s', JSON.stringify(depSet));
Expand All @@ -129,7 +133,7 @@ module.exports = CoreObject.extend({
debug('Write package.json with: \n', JSON.stringify(newPackageJSON));

fs.writeFileSync(packageJSONFile, JSON.stringify(newPackageJSON, null, 2));
},
}

_packageJSONForDependencySet(packageJSON, depSet) {
this._overridePackageJSONDependencies(packageJSON, depSet, 'dependencies');
Expand All @@ -144,7 +148,7 @@ module.exports = CoreObject.extend({
}

return packageJSON;
},
}

_overridePackageJSONDependencies(packageJSON, depSet, kindOfDependency) {
if (!depSet[kindOfDependency]) {
Expand Down Expand Up @@ -177,15 +181,15 @@ module.exports = CoreObject.extend({
}
}
});
},
}

async _restoreOriginalDependencies() {
await this.backup.restoreFiles([this.packageJSON, this.packageLock, this.yarnLock]);
await this.backup.cleanUp();
await this._install();
},
}

async _backupOriginalDependencies() {
await this.backup.addFiles([this.packageJSON, this.packageLock, this.yarnLock]);
},
});
}
};
42 changes: 22 additions & 20 deletions lib/dependency-manager-adapters/pnpm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const CoreObject = require('core-object');
const fs = require('fs-extra');
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:pnpm');
Expand All @@ -11,21 +10,24 @@ const semverGte = require('semver/functions/gte');
const PACKAGE_JSON = 'package.json';
const PNPM_LOCKFILE = 'pnpm-lock.yaml';

module.exports = CoreObject.extend({
module.exports = class {
// This still needs to be `npm` because we're still reading the dependencies
// from the `npm` key of the ember-try config.
configKey: 'npm',
configKey = 'npm';

constructor(options) {
this.buildManagerOptions = options.buildManagerOptions;
this.cwd = options.cwd;
this.managerOptions = options.managerOptions;
this.run = options.run || require('../utils/run');

init() {
this._super.apply(this, arguments);
this.backup = new Backup({ cwd: this.cwd });
this.run = this.run || require('../utils/run');
},
}

async setup() {
await this._throwOnResolutionMode();
await this.backup.addFiles([PACKAGE_JSON, PNPM_LOCKFILE]);
},
}

async changeToDependencySet(depSet) {
await this.applyDependencySet(depSet);
Expand All @@ -44,7 +46,7 @@ module.exports = CoreObject.extend({
debug('Switched to dependencies: \n', currentDeps);

return currentDeps;
},
}

async cleanup() {
try {
Expand All @@ -54,7 +56,7 @@ module.exports = CoreObject.extend({
} catch (e) {
console.log('Error cleaning up scenario:', e); // eslint-disable-line no-console
}
},
}

_findCurrentVersionOf(packageName) {
let filename = path.join(this.cwd, 'node_modules', packageName, PACKAGE_JSON);
Expand All @@ -63,7 +65,7 @@ module.exports = CoreObject.extend({
} else {
return null;
}
},
}

/**
* pnpm versions 8.0.0 through 8.6.* have the `resolution-mode` setting inverted to
Expand All @@ -82,12 +84,12 @@ module.exports = CoreObject.extend({
'You are using an old version of pnpm that uses wrong resolution mode that violates ember-try expectations. Please either upgrade pnpm or set `resolution-mode` to `highest` in `.npmrc`.',
);
}
},
}

async _getPnpmVersion() {
let result = await this.run('pnpm', ['--version'], { cwd: this.cwd, stdio: 'pipe' });
return result.stdout.split('\n')[0];
},
}

async _getResolutionMode() {
let result = await this.run('pnpm', ['config', 'get', 'resolution-mode'], {
Expand All @@ -96,7 +98,7 @@ module.exports = CoreObject.extend({
});

return result.stdout.split('\n')[0];
},
}

_isResolutionModeWrong(versionStr, resolutionMode) {
// The `resolution-mode` is not set explicitly, and the current pnpm version makes it default
Expand All @@ -106,7 +108,7 @@ module.exports = CoreObject.extend({
}

return false;
},
}

async _install(depSet) {
let mgrOptions = this.managerOptions || [];
Expand Down Expand Up @@ -134,7 +136,7 @@ module.exports = CoreObject.extend({
debug('Run pnpm install with options %s', mgrOptions);

await this.run('pnpm', [].concat(['install'], mgrOptions), { cwd: this.cwd });
},
}

async applyDependencySet(depSet) {
debug('Changing to dependency set: %s', JSON.stringify(depSet));
Expand All @@ -155,7 +157,7 @@ module.exports = CoreObject.extend({
// diff compared to the original locked dependency set.

await this.backup.restoreFile(PNPM_LOCKFILE);
},
}

_packageJSONForDependencySet(packageJSON, depSet) {
this._overridePackageJSONDependencies(packageJSON, depSet, 'dependencies');
Expand All @@ -167,7 +169,7 @@ module.exports = CoreObject.extend({
this._overridePackageJSONDependencies(packageJSON, depSet, 'overrides');

return packageJSON;
},
}

_overridePackageJSONDependencies(packageJSON, depSet, kindOfDependency) {
if (!depSet[kindOfDependency]) {
Expand All @@ -188,5 +190,5 @@ module.exports = CoreObject.extend({
packageJSON[kindOfDependency][packageName] = version;
}
}
},
});
}
};
30 changes: 16 additions & 14 deletions lib/dependency-manager-adapters/workspace.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
'use strict';

const CoreObject = require('core-object');
const fs = require('fs-extra');
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:workspaces');
const walkSync = require('walk-sync');

const NpmAdapter = require('./npm');

module.exports = CoreObject.extend({
init() {
this._super.apply(this, arguments);
this.run = this.run || require('../utils/run');
module.exports = class {
packageJSON = 'package.json';

constructor(options) {
this.buildManagerOptions = options.buildManagerOptions;
this.cwd = options.cwd;
this.managerOptions = options.managerOptions;
this.run = options.run || require('../utils/run');
this.useYarnCommand = options.useYarnCommand ?? false;

if (!this.useYarnCommand) {
throw new Error(
Expand Down Expand Up @@ -51,17 +55,15 @@ module.exports = CoreObject.extend({
buildManagerOptions: this.buildManagerOptions,
});
});
},

packageJSON: 'package.json',
}

setup(options) {
if (!options) {
options = {};
}

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

async changeToDependencySet(depSet) {
// TODO: What should this do for tables? Nesting? Needs different output
Expand All @@ -83,11 +85,11 @@ module.exports = CoreObject.extend({
debug('Switched to dependencies: \n', currentDeps);

return currentDeps;
},
}

cleanup() {
return Promise.all(this._packageAdapters.map((adapter) => adapter.cleanup()));
},
}

_install(depSet) {
let mgrOptions = this.managerOptions || [];
Expand All @@ -113,9 +115,9 @@ module.exports = CoreObject.extend({
debug('Run yarn install with options %s', mgrOptions);

return this.run('yarn', ['install', ...mgrOptions], { cwd: this.cwd });
},
}

_findCurrentVersionOf(dep) {
return this._packageAdapters[0]._findCurrentVersionOf(dep);
},
});
}
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"dependencies": {
"chalk": "^4.1.2",
"cli-table3": "^0.6.0",
"core-object": "^3.1.5",
"debug": "^4.3.2",
"ember-try-config": "^4.0.0",
"execa": "^4.1.0",
Expand Down
21 changes: 10 additions & 11 deletions test/helpers/stub-dependency-manager-adapter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
'use strict';

const CoreObject = require('core-object');
const RSVP = require('rsvp');

module.exports = CoreObject.extend({
module.exports = class {
setup() {
return RSVP.resolve();
},
return Promise.resolve();
}

changeToDependencySet() {
return RSVP.resolve([
return Promise.resolve([
{
name: 'testDep',
versionExpected: '2.0.0',
versionSeen: '2.1.0',
},
]);
},
}

cleanup() {
return RSVP.resolve();
},
});
return Promise.resolve();
}
};
Loading

0 comments on commit b4bc68f

Please sign in to comment.