-
Notifications
You must be signed in to change notification settings - Fork 29
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
macOS CLI script tweaks #1148
macOS CLI script tweaks #1148
Conversation
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.
Thanks for addressing my feedback! One of my comments here is opinion, but the other one should be addressed, I think.
src/modules/cli/lib/install-macos.ts
Outdated
const fileType = await getFileType( cliSymlinkPath ); | ||
|
||
// Avoid overwriting an existing file if it's not a symlink. | ||
if ( fileType === FileType.NonSymlink ) { | ||
throw new Error( InstallError.FileAlreadyExists ); |
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.
Studio isn't my project, so please take this as opinion beyond the scope of reviewing the shell script. :)
IMO these changes go beyond YAGNI and add unnecessary indirection. getFileType
isn't used anywhere else, nor is FileType
. There will be less code to maintain and fewer jumps required of the reader if we inline a throw/catch construct with the system call in plain sight.
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.
That's fair. I've inlined this logic
src/modules/cli/lib/install-macos.ts
Outdated
let message = __( 'Please ensure you grant Studio admin permissions when prompted.' ); | ||
if ( error instanceof Error && error.message === InstallError.FileAlreadyExists ) { | ||
message = sprintf( | ||
/* translators: 1: Installation path */ | ||
__( | ||
'The installation path %1$s is already occupied by a file or directory. Please remove it and try again.' | ||
), | ||
cliSymlinkPath | ||
); | ||
} | ||
|
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.
This fails to propagate legitimate errors occurred during execution of the sudo'd command. Try it yourself by exiting early from install-studio-cli.sh
with exit 1
, or even echoing an error and exiting with 1. The code assumes a failure will be due to permissions not being granted, but that can be verified by looking at error.message
:
Problem | error.message |
---|---|
Permission not granted | User did not grant permission. |
Non-zero exit with no echoed output | Command failed: /bin/sh "/Applications/Studio.app/Contents/Resources/bin/install-studio-cli.sh" |
Non-zero exit with echoed output | Command failed: /bin/sh "/Applications/Studio.app/Contents/Resources/bin/install-studio-cli.sh"\n Error: Foo |
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.
This is true, but I think it's fair to assume that most errors generated here will be due to missing permissions. Moreover, the logic in install-studio-cli.sh
is unlikely to generate errors (especially considering that we verify that cliSymlinkPath
is not occupied before running the script).
Still, I added some additional error message parsing here for completeness' sake, so we now also distinguish between known permission errors and unknown errors.
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.
# This script is used to install the Studio CLI on macOS. It creates a symlink at CLI_SYMLINK_PATH | ||
# (e.g. /usr/local/bin/studio) pointing to the packaged Studio CLI JS file at CLI_PACKAGED_PATH. |
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.
CLI_SYMLINK_PATH
and CLI_PACKAGED_PATH
are great names 👍
Similar to what I proposed ( CLI_SYMLINK_PATH / CLI_TARGET_PATH ), but CLI_PACKAGED_PATH
is much clear 👍
src/modules/cli/lib/install-macos.ts
Outdated
const installScriptPath = path.join( binPath, 'install-studio-cli.sh' ); | ||
|
||
enum InstallError { |
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.
Hm, it seems, I remember that either Tumblr or Dotcom or both didn't recommend using enums, due to tree shaking issues or something like that. Do you remember something about it? Or maybe I remember something incorrectly 😅
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.
Let's wrap strings with translation function.
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.
These error messages aren't displayed to users, so we shouldn't need to translate them. You're right that we don't need an enum, though. I've changed it to regular constants.
src/modules/cli/lib/install-macos.ts
Outdated
try { | ||
const stats = await lstat( cliSymlinkPath ); | ||
|
||
if ( ! stats.isSymbolicLink() ) { | ||
throw new Error( InstallError.FileAlreadyExists ); | ||
} | ||
} catch ( error ) { | ||
// File does not exist, do nothing | ||
} |
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.
Thanks for addressing my feedback in general.
I don't want to be the one delaying this PR any longer, so please use your best judgment here. I just wanted to point out, though, that — although they are unlikely — lstat
can potentially throw 13 different errors. So this empty catch
block seems a bit bold to me. This is what I meant earlier with propagating the error.
(Currently traveling back from a meetup, so I won't be very responsive.)
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.
Checking for ENOENT errors specifically is fair 👍 I've made that change
Suspect IssuesThis pull request was deployed and Sentry observed the following issues:
Did you find this useful? React with a 👍 or 👎 |
Related issues
Proposed Changes
ln
utility documentation, thesource
andtarget
terms refer to the opposite of what you'd expect (if you expect that "target" means link destination and "source" means symlink path). To avoid additional confusion, I've changed the terms to "symlink path" and "packaged path"./usr/local/bin/studio
command
prefixes.exec
to replace the shell process with the node process.Tip
An interesting piece of trivia is that the GNU
ln
utility apparently uses the opposite meaning for "target".Testing Instructions
Same as #1132
Pre-merge Checklist