|
| 1 | +<div align="center"> |
| 2 | + <h1>packtory</h1> |
| 3 | + <p><i>Effortless code bundling and publishing for npm packages</i></p> |
| 4 | +</div> |
| 5 | + |
| 6 | +🚀 **Simplify and Automate Your Code Bundling and Publishing Workflow with packtory** 🚀 |
| 7 | + |
| 8 | +Tired of restrictive monorepo conventions? Fed up with complex workspace setups? Want your monorepo to feel as smooth as a single codebase, effortlessly referencing local files? Think semantic versioning (semver) adds unnecessary complexity, and every version should be treated as potentially breaking anyway? Look no further. |
| 9 | + |
| 10 | +Say goodbye to: |
| 11 | + |
| 12 | +- 🔗 Cumbersome workspaces |
| 13 | +- 📦 Dependency linking during development |
| 14 | +- 🙅♂️ Manual file selection (e.g. via `.npmignore` or `files`) |
| 15 | +- 📄 Shipping unnecessary files (e.g. build configs, tests) |
| 16 | +- 🔄 Manual versioning |
| 17 | + |
| 18 | +## 🌟 **Introducing packtory: Your Code Organization and Packaging Game Changer** 🌟 |
| 19 | + |
| 20 | +**Key Features:** |
| 21 | + |
| 22 | +- **Organize with Freedom**: Manage your monorepo without confining conventions or workspace limitations. packtory simplifies it, just like a single codebase. |
| 23 | +- **Effortless Dependency Bundling**: Forget manual dependency linking. packtory automatically detects and bundles dependencies, freeing you to focus on your code. |
| 24 | +- **Clean and Efficient Packaging**: Package only essential files, excluding devDependencies, CI configurations, and tests. Keep your npm package clean and efficient. |
| 25 | +- **Revolutionary Automatic Versioning**: Choose manual versioning or let packtory handle it. In automatic mode, it calculates versions intelligently, ensuring reproducibility without complexity. |
| 26 | +- **Seamless CI Pipeline Integration**: Easily integrate packtory into your CI pipelines for automatic publishing with every commit. No more intricate checks to decide what to publish. |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +The quickest way to leverage packtory is through its command-line interface (CLI). |
| 31 | + |
| 32 | +**Installation:** |
| 33 | + |
| 34 | +```bash |
| 35 | +npm install -D @packtory/cli |
| 36 | +``` |
| 37 | + |
| 38 | +**Configuration:** |
| 39 | + |
| 40 | +Create a configuration file named `packtory.config.js` in your project's root: |
| 41 | + |
| 42 | +```js |
| 43 | +import path from 'node:path'; |
| 44 | +import fs from 'node:fs'; |
| 45 | + |
| 46 | +export const config = { |
| 47 | + // Customize your registry settings, if needed |
| 48 | + registrySettings: { token: process.env.NPM_TOKEN }, |
| 49 | + |
| 50 | + // Common settings shared among packages |
| 51 | + commonPackageSettings: { |
| 52 | + sourcesFolder: path.join(process.cwd(), 'dist/'), |
| 53 | + mainPackageJson: fs.readFileSync('./package.json', { encoding: 'utf8' }) |
| 54 | + }, |
| 55 | + |
| 56 | + // Define your packages |
| 57 | + packages: [ |
| 58 | + { |
| 59 | + name: 'first-package', |
| 60 | + entryPoints: [ { js: 'first.js' } ], |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'second-package', |
| 64 | + entryPoints: [ { js: 'second.js' } ], |
| 65 | + entryPoints: , |
| 66 | + bundleDependencies: ['first'] |
| 67 | + } |
| 68 | + ] |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +**Publishing:** |
| 73 | + |
| 74 | +Execute the following command from the root of your project, no worries it runs in dry-run mode by default: |
| 75 | + |
| 76 | +``` |
| 77 | +npx packtory publish |
| 78 | +``` |
| 79 | + |
| 80 | +For more details about the CLI application have a look at the [full documentation](./source/packages/command-line-interface/readme.md). |
| 81 | + |
| 82 | +## Concept |
| 83 | + |
| 84 | +### Bundling |
| 85 | + |
| 86 | +Packtory guarantees minimal packages with: |
| 87 | + |
| 88 | +- No published `devDependencies` |
| 89 | +- No unnecessary files, including CI configurations |
| 90 | + |
| 91 | +**How Bundling Works:** |
| 92 | + |
| 93 | +1. All source files referenced from the entry point files are resolved into a graph. |
| 94 | +2. Imports of `node_modules` and node built-ins are detected and tracked to create a minimal `package.json` later. |
| 95 | +3. If bundle dependencies are given, some import statements will be rewritten. For example, if a file in package `first` imports a file in package `second`, the import statement will be rewritten accordingly (e.g., from `import bar from './bar.js'` to `import bar from 'second/bar.js'`). |
| 96 | +4. A `package.json` will be generated, and the version numbers of `node_modules` will be taken from the `mainPackageJson` provided in the configuration. |
| 97 | + |
| 98 | +### Publishing |
| 99 | + |
| 100 | +**How Publishing Works:** |
| 101 | + |
| 102 | +Packtory supports two versioning modes: |
| 103 | + |
| 104 | +1. **Automatic Versioning (Default) 🔄:** |
| 105 | + |
| 106 | + - Fetch the version details of the latest information available from the registry. |
| 107 | + - Download and extract the tarball of the latest version in-memory. |
| 108 | + - Compare the contents of all files from the downloaded tarball with the contents of all files resolved from the bundler: |
| 109 | + - If all files are the same, no new version is needed. |
| 110 | + - If there are any differences, increase the latest version number by one (patch version), generate a new `package.json`, create a tarball, and publish the new version. |
| 111 | + - If no version is available in the registry, an initial version will be built and published with version `0.0.1` (default but can be changed in the configuration). |
| 112 | + |
| 113 | +2. **Manual Versioning:** |
| 114 | + - Provide the exact version number in the configuration. |
| 115 | + |
| 116 | +This explanation provides a comprehensive overview of the bundling and publishing processes in Packtory. Feel free to make any further adjustments or let me know if there's anything specific you'd like to emphasize. |
| 117 | + |
| 118 | +## Configuration |
| 119 | + |
| 120 | +The configuration for `packtory` is an object with the following properties: |
| 121 | + |
| 122 | +1. **`registrySettings`** (Required): |
| 123 | + |
| 124 | + - An object with at least a required `token` for authentication. |
| 125 | + - Optionally, you can provide a custom `registryUrl` for non-default registries. |
| 126 | + |
| 127 | +2. **`commonPackageSettings`** (Optional): |
| 128 | + |
| 129 | + - Defines settings that can be shared for all packages. |
| 130 | + - Allowed settings: `sourcesFolder`, `mainPackageJson`, `includeSourceMapFiles`, `additionalFiles`, `additionalPackageJsonAttributes`. |
| 131 | + |
| 132 | +3. **`packages`** (Required, Array): |
| 133 | + |
| 134 | + - An array of per-package configurations. |
| 135 | + - Each per-package configuration has the following settings: |
| 136 | + |
| 137 | + - **`name`** (Required, String): |
| 138 | + |
| 139 | + - Must be unique; the name of the package. |
| 140 | + |
| 141 | + - **`sourcesFolder`** (Required): |
| 142 | + |
| 143 | + - The absolute path to the base folder of the source files. |
| 144 | + - All other file paths are resolved relative to this path. |
| 145 | + |
| 146 | + - **`mainPackageJson`** (Required): |
| 147 | + |
| 148 | + - The parsed content of the project's `package.json`. |
| 149 | + - Needed to obtain version numbers of third-party dependencies. |
| 150 | + |
| 151 | + - **`entryPoints`** (Required, Array of Objects): |
| 152 | + |
| 153 | + - An array of entry points with the following shape: `{ js: 'file.js', declarationFile: 'file.d.ts' }`. |
| 154 | + - The `js` property is required, while `declarationFile` is optional. |
| 155 | + |
| 156 | + - **`includeSourceMapFiles`** (Optional, Boolean, Default: `false`): |
| 157 | + |
| 158 | + - If `true`, the bundler will look for and include source map files in the final package. |
| 159 | + |
| 160 | + - **`additionalFiles`** (Optional, Array of File Descriptions): |
| 161 | + |
| 162 | + - An array to add additional files to the package that are not automatically resolved. |
| 163 | + - Example: `{ sourceFilePath: 'LICENSE', targetFilePath: 'LICENSE' }`. |
| 164 | + - If defined in both per-package and common settings, they are merged. |
| 165 | + |
| 166 | + - **`additionalPackageJsonAttributes`** (Optional, Object): |
| 167 | + |
| 168 | + - An object to be merged directly into the generated `package.json`. |
| 169 | + - Useful for setting meta properties like `description` or `keywords`. |
| 170 | + - If defined in both per-package and common settings, they are merged. |
| 171 | + |
| 172 | + - **`bundleDependencies`** (Optional, Array of Strings): |
| 173 | + |
| 174 | + - An array of package names to mark as dependencies, allowing the bundler to substitute import statements accordingly. |
| 175 | + |
| 176 | + - **`bundlePeerDependencies`** (Optional, Array of Strings): |
| 177 | + - Similar to `bundleDependencies` but represented as `peerDependencies` in the generated `package.json`. |
| 178 | + |
| 179 | +**Note**: Per-package settings override or merge with common settings when both are defined. |
| 180 | + |
| 181 | +This comprehensive configuration allows fine-tuning for individual packages and provides flexibility in defining dependencies and additional files. |
| 182 | + |
| 183 | +## Example Use-Cases |
| 184 | + |
| 185 | +### 1. Creating CLI Tools |
| 186 | + |
| 187 | +Suppose you have a project with a utility library (`image-resizer-lib`) and a corresponding CLI tool (`image-resizer-cli`) with bin entry points. `packtory` simplifies the bundling and publishing of these packages while ensuring clean and minimal npm packages. |
| 188 | + |
| 189 | +```javascript |
| 190 | +// packtory.config.js |
| 191 | +export const config = { |
| 192 | + registrySettings: { token: process.env.NPM_TOKEN }, |
| 193 | + commonPackageSettings: { |
| 194 | + sourcesFolder: path.join(process.cwd(), 'dist/'), |
| 195 | + mainPackageJson: fs.readFileSync('./package.json', { encoding: 'utf8' }) |
| 196 | + }, |
| 197 | + packages: [ |
| 198 | + { |
| 199 | + name: 'image-resizer-lib', |
| 200 | + entryPoints: [{ js: 'lib.js' }] |
| 201 | + }, |
| 202 | + { |
| 203 | + name: 'image-resizer-cli', |
| 204 | + entryPoints: [{ js: 'cli.js' }], |
| 205 | + bundleDependencies: ['image-resizer-lib'] |
| 206 | + } |
| 207 | + ] |
| 208 | +}; |
| 209 | +``` |
| 210 | + |
| 211 | +### 2. Managing Complex Dependencies |
| 212 | + |
| 213 | +Consider a scenario where you have an ecosystem of packages like `awesome-logger`, `awesome-logger-adapter`, and `awesome-logger-adapter-awesome-target`. `packtory` simplifies the bundling and publishing process, automatically managing dependencies between these packages. |
| 214 | + |
| 215 | +```javascript |
| 216 | +// packtory.config.js |
| 217 | +export const config = { |
| 218 | + registrySettings: { token: process.env.NPM_TOKEN }, |
| 219 | + commonPackageSettings: { |
| 220 | + sourcesFolder: path.join(process.cwd(), 'src/'), |
| 221 | + mainPackageJson: fs.readFileSync('./package.json', { encoding: 'utf8' }) |
| 222 | + }, |
| 223 | + packages: [ |
| 224 | + { |
| 225 | + name: 'awesome-logger', |
| 226 | + entryPoints: [{ js: 'index.js' }] |
| 227 | + }, |
| 228 | + { |
| 229 | + name: 'awesome-logger-adapter', |
| 230 | + entryPoints: [{ js: 'adapter.js' }], |
| 231 | + bundleDependencies: ['awesome-logger'] |
| 232 | + }, |
| 233 | + { |
| 234 | + name: 'awesome-logger-adapter-awesome-target', |
| 235 | + entryPoints: [{ js: 'target.js' }], |
| 236 | + bundleDependencies: ['awesome-logger', 'awesome-logger-adapter'] |
| 237 | + } |
| 238 | + ] |
| 239 | +}; |
| 240 | +``` |
| 241 | + |
| 242 | +These examples demonstrate how `packtory` adapts to different project structures and facilitates the efficient bundling and publishing of packages with varying dependencies. |
0 commit comments