Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 6, 2022
0 parents commit 17ceb31
Show file tree
Hide file tree
Showing 15 changed files with 3,861 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto eol=lf
20 changes: 20 additions & 0 deletions .github/workflows/package-size-report.yml
@@ -0,0 +1,20 @@
name: Package Size Report

on:
pull_request:
branches: [ master, develop ]

jobs:
pkg-size-report:
name: Package Size Report
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Package size report
id: pkg-size-report
uses: privatenumber/pkg-size-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
@@ -0,0 +1,34 @@
name: Release

on:
push:
branches: master

jobs:
release:
name: Release
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Install dependencies
run: npx ci

- name: Build
run: npm run build

- name: Lint
run: npm run lint

- name: Release
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,30 @@
name: Test

on:
push:
branches: [develop]
pull_request:
branches: [master, develop]

jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Install dependencies
run: npx ci

- name: Build
run: npm run build

- name: Lint
run: npm run lint
26 changes: 26 additions & 0 deletions .gitignore
@@ -0,0 +1,26 @@
# macOS
.DS_Store

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Dependency directories
node_modules/

# Output of 'npm pack'
*.tgz

# dotenv environment variables file
.env
.env.test

# VSCode
.vscode

# Distribution
dist
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
v16.16.0
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
117 changes: 117 additions & 0 deletions README.md
@@ -0,0 +1,117 @@
# git-publish

Publish your npm package to a Git branch.

<sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>

## Why?

For testing published packages without publishing to npm.

Making a prerelease to npm using `npm publish` has the following drawbacks:

- Needs a unique version for every release.

- Hard to remove due to npm's [strict unpublish policy](https://docs.npmjs.com/policies/unpublish).

- Cumbersome to verify the contents of the published package.

- Production environment—risky if you make a mistake (eg. accidentally publish as stable).

Using `npm link` has the drawback of not running [npm life cycle scripts](https://docs.npmjs.com/cli/v8/using-npm/scripts#life-cycle-scripts) and including non-publishable assets.


In contrast, `git-publish` has the following benefits:

- **Unversioned** Instead of versions, branch names are used. Branches can be updated to reflect latest change.

- **Deletable** Simply delete the branch when you're done with it.

- **Browsable** Use GitHub to verify the contents of the branch. You can even share a link for others to see.

- **Dev environment** Low risk of mistakes.

- **Simulates `npm publish`** Runs npm life cycle scripts and only includes publishable assets.

## Usage

Publish your npm package to a branch on the Git repository:

```sh
npx git-publish
```

> **⚠️ Warning:** This command will force-push to the remote branch `npm/<current branch>`. Make sure there are no unsaved changes there.

### Global install
Keep the command handy by installing it globally:

```sh
npm install -g git-publish
```

When globally installed, you can use it without `npx`:
```sh
git-publish
```

### Flags
| Flag | Description |
| - | - |
| `-b, --branch <branch name>` | The branch to publish the package to. Defaults to prefixing "npm/" to the current branch or tag name. |
| `-r, --remote <remote>` | The remote to push to. (default: `origin`) |
| `-d, --dry` | Dry run mode. Will not build, commit, or push to the remote. |
| `-h, --help` | Show help |
| `--version` | Show version |

## FAQ

### What are some use-cases where this is useful?
- When you want to test a new package that isn't ready to be published on npm.

- When you're contributing to an open source project so you don't have publish access, but want to test the changes in a production-like environment.

- When you want to test in a remote environment so you can't use `npm link`.

- When you want to avoid using `npm link` because of symlink complexities.


### How can I include a build step?

Call the build command it in the [`prepack` script](https://docs.npmjs.com/cli/v8/using-npm/scripts#:~:text=on%20npm%20publish.-,prepack,-Runs%20BEFORE%20a).

### What does this script do?

1. Run [npm hooks](https://docs.npmjs.com/cli/v8/using-npm/scripts) `prepare` & `prepack`
2. Create a temporary branch by prefixing the current branch with the `npm/` namespace
3. Detect and commit the [npm publish files](https://github.com/npm/npm-packlist)
4. Force push the branch to remote
5. Delete local branch from Step 2
6. Print the installation command for the branch

### How is this different from simply committing the files to a branch?

- There can be missing distribution files (eg. files outside of `dist`). _git-publish_ uses [npm-packlist](https://github.com/npm/npm-packlist) —the same library `npm publish` uses—to detect publish files declared 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 tooling.

- npm hooks are not executed. _git-publish_ simulates package packing and runs hooks `prepare` and `prepack`.

### Can I publish to and install from a private repository?

Yes, if using a Git client authorized to access the private repository.

If it must be publicly accessible, you can set the `--remote <remote>` flag to push the publish assets to a public repository. It's recommended to compile and minify the code if doing this with private code.


#### User story
You want to test a branch 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 can publish the branch to _Repo B_ to install it from there:

```sh
$ npx git-publish --remote git@github.com:repo-b.git --branch test-pkg

✔ Successfully published branch! Install with command:
→ npm i 'repo-b#test-pkg'
```
64 changes: 64 additions & 0 deletions package.json
@@ -0,0 +1,64 @@
{
"name": "git-publish",
"version": "0.0.0-semantic-release",
"description": "Publish your npm package to a GitHub repository branch",
"keywords": [
"npm",
"publish",
"git",
"github",
"branch",
"branches"
],
"license": "MIT",
"repository": "privatenumber/git-publish",
"funding": "https://github.com/privatenumber/git-publish?sponsor=1",
"author": {
"name": "Hiroki Osame",
"email": "hiroki.osame@gmail.com"
},
"files": [
"bin",
"dist"
],
"bin": "dist/index.js",
"scripts": {
"build": "pkgroll --minify --target=node12.19",
"lint": "eslint .",
"prepack": "npm run build",
"pretest": "npm run build",
"test": "tsx tests"
},
"dependencies": {
"yoga-layout-prebuilt": "^1.10.0"
},
"devDependencies": {
"@pvtnbr/eslint-config": "^0.26.2",
"@types/node": "^18.6.4",
"@types/npm-packlist": "^3.0.0",
"cleye": "^1.2.1",
"eslint": "^8.21.0",
"execa": "^6.1.0",
"fs-fixture": "^1.1.0",
"manten": "^0.2.1",
"npm-packlist": "^5.1.1",
"pkgroll": "^1.4.0",
"tasuku": "^2.0.0",
"tsx": "^3.8.0",
"typescript": "^4.7.4"
},
"eslintConfig": {
"extends": "@pvtnbr",
"rules": {
"@typescript-eslint/no-shadow": [
"error",
{
"allow": [
"task",
"setTitle"
]
}
]
}
}
}

0 comments on commit 17ceb31

Please sign in to comment.