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

Restart prettier_d if it was killed #2

Merged
merged 2 commits into from
Jan 20, 2018
Merged
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
74 changes: 45 additions & 29 deletions bin/prettier-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fse = require('fs-extra')
const sh = require('shell-tag')
const fugitSort = require('fugit')
const cp = require('child_process')
const isPortReachable = require('is-port-reachable')

const args = process.argv.slice(2)

Expand All @@ -23,36 +24,51 @@ const gitConfigPathBackup = gitConfigPath + '.prettier-diff'
fse.copySync(gitConfigPath, gitConfigPathBackup)

const prettierdDotfilePath = path.resolve(os.homedir(), '.prettier_d')
if (!fse.existsSync(prettierdDotfilePath)) {
const prettierdPath = path.resolve(
__dirname,
'..',
'node_modules',
'.bin',
'prettier_d'
)
sh`${prettierdPath} start`
}
isPrettierdReachable(prettierdDotfilePath).then(reachable => {
if (!reachable) {
const prettierdPath = path.resolve(
__dirname,
'..',
'node_modules',
'.bin',
'prettier_d'
)
sh`${prettierdPath} start`
}

try {
sh`git config diff.prettier.textconv ${textconvPath}`
const dotGitAttributesContent = fse.existsSync(dotGitAttributesPath)
? fse.readFileSync(dotGitAttributesPath).toString()
: ''
fse.appendFileSync(
gitAttributesPath,
`* diff=prettier\n${dotGitAttributesContent}`
)
try {
sh`git config diff.prettier.textconv ${textconvPath}`
const dotGitAttributesContent = fse.existsSync(dotGitAttributesPath)
? fse.readFileSync(dotGitAttributesPath).toString()
: ''
fse.appendFileSync(
gitAttributesPath,
`* diff=prettier\n${dotGitAttributesContent}`
)

const sortedArgs = fugitSort(
['diff', '--ignore-space-change', '--ignore-blank-lines'].concat(args)
)
cp.spawnSync('git', sortedArgs, {
stdio: 'inherit',
})
} catch (err) {
// nothing to do here
} finally {
fse.renameSync(gitAttributesPathBackup, gitAttributesPath)
fse.renameSync(gitConfigPathBackup, gitConfigPath)
}
})

const sortedArgs = fugitSort(
['diff', '--ignore-space-change', '--ignore-blank-lines'].concat(args)
function isPrettierdReachable(prettierdDotfilePath) {
return Promise.resolve(
fse.existsSync(prettierdDotfilePath) &&
isPortReachable(
Number.parseInt(
fse
.readFileSync(prettierdDotfilePath, { encoding: 'utf8' })
.split(' ')[0]
)
)
)
cp.spawnSync('git', sortedArgs, {
stdio: 'inherit',
})
} catch (err) {
// nothing to do here
} finally {
fse.renameSync(gitAttributesPathBackup, gitAttributesPath)
fse.renameSync(gitConfigPathBackup, gitConfigPath)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"fs-extra": "^4.0.1",
"fugit": "^1.1.0",
"is-port-reachable": "^2.0.0",
"json-align": "^0.1.0",
"json-stable-stringify": "^1.0.1",
"prettier_d": "https://github.com/josephfrazier/prettier_d.js",
Expand Down
7 changes: 7 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ function containsOnce () {
cat | grep "$1" | wc -l | grep '\<1$' >/dev/null
}

# Ensure that running prettier-diff restarts the prettier_d process if it was killed.
# If it doesn't, the subsequent tests will fail.
# https://github.com/josephfrazier/prettier-diff/issues/1
PORT=`cat ~/.prettier_d | cut -d" " -f1` # taken from textconv-prettier.sh
fuser -k $PORT/tcp # https://stackoverflow.com/questions/11583562/how-to-kill-a-process-running-on-particular-port-in-linux/11596144#11596144
./bin/prettier-diff.js

assertEmptyPrettierDiff test/js.js test/js.uglified.js
assertEmptyPrettierDiff test/json.json test/json.uglified.json
assertEmptyPrettierDiff test/xregexp.js test/xregexp.uglified.js
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ is-path-inside@^1.0.0:
dependencies:
path-is-inside "^1.0.1"

is-port-reachable@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-port-reachable/-/is-port-reachable-2.0.0.tgz#54d13d654917eb433ae3ee2dcbc3774f2cd44eb2"

is-promise@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa"
Expand Down