Skip to content

Commit

Permalink
docs: add README + some automation
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdchn committed Dec 20, 2024
1 parent b904455 commit 1a0aed9
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 3 deletions.
173 changes: 172 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,172 @@
Test CLI Rust to publish to npm
# Rust-NPM Package Template

This repository provides a template for creating NPM packages with Rust, allowing you to leverage Rust's performance and safety features while distributing your package through NPM.

## 🚀 Features

- Seamless integration between Rust and Node.js
- Automated setup process
- Version management system
- Pre-configured project structure
- NPM package distribution ready
- Automatic configuration of Cargo.toml and package.json

## 📦 Prerequisites

- Node.js (v20 or higher)
- Rust (latest stable version)
- Cargo (comes with Rust)
- pnpm

## 🛠 Getting Started

1. Clone this repository:

```bash
git clone [your-repository-url]
cd [repository-name]
```

2. Run the setup script:

```bash
npm run setup
```

During setup, you'll be prompted to provide:

- Package name
- Description
- Author information
- Repository URL
- Keywords (default: cli, rust)
- License (default: MIT)

> **Note**: You don't need to manually edit `Cargo.toml` or `package.json` files. The setup script automatically configures both files based on your inputs, ensuring perfect synchronization between Rust and Node.js configurations.
## 📝 Project Structure

```
.
├── src/ # Rust source code
├── scripts/ # JavaScript setup and utility scripts
├── config.json # Project configuration
├── Cargo.toml # Rust dependencies and configuration (auto-generated)
└── package.json # NPM package configuration (auto-generated)
```

## 🔄 Version Management & Release Process

1. Bump the version of your package:

```bash
pnpm run bump
```

This will:

- Increment the version in all necessary files
- Update both Rust and NPM configurations
- Stage the changes for commit

2. Commit and push the changes:

```bash
git add .
git commit -m "chore: bump version to x.x.x"
git push
```

3. Create and push a tag to trigger the release:

```bash
git tag vx.x.x
git push origin vx.x.x
```

> **Note**: Replace `x.x.x` with your actual version number (e.g., v1.0.0)
## 🚀 CI/CD with GitHub Actions

This template comes with automated workflows for building, testing, and publishing your package:

### Automated Builds

The build workflow is triggered on:

- Every push to main branch
- Pull requests to main branch
- Manual trigger

The workflow:

1. Builds the Rust binary
2. Runs tests
3. Ensures code quality
4. Creates build artifacts

### Publishing Process

The publish workflow is triggered when:

- A new tag matching the pattern `vx.x.x` is pushed
- Example: `v1.0.0`, `v2.1.3`

The workflow automatically:

1. Builds the package for all supported platforms
2. Runs the test suite
3. Publishes to NPM with your configured credentials
4. Creates a GitHub release with built artifacts

### Setting Up CI/CD

1. Add your NPM token to GitHub repository secrets:

- Go to your repository Settings > Secrets
- Add a new secret named `NPM_TOKEN`
- Paste your NPM access token

2. Enable GitHub Actions:
- Go to your repository Settings > Actions
- Ensure Actions are enabled for your repository

> **Note**: The build and publish processes are fully automated through GitHub Actions. Publishing is triggered by pushing a tag in the format `vx.x.x` - there's no need to manually publish to NPM.
## 🚀 Building

To build the Rust binary:

```bash
cargo build
```

For production release:

```bash
cargo build --release
```

## 📦 Publishing

1. Build your package:

```bash
pnpm run build
```

2. Test your package:

```bash
pnpm test
```

3. Publish to NPM:

```bash
pnpm publish
```

## 📄 License

This project is licensed under the MIT License - see the LICENSE file for details.
50 changes: 50 additions & 0 deletions scripts/bump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';

function getCurrentVersion() {
const configPath = resolve(__dirname, '../config.json');
const config = JSON.parse(readFileSync(configPath, 'utf8'));
return config.version;
}

function bumpVersion() {
try {
// Run npm version patch to increment version
execSync('pnpm version patch --no-git-tag-version');

// Get the new version from package.json
const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
const newVersion = packageJson.version;

// Update config.json
const configPath = resolve(__dirname, '../config.json');
const config = JSON.parse(readFileSync(configPath, 'utf8'));
config.version = newVersion;
writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');

// Update all configuration files
require('./update-config');

// Git operations (local only)
execSync('git add .');
execSync(`git commit -m "chore: bump version to ${newVersion}"`);
execSync(`git tag v${newVersion}`);

console.log(`✨ Successfully bumped version to ${newVersion}`);
console.log('\nNext steps:');
console.log('1. Review the changes');
console.log('2. Push the changes:');
console.log(' git push');
console.log('3. Push the tag to trigger the release:');
console.log(` git push origin v${newVersion}`);
console.log(
'\n🚀 Once pushed, GitHub Actions will handle the release process',
);
} catch (error) {
console.error('❌ Error during version bump:', error.message);
process.exit(1);
}
}

bumpVersion();
7 changes: 5 additions & 2 deletions scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ async function setup() {
console.log('\n✨ Setup complete! Your project is ready.');
console.log(`\nNext steps:`);
console.log(`1. Check the generated configuration files`);
console.log(`2. Run \`cargo build\` to compile the Rust project`);
console.log(`3. Use \`pnpm run bump\` to increment version when needed`);
console.log(`2. Commit your changes when ready`);
console.log(`3. Run \`cargo build\` to compile the Rust project`);
console.log(
`4. Use \`pnpm run bump\` when you need to release a new version`,
);

rl.close();
}
Expand Down

0 comments on commit 1a0aed9

Please sign in to comment.