Last active
March 2, 2026 18:46
-
-
Save devhammed/b16abe0244b1bd40a86c5d6a4cc57e56 to your computer and use it in GitHub Desktop.
Adonis.js "buildFinished" hook that copies monorepo packages into the build folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { hooks } from '@adonisjs/core/app' | |
| import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises' | |
| import path from 'node:path' | |
| const PACKAGE_PREFIX = 'flowshub-' | |
| export default hooks.buildFinished(async (bundler) => { | |
| const repoRoot = path.resolve(bundler.cwdPath, '../../') | |
| const buildDir = path.join(bundler.cwdPath, 'build') | |
| const buildPackagesDir = path.join(buildDir, 'packages') | |
| const buildPkgPath = path.join(buildDir, 'package.json') | |
| const rawPackageJson = await readFile(buildPkgPath, 'utf8') | |
| const packageJson = JSON.parse(rawPackageJson) | |
| bundler.ui.logger.info('Bundling internal packages...') | |
| await rm(buildPackagesDir, { recursive: true, force: true }) | |
| await mkdir(buildPackagesDir, { recursive: true }) | |
| if (!packageJson.dependencies) { | |
| packageJson.dependencies = {} | |
| } | |
| for (const pkg of Object.keys(packageJson.dependencies)) { | |
| if (!pkg.startsWith(PACKAGE_PREFIX)) { | |
| continue | |
| } | |
| const shortName = pkg.slice(PACKAGE_PREFIX.length) | |
| const src = path.join(repoRoot, 'packages', shortName) | |
| const dest = path.join(buildPackagesDir, shortName) | |
| await mkdir(dest, { recursive: true }) | |
| await cp(path.join(src, 'package.json'), path.join(dest, 'package.json'), { | |
| force: true, | |
| }) | |
| await cp(path.join(src, 'dist'), path.join(dest, 'dist'), { | |
| recursive: true, | |
| force: true, | |
| }) | |
| packageJson.dependencies[pkg] = `file:./packages/${shortName}` | |
| } | |
| await writeFile(buildPkgPath, JSON.stringify(packageJson, null, 2)) | |
| bundler.ui.logger.success('Internal packages bundled successfully.') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment