From ec0274d0c17b8c1041c8e9ba3e15866644796933 Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Thu, 24 Feb 2022 22:18:25 -0500 Subject: [PATCH] feat: support remote url (#2) --- README.md | 29 +++++++++++++++++++++++------ src/index.ts | 11 +++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8d85e23..67e4c40 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,13 @@ npm install 'organization/repository#built-branch' Built branches are useful for quickly testing changes and can be preferrable over permanently publishing a prerelease to npm. ### When would I use this? -When you want to test install an npm package by publishing it to a GitHub branch before publishing it to npm. +When you want to test install an in-development npm package by publishing it to a GitHub branch instead of npm. -> _But you can publish private test packages on npm too!_ +> _But you can prepublish private test packages on npm too!_ Personally, I prefer to use GitHub + Git over npm for testing packages because I'll have more control with better UI/UX. -A _built branch_ is innately impermanent because it constantly gets force-pushed, and the branch can be easily deleted via commandline or GitHub.com. npm requires version bumping every test package and has a strict [Unpublish policy](https://docs.npmjs.com/policies/unpublish). +A _built branch_ is innately impermanent because it constantly gets force-pushed, and the branch can be easily deleted via commandline or GitHub UI. On top of that, it's easily sharable by link—to install or to look at the source. npm requires version bumping every test package, has a strict [Unpublish policy](https://docs.npmjs.com/policies/unpublish), and does not make it easy to view the code before installing. Use-cases: - When you want to test a new package that isn't ready to be published on npm. @@ -73,11 +73,10 @@ $ git commit -nam built $ git push ``` -However, this will not yield the same exact output as `npm publish`: -- There might be distribution files outside of `dist` (eg. multiple files at the package root). _build-this-branch_ uses [npm-packlist](https://github.com/npm/npm-packlist) —the same library `npm publish` uses—to detect publish files specified in `package.json#files`. +However, this will not yield the same exact output as `npm publish` because: +- There can be missing distribution files (eg. multiple files at the package root). _build-this-branch_ uses [npm-packlist](https://github.com/npm/npm-packlist) —the same library `npm publish` uses—to detect publish files specified via `package.json#files` and `.npmignore`. - Irrelevant files are committed (eg. source files). This can slow down installation or even interfere with the library behavior. For example, if your project has development configuration files, they can accidentally be read by the dependent. - ### What does this script do? This script does the following to make a _built branch_: @@ -88,3 +87,21 @@ This script does the following to make a _built branch_: 4. Force pushes up to remote 5. Deletes local built branch 6. Prints the installation command for the built branch + +### Can I install from a built branch hosted on a private repository? + +Yes, if it's being installed from a Git client that's authorized to access the private repository. + +However, if you're comfortable publishing the built assets to a public repository (given it's minified, mangled), you can use the `--remote ` flag to push to another repository that the client has access to (eg. public repository). + +#### User story +You want to test a built branch hosted on a private repository _Repo A_, but GitHub Actions on the consuming project _Repo B_ doesn't have access to the private repository so `npm install` fails. + +To work around this, you push the built branch to _Repo B_ to install it from there: + +```sh +$ npx build-this-branch --remote git@github.com:repo-b.git --branch test-pkg + +✔ Successfully built branch! Install with command: + → npm i 'repo-b#test-pkg' +``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 3eafa48..cb7d7d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -231,8 +231,15 @@ const { stringify } = JSON; } if (success) { - const { stdout } = await execa('git', ['remote', 'get-url', remote]); - const parsedGitUrl = stdout.match(/github\.com:(.+)\.git$/); + let remoteUrl = remote; + + // If "remote" is a git remote alias, resolve it to the actual remote URL + try { + const { stdout } = await execa('git', ['remote', 'get-url', remoteUrl]); + remoteUrl = stdout.trim(); + } catch {} + + const parsedGitUrl = remoteUrl.match(/github\.com:(.+)\.git$/); if (parsedGitUrl) { const [, repo] = parsedGitUrl;