Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

feat: make a wrapper script for Lerna that autodetects the right remote #638

Merged
merged 1 commit into from
Sep 2, 2020

Conversation

wincent
Copy link
Contributor

@wincent wincent commented Sep 2, 2020

As I discovered in my first attempt at publishing the JS toolkit yesterday, Lerna will push to your "origin" remote, which in my case was my fork; ie. my git remote -v output is:

origin	git@github.com:wincent/liferay-js-toolkit.git (fetch)
origin	git@github.com:wincent/liferay-js-toolkit.git (push)
upstream	https://github.com/liferay/liferay-js-toolkit.git (fetch)
upstream	https://github.com/liferay/liferay-js-toolkit.git (push)
upstream-rw	git@github.com:liferay/liferay-js-toolkit.git (fetch)
upstream-rw	git@github.com:liferay/liferay-js-toolkit.git (push)

This commit adds a wrapper that uses this pipeline to try and pick the right remote automatically:

  • git remote -v: list all remotes.
  • grep 'github.com[:/]liferay/': Pick out "liferay" ones.
  • grep '(push)': Ignore "(fetch)" URLs, look at "(push)" URLs.
  • sort -k2: Sort by column 2 (URL); this prioritizes SSH URLs.
  • head -1: Grab winner.
  • cut -f1: Print the name (ie. "upstream-rw").

We prefer SSH URLs because they are more likely to be the preferred URLs for write access.

Updated the docs accordingly.

Test plan:

  1. Prepended "echo" to those "yarn" commands so that I could run without
    actually publishing.

  2. Run "yarn release"; see it print:

    yarn run lerna --help --force-publish=* --exact --git-remote=upstream-rw
    
  3. Edit then "grep" to search for "liferayz" instead of "liferay"; see:

    warning: could not locate a "liferay" remote
    Proceed using "origin" remote? n
    Aborted.
    
  4. Repeat, but this time answer "y":

    warning: could not locate a "liferay" remote
    Proceed using "origin" remote? y
    yarn run lerna --force-publish=* --exact
    

As I discovered in my first attempt at publishing the JS toolkit
yesterday, Lerna will push to your "origin" remote, which in my case was
my fork; ie. my `git remote -v` output is:

    origin	git@github.com:wincent/liferay-js-toolkit.git (fetch)
    origin	git@github.com:wincent/liferay-js-toolkit.git (push)
    upstream	https://github.com/liferay/liferay-js-toolkit.git (fetch)
    upstream	https://github.com/liferay/liferay-js-toolkit.git (push)
    upstream-rw	git@github.com:liferay/liferay-js-toolkit.git (fetch)
    upstream-rw	git@github.com:liferay/liferay-js-toolkit.git (push)

This commit adds a wrapper that uses this pipeline to try and pick the
right remote automatically:

-   `git remote -v`: list all remotes.
-   `grep 'github.com[:/]liferay/'`: Pick out "liferay" ones.
-   `grep '(push)'`: Ignore "(fetch)" URLs, look at "(push)" URLs.
-   `sort -k2`: Sort by column 2 (URL); this prioritizes SSH URLs.
-   `head -1`: Grab winner.
-   `cut -f1`: Print the name (ie. "upstream-rw").

We prefer SSH URLs because they are more likely to be the preferred URLs
for write access.

Updated the docs accordingly.

Test plan:

1.  Prepended "echo" to those "yarn" commands so that I could run without
    actually publishing.

2.  Run "yarn release"; see it print:

    ```
    yarn run lerna --help --force-publish=* --exact --git-remote=upstream-rw
    ```

3.  Edit then "grep" to search for "liferayz" instead of "liferay"; see:

    ```
    warning: could not locate a "liferay" remote
    Proceed using "origin" remote? n
    Aborted.
    ```

4.  Repeat, but this time answer "y":

    ```
    warning: could not locate a "liferay" remote
    Proceed using "origin" remote? y
    yarn run lerna --force-publish=* --exact
    ```
@wincent
Copy link
Contributor Author

