From 7d2080346a21915bf2d461e1c9e94b0df15a5b65 Mon Sep 17 00:00:00 2001 From: Max O'Cull Date: Tue, 12 Mar 2019 23:04:13 -0400 Subject: [PATCH] Add docs, cleanup --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++++- index.js | 11 ++++++++--- package.json | 3 +-- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2de8a39..ac02938 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,44 @@ # openscad-format -An opinionated formatter for the OpenSCAD language. +A source code formatter for the OpenSCAD language. + +Currently it's opinionated, but there may be improvements in the future which +allow you to alter the code style it enforces. + +## Install +The utility is available on [npm](), you can install it like so: +``` +$ npm install -g openscad-format +``` +It packages `clang-format` with it for most platforms, so you don't need to +worry about installing it. + +## Use +`openscad-format` is designed to be simple and flexible to use: +``` +$ openscad-format --help +Usage: openscad-format [options] + +Options: + --version Show version number [boolean] + -i, --input Input file to read, file globs allowed (quotes recommended) + -o, --output Output file to write + -h, --help Show help [boolean] + +Examples: + openscad-format -i input.scad -o output.scad Formats input.scad and saves it as + output.scad + openscad-format < input.scad > output.scad Formats input.scad and saves it as + output.scad + openscad-format < input.scad Formats input.scad and writes to stdout + cat input.scad | openscad-format | less Formats input.scad and displays in less + openscad-format -i './**/*.scad' Formats all *.scad files recursively + and writes them to their respective + files + +This utility requires clang-format, but this is automatically installed for most +platforms. +``` + +## Contribute +Make sure your PR's pass the unit tests, are free of ESLint errors. To check, +run `npm run all` and it will guide you through what needs to be done. diff --git a/index.js b/index.js index acfca9b..a1f071d 100755 --- a/index.js +++ b/index.js @@ -16,13 +16,13 @@ const { argv } = require('yargs') .example('$0 -i \'./**/*.scad\'', 'Formats all *.scad files recursively and writes them to their respective files') .alias('i', 'input') .nargs('i', 1) - .describe('i', 'Input file to read, file globs allowed (qoutes recommended)') + .describe('i', 'Input file to read, file globs allowed (quotes recommended)') .alias('o', 'output') .nargs('o', 1) .describe('o', 'Output file to write') .help('h') .alias('h', 'help') - .epilog('This utility requies clang-format, but this is automatically installed for most platforms.'); + .epilog('This utility requires clang-format, but this is automatically installed for most platforms.'); tmp.setGracefulCleanup(); @@ -141,6 +141,7 @@ async function format(str, tmpDir) { return result; } catch (err) { console.error('Failure while formatting with Clang', err); + throw err; } } @@ -156,7 +157,8 @@ async function feed(input, output, tmpDir) { str = str.toString(); if (!str) { - console.warn(`Contents of ${input} is empty; skipping ...`); + // Do not write to output since we sometimes use stdout. + // console.warn(`Contents of ${input} is empty; skipping ...`); return ''; } @@ -178,6 +180,7 @@ async function feed(input, output, tmpDir) { throw new Error('Failed to format content string'); } catch (err) { console.error('Failed to feed to formatter and write output', err); + throw err; } } @@ -247,9 +250,11 @@ async function main(input, output) { return resultList; } catch (err) { console.error(err); + throw err; } } catch (err) { console.error(err); + throw err; } } diff --git a/package.json b/package.json index 659192a..a6e352f 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,8 @@ "scripts": { "lint": "eslint .", "fix": "eslint . --fix", - "cmd": "node index.js", "test": "mocha", - "demo": "node index.js -i source.scad" + "all": "eslint . --fix && mocha" }, "repository": { "type": "git",