Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit with code EX_DATAERR if --list-different and formatting needed #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Format a stream from `stdin`:
- [ ] Add support for a `.luafmt` preferences file

## Testing
`lua-fmt` uses [jest](https://facebook.github.io/jest/) for automated testing.
`lua-fmt` uses [jest](https://facebook.github.io/jest/) for automated testing (`gulp test`).

Among the user-created tests in the `test/` folder, a copy of the `lua-5.3.4` tests are executed after formatting to ensure the code remains syntactically correct after formatting. For this reason, please do not modify the `lua-5.3.4-tests` folder unless updating with new tests from the official Lua tests. To run these tests, `lua53` is expected to be available on the `PATH`.

Expand Down
25 changes: 21 additions & 4 deletions bin/luafmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ program
.usage('[options] [file]')
.option('--stdin', 'Read code from stdin')
.option('-l, --line-width <width>', 'Maximum length of a line before it will be wrapped',
myParseInt, defaultOptions.lineWidth)
myParseInt, defaultOptions.lineWidth)
.option('-i, --indent-count <count>', 'Number of characters to indent', myParseInt, defaultOptions.indentCount)
.option('--use-tabs', 'Use tabs instead of spaces for indentation')
.option('-w, --write-mode <mode>', 'Mode for output', parseWriteMode, defaultOptions.writeMode);
.option('-w, --write-mode <mode>', 'Mode for output', parseWriteMode, defaultOptions.writeMode)
.option('-l, --list-different', 'Print if files are different from desired formatting.'
+ 'If there are differences the script errors out, which is useful in a CI scenario.');

program.parse(process.argv);

Expand All @@ -74,16 +76,30 @@ function printError(filename: string, err: Error) {
}
}


function exitWithCorrectExitCode(filename: string, originalDocument: string, formattedDocument: string,
options: UserOptions) {
if (options.listDifferent && originalDocument !== formattedDocument) {
console.error(`Change needed when formatting ${filename}`);
process.exit(65);
}
process.exit(0);
}

const customOptions: UserOptions = {
lineWidth: program.lineWidth,
indentCount: program.indentCount,
useTabs: program.useTabs,
writeMode: program.writeMode
writeMode: program.writeMode,
listDifferent: program.listDifferent
};

if (program.stdin) {
getStdin().then(input => {
printFormattedDocument('<stdin>', input, formatText(input, customOptions), customOptions);
const formatted = formatText(input, customOptions);

printFormattedDocument('<stdin>', input, formatted, customOptions);
exitWithCorrectExitCode('<stdin>', input, formatted, customOptions);
}).catch((err: Error) => {
printError('<stdin>', err);
});
Expand All @@ -107,6 +123,7 @@ if (program.stdin) {
const formatted = formatText(input, customOptions);

printFormattedDocument(filename, input, formatted, customOptions);
exitWithCorrectExitCode(filename, input, formatted, customOptions);
} catch (err) {
printError(filename, err);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lua-fmt",
"version": "2.6.0",
"version": "2.7.0",
"description": "Format Lua code",
"author": "trixnz",
"homepage": "https://github.com/trixnz/lua-fmt",
Expand Down
4 changes: 3 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Options {
linebreakMultipleAssignments: boolean;
quotemark: Quotemark;
writeMode: WriteMode;
listDifferent: boolean;
}

export type UserOptions = Partial<Options>;
Expand All @@ -24,7 +25,8 @@ export const defaultOptions: Options = {
useTabs: false,
linebreakMultipleAssignments: false,
quotemark: 'double',
writeMode: WriteMode.StdOut
writeMode: WriteMode.StdOut,
listDifferent: false
};

/** Returns the quotation mark to use from the provided option. */
Expand Down