From b11cf206ca3fbf37d7e70fab1de54a0cbd0fdd78 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Sat, 20 Jan 2018 10:20:32 -0500 Subject: [PATCH 1/2] Add test for restarting killed prettier_d This was reported in https://github.com/josephfrazier/prettier-diff/issues/1 --- test/test.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test.sh b/test/test.sh index 4e46cdb..f1512f6 100755 --- a/test/test.sh +++ b/test/test.sh @@ -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 From cedf512b4028b9643941a084ecdc67252c0880f2 Mon Sep 17 00:00:00 2001 From: Joseph Frazier <1212jtraceur@gmail.com> Date: Sat, 20 Jan 2018 09:37:14 -0500 Subject: [PATCH 2/2] Restart prettier_d if it was killed This fixes https://github.com/josephfrazier/prettier-diff/issues/1 --- bin/prettier-diff.js | 74 +++++++++++++++++++++++++++----------------- package.json | 1 + yarn.lock | 4 +++ 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/bin/prettier-diff.js b/bin/prettier-diff.js index 7111b6b..a1a53c1 100755 --- a/bin/prettier-diff.js +++ b/bin/prettier-diff.js @@ -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) @@ -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) } diff --git a/package.json b/package.json index 073e629..a85d6ac 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 39daed9..12d5c38 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"