Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/fontaine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Or, with `yarn`
yarn add -D fontaine
```

## CLI usage

You can also run fontaine directly against a CSS file:

```bash
npx fontaine ./src/styles.css ./dist/styles.css
```

If the output file is omitted, fontaine writes to `<input>.fontaine.css`.

## Usage

```js
Expand Down
3 changes: 3 additions & 0 deletions packages/fontaine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"bin": {
"fontaine": "./dist/cli.mjs"
},
"files": [
"dist"
],
Expand Down
90 changes: 90 additions & 0 deletions packages/fontaine/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node
import type { FontaineTransformOptions } from './transform'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { basename, dirname, extname, resolve } from 'node:path'
import process from 'node:process'
import { pathToFileURL } from 'node:url'
import { FontaineTransform } from './transform'

/** Options for transforming a CSS file from the fontaine CLI or API. */
export interface FontaineCliOptions extends Partial<FontaineTransformOptions> {
/** CSS file to transform. */
input: string
/** Output file path. Defaults to `<input>.fontaine.css`. */
output?: string
}

/**
* Transform a CSS file with Fontaine and write the generated output.
*
* @returns The resolved output path.
*/
export async function transformCssFile({ input, output, ...options }: FontaineCliOptions): Promise<string> {
const inputPath = resolve(input)
const source = readFileSync(inputPath, 'utf8')
const plugin = FontaineTransform.rollup({
fallbacks: ['Arial'],
...options,
resolvePath: options.resolvePath || (id => pathToFileURL(resolve(dirname(inputPath), id)).toString()),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
})
const transformPlugin = Array.isArray(plugin) ? plugin[0]! : plugin
const transform = transformPlugin.transform
if (!transform || typeof transform === 'string' || typeof transform === 'function')
throw new Error('Fontaine transform hook is unavailable')

const transformed = await transform.handler.call({} as any, source, inputPath)
const code = typeof transformed === 'string' ? transformed : transformed?.code || source
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
const outputPath = output
? resolve(output)
: `${inputPath.slice(0, Math.max(0, inputPath.length - extname(inputPath).length))}.fontaine.css`

mkdirSync(dirname(outputPath), { recursive: true })
writeFileSync(outputPath, code)
return outputPath
}

/** Print command-line usage information. */
function printHelp() {
console.log(`Usage: fontaine <input.css> [output.css]

Transforms a CSS file and writes fallback font-face rules using fontaine.

Arguments:
input.css CSS file to transform
output.css Output path. Defaults to <input>.fontaine.css
`)
}

/** Run the Fontaine command-line interface. */
async function main() {
const [, , ...args] = process.argv

if (args.includes('-h') || args.includes('--help')) {
printHelp()
return
}

const [input, output] = args

if (!input) {
printHelp()
process.exitCode = 1
return
}

if (!existsSync(input)) {
console.error(`Input file not found: ${input}`)
process.exitCode = 1
return
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const outputPath = await transformCssFile({ input, output })
console.log(`Generated ${basename(outputPath)}`)
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
}
3 changes: 2 additions & 1 deletion packages/fontaine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export { transformCssFile } from './cli'
export type { FontaineCliOptions } from './cli'
export { generateFallbackName, generateFontFace } from './css'
export { DEFAULT_CATEGORY_FALLBACKS, type FontCategory, resolveCategoryFallbacks } from './fallbacks'
export type { ResolveCategoryFallbacksOptions } from './fallbacks'
export { getMetricsForFamily, readMetrics } from './metrics'

export { FontaineTransform } from './transform'
export type { FontaineTransformOptions } from './transform'
2 changes: 1 addition & 1 deletion packages/fontaine/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs'
import { defineConfig } from 'tsdown'

export default defineConfig({
entry: ['src/index.ts'],
entry: ['src/index.ts', 'src/cli.ts'],
format: ['es', 'cjs'],
dts: {
oxc: true,
Expand Down