-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eliminate trivial underscore #147
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
|
||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
const _ = require('underscore-plus'); | ||
const semver = require('semver'); | ||
const config = require('./apm'); | ||
const git = require('./git'); | ||
|
@@ -58,7 +57,7 @@ class Command { | |
sanitizePackageNames(packageNames) { | ||
packageNames ??= []; | ||
packageNames = packageNames.map(packageName => packageName.trim()); | ||
return _.compact(_.uniq(packageNames)); | ||
return Array.from(new Set(packageNames)).filter(Boolean); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome work on this new methodology. I do want to point out that I did some light testing of this method compared to the original, and this new method does in fact shave off about The only thing I'd like to request, removing the names of the previous functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be hard to object against a banal comment for some logic that will probably never change but still, I do think that this concern is overreaching. I specifically chose |
||
} | ||
|
||
logSuccess() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const _ = require('underscore-plus'); | ||
const yargs = require('yargs'); | ||
|
||
const config = require('./apm'); | ||
|
@@ -43,31 +42,25 @@ cmd-shift-o to run the package out of the newly cloned repository.\ | |
return options.alias('h', 'help').describe('help', 'Print this usage message'); | ||
} | ||
|
||
getRepositoryUrl(packageName) { | ||
return new Promise((resolve, reject) => { | ||
const requestSettings = { | ||
url: `${config.getAtomPackagesUrl()}/${packageName}`, | ||
json: true | ||
}; | ||
return request.get(requestSettings, (error, response, body) => { | ||
body ??= {}; | ||
if (error != null) { | ||
return void reject(`Request for package information failed: ${error.message}`); | ||
} | ||
|
||
if (response.statusCode === 200) { | ||
const repositoryUrl = body.repository.url; | ||
if (repositoryUrl) { | ||
return void resolve(repositoryUrl); | ||
} | ||
|
||
return void reject(`No repository URL found for package: ${packageName}`); | ||
} | ||
async getRepositoryUrl(packageName) { | ||
const requestSettings = { | ||
url: `${config.getAtomPackagesUrl()}/${packageName}`, | ||
json: true | ||
}; | ||
const response = await request.get(requestSettings).catch(error => Promise.reject(`Request for package information failed: ${error.message}`)); | ||
const body = response.body ?? {}; | ||
|
||
if (response.statusCode === 200) { | ||
const repositoryUrl = body.repository.url; | ||
if (repositoryUrl) { | ||
return repositoryUrl; | ||
} | ||
|
||
throw `No repository URL found for package: ${packageName}`; | ||
} | ||
|
||
const message = request.getErrorMessage(body, error); | ||
return void reject(`Request for package information failed: ${message}`); | ||
}); | ||
}); | ||
const message = request.getErrorMessage(body, error); | ||
throw `Request for package information failed: ${message}`; | ||
} | ||
|
||
async cloneRepository(repoUrl, packageDirectory, options) { | ||
|
@@ -87,16 +80,17 @@ cmd-shift-o to run the package out of the newly cloned repository.\ | |
} | ||
|
||
installDependencies(packageDirectory, options) { | ||
process.chdir(packageDirectory); | ||
const installOptions = _.clone(options); | ||
process.chdir(packageDirectory); | ||
|
||
return new Install().run(installOptions); | ||
return new Install().run({...options}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I was going to say we should hold off using spread syntax instead of This worried me, but then I tested and checked with Considering the object we are working with is nested, I wonder how much we really need to be doing anything other than passing the value on here. Hard to say, and a discussion for another time. But this should be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, almost sure I checked all functions and what they do, and also, I don't think it's all that obscure; specifically, I feel that merely passing a shallow copy without saying anything about why that's necessary is way more obscure than the spread syntax way of creating a shallow copy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally agree that doing a shallow copy with no comment originally is more obscure, because when I wrote that even I was wondering why they ever needed to do that. But I'm sure you checked the functions and what they do because I couldn't find any differing behavior in your changes which is awesome. |
||
} | ||
|
||
linkPackage(packageDirectory, options) { | ||
const linkOptions = _.clone(options); | ||
const linkOptions = { | ||
...options, | ||
commandArgs: [packageDirectory, '--dev'] | ||
}; | ||
|
||
linkOptions.commandArgs = [packageDirectory, '--dev']; | ||
return new Link().run(linkOptions); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,21 +59,21 @@ Disables the named package(s).\ | |
try { | ||
const installedPackages = await this.getInstalledPackages(); | ||
const installedPackageNames = Array.from(installedPackages).map((pkg) => pkg.name); | ||
const uninstalledPackageNames = _.difference(packageNames, installedPackageNames); | ||
if (uninstalledPackageNames.length > 0) { | ||
console.log(`Not Installed:\n ${uninstalledPackageNames.join('\n ')}`); | ||
const notInstalledPackageNames = packageNames.filter(elem => !installedPackageNames.includes(elem)); | ||
if (notInstalledPackageNames.length > 0) { | ||
console.log(`Not Installed:\n ${notInstalledPackageNames.join('\n ')}`); | ||
} | ||
|
||
// only installed packages can be disabled | ||
packageNames = _.difference(packageNames, uninstalledPackageNames); | ||
packageNames = packageNames.filter(elem => installedPackageNames.includes(elem)); | ||
|
||
if (packageNames.length === 0) { | ||
return "Please specify a package to disable"; //errors as return values atm | ||
} | ||
|
||
const keyPath = '*.core.disabledPackages'; | ||
const disabledPackages = _.valueForKeyPath(settings, keyPath) ?? []; | ||
const result = _.union(disabledPackages, packageNames); | ||
const result = Array.from(new Set([...disabledPackages, ...packageNames])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit worried about this code being rather esoteric looking, as in not being immediately understood in it's purpose at a glance. const result = disabledPackages.concat(packageNames.filter(elem => !disabledPackages.includes(elem))); With performance being roughly:
So considering this we should stick with what you've written, but I'd suggest again we add a comment to ensure the purpose of this is understood at a glance. Likely being able to use the comment from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of a hack, after all; one that I probably found on SO. I don't know whose idea was to release Set in Javascript without adding set operations... they are coming with Node 22 iirc. I'm rather surprised I didn't outright add a TODO to replace these calls once the actual set operations land... it needs to be taken care of, one way or another. |
||
_.setValueForKeyPath(settings, keyPath, result); | ||
|
||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
const assert = require('assert'); | ||
const path = require('path'); | ||
|
||
const _ = require('underscore-plus'); | ||
const async = require('async'); | ||
const CSON = require('season'); | ||
const yargs = require('yargs'); | ||
|
@@ -82,7 +81,11 @@ package names to install with optional versions using the | |
|
||
fs.makeTreeSync(this.atomDirectory); | ||
|
||
const env = _.extend({}, process.env, {HOME: this.atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()}); | ||
const env = { | ||
...process.env, | ||
HOME: this.atomNodeDirectory, | ||
RUSTUP_HOME: config.getRustupHomeDirPath() | ||
}; | ||
this.addBuildEnvVars(env); | ||
|
||
const installOptions = {env}; | ||
|
@@ -194,7 +197,11 @@ Run ppm -v after installing Git to see what version has been detected.\ | |
|
||
fs.makeTreeSync(this.atomDirectory); | ||
|
||
const env = _.extend({}, process.env, {HOME: this.atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()}); | ||
const env = { | ||
...process.env, | ||
HOME: this.atomNodeDirectory, | ||
RUSTUP_HOME: config.getRustupHomeDirPath() | ||
}; | ||
this.addBuildEnvVars(env); | ||
|
||
const installOptions = {env}; | ||
|
@@ -209,32 +216,26 @@ Run ppm -v after installing Git to see what version has been detected.\ | |
// packageName - The string name of the package to request. | ||
// | ||
// return value - A Promise that rejects with an appropriate error or resolves to the response body | ||
requestPackage(packageName) { | ||
async requestPackage(packageName) { | ||
const requestSettings = { | ||
url: `${config.getAtomPackagesUrl()}/${packageName}`, | ||
json: true, | ||
retries: 4 | ||
}; | ||
return new Promise((resolve, reject) => { | ||
request.get(requestSettings, (error, response, body) => { | ||
let message; | ||
body ??= {}; | ||
if (error != null) { | ||
message = `Request for package information failed: ${error.message}`; | ||
if (error.status) { message += ` (${error.status})`; } | ||
return void reject(message); | ||
} | ||
if (response.statusCode !== 200) { | ||
message = request.getErrorMessage(body, error); | ||
return void reject(`Request for package information failed: ${message}`); | ||
} | ||
if (!body.releases.latest) { | ||
return void reject(`No releases available for ${packageName}`); | ||
} | ||
|
||
resolve(body); | ||
}); | ||
const response = await request.get(requestSettings).catch(error => { | ||
const message = request.getErrorMessage(body, error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first parameter here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this was "too clever". I rather don't want to change the logic here at the moment, I will revert to the original way of producing the error message here. |
||
throw `Request for package information failed: ${message}`; | ||
}); | ||
const body = response.body ?? {}; | ||
if (response.statusCode !== 200) { | ||
const message = request.getErrorMessage(body, null); | ||
throw `Request for package information failed: ${message}`; | ||
} | ||
if (!body.releases.latest) { | ||
throw `No releases available for ${packageName}`; | ||
} | ||
|
||
return body; | ||
} | ||
|
||
// Is the package at the specified version already installed? | ||
|
@@ -369,7 +370,10 @@ Run ppm -v after installing Git to see what version has been detected.\ | |
// | ||
// return value - A Promise that rejects with an error or resolves without a value | ||
async installPackageDependencies(options) { | ||
options = _.extend({}, options, {installGlobally: false}); | ||
options = { | ||
...options, | ||
installGlobally: false | ||
}; | ||
const commands = []; | ||
const object = this.getPackageDependencies(options.cwd); | ||
for (let name in object) { | ||
|
@@ -452,7 +456,11 @@ Run ppm -v after installing Git to see what version has been detected.\ | |
|
||
fs.makeTreeSync(this.atomDirectory); | ||
|
||
const env = _.extend({}, process.env, {HOME: this.atomNodeDirectory, RUSTUP_HOME: config.getRustupHomeDirPath()}); | ||
const env = { | ||
...process.env, | ||
HOME: this.atomNodeDirectory, | ||
RUSTUP_HOME: config.getRustupHomeDirPath() | ||
}; | ||
this.addBuildEnvVars(env); | ||
|
||
const buildOptions = {env}; | ||
|
@@ -723,7 +731,7 @@ with Pulsar will be used.\ | |
const iteratee = async fn => await fn(); | ||
try { | ||
let installedPackagesInfo = await async.mapSeries(commands, iteratee); | ||
installedPackagesInfo = _.compact(installedPackagesInfo); | ||
installedPackagesInfo = installedPackagesInfo.filter(Boolean); | ||
installedPackagesInfo = installedPackagesInfo.filter((item, idx) => packageNames[idx] !== "."); | ||
if (options.argv.json) { console.log(JSON.stringify(installedPackagesInfo, null, " ")); } | ||
} catch (error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanting to add, since this one made me a bit nervous, I've tested to confirm this behavior will match what
underscore-plus
does on it's particular version.Ensuring that variables are overriden properly based on these news ones assigned. Smart use, and one of my favourites, of spread syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wanted to be sure and specifically checked MDN - it's guaranteed that what comes later overrides earlier values. I really share the mixed feelings here: if everybody knew this, probably we wouldn't fear it but still, it doesn't seem trivial...