๐Ÿ“ฆ himself65 / create-addon

๐Ÿ“„ README.md ยท 255 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255# create-addon

๐Ÿš€ Create Node.js native addons with ease

A modern CLI tool for scaffolding Node.js native addon projects with an interactive interface. Generate boilerplate code
for C++, Objective-C, and Swift addons with platform-specific templates.

## Features

- ๐ŸŽฏ **Interactive CLI** - Beautiful terminal UI built with React and Ink
- ๐Ÿ—๏ธ **Multiple Templates** - Support for C++, Objective-C, and Swift
- ๐Ÿ”ง **Platform-Specific** - Templates optimized for Linux, Windows, and macOS
- โšก **Modern N-API** - Uses `node-addon-api` for modern native bindings
- ๐ŸŽจ **EventEmitter Pattern** - Built-in event system for native-to-JS communication
- ๐Ÿ“ฆ **Ready to Build** - Pre-configured with node-gyp and build scripts

## Installation

```bash
npm install -g create-addon
```

Or use directly with npx:

```bash
npx create-addon my-project
```

## Quick Start

### Interactive Mode

Launch the interactive CLI to create a new project:

```bash
create-addon
```

### Semi-Interactive Mode

Provide a project name and select a template interactively:

```bash
create-addon my-awesome-addon
```

### Non-Interactive Mode

Create a project with all options specified:

```bash
create-addon my-awesome-addon --template cpp-linux
```

## Templates

### ๐Ÿง cpp-linux

C++ addon optimized for Linux platforms with GTK+ integration.

**Features:**

- GTK+ GUI components
- Event-driven architecture
- Todo management functionality
- Platform detection and safety checks

### ๐ŸชŸ cpp-win32

C++ addon designed for Windows platforms.

**Features:**

- Windows-specific APIs
- Native Win32 integration
- Event system for UI interactions
- Build configuration for Windows

### ๐ŸŽ objective-c

Objective-C addon for macOS with native Cocoa integration.

**Features:**

- Native macOS UI components
- Todo management with Objective-C classes
- Bridge between Objective-C and JavaScript
- macOS-specific functionality

### ๐Ÿฆ‰ swift

Swift addon for macOS with modern Swift integration.

**Features:**

- Modern Swift code
- Native Swift-to-JavaScript bridge
- Event-driven communication
- macOS platform optimization

## Usage Examples

### Creating Different Types of Addons

```bash
# Create a C++ addon for Linux
create-addon my-cpp-addon --template cpp-linux

# Create a Swift addon for macOS
create-addon my-swift-addon --template swift

# Create an Objective-C addon
create-addon my-objc-addon --template objective-c

# Create a Windows C++ addon
create-addon my-win-addon --template cpp-win32
```

### Skip Dependency Installation

```bash
create-addon my-project --skip-install
```

### Command Line Options

```bash
create-addon [project-name] [options]

Options:
  -t, --template <template>  Template to use (cpp-linux, cpp-win32, objective-c, swift)
  --skip-install            Skip installing dependencies
  -h, --help               Show help
```

## Generated Project Structure

Each generated project includes:

```
my-project/
โ”œโ”€โ”€ binding.gyp          # node-gyp build configuration
โ”œโ”€โ”€ package.json         # Project configuration and scripts
โ”œโ”€โ”€ src/                 # Native source code
โ”‚   โ”œโ”€โ”€ addon.cc         # Main addon implementation
โ”‚   โ””โ”€โ”€ ...              # Additional source files
โ”œโ”€โ”€ include/             # Header files
โ”‚   โ””โ”€โ”€ *.h              # Platform-specific headers
โ””โ”€โ”€ js/
    โ””โ”€โ”€ index.js         # JavaScript wrapper with EventEmitter
```

## Building Your Addon

After creating your project:

```bash
cd my-project

# Install dependencies
npm install

# Build the native addon
npm run build

# Clean build artifacts
npm run clean

# Build for Electron (if needed)
npm run build-electron
```

## Using Your Addon

Generated addons follow a consistent EventEmitter pattern:

```javascript
const addon = require("./js/index.js");

// Basic usage
console.log(addon.helloWorld("Hello from Node.js!"));

// GUI interaction (platform-specific)
addon.helloGui();

// Event handling
addon.on("todoAdded", (todo) => {
  console.log("New todo:", todo);
});

addon.on("todoUpdated", (todo) => {
  console.log("Updated todo:", todo);
});

addon.on("todoDeleted", (todo) => {
  console.log("Deleted todo:", todo);
});
```

## Development

### Prerequisites

- Node.js 16+
- Python 3.x (for node-gyp)
- Platform-specific build tools:
  - **Linux**: build-essential, GTK+ development libraries
  - **Windows**: Visual Studio Build Tools
  - **macOS**: Xcode Command Line Tools

### Building the CLI

```bash
# Clone the repository
git clone https://github.com/himself65/create-addon.git
cd create-addon

# Install dependencies
pnpm install

# Build the CLI
pnpm run build

# Test locally
node dist/bin.js
```

### Development Mode

```bash
# Build with watch mode
pnpm run dev
```

## Contributing

We welcome contributions! Please feel free to submit a Pull Request.

### Development Setup

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Build and test (`pnpm run build`)
5. Commit your changes (`git commit -m 'Add some amazing feature'`)
6. Push to the branch (`git push origin feature/amazing-feature`)
7. Open a Pull Request

## License

MIT ยฉ [himself65](https://github.com/himself65)

Credits to the Felix [electron-native-code-demos](https://github.com/felixrieseberg/electron-native-code-demos)
for inspiration and initial templates.