theme.ts

Raw source
import fs from 'node:fs/promises'
import path from 'node:path'
import fsExtra from 'fs-extra'
import * as sass from 'sass'
import { discoverPackages, projectRoot } from './internal/build/workspaces'
import { defaultBuildConcurrency, mapWithConcurrency } from './internal/build/concurrency'
import { preparePackageReadme } from './internal/build/package-readme'
import { createConfiguredPackage } from './internal/build/package-output'
import { loadPackageConfiguration } from './internal/build/package-config'

await loadPackageConfiguration(projectRoot)

const args = process.argv.slice(2)
if (args.length > 1 || args.some((arg) => arg.startsWith('-'))) {
  throw new Error('Usage: yarn build:theme [nf-theme-chalk|nf-theme-oem|chalk|oem|@94ai-plus/<theme>]')
}
const requested = args[0]
const availableThemes = (await discoverPackages({ includeThemes: true })).filter(
  (pkg) => pkg.manifest.commonUiPlus?.kind === 'theme',
)
const themes = requested
  ? availableThemes.filter((pkg) => pkg.shortName === requested || pkg.manifest.name === requested || pkg.shortName === `nf-theme-${requested}`)
  : availableThemes

if (themes.length === 0) {
  throw new Error(`No theme package matched "${requested ?? ''}". Available themes: ${availableThemes.map((pkg) => pkg.shortName).join(', ')}.`)
}

const concurrency = defaultBuildConcurrency()
await mapWithConcurrency(themes, Math.min(2, concurrency), async (theme) => {
  const sourceDir = path.join(theme.dir, 'src')
  const distDir = path.join(theme.dir, 'dist')
  const cssDir = path.join(distDir, 'css')
  await fs.rm(distDir, { recursive: true, force: true })
  await fs.mkdir(cssDir, { recursive: true })
  const entries = (await fs.readdir(sourceDir)).filter(
    (name) => name.endsWith('.scss') && !name.startsWith('_'),
  )
  await mapWithConcurrency(entries, concurrency, async (entry) => {
    const result = await sass.compileAsync(path.join(sourceDir, entry), {
      loadPaths: [sourceDir, path.join(projectRoot, 'node_modules')],
      style: 'expanded',
      sourceMap: true,
    })
    const output = path.join(cssDir, entry.replace(/\.scss$/, '.css'))
    await fs.writeFile(output, result.css)
    await fs.writeFile(`${output}.map`, JSON.stringify(result.sourceMap))
  })
  await fsExtra.copy(sourceDir, path.join(distDir, 'scss'))
  // Preserve CSS-relative assets in both supported stylesheet entry points.
  for (const entry of await fs.readdir(sourceDir, { withFileTypes: true })) {
    if (entry.isDirectory()) {
      await fsExtra.copy(path.join(sourceDir, entry.name), path.join(cssDir, entry.name))
      await fsExtra.copy(path.join(sourceDir, entry.name), path.join(distDir, 'style', entry.name))
    }
  }
  await fsExtra.copy(
    path.join(cssDir, 'index.css'),
    path.join(distDir, 'style/index.css'),
  )
  await preparePackageReadme(theme)
  const mirror = path.join(projectRoot, 'lib', theme.shortName)
  await fs.rm(mirror, { recursive: true, force: true })
  await fsExtra.copy(distDir, path.join(mirror, 'dist'))
  await fsExtra.copy(theme.packageJsonPath, path.join(mirror, 'package.json'))
  await fsExtra.copy(path.join(theme.dir, 'README.md'), path.join(mirror, 'README.md'))
  await fsExtra.copy(path.join(theme.dir, 'LICENSE'), path.join(mirror, 'LICENSE'))
  await createConfiguredPackage(theme)
  console.log(`Built ${theme.manifest.name}`)
})