build-a-npm
is a robust and user-friendly CLI tool designed to simplify the creation, management, and publishing of Node.js packages. With an interactive setup, automatic version bumping, and seamless integration with npmjs.com and GitHub Packages, itβs the perfect companion for developers looking to streamline their package development workflow. π
π Overview
build-a-npm
is an open-source CLI tool that empowers developers to scaffold, manage, and publish Node.js packages effortlessly. It offers an interactive setup process, generates a modern project structure, and supports advanced features like automated version bumping, GitHub Actions for CI/CD, and TypeScript integration. Whether youβre a beginner creating your first package or an experienced developer maintaining a monorepo, build-a-npm
ensures a smooth and efficient workflow across Windows, macOS, and Linux. π
β¨ Features
- π§ Interactive Setup: Guided prompts for package details, including name, version, author, license, and more.
- π’ Automatic Version Bumping: Supports
patch
,minor
, andmajor
version increments with automatedpackage.json
updates. - π Dual Publishing: Publish to npmjs.com, GitHub Packages, or both with a single command.
- π€ GitHub Actions Integration: Generates workflows for automated publishing and documentation deployment.
- π Git Integration: Initializes a git repository and includes scripts for committing and pushing changes.
- π TypeScript Support: Optional TypeScript setup for modern JavaScript development.
- π Comprehensive File Generation: Creates essential files like
package.json
,index.js
,README.md
,.gitignore
,.npmignore
, and more. - π Package Upgrades: Updates existing packages to leverage the latest
build-a-npm
features without affecting custom code. - π Cross-Platform: Works seamlessly on Windows, macOS, and Linux.
- π GitHub Pages Support: Generates documentation and publishes it to GitHub Pages.
- π§ CI/CD Support: Templates for GitHub Actions, CircleCI, and GitLab CI.
π¦ Installation
Option 1: Install Globally
npm install -g build-a-npm
Run build-a-npm
commands directly from your terminal. π
Option 2: Use via npx
npx build-a-npm <command>
Execute the latest version without global installation. π
π Usage
build-a-npm
offers two primary commands: init
and upgrade
, along with a robust publishing workflow and documentation support.
π οΈ Creating a New Package
The init
command scaffolds a new Node.js package with a guided setup process.
npx build-a-npm init
What It Does:
- π Prompts for package details, including:
- Directory: Create a new directory (same as package name, custom, or none).
- Monorepo Support: Configure for monorepo setups with
npm
,pnpm
, oryarn
. - Publishing: Choose npmjs.com, GitHub Packages, or both.
- Access Level: Public or private package.
- GitHub Workflow: Optional GitHub Actions setup for CI/CD.
- GitHub Repository: Auto-create a GitHub repository.
- GitHub Pages: Enable documentation publishing.
- CI/CD Provider: GitHub Actions, GitLab CI, CircleCI, or none.
- Package Details: Name, version, description, author, license, keywords, etc.
- TypeScript/ESLint/Prettier: Optional configurations for modern development.
- Dependencies: Add dependencies and devDependencies with version control.
- Custom Scripts: Add custom
package.json
scripts.
- π Creates a project directory (if specified).
- π Generates essential files (see Generated Files).
- π Initializes a git repository (unless
--no-git
is used). - π€ Sets up CI/CD workflows (if enabled).
Optional Flags:
--no-git
: Skip git repository initialization.--sample
: Use predefined sample data to skip prompts.--verbose
: Display detailed logs.
Example:
npx build-a-npm init --sample --no-git
π Upgrading an Existing Package
The upgrade
command enhances existing packages with the latest build-a-npm
features.
npx build-a-npm upgrade
What It Does:
- β
Verifies
package.json
andbuild-a-npm
indevDependencies
. - π Updates
package.json
scripts and dependencies. - π Adds missing files (e.g.,
.npmrc
,.gitignore
,.npmignore
, CI/CD workflows). - π‘οΈ Preserves existing code and configurations.
π€ Publishing a Package
The publish.js
script manages version bumping and publishing to registries. The generated package.json
includes scripts for various publishing scenarios:
Script | Description |
---|---|
npm run publish |
Alias for publish:patch . |
npm run publish:patch |
Patch version bump (0.0.1 β 0.0.2 ) and Publish |
npm run publish:minor |
Minor version bump (0.1.0 β 0.2.0 ) and Publish |
npm run publish:major |
Major version bump (1.0.0 β 2.0.0 ) and Publish |
npm run pub |
Alias for publish:patch . |
npm run pub:pat |
Alias for publish:patch . |
npm run pub:min |
Alias for publish:minor . |
npm run pub:maj |
Alias for publish:major . |
npm run nogit:patch |
Patch bump and publish without git operations. |
npm run nogit:minor |
Minor bump and publish without git operations. |
npm run nogit:major |
Major bump and publish without git operations. |
npm run do |
Runs git , then publish:patch . |
npm run git |
Generates index.html , commits, and pushes to GitHub. |
npm run index |
Generates index.html for GitHub Pages. |
npm run format |
Formats code with Prettier. |
Example:
npm run publish:minor
This will:
- π’ Bump the minor version in
package.json
. - π€ Publish to npmjs.com and/or GitHub Packages.
- π Restore
package.json
(keeping the bumped version). - π Commit and push changes to git (if enabled).
- π Log publishing details via
publishConsole.js
.
π Publishing the Documentation
If GitHub Pages is enabled during init
, build-a-npm
generates a WEBPAGE.md
file to create an index.html
file for documentation.
Steps:
Enable GitHub Pages:
- During
init
, selectYes
for "Publish a Documentation on GitHub Pages?". - Provide a GitHub fine-grained Personal Access Token with
pages:write
scope.
- During
Generate Documentation:
npm run index
This runs
generateIndex.js
to createindex.html
from yourREADME.md
.Publish to GitHub Pages:
- The
.github/workflows/publish.yml
workflow (if enabled) automatically deploysindex.html
to GitHub Pages on pushes to themain
branch. - Alternatively, manually commit and push
index.html
to thegh-pages
branch.
- The
Requirements:
- A GitHub repository with Pages enabled.
- A fine-grained token with
pages:write
scope, added to.npmrc
or GitHub Secrets (GITHUB_PAGES_TOKEN
).
π Generated Files
build-a-npm
creates a comprehensive set of files to ensure a modern and functional package:
- π
package.json
: Defines metadata, scripts, dependencies, and publishing configuration. - π οΈ
index.js
: Main entry point with a sample implementation. - π
README.md
: Starter documentation with usage instructions. - π
LICENSE
: License file (MIT, ISC, Apache-2.0, GPL-3.0, or Unlicense). - π
.gitignore
: Ignores common Node.js files (e.g.,node_modules
). - π
.npmignore
: Specifies files to exclude from npm publishing. - π
.npmrc
: Configures GitHub Packages authentication (if enabled). - π€
.github/workflows/publish.yml
: GitHub Actions workflow for CI/CD (if enabled). - π
generateIndex.js
: Script to generateindex.html
for GitHub Pages (if enabled). - π§
.prettierrc
: Prettier configuration for code formatting (if enabled). - π
.eslintrc.json
: ESLint configuration for linting (if enabled). - π§ͺ
test.js
: Sample test file (if a testing framework is selected). - βοΈ
tsconfig.json
: TypeScript configuration (if TypeScript is enabled).
βοΈ Configuration
π npmjs.com Setup
- Create an npm account and log in:
npm login
- Add
NPM_TOKEN
to GitHub Secrets for automated publishing:- Go to
Settings > Secrets and variables > Actions > New repository secret
. - Add
NPM_TOKEN
with your npm token.
- Go to
- Run a publish command (e.g.,
npm run publish
).
π GitHub Packages Setup
- Create a GitHub Classic Personal Access Token with
repo
andwrite:packages
scopes. - During
init
, provide the token or add it to.npmrc
://npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
- Add
GITHUB_TOKEN
to GitHub Secrets. - Ensure the repository exists at
https://github.com/<username>/<repo-name>
.
π€ GitHub Actions for Auto-Publishing
- Add
NPM_TOKEN
andGITHUB_TOKEN
to GitHub Secrets. - The
.github/workflows/publish.yml
workflow runs on pushes tomain
, detecting version bump types (major
,minor
,patch
) from commit messages.
π Scripts in package.json
The package.json
includes a rich set of scripts for development and publishing:
{
"scripts": {
"publish": "node node_modules/build-a-npm/publish.js --patch --npmjs --github && npm run git && npm run console",
"publish:patch": "node node_modules/build-a-npm/publish.js --patch --npmjs --github && npm run git && npm run console",
"publish:minor": "node node_modules/build-a-npm/publish.js --minor --npmjs --github && npm run git && npm run console",
"publish:major": "node node_modules/build-a-npm/publish.js --major --npmjs --github && npm run git && npm run console",
"pub": "npm run publish:patch",
"pub:pat": "npm run publish:patch",
"pub:min": "npm run publish:minor",
"pub:maj": "npm run publish:major",
"nogit:patch": "node node_modules/build-a-npm/publish.js --patch --npmjs --github && npm run console",
"nogit:minor": "node node_modules/build-a-npm/publish.js --minor --npmjs --github && npm run console",
"nogit:major": "node node_modules/build-a-npm/publish.js --major --npmjs --github && npm run console",
"do": "npm run git && node publish.js --patch --github --npmjs && npm run console",
"git": "npm run index && git add . && git commit -m \"chore: updates\" && git branch -M main && (git remote get-url origin || git remote add origin https://github.com/hsinghhira/build-a-npm.git) && git push",
"index": "node generateIndex.js",
"console": "node node_modules/build-a-npm/lib/publishConsole.js",
"format": "prettier --write .",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
π§ Example Workflow
Initialize a Package:
npx build-a-npm init
Answer prompts to configure your package.
Install Dependencies:
npm install
Develop Your Package:
Editindex.js
to add your functionality.Format Code:
npm run format
Publish the Package:
npm run publish
Update and Republish:
npm run publish:minor
Publish Documentation (if enabled):
npm run index
π οΈ Troubleshooting
Problem | Solution |
---|---|
Permission Error | Run as Administrator (Windows) or use sudo (macOS/Linux). |
GitHub Token Issue | Ensure tokens have repo , write:packages , or pages:write scopes. |
Publish Failure | Verify network, NPM_TOKEN , and GITHUB_TOKEN in .npmrc or Secrets. |
Package Name Conflict | Check npm registry for existing names; use a unique or scoped name. |
Git Issues | Use --no-git or resolve git conflicts manually. |
π€ Contributing
We welcome contributions! To get involved:
- Visit the GitHub Repository.
- Open an issue for bugs or feature requests.
- Submit a pull request with your changes, following the repositoryβs guidelines.
π© Contact me
Feel free to contact me related to anything:
π Contact me
π License
MIT License
Copyright (c) 2025 Harman Singh Hira
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.