wincent commented Sep 2, 2020

Forgot to add: --git-remote was added to Lerna in this PR.

And also, I originally wrote this as a JS script, but switched to Bash when I released you can't do a real exec in Node (ie. replace running process with another one, in order for things like interactive prompts and unbuffered output to work nicely).

@izaera
Copy link
Member

izaera commented Sep 2, 2020

I would migrate it to JS so that it is multiplatform if it is not too much work. In the past we had unix scripts and people had problems contributing to the repo so I now do everything in pure JS and slowly migrate remaining scripts.

In this case, if it is too much work, we can leave it like this as it is only for release and only @wincent and me publish releases.

Also, I would have to test it in Linux, given that some commands differ between Mac (~BSD) and Linux. I'm specifically worried about the cut though I think it will work, but who knows 🙄 ... But I can leave that test for the next release :-)

@izaera
Copy link
Member

izaera commented Sep 2, 2020

Sorry , I didn't read this:

And also, I originally wrote this as a JS script, but switched to Bash when I released you can't do a real exec in Node (ie. replace running process with another one, in order for things like interactive prompts and unbuffered output to work nicely).

So I guess we can merge 🤷

@wincent
Copy link
Contributor Author

wincent commented Sep 2, 2020

I would migrate it to JS so that it is multiplatform if it is not too much work. In the past we had unix scripts and people had problems contributing to the repo so I now do everything in pure JS and slowly migrate remaining scripts.

Yep, it's a good practice, and why I started in JS. But I had the same thought you did that the only people who will run yarn release are us (Linux and macOS users), so hopefully a Bash script won't hurt here.

Tested on Linux (well, on Amazon Linux):

[ec2-user@masochist ~]$ cd /tmp
[ec2-user@masochist tmp]$ mkdir foobar
[ec2-user@masochist tmp]$ cd foobar
[ec2-user@masochist foobar]$ git init
Initialized empty Git repository in /tmp/foobar/.git/
[ec2-user@masochist foobar]$ git remote add origin git@github.com:wincent/liferay-js-toolkit.git
[ec2-user@masochist foobar]$ git remote add upstream https://github.com/liferay/liferay-js-toolkit.git
[ec2-user@masochist foobar]$ git remote add upstrream git@github.com:liferay/liferay-js-toolkit.git
[ec2-user@masochist foobar]$ git remote -v
origin	git@github.com:wincent/liferay-js-toolkit.git (fetch)
origin	git@github.com:wincent/liferay-js-toolkit.git (push)
upstream	https://github.com/liferay/liferay-js-toolkit.git (fetch)
upstream	https://github.com/liferay/liferay-js-toolkit.git (push)
upstrream	git@github.com:liferay/liferay-js-toolkit.git (fetch)
upstrream	git@github.com:liferay/liferay-js-toolkit.git (push)
[ec2-user@masochist foobar]$ REMOTE=$(git remote -v | grep 'github.com[:/]liferay/' | grep '(push)' | sort -k2 | head -1 | cut -f1)
[ec2-user@masochist foobar]$ echo $REMOTE
upstrream

@wincent
Copy link
Contributor Author

wincent commented Sep 2, 2020

The upstrream typo is proof that I didn't fake the test.

@wincent
Copy link
Contributor Author

wincent commented Sep 2, 2020

Going to merge this and try it out. The only way to do a real end-to-end test is to actually cut a release, which we need to do anyway.

@wincent wincent merged commit 71e28a0 into master Sep 2, 2020
@wincent wincent deleted the wincent/auto-remote branch September 2, 2020 12:35
wincent added a commit that referenced this pull request Sep 2, 2020
As noted here:

#638 (comment)

> The only way to do a real end-to-end test is to actually cut a
> release, which we need to do anyway.

And indeed, when we try it, we see:

    ERR! lerna A command is required. Pass --help to see all available
    commands and options.
@wincent
Copy link
Contributor Author

wincent commented Sep 2, 2020

Needed this little hotfix. 😀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants