Publishr
A tool for harmonious publishing of git and npm packages.
Publishr allows you to consistently publish different files in git and npm using an npm version workflow, which enables efficient installation from both types of repository.
Motivation
It can be troublesome to enable package installation from both npm and git repositories, especially when a project includes build steps. One inefficient publishing solution entails saving both source and compiled files to git and npm. Another less than ideal solution requires installing heavy build dependencies in production. Depending on the size of your repository, these solutions can be a burden for both development and production. Ideally, the git repository only contains source code and the npm repository contains compiled code. Furthermore, the npm repository should not contain any large build dependencies. Publishr solves these problems by tapping into npm's version/publish lifecycle scripts.
Installation
$ npm install publishrSetup
- Save all build dependencies to
package.jsonasdependencies. - Save placeholder (ex.
.someconfig.publishr) files that should be replaced in the npm repo. - Add a
publishrconfig topackage.json. - Use
publishr.dependenciesto describe which build dependencies to replace in the npm repo. - Use
publishr.filesto describe files to replace/create in the npm repo. - Use
publishr.scriptsto describe scripts to add/replace/remove in the npm repo. - Add
publishr postversionto npm's postversion script. - Add
publishr postpublishto npm's postpublish script.
Configuration
publishr.dependencies- Describes build dependencies to replace in the npm repo.- Takes an array of regular expression strings
["^babel$"]matches onlybabel["^babel"]matchesbabel,babel-core["babel"]matchesbabel,babel-core,is-babel
- Takes an array of regular expression strings
publishr.files- Describes files to replace/create in the npm repo.- Takes an object of oldFile keys to newFile values
{".npmignore": ".npmignore.publishr"}replaces/creates.npmignorewith.npmignore.publishr
- Takes an object of oldFile keys to newFile values
publishr.scripts- Describes files to add/replace/remove in the npm repo.- Takes an object of script name keys to command values.
{"hello": "echo hello"}adds/replaces the test scripthellowith the commandecho hello{"postinstall": ""}removes thepostinstallscript.
- Takes an object of script name keys to command values.
Publishing
- Run
publishr dry-runto test your configuration. - If the dry run fails, fix all errors and go back to
1. - Run your version command.
- Run your publish command.
Example
An example package.json file will look something like this:
{
"name": "some-neat-project",
"version": "0.0.1",
"dependencies": {
"lodash": "^4.0.0",
"babel-core": "^6.0.0"
},
"devDependencies": {
"eslint": "^1.0.0"
},
"scripts": {
"build": "gulp build",
"postinstall": "npm run build",
"postpublish": "publishr postpublish",
"postversion": "publishr postversion"
},
"publishr": {
"dependencies": ["^babel"],
"files": {
".npmignore": ".npmignore.publishr",
".someconfig": ".someconfig.publishr"
},
"scripts": {
"build": "echo 'No Build Needed'",
"extra": "echo 'Extra Script'",
"postinstall": ""
}
}
}The above configuration tells publishr to do a few things:
- Move all
dependenciesmatching the regular expression^babeltodevDependenciesbefore publishing to npm. - Replace
.npmignorewith the contents of.npmignore.publishrbefore publishing to npm. - Replace
.someconfigwith the contents of.someconfig.publishrbefore publishing to npm. - Replace the
buildscript withecho 'No Build Needed'before publishing to npm. - Add the
extrascript before publishing to npm. - Remove the
postinstallscript before publishing to npm.
The version command will look something like this:
$ npm version patchResult:
v0.0.2
> some-neat-project@0.0.2 postversion /some/path
> publishr postversionThe publish command will look something like this:
$ npm publishResult:
+ some-neat-project@0.0.2
> some-neat-project@0.0.2 postpublish /some/path
> publishr postpublish
When all is said and done, the git and npm repo will have different versions of package.json, .npmignore, and .someconfig. Your npm package will install as quickly as possible and you still support installing from a git repo.
Usage
Usage: publishr <command> [options]
Commands:
dry-run Perform a dry run of postversion and postpublish
postpublish Clean up any actions taken by postversion
postversion Create and overwrite files for publishing
Options:
-h, --help Show help [boolean]
-V, --verbose Log each step during postversion/postpublish [boolean]
-v, --version Show version number [boolean]