#!/usr/bin/env node const fs = require('fs') const path = require('path') const args = require('minimist')(process.argv.slice(2)) const oslo = require('../src/index') const OsloError = require('../src/osloError') class OsloCLI { constructor(opts) { this.context = {} this.eval(opts) } eval(opts) { if (opts.l) { this.context = JSON.parse(fs.readFileSync(this.absolutePath(opts.l))) } let output if (opts.f && opts.f !== true) { output = this.file(path.join(process.cwd(), opts.f)) } else if (opts.d && opts.d !== true) { if (!opts.o) { console.log( 'Output directory required. Invoke oslo with `-o path/to/output/directory`', ) process.exit() } output = this.directory( this.absolutePath(opts.d), this.absolutePath(opts.o), ) } else if (opts.e && opts.e !== true) { output = this.inline(opts.e) } else { this.printUsage() process.exit() } if (output.constructor == OsloError) { this.error(output) } if (opts.o) { this.output(opts, output) } else { process.stdout.write(output) process.stdout.write('\n') } } file(file) { const contents = fs.readFileSync(file).toString() return oslo(contents, this.context) } directory(directory) { let files = {} fs.readdirSync(directory).forEach(item => { const absolutePath = this.absolutePath(item, directory) if (fs.lstatSync(absolutePath).isDirectory()) { files = Object.assign(files, this.directory(absolutePath)) } else { let output = this.file(absolutePath) files[absolutePath] = output } }) return files } inline(source) { return oslo(source.toString(), this.context) } absolutePath(file, root = null) { if (!root) { root = process.cwd() } return path.join(root, file) } output(opts, output) { const absoluteOutputPath = this.absolutePath(opts.o) if (opts.f || opts.e) { fs.writeFile(absoluteOutputPath, output, function(err) { if (err) throw err }) } else if (opts.d) { const absoluteInputPath = this.absolutePath(opts.d) Object.keys(output).forEach(function(file) { const absoluteOutputFilePath = file .replace(absoluteInputPath, absoluteOutputPath) .replace(/\.oslo$/, '.html') const parentDirectory = path.dirname(absoluteOutputFilePath) if (!fs.existsSync(parentDirectory)) { fs.mkdirSync(parentDirectory) } fs.writeFile(absoluteOutputFilePath, output[file], function(err) { if (err) throw err console.log( `Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`, ) }) }) } } printUsage() { console.log( `USAGE -d The path to a directory. The directory will be recursively scanned. An output directory is required with this option. oslo -d templates -e Pass oslo code directly. oslo -e '(div "Hello world")' -f The path to a single file. oslo -f index.oslo -l The path to a json file to use as context for compilation oslo -f index.oslo -l data.json -o Where to direct the output. If no path is provided, output will be directed to stdout. When used with -f, the argument should be the path to a file. When used with -d, the argument should be the path to a directory, which will be created if it does not already exist. oslo -f index.oslo -o index.html oslo -d templates -o html`, ) } error(err) { console.log(err.message) process.exit() } } new OsloCLI(args)