recital / exporters / ink / cli.js
cli.js
Raw
#!/usr/bin/env node
/**
 * Copyright (c) 2022 Amorphous
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


const stageToInk = require('./dist/stage-ink').stageToInk
const argv = require('minimist')(process.argv.slice(2))
const fs = require('fs')

const pkg = require('./package.json')

const main = () => {
	if (argv.v || argv.version) {
		console.log(pkg.version)
		return
	}

	if (!argv._ || argv._.length === 0) {
		console.log(`Usage: stage-ink input-file.stage -o outputfile.ink
	`)
		return
	}

	const config = {}
	if (argv.stats || argv.s) {
		config.addStats = true
	}

	const results = []
	for (let i = 0; i < argv._.length; i++) {
		const file = argv._[i]
		const fileString = fs.readFileSync(file, 'utf-8')
		const inkString = stageToInk(fileString, config)

		results.push(inkString)
	}

	const str = results.join('\n')

	if (argv.o) {
		fs.writeFileSync(argv.o, str)
	} else {
		console.log(str)
	}
}

main()