Skip to content

Commit 1a0aed9

Browse files
committed
docs: add README + some automation
1 parent b904455 commit 1a0aed9

File tree

3 files changed

+227
-3
lines changed

3 files changed

+227
-3
lines changed

README.md

Lines changed: 172 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,172 @@
1-
Test CLI Rust to publish to npm
1+
# Rust-NPM Package Template
2+
3+
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.
4+
5+
## 🚀 Features
6+
7+
- Seamless integration between Rust and Node.js
8+
- Automated setup process
9+
- Version management system
10+
- Pre-configured project structure
11+
- NPM package distribution ready
12+
- Automatic configuration of Cargo.toml and package.json
13+
14+
## 📦 Prerequisites
15+
16+
- Node.js (v20 or higher)
17+
- Rust (latest stable version)
18+
- Cargo (comes with Rust)
19+
- pnpm
20+
21+
## 🛠 Getting Started
22+
23+
1. Clone this repository:
24+
25+
```bash
26+
git clone [your-repository-url]
27+
cd [repository-name]
28+
```
29+
30+
2. Run the setup script:
31+
32+
```bash
33+
npm run setup
34+
```
35+
36+
During setup, you'll be prompted to provide:
37+
38+
- Package name
39+
- Description
40+
- Author information
41+
- Repository URL
42+
- Keywords (default: cli, rust)
43+
- License (default: MIT)
44+
45+
> **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.
46+
47+
## 📝 Project Structure
48+
49+
```
50+
.
51+
├── src/ # Rust source code
52+
├── scripts/ # JavaScript setup and utility scripts
53+
├── config.json # Project configuration
54+
├── Cargo.toml # Rust dependencies and configuration (auto-generated)
55+
└── package.json # NPM package configuration (auto-generated)
56+
```
57+
58+
## 🔄 Version Management & Release Process
59+
60+
1. Bump the version of your package:
61+
62+
```bash
63+
pnpm run bump
64+
```
65+
66+
This will:
67+
68+
- Increment the version in all necessary files
69+
- Update both Rust and NPM configurations
70+
- Stage the changes for commit
71+
72+
2. Commit and push the changes:
73+
74+
```bash
75+
git add .
76+
git commit -m "chore: bump version to x.x.x"
77+
git push
78+
```
79+
80+
3. Create and push a tag to trigger the release:
81+
82+
```bash
83+
git tag vx.x.x
84+
git push origin vx.x.x
85+
```
86+
87+
> **Note**: Replace `x.x.x` with your actual version number (e.g., v1.0.0)
88+
89+
## 🚀 CI/CD with GitHub Actions
90+
91+
This template comes with automated workflows for building, testing, and publishing your package:
92+
93+
### Automated Builds
94+
95+
The build workflow is triggered on:
96+
97+
- Every push to main branch
98+
- Pull requests to main branch
99+
- Manual trigger
100+
101+
The workflow:
102+
103+
1. Builds the Rust binary
104+
2. Runs tests
105+
3. Ensures code quality
106+
4. Creates build artifacts
107+
108+
### Publishing Process
109+
110+
The publish workflow is triggered when:
111+
112+
- A new tag matching the pattern `vx.x.x` is pushed
113+
- Example: `v1.0.0`, `v2.1.3`
114+
115+
The workflow automatically:
116+
117+
1. Builds the package for all supported platforms
118+
2. Runs the test suite
119+
3. Publishes to NPM with your configured credentials
120+
4. Creates a GitHub release with built artifacts
121+
122+
### Setting Up CI/CD
123+
124+
1. Add your NPM token to GitHub repository secrets:
125+
126+
- Go to your repository Settings > Secrets
127+
- Add a new secret named `NPM_TOKEN`
128+
- Paste your NPM access token
129+
130+
2. Enable GitHub Actions:
131+
- Go to your repository Settings > Actions
132+
- Ensure Actions are enabled for your repository
133+
134+
> **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.
135+
136+
## 🚀 Building
137+
138+
To build the Rust binary:
139+
140+
```bash
141+
cargo build
142+
```
143+
144+
For production release:
145+
146+
```bash
147+
cargo build --release
148+
```
149+
150+
## 📦 Publishing
151+
152+
1. Build your package:
153+
154+
```bash
155+
pnpm run build
156+
```
157+
158+
2. Test your package:
159+
160+
```bash
161+
pnpm test
162+
```
163+
164+
3. Publish to NPM:
165+
166+
```bash
167+
pnpm publish
168+
```
169+
170+
## 📄 License
171+
172+
This project is licensed under the MIT License - see the LICENSE file for details.

scripts/bump.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { execSync } from 'child_process';
2+
import { readFileSync, writeFileSync } from 'fs';
3+
import { resolve } from 'path';
4+
5+
function getCurrentVersion() {
6+
const configPath = resolve(__dirname, '../config.json');
7+
const config = JSON.parse(readFileSync(configPath, 'utf8'));
8+
return config.version;
9+
}
10+
11+
function bumpVersion() {
12+
try {
13+
// Run npm version patch to increment version
14+
execSync('pnpm version patch --no-git-tag-version');
15+
16+
// Get the new version from package.json
17+
const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
18+
const newVersion = packageJson.version;
19+
20+
// Update config.json
21+
const configPath = resolve(__dirname, '../config.json');
22+
const config = JSON.parse(readFileSync(configPath, 'utf8'));
23+
config.version = newVersion;
24+
writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');
25+
26+
// Update all configuration files
27+
require('./update-config');
28+
29+
// Git operations (local only)
30+
execSync('git add .');
31+
execSync(`git commit -m "chore: bump version to ${newVersion}"`);
32+
execSync(`git tag v${newVersion}`);
33+
34+
console.log(`✨ Successfully bumped version to ${newVersion}`);
35+
console.log('\nNext steps:');
36+
console.log('1. Review the changes');
37+
console.log('2. Push the changes:');
38+
console.log(' git push');
39+
console.log('3. Push the tag to trigger the release:');
40+
console.log(` git push origin v${newVersion}`);
41+
console.log(
42+
'\n🚀 Once pushed, GitHub Actions will handle the release process',
43+
);
44+
} catch (error) {
45+
console.error('❌ Error during version bump:', error.message);
46+
process.exit(1);
47+
}
48+
}
49+
50+
bumpVersion();

scripts/setup.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ async function setup() {
5151
console.log('\n✨ Setup complete! Your project is ready.');
5252
console.log(`\nNext steps:`);
5353
console.log(`1. Check the generated configuration files`);
54-
console.log(`2. Run \`cargo build\` to compile the Rust project`);
55-
console.log(`3. Use \`pnpm run bump\` to increment version when needed`);
54+
console.log(`2. Commit your changes when ready`);
55+
console.log(`3. Run \`cargo build\` to compile the Rust project`);
56+
console.log(
57+
`4. Use \`pnpm run bump\` when you need to release a new version`,
58+
);
5659

5760
rl.close();
5861
}

0 commit comments

Comments
 (0)