Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created June 14, 2021 01:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/7291857132fd1f9a8d3279788dced747 to your computer and use it in GitHub Desktop.
Save reconbot/7291857132fd1f9a8d3279788dced747 to your computer and use it in GitHub Desktop.
Build and watch commands for esbuild and architect https://arc.codes
import { buildFunctions, findFunctions } from './lib-build'
async function run() {
const funcs = await findFunctions()
await buildFunctions(funcs)
}
run()
import { promisify } from 'util'
import { parse, join } from 'path'
import { build } from 'esbuild'
import globStandard from 'glob'
const glob = promisify(globStandard)
export async function findFunctions() {
const files = await glob('ts-src/**/*.{ts,js}')
const notTests = files.filter(filename => !filename.match('.test.ts$'))
return notTests.map(src => {
const { dir, name } = parse(src)
const dest = join(dir.replace(/^ts-src/, 'src'), `${name}.js`)
return {
src,
dest,
}
})
}
type AsyncFuncReturn<T> = T extends (...args: unknown[]) => Promise<infer U> ? U : never
export type FindFunctionReturn = AsyncFuncReturn<typeof findFunctions>
export async function buildFunctions(entries: FindFunctionReturn) {
const transforms = []
for (const entry of entries) {
console.log(`Building ${entry.src} -> ${entry.dest}`)
transforms.push(build({
entryPoints: [entry.src],
outfile: entry.dest,
bundle: true,
plugins: [],
platform: 'node',
minify: false,
external: ['aws-sdk'],
target: ['node14'],
}))
}
await Promise.all(transforms)
}
import sandbox from '@architect/sandbox'
import watch from 'node-watch'
import { buildFunctions, findFunctions } from './lib-build'
const outputs = new Set()
async function findAndBuild() {
outputs.clear()
const funcs = await findFunctions()
for (const { dest } of funcs) {
outputs.add(dest)
}
await buildFunctions(funcs)
}
const startWatch = () => watch(
'./',
{
recursive: true,
filter(file, skip) {
if (outputs.has(file)) {
return false
}
// skip node_modules
if (/\/node_modules/.test(file)) return skip
// skip .git folder
if (/\.git/.test(file)) return skip
// only watch for relevant files
return /\.(ts|js|graphql|arc|)$/.test(file)
},
},
(evt, name) => {
console.log(`${name} has changed re-building`)
findAndBuild().catch(e => {
console.error('Build Error', e)
})
},
)
async function run() {
await findAndBuild().catch(e => {
console.error('Build Error', e)
})
await sandbox.start()
startWatch()
}
run().catch(err => {
console.error(err)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment