Skip to content

Commit

Permalink
yarn format and rename child_process -> cp
Browse files Browse the repository at this point in the history
This is a prime example of the type of commit that `prettier-diff` helps
you ignore the noise of.
  • Loading branch information
josephfrazier committed Apr 3, 2017
1 parent a3ba442 commit 8cc0119
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
80 changes: 40 additions & 40 deletions bin/prettier-diff
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
#!/usr/bin/env node

const isGitRepo = require('is-git-repository').default;
const isPathGlobal = require('is-path-global');
const path = require('path');
const yargs = require('yargs');
const fs = require('fs');
const child_process = require('child_process');
const prettierDiff = require('../');
const tempWrite = require('temp-write');
const shell = require('shell-escape-tag').default;
const isGitRepo = require('is-git-repository').default
const isPathGlobal = require('is-path-global')
const path = require('path')
const yargs = require('yargs')
const fs = require('fs')
const cp = require('child_process')
const prettierDiff = require('../')
const tempWrite = require('temp-write')
const shell = require('shell-escape-tag').default

const args = process.argv.slice(2);
const hasGitArgs = args.length === 0 || !args.every(fs.existsSync);
const isDifftool = process.env.BASE;
const args = process.argv.slice(2)
const hasGitArgs = args.length === 0 || !args.every(fs.existsSync)
const isDifftool = process.env.BASE

// If we're in a git repo, but not already running as a difftool,
// run as a difftool. This allows simpler syntax like
// `prettier-diff head^ bin/prettier-diff`
//
// TODO figure out why the `hasGitArgs` check is necessary to pass tests
if (isGitRepo() && hasGitArgs && !isDifftool) {
const scriptName = process.argv[1];
const command = shell`git difftool --no-prompt --extcmd=${scriptName} ${args}`;
pipeLessSync(command, { stdio: 'inherit' });
process.exit();
const scriptName = process.argv[1]
const command = shell`git difftool --no-prompt --extcmd=${scriptName} ${args}`
pipeLessSync(command, {stdio: 'inherit'})
process.exit()
}

const map = (f, arr) => arr.map(x => f(x));
const map = (f, arr) => arr.map(x => f(x))

const [fromPath, toPath] = parseArgs();
const [fromContent, toContent] = map(fs.readFileSync, [fromPath, toPath]);
const [fromPath, toPath] = parseArgs()
const [fromContent, toContent] = map(fs.readFileSync, [fromPath, toPath])

const { fromPretty, toPretty } = prettierDiff({
const {fromPretty, toPretty} = prettierDiff({
fromPath,
toPath,
fromContent,
toContent,
});
toContent
})

const [fromTmp, toTmp] = map(tempWrite.sync, [fromPretty, toPretty]);
const [fromTmp, toTmp] = map(tempWrite.sync, [fromPretty, toPretty])

// If process.env.BASE is present, we're running as a git difftool,
// so use the correct paths. See https://git-scm.com/docs/git-difftool
const fromPathClean = process.env.BASE || fromPath;
const toPathClean = process.env.BASE || toPath;
const fromPathClean = process.env.BASE || fromPath
const toPathClean = process.env.BASE || toPath

try {
const diffOutput = child_process
const diffOutput = cp
.execSync(shell`git diff --color --no-index ${fromTmp} ${toTmp} | cat`)
.toString()
.replace(`--- a${fromTmp}`, `--- a/${fromPathClean}`)
.replace(`+++ b${toTmp}`, `+++ a/${toPathClean}`);
.replace(`+++ b${toTmp}`, `+++ a/${toPathClean}`)

const command = shell`${__dirname}/../node_modules/.bin/diff-so-fancy`;
const command = shell`${__dirname}/../node_modules/.bin/diff-so-fancy`

pipeLessSync(command, {
input: diffOutput,
stdio: ['pipe', 'inherit', 'inherit'],
});
stdio: ['pipe', 'inherit', 'inherit']
})
} catch (err) {
process.exitCode = err.status;
process.exitCode = err.status
} finally {
map(fs.unlinkSync, [fromTmp, toTmp]);
map(fs.unlinkSync, [fromTmp, toTmp])
}

function pipeLessSync(command, options) {
command += ' | less --tabs=2 -RFX';
child_process.execSync(command, options);
function pipeLessSync (command, options) {
command += ' | less --tabs=2 -RFX'
cp.execSync(command, options)
}

function parseArgs() {
function parseArgs () {
// yargs shows the full path to binaries that are in $PATH,
// so work around that by substituting the basename if possible.
// See https://github.com/yargs/yargs/issues/538
let scriptName = process.argv[1];
let scriptName = process.argv[1]
if (isPathGlobal(scriptName)) {
scriptName = path.basename(scriptName);
scriptName = path.basename(scriptName)
} else {
scriptName = '$0';
scriptName = '$0'
}

return yargs
Expand All @@ -92,5 +92,5 @@ function parseArgs() {
2,
'Not enough arguments: got $0, need exactly 2',
'Too many arguments: got $0, need exactly 2'
).argv._;
).argv._
}
42 changes: 21 additions & 21 deletions index.js
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,')
}

0 comments on commit 8cc0119

Please sign in to comment.