Created
August 26, 2020 05:34
-
-
Save davazp/e61660dc01bab5a16cd6c24603948b56 to your computer and use it in GitHub Desktop.
ninja.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Simplest and least ambitious task-orchestration for Node-based projects | |
https://ninja-build.org/manual.html | |
*/ | |
import { spawn } from "child_process"; | |
import { promises as fs } from "fs"; | |
import glob from "glob"; | |
import path from "path"; | |
let output = ` | |
build always: phony | |
`; | |
interface RuleDefinition { | |
name: string; | |
description: string; | |
command: string; | |
inputs?: string[]; | |
outputs?: string[]; | |
cwd?: string; | |
always?: boolean; | |
console?: boolean; | |
} | |
export function rule({ | |
name, | |
description, | |
command, | |
cwd, | |
inputs = [], | |
outputs = [], | |
always = false, | |
console = false, | |
}: RuleDefinition) { | |
const cd = cwd ? `cd ${cwd}` : "true"; | |
const marker = `.flow-build/${name}.marker`; | |
output += ` | |
rule ${name} | |
command = ( ${cd} && ( ${command} ) ) && touch $marker | |
description = "${description}" | |
build ${marker} ${outputs.join(" ")}: ${name} ${inputs.join(" ")} ${ | |
always ? "| always" : "" | |
} | |
marker = ${marker} | |
${console ? "pool = console" : ""} | |
`; | |
return marker; | |
} | |
export function files(pattern: string) { | |
return glob.sync(pattern); | |
} | |
export function build() { | |
fs.writeFile("build.ninja", output); | |
spawn("ninja", { stdio: "inherit" }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment