Terrace/repo/build.js
2023-02-11 22:30:10 -05:00

70 lines
2.1 KiB
JavaScript

import fs from 'node:fs/promises'
import { useDocument } from '@terrace/js'
import { createFileReader } from '@terrace/js/readers/node-readline'
function useHelpers({ next, level, head, tail }) {
async function kvObject(handleValue) {
const object = {}
const rootLevel = level()
while (await next(rootLevel)) {
if (!head()) continue
object[head()] = handleValue ? await handleValue(level()) : tail().trim()
}
return object
}
return { kvObject }
}
async function buildPackage() {
const { next, level, head, tail, match } = useDocument(createFileReader('./repo/package.tce'))
const { kvObject } = useHelpers({ next, level, head, tail })
const pkg = {}
while (await next()) {
if (match('name')) pkg.name = tail().trim()
if (match('version')) pkg.version = tail().trim()
if (match('license')) pkg.license = tail().trim()
if (match('type')) pkg.type = tail().trim()
if (match('private')) pkg.private = tail().trim() === 'true'
if (match('scripts')) pkg.scripts = await kvObject()
if (match('devDependencies')) pkg.devDependencies = await kvObject()
}
await fs.writeFile('./package.json', JSON.stringify(pkg, null, ' '))
}
async function buildTurbo() {
const { next, level, head, tail, match } = useDocument(createFileReader('./repo/turbo.tce'))
const { kvObject } = useHelpers({ next, level, head, tail })
const turbo = {}
while (await next()) {
if (match('#schema')) turbo['$schema'] = tail().trim()
if (match('pipeline')) turbo.pipeline = await (async () => {
const pipeline = {}
const rootLevel = level()
while (await next(rootLevel)) {
if (!head()) continue
const entry = pipeline[head()] = {}
const pipelineLevel = level()
while(await next(pipelineLevel)) {
if (match('dependsOn')) entry.dependsOn = tail().trim().split(' ')
if (match('outputs')) entry.outputs = tail().trim().split(' ')
}
}
return pipeline
})()
}
await fs.writeFile('./turbo.json', JSON.stringify(turbo, null, ' '))
}
buildPackage()
buildTurbo()