123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #!/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)
|