-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yarn format
and rename child_process -> cp
This is a prime example of the type of commit that `prettier-diff` helps you ignore the noise of.
- Loading branch information
1 parent
a3ba442
commit 8cc0119
Showing
2 changed files
with
61 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
const prettier = require('prettier'); | ||
const stringify = require('json-stable-stringify'); | ||
const diff = require('diff'); | ||
const prettier = require('prettier') | ||
const stringify = require('json-stable-stringify') | ||
const diff = require('diff') | ||
|
||
// https://github.com/prettier/prettier/tree/a707dda53b13a6956a825609f30baead7ef08a59#api | ||
const defaultPrettierOptions = { | ||
printWidth: 80, | ||
tabWidth: 2, | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
bracketSpacing: true, | ||
}; | ||
bracketSpacing: true | ||
} | ||
|
||
module.exports = function( | ||
module.exports = function ( | ||
{ | ||
fromPath, | ||
toPath, | ||
fromContent, | ||
toContent, | ||
prettierOptions = defaultPrettierOptions, | ||
prettierOptions = defaultPrettierOptions | ||
} | ||
) { | ||
fromContent = fromContent.toString(); | ||
toContent = toContent.toString(); | ||
fromContent = fromContent.toString() | ||
toContent = toContent.toString() | ||
|
||
let fromPretty = fromContent; | ||
let toPretty = toContent; | ||
let fromPretty = fromContent | ||
let toPretty = toContent | ||
|
||
// try to format JS files | ||
try { | ||
fromPretty = prettier.format(fromContent, prettierOptions); | ||
toPretty = prettier.format(toContent, prettierOptions); | ||
fromPretty = prettier.format(fromContent, prettierOptions) | ||
toPretty = prettier.format(toContent, prettierOptions) | ||
} catch (err) {} | ||
|
||
// try to format JSON files | ||
// prettier doesn't do this currently: https://github.com/prettier/prettier/issues/322 | ||
try { | ||
fromPretty = jsonPrettify(fromContent); | ||
toPretty = jsonPrettify(toContent); | ||
fromPretty = jsonPrettify(fromContent) | ||
toPretty = jsonPrettify(toContent) | ||
} catch (err) {} | ||
|
||
const patch = diff.createTwoFilesPatch( | ||
fromPath, | ||
toPath, | ||
fromPretty, | ||
toPretty | ||
); | ||
) | ||
|
||
return { fromPretty, toPretty, patch }; | ||
}; | ||
return {fromPretty, toPretty, patch} | ||
} | ||
|
||
function jsonPrettify(jsonString) { | ||
const sorted = stringify(JSON.parse(jsonString), { space: 2 }); | ||
function jsonPrettify (jsonString) { | ||
const sorted = stringify(JSON.parse(jsonString), {space: 2}) | ||
// Put a comma after strings, numbers, objects, arrays, `true`, `false`, or | ||
// `null` at the end of a line. See the grammar at http://json.org/ | ||
return sorted.replace(/(["\d}\]el])$/gm, '$1,'); | ||
return sorted.replace(/(["\d}\]el])$/gm, '$1,') | ||
} |