Skip to content

Commit

Permalink
feat: support remote url (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Feb 25, 2022
1 parent d3b71d2 commit ec0274d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
29 changes: 23 additions & 6 deletions README.md
Expand Up @@ -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.
Expand All @@ -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_:
Expand All @@ -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 <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'
```
11 changes: 9 additions & 2 deletions src/index.ts
Expand Up @@ -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;
Expand Down

0 comments on commit ec0274d

Please sign in to comment.