-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): ✨ add monorepo lib generator
- Loading branch information
Showing
5 changed files
with
132 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
10 changes: 5 additions & 5 deletions
10
packages/cli/assets/templates/monorepo-lib/package.json.hbs
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,111 @@ | ||
import { readdir, readFile, writeFile } from 'node:fs/promises' | ||
import { resolve } from 'node:path' | ||
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises' | ||
import { join, relative, resolve } from 'node:path' | ||
import { cwd } from 'node:process' | ||
|
||
import Handlebars from 'handlebars' | ||
|
||
import { Logger } from '@/utils' | ||
|
||
interface GenerateMonorepoLibOptions { | ||
name: string | ||
description: string | ||
packageName: string | ||
packageOrgName: string | ||
packageDescription: string | ||
org: string | ||
repo: string | ||
} | ||
|
||
interface PathInfo { | ||
type: 'dir' | 'file' | ||
path: string | ||
relativePath: string | ||
level: number | ||
} | ||
|
||
enum PathType { | ||
DIR = 'dir', | ||
FILE = 'file' | ||
} | ||
|
||
export const generateMonorepoLib = async (options: GenerateMonorepoLibOptions) => { | ||
const { name } = options | ||
const { packageName } = options | ||
const sourcePath = resolve(import.meta.dirname, '../assets/templates/monorepo-lib') | ||
const targetPath = resolve(cwd(), 'bit-ocean.config.js', '../packages', name) | ||
const targetPath = resolve(cwd(), 'bit-ocean.config.js', '../packages', packageName) | ||
|
||
try { | ||
const templatePaths = await readdir(sourcePath, { | ||
recursive: true | ||
}) | ||
|
||
const processTemplateFile = async (file: string) => { | ||
const templateContent = await readFile(file, 'utf-8') | ||
const renderedContent = Handlebars.compile(templateContent)(options) | ||
await writeFile(resolve(targetPath, file), renderedContent) | ||
const templatePaths = await getTemplatePaths(sourcePath) | ||
|
||
const processTemplateFile = async (p: PathInfo) => { | ||
const targetWritePath = resolve(targetPath, p.relativePath) | ||
let byteCount: number = 0 | ||
if (p.type === PathType.DIR) { | ||
await mkdir(targetWritePath) | ||
} else { | ||
const templateContent = await readFile(p.path, 'utf-8') | ||
const renderedContent = Handlebars.compile(templateContent)(options) | ||
await writeFile(targetWritePath, renderedContent) | ||
byteCount = Buffer.byteLength(renderedContent, 'utf-8') | ||
} | ||
Logger.success( | ||
`CREATED ${relative(cwd(), targetWritePath)}${byteCount ? ` (${byteCount} bytes)` : ''}` | ||
) | ||
} | ||
|
||
// TODO: Check if the target path already exists, and if so, ask the user if they want to overwrite it | ||
try { | ||
await rm(targetPath, { recursive: true }) | ||
} catch { | ||
// | ||
} | ||
|
||
await Promise.all(templatePaths.map(processTemplateFile)) | ||
await mkdir(targetPath) | ||
|
||
while (templatePaths.length) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await processTemplateFile(templatePaths.shift()!) | ||
} | ||
} catch (err) { | ||
Logger.error(err as Error) | ||
} | ||
} | ||
|
||
async function getTemplatePaths(rootPath: string) { | ||
const results: PathInfo[] = [] | ||
|
||
async function traverseDirectory(currentPath: string, level = 0) { | ||
try { | ||
const entries = await readdir(currentPath, { withFileTypes: true }) | ||
|
||
const entryPromise = entries.map(async (entry) => { | ||
const entryPath = join(currentPath, entry.name) | ||
const relativePath = relative( | ||
rootPath, | ||
entryPath.endsWith('.hbs') ? entryPath.slice(0, -4) : entryPath | ||
) | ||
|
||
if (entry.isDirectory()) { | ||
results.push({ | ||
type: PathType.DIR, | ||
path: entryPath, | ||
relativePath, | ||
level | ||
}) | ||
await traverseDirectory(entryPath, level + 1) | ||
} else { | ||
results.push({ | ||
type: PathType.FILE, | ||
path: entryPath, | ||
relativePath, | ||
level | ||
}) | ||
} | ||
}) | ||
|
||
await Promise.all(entryPromise) | ||
} catch (err) { | ||
Logger.error(err as Error) | ||
} | ||
} | ||
|
||
await traverseDirectory(rootPath) | ||
return results.toSorted((a, b) => a.level - b.level) | ||
} |