Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/monoman. Manage monorepo common files.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import { glob, readFile, unlink, writeFile } from 'node:fs/promises'import { createRequire } from 'node:module'import path from 'node:path'import process from 'node:process'import { toArray } from '@antfu/utils'import consola from 'consola'import { loadConfig } from 'unconfig'import type { Config, Context } from './types'
export * from './types'
const require = createRequire(import.meta.url)const yaml = require('js-yaml')
export function defineConfig(config: Config): Config { return config}
export async function globAsync( include: string | string[], exclude: string[], cwd: string,): Promise<string[]> { const iter = glob(include, { exclude, cwd }) const matched: string[] = [] for await (const file of iter) { matched.push(path.resolve(cwd, file)) } return matched}
export async function run({ write = true, check,}: { check?: boolean write?: boolean}): Promise<void> { const { config, source } = await getConfig()
for (const item of Array.from(config)) { const files = await globAsync( item.include, toArray(item.exclude), path.dirname(source), )
for (const filePath of files) { if (check) consola.info(`Checking ${filePath}`)
const context: Context = { files, filePath }
const actual = await readFile(filePath, 'utf8').catch(() => null) let expected: string | null | undefined
if (item.type === 'text') { if (item.contents) expected = await item.contents(actual, context) } else if (item.type === 'json' && item.contents) { expected = `${JSON.stringify( await item.contents(actual ? JSON.parse(actual) : null, context), null, 2, )}\n` } else if (item.type === 'yaml' && item.contents) { expected = yaml.dump( await item.contents(actual ? yaml.load(actual) : null, context), item.dumpOptions, ) }
if (expected === actual) continue
if (check) { if (expected === null) { throw new TypeError(`File ${filePath} should be deleted but exists`) } else { consola.fatal( `File ${filePath} is not up to date, run with --write to update it.`, ) process.exit(1) } } else if (write) { if (expected === null) { // should be deleted await unlink(filePath) } else if (expected !== undefined) await writeFile(filePath, expected, 'utf8') } } }
if (check) consola.success('Check passed!') else if (write) consola.success('Files are up to date!')}
export async function getConfig(): Promise<{ config: Config source: string}> { const { config, sources } = await loadConfig<Config>({ defaults: [], sources: [ { files: ['monoman.config', 'mono.config'], extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''], }, ], }) return { config: Object.values(config), source: sources[0], }}