|
| 1 | +import { writeFile } from 'node:fs/promises' |
| 2 | +import { dirname, join } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +import layouts from '@metalsmith/layouts' |
| 6 | +import * as types from '@seamapi/types/connect' |
| 7 | +import type { Builder, Command, Describe, Handler } from 'landlubber' |
| 8 | +import Metalsmith from 'metalsmith' |
| 9 | +import { mkdirp } from 'mkdirp' |
| 10 | + |
| 11 | +import { |
| 12 | + blueprint, |
| 13 | + getHandlebarsPartials, |
| 14 | + handlebarsHelpers, |
| 15 | +} from '@seamapi/smith' |
| 16 | + |
| 17 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 18 | +interface Options {} |
| 19 | + |
| 20 | +export const command: Command = 'blueprint' |
| 21 | + |
| 22 | +export const describe: Describe = 'Generate content from blueprint' |
| 23 | + |
| 24 | +export const builder: Builder = { |
| 25 | + root: { |
| 26 | + type: 'string', |
| 27 | + default: 'test/fixtures/handlebars/partials', |
| 28 | + describe: 'TODO', |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +export const handler: Handler<Options> = async ({ logger }) => { |
| 33 | + const rootDir = dirname(fileURLToPath(import.meta.url)) |
| 34 | + |
| 35 | + await mkdirp('tmp') |
| 36 | + await writeFile(join('tmp', 'README.md'), Buffer.from('')) |
| 37 | + |
| 38 | + const partials = await getHandlebarsPartials('examples/layouts/partials') |
| 39 | + |
| 40 | + logger.info('Generating from blueprint') |
| 41 | + |
| 42 | + Metalsmith(rootDir) |
| 43 | + .source('../tmp') |
| 44 | + .destination('../tmp/dist') |
| 45 | + .clean(true) |
| 46 | + .use(blueprint({ types })) |
| 47 | + .use( |
| 48 | + layouts({ |
| 49 | + default: 'default.hbs', |
| 50 | + engineOptions: { |
| 51 | + noEscape: true, |
| 52 | + helpers: handlebarsHelpers, |
| 53 | + partials, |
| 54 | + }, |
| 55 | + }), |
| 56 | + ) |
| 57 | + .use((files: Metalsmith.Files, metalsmith: Metalsmith): void => { |
| 58 | + files['metadata.json'] = { |
| 59 | + contents: Buffer.from(JSON.stringify(metalsmith.metadata())), |
| 60 | + layout: 'default.hbs', |
| 61 | + } |
| 62 | + }) |
| 63 | + .build((err) => { |
| 64 | + if (err != null) throw err |
| 65 | + }) |
| 66 | +} |
0 commit comments