recital / exporters / fountain / 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 stageToFountain = require('./dist/stage-fountain').stageToFountain
const argv = require('minimist')(process.argv.slice(2))
const fs = require('fs')
const pkg = require('./package.json')

const main = () => {
	if (argv.v) {
		console.log(pkg.version)
		return
	}
	if (!argv._ || argv._.length === 0) {
		console.log(
			'Usage: stage-fountain input-file.stage optional-file-2.stage -o outputfile.fountain'
		)
		return
	}

	let str = ''
	for (let file of argv._) {
		const fileString = fs.readFileSync(file, 'utf-8')
		const fountainString = stageToFountain(fileString)

		str += fountainString + '\n\n'
	}

	// make sure we end with one newline at the end.
	str = str.trim() + '\n'

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

main()