From cad21c484b6814517861214211dfeb8a520e3fe6 Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 14:35:21 +0300 Subject: [PATCH 1/9] feat: Add --update and --watch options --- bin.js | 31 +++++++++++++++--- package.json | 7 +++- update-and-show-snapshots.js | 19 +++++++++++ watch-and-show-snapshots.js | 27 ++++++++++++++++ yarn.lock | 62 +++++++++++++++++++++++++++++++++--- 5 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 update-and-show-snapshots.js create mode 100644 watch-and-show-snapshots.js diff --git a/bin.js b/bin.js index d60780f..688b19a 100755 --- a/bin.js +++ b/bin.js @@ -3,9 +3,13 @@ let chalk = require('chalk') let showSnapshots = require('./show-snapshots') +let updateAndShowSnaphots = require('./update-and-show-snapshots') +let watchAndShowSnaphots = require('./watch-and-show-snapshots') let showVersion = require('./show-version') let showHelp = require('./show-help') +let cwd = process.cwd() + function error (message) { process.stderr.write(chalk.red(message) + '\n') } @@ -16,17 +20,36 @@ function print (...lines) { async function run () { let arg = process.argv[2] || '' + let filter = process.argv[3] || '' + if (arg === '--version') { showVersion(print) - } else if (arg === '--help') { + return + } + + if (arg === '--help') { showHelp(print) - } else if (arg.startsWith('--')) { + return + } + + if (arg === '--update') { + await showSnapshots(print, cwd, filter) + return + } + + if (arg === '--watch') { + (await watchAndShowSnaphots(print, cwd, filter)) + .on('error', err => error(err.stack || err)) + return + } + + if (arg.startsWith('--')) { error(`Unknown argument ${ arg }\n`) showHelp(print) process.exit(1) - } else { - await showSnapshots(print, process.cwd(), arg) } + + await updateAndShowSnaphots(print, cwd, arg) } run().catch(e => { diff --git a/package.json b/package.json index 373fa1a..665e78d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ }, "dependencies": { "chalk": "^3.0.0", - "globby": "^11.0.0" + "chokidar": "^3.3.1", + "globby": "^11.0.0", + "parse-gitignore": "^1.0.1" }, "author": "Andrey Sitnik ", "license": "MIT", @@ -61,5 +63,8 @@ "CLI", "backtick" ] + }, + "peerDependencies": { + "jest": "^25.1.0" } } diff --git a/update-and-show-snapshots.js b/update-and-show-snapshots.js new file mode 100644 index 0000000..58c8637 --- /dev/null +++ b/update-and-show-snapshots.js @@ -0,0 +1,19 @@ +let { exec } = require('child_process') +let { promisify } = require('util') +let chalk = require('chalk') + +let showSnapshots = require('./show-snapshots') + +let execCommand = promisify(exec) + +module.exports = async function updateAndShowSnaphots (print, cwd, filter) { + print(chalk.blue('Updating snapshots... \n')) + + await execCommand('"./node_modules/.bin/jest" -u', { + cwd + }) + + print(chalk.green('Snapshots updated! \n')) + + await showSnapshots(print, cwd, filter) +} diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js new file mode 100644 index 0000000..0c3e7f4 --- /dev/null +++ b/watch-and-show-snapshots.js @@ -0,0 +1,27 @@ +let fs = require('fs') +let chokidar = require('chokidar') +let parseGitignore = require('parse-gitignore') + +let updateAndShowSnaphots = require('./update-and-show-snapshots') + +module.exports = async function watchAndShowSnaphots (print, cwd, filter) { + let ignored = ['.git', 'node_modules'] + + if (fs.existsSync(`${ cwd }/.gitignore`)) { + ignored = [...new Set([ + ...ignored, + ...parseGitignore(fs.readFileSync(`${ cwd }/.gitignore`)).map( + p => /\/$/.test(p) ? p.replace(/\/$/, '') : p + ) + ])] + } + + let watcher = chokidar.watch('**/*.js', { + cwd, ignored + }) + + return watcher + .on('change', async () => { + await updateAndShowSnaphots(print, cwd, filter) + }) +} diff --git a/yarn.lock b/yarn.lock index 2f58cfd..f083a0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -179,6 +179,14 @@ any-observable@^0.3.0: resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.3.0.tgz#af933475e5806a67d0d7df090dd5e8bef65d119b" integrity sha512-/FQM1EDkTsf63Ub2C6O7GuYFDsSXUwsaZDurV0np41ocwq0jthUAYCmhBX9f+KwlaCgIuWyr/4WlUQUBfKfZog== +anymatch@~3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142" + integrity sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg== + dependencies: + normalize-path "^3.0.0" + picomatch "^2.0.4" + argparse@^1.0.7: version "1.0.10" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" @@ -257,6 +265,11 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" +binary-extensions@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.0.0.tgz#23c0df14f6a88077f5f986c0d167ec03c3d5537c" + integrity sha512-Phlt0plgpIIBOGTT/ehfFnbNlfsDEiqmzE2KRXoX1bLIlir4X/MR+zSyBEkL05ffWgnRSf/DXv+WrUAVr93/ow== + brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" @@ -265,7 +278,7 @@ brace-expansion@^1.1.7: balanced-match "^1.0.0" concat-map "0.0.1" -braces@^3.0.1: +braces@^3.0.1, braces@~3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107" integrity sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A== @@ -339,6 +352,21 @@ chardet@^0.7.0: resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e" integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA== +chokidar@^3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.3.1.tgz#c84e5b3d18d9a4d77558fef466b1bf16bbeb3450" + integrity sha512-4QYCEWOcK3OJrxwvyyAOxFuhpvOVCYkr33LPfFNBjAD/w3sEzWsp2BUOkI4l9bHvWioAd0rc6NlHUOEaWkTeqg== + dependencies: + anymatch "~3.1.1" + braces "~3.0.2" + glob-parent "~5.1.0" + is-binary-path "~2.1.0" + is-glob "~4.0.1" + normalize-path "~3.0.0" + readdirp "~3.3.0" + optionalDependencies: + fsevents "~2.1.2" + ci-info@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46" @@ -1096,6 +1124,11 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= +fsevents@~2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.1.2.tgz#4c0a1fb34bc68e543b4b82a9ec392bfbda840805" + integrity sha512-R4wDiBwZ0KzpgOWetKDug1FZcYhqYnUYKtfZYt4mD5SBz76q0KR4Q9o7GIPamsVPGmW3EYPPJ0dOOjvx32ldZA== + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" @@ -1137,7 +1170,7 @@ getpass@^0.1.1: dependencies: assert-plus "^1.0.0" -glob-parent@^5.0.0, glob-parent@^5.1.0: +glob-parent@^5.0.0, glob-parent@^5.1.0, glob-parent@~5.1.0: version "5.1.0" resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.0.tgz#5f4c1d1e748d30cd73ad2944b3577a81b081e8c2" integrity sha512-qjtRgnIVmOfnKUE3NJAQEdk+lKrxfw8t5ke7SXtfMTHcjsBfOfWXCQfdb30zfDoZQ2IRSIiidmjtbHZPZ++Ihw== @@ -1352,6 +1385,13 @@ is-arrayish@^0.2.1: resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" integrity sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0= +is-binary-path@~2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-2.1.0.tgz#ea1f7f3b80f064236e83470f86c09c254fb45b09" + integrity sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw== + dependencies: + binary-extensions "^2.0.0" + is-callable@^1.1.4, is-callable@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.5.tgz#f7e46b596890456db74e7f6e976cb3273d06faab" @@ -1389,7 +1429,7 @@ is-fullwidth-code-point@^3.0.0: resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d" integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg== -is-glob@^4.0.0, is-glob@^4.0.1: +is-glob@^4.0.0, is-glob@^4.0.1, is-glob@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc" integrity sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg== @@ -1854,7 +1894,7 @@ normalize-package-data@^2.3.2, normalize-package-data@^2.5.0: semver "2 || 3 || 4 || 5" validate-npm-package-license "^3.0.1" -normalize-path@^3.0.0: +normalize-path@^3.0.0, normalize-path@~3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65" integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== @@ -2055,6 +2095,11 @@ parent-module@^1.0.0: dependencies: callsites "^3.0.0" +parse-gitignore@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/parse-gitignore/-/parse-gitignore-1.0.1.tgz#8b9dc57f17b810d495c5dfa62eb07caffe7758c7" + integrity sha512-UGyowyjtx26n65kdAMWhm6/3uy5uSrpcuH7tt+QEVudiBoVS+eqHxD5kbi9oWVRwj7sCzXqwuM+rUGw7earl6A== + parse-json@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" @@ -2127,7 +2172,7 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= -picomatch@^2.0.5: +picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.0.7: version "2.2.1" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.1.tgz#21bac888b6ed8601f831ce7816e335bc779f0a4a" integrity sha512-ISBaA8xQNmwELC7eOjqFKMESB2VIqt4PPDD0nsS95b/9dZXvVKOlz9keMSnoGGKcOHXfTvDD6WMaRoSc9UuhRA== @@ -2232,6 +2277,13 @@ read-pkg@^5.2.0: parse-json "^5.0.0" type-fest "^0.6.0" +readdirp@~3.3.0: + version "3.3.0" + resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.3.0.tgz#984458d13a1e42e2e9f5841b129e162f369aff17" + integrity sha512-zz0pAkSPOXXm1viEwygWIPSPkcBYjW1xU5j/JBh5t9bGCJwa6f9+BJa6VaB2g+b55yVrmXzqkyLf4xaWYM0IkQ== + dependencies: + picomatch "^2.0.7" + rechoir@^0.6.2: version "0.6.2" resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384" From f234880a9f3ebfea423bf7fc3ee5de1d95130930 Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 19:07:28 +0300 Subject: [PATCH 2/9] fix: Mixed up methods --- bin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin.js b/bin.js index 688b19a..453738f 100755 --- a/bin.js +++ b/bin.js @@ -33,7 +33,7 @@ async function run () { } if (arg === '--update') { - await showSnapshots(print, cwd, filter) + await updateAndShowSnaphots(print, cwd, filter) return } @@ -49,7 +49,7 @@ async function run () { process.exit(1) } - await updateAndShowSnaphots(print, cwd, arg) + await showSnapshots(print, cwd, arg) } run().catch(e => { From b71cd2428decf8ae0ebc9da3f274b10e74b3d4fc Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 19:12:20 +0300 Subject: [PATCH 3/9] fix: Update snaphots command output --- bin.js | 4 ++-- package.json | 3 --- update-and-show-snapshots.js | 11 ++++++++--- watch-and-show-snapshots.js | 6 ++++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bin.js b/bin.js index 453738f..e906462 100755 --- a/bin.js +++ b/bin.js @@ -33,12 +33,12 @@ async function run () { } if (arg === '--update') { - await updateAndShowSnaphots(print, cwd, filter) + await updateAndShowSnaphots(print, error, cwd, filter) return } if (arg === '--watch') { - (await watchAndShowSnaphots(print, cwd, filter)) + (await watchAndShowSnaphots(print, error, cwd, filter)) .on('error', err => error(err.stack || err)) return } diff --git a/package.json b/package.json index 665e78d..cbb143b 100644 --- a/package.json +++ b/package.json @@ -63,8 +63,5 @@ "CLI", "backtick" ] - }, - "peerDependencies": { - "jest": "^25.1.0" } } diff --git a/update-and-show-snapshots.js b/update-and-show-snapshots.js index 58c8637..0cd297b 100644 --- a/update-and-show-snapshots.js +++ b/update-and-show-snapshots.js @@ -6,14 +6,19 @@ let showSnapshots = require('./show-snapshots') let execCommand = promisify(exec) -module.exports = async function updateAndShowSnaphots (print, cwd, filter) { - print(chalk.blue('Updating snapshots... \n')) +async function updateAndShowSnaphots (print, error, cwd, filter) { + print(chalk.blue('\nUpdating snapshots... \n')) - await execCommand('"./node_modules/.bin/jest" -u', { + let { stdout, stderr } = await execCommand('"./node_modules/.bin/jest" -u', { cwd }) + print(chalk.grey(stdout)) + error(stderr) + print(chalk.green('Snapshots updated! \n')) await showSnapshots(print, cwd, filter) } + +module.exports = updateAndShowSnaphots diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js index 0c3e7f4..de2dfc0 100644 --- a/watch-and-show-snapshots.js +++ b/watch-and-show-snapshots.js @@ -4,7 +4,7 @@ let parseGitignore = require('parse-gitignore') let updateAndShowSnaphots = require('./update-and-show-snapshots') -module.exports = async function watchAndShowSnaphots (print, cwd, filter) { +async function watchAndShowSnaphots (print, error, cwd, filter) { let ignored = ['.git', 'node_modules'] if (fs.existsSync(`${ cwd }/.gitignore`)) { @@ -22,6 +22,8 @@ module.exports = async function watchAndShowSnaphots (print, cwd, filter) { return watcher .on('change', async () => { - await updateAndShowSnaphots(print, cwd, filter) + await updateAndShowSnaphots(print, error, cwd, filter) }) } + +module.exports = watchAndShowSnaphots From 1cd59346ac71b3763af6f8e4dea6d9a9227beb9c Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 20:39:57 +0000 Subject: [PATCH 4/9] fix: Apply review notes #1 --- bin.js | 4 ++-- update-and-show-snapshots.js | 20 ++++++++------------ watch-and-show-snapshots.js | 4 ++-- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/bin.js b/bin.js index e906462..453738f 100755 --- a/bin.js +++ b/bin.js @@ -33,12 +33,12 @@ async function run () { } if (arg === '--update') { - await updateAndShowSnaphots(print, error, cwd, filter) + await updateAndShowSnaphots(print, cwd, filter) return } if (arg === '--watch') { - (await watchAndShowSnaphots(print, error, cwd, filter)) + (await watchAndShowSnaphots(print, cwd, filter)) .on('error', err => error(err.stack || err)) return } diff --git a/update-and-show-snapshots.js b/update-and-show-snapshots.js index 0cd297b..2b026bd 100644 --- a/update-and-show-snapshots.js +++ b/update-and-show-snapshots.js @@ -1,24 +1,20 @@ -let { exec } = require('child_process') -let { promisify } = require('util') +let { spawn } = require('child_process') let chalk = require('chalk') let showSnapshots = require('./show-snapshots') -let execCommand = promisify(exec) - -async function updateAndShowSnaphots (print, error, cwd, filter) { +async function updateAndShowSnaphots (print, cwd, filter) { print(chalk.blue('\nUpdating snapshots... \n')) - let { stdout, stderr } = await execCommand('"./node_modules/.bin/jest" -u', { - cwd + let spawned = spawn('npx', ['jest', '-u'], { + cwd, stdio: 'inherit' }) - print(chalk.grey(stdout)) - error(stderr) - - print(chalk.green('Snapshots updated! \n')) + spawned.on('exit', async () => { + print(chalk.green('\nSnapshots updated! \n')) - await showSnapshots(print, cwd, filter) + await showSnapshots(print, cwd, filter) + }) } module.exports = updateAndShowSnaphots diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js index de2dfc0..e95fc19 100644 --- a/watch-and-show-snapshots.js +++ b/watch-and-show-snapshots.js @@ -4,7 +4,7 @@ let parseGitignore = require('parse-gitignore') let updateAndShowSnaphots = require('./update-and-show-snapshots') -async function watchAndShowSnaphots (print, error, cwd, filter) { +async function watchAndShowSnaphots (print, cwd, filter) { let ignored = ['.git', 'node_modules'] if (fs.existsSync(`${ cwd }/.gitignore`)) { @@ -22,7 +22,7 @@ async function watchAndShowSnaphots (print, error, cwd, filter) { return watcher .on('change', async () => { - await updateAndShowSnaphots(print, error, cwd, filter) + await updateAndShowSnaphots(print, cwd, filter) }) } From 59983d2ebfdff4d4b72cc4a8656ed0583481e6bf Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 20:53:30 +0000 Subject: [PATCH 5/9] fix: Apply review notes #2 --- bin.js | 27 +++++++-------------------- update-and-show-snapshots.js | 5 ++--- watch-and-show-snapshots.js | 9 ++++----- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/bin.js b/bin.js index 453738f..64802de 100755 --- a/bin.js +++ b/bin.js @@ -24,32 +24,19 @@ async function run () { if (arg === '--version') { showVersion(print) - return - } - - if (arg === '--help') { + } else if (arg === '--help') { showHelp(print) - return - } - - if (arg === '--update') { + } else if (arg === '--update') { await updateAndShowSnaphots(print, cwd, filter) - return - } - - if (arg === '--watch') { - (await watchAndShowSnaphots(print, cwd, filter)) - .on('error', err => error(err.stack || err)) - return - } - - if (arg.startsWith('--')) { + } else if (arg === '--watch') { + await watchAndShowSnaphots(print, error, cwd, filter) + } else if (arg.startsWith('--')) { error(`Unknown argument ${ arg }\n`) showHelp(print) process.exit(1) + } else { + await showSnapshots(print, cwd, arg) } - - await showSnapshots(print, cwd, arg) } run().catch(e => { diff --git a/update-and-show-snapshots.js b/update-and-show-snapshots.js index 2b026bd..4369584 100644 --- a/update-and-show-snapshots.js +++ b/update-and-show-snapshots.js @@ -6,13 +6,12 @@ let showSnapshots = require('./show-snapshots') async function updateAndShowSnaphots (print, cwd, filter) { print(chalk.blue('\nUpdating snapshots... \n')) - let spawned = spawn('npx', ['jest', '-u'], { + let spawned = spawn('npx', ['jest', '--no-install', '-u'], { cwd, stdio: 'inherit' }) spawned.on('exit', async () => { - print(chalk.green('\nSnapshots updated! \n')) - + print('\n') await showSnapshots(print, cwd, filter) }) } diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js index e95fc19..ad5f3bb 100644 --- a/watch-and-show-snapshots.js +++ b/watch-and-show-snapshots.js @@ -4,7 +4,7 @@ let parseGitignore = require('parse-gitignore') let updateAndShowSnaphots = require('./update-and-show-snapshots') -async function watchAndShowSnaphots (print, cwd, filter) { +async function watchAndShowSnaphots (print, error, cwd, filter) { let ignored = ['.git', 'node_modules'] if (fs.existsSync(`${ cwd }/.gitignore`)) { @@ -20,10 +20,9 @@ async function watchAndShowSnaphots (print, cwd, filter) { cwd, ignored }) - return watcher - .on('change', async () => { - await updateAndShowSnaphots(print, cwd, filter) - }) + watcher + .on('change', () => updateAndShowSnaphots(print, cwd, filter)) + .on('error', err => error(err.stack || err)) } module.exports = watchAndShowSnaphots From 47b16deb07913d4f8d302ca9ac6ce85cf49405fd Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 21:16:15 +0000 Subject: [PATCH 6/9] fix: Apply review notes #3 --- update-and-show-snapshots.js | 7 ++----- watch-and-show-snapshots.js | 7 +++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/update-and-show-snapshots.js b/update-and-show-snapshots.js index 4369584..1e0430f 100644 --- a/update-and-show-snapshots.js +++ b/update-and-show-snapshots.js @@ -1,18 +1,15 @@ let { spawn } = require('child_process') -let chalk = require('chalk') let showSnapshots = require('./show-snapshots') async function updateAndShowSnaphots (print, cwd, filter) { - print(chalk.blue('\nUpdating snapshots... \n')) - let spawned = spawn('npx', ['jest', '--no-install', '-u'], { cwd, stdio: 'inherit' }) - spawned.on('exit', async () => { + spawned.on('exit', () => { print('\n') - await showSnapshots(print, cwd, filter) + showSnapshots(print, cwd, filter) }) } diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js index ad5f3bb..c40d3f5 100644 --- a/watch-and-show-snapshots.js +++ b/watch-and-show-snapshots.js @@ -1,16 +1,19 @@ let fs = require('fs') +let path = require('path') let chokidar = require('chokidar') let parseGitignore = require('parse-gitignore') let updateAndShowSnaphots = require('./update-and-show-snapshots') +let rootPath = path.dirname(require.main.filename) + async function watchAndShowSnaphots (print, error, cwd, filter) { let ignored = ['.git', 'node_modules'] - if (fs.existsSync(`${ cwd }/.gitignore`)) { + if (fs.existsSync(`${ rootPath }/.gitignore`)) { ignored = [...new Set([ ...ignored, - ...parseGitignore(fs.readFileSync(`${ cwd }/.gitignore`)).map( + ...parseGitignore(fs.readFileSync(`${ rootPath }/.gitignore`)).map( p => /\/$/.test(p) ? p.replace(/\/$/, '') : p ) ])] From 177f61ae06be7e4db3c7482ac9a7222c7f80b5b6 Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Thu, 20 Feb 2020 21:42:46 +0000 Subject: [PATCH 7/9] fix: Apply review notes #4 --- package.json | 3 ++- watch-and-show-snapshots.js | 10 ++++++---- yarn.lock | 7 +++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index cbb143b..7e68453 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "chalk": "^3.0.0", "chokidar": "^3.3.1", "globby": "^11.0.0", - "parse-gitignore": "^1.0.1" + "parse-gitignore": "^1.0.1", + "pkg-up": "^3.1.0" }, "author": "Andrey Sitnik ", "license": "MIT", diff --git a/watch-and-show-snapshots.js b/watch-and-show-snapshots.js index c40d3f5..7ef17c3 100644 --- a/watch-and-show-snapshots.js +++ b/watch-and-show-snapshots.js @@ -1,19 +1,21 @@ let fs = require('fs') let path = require('path') let chokidar = require('chokidar') +let pkgUp = require('pkg-up') let parseGitignore = require('parse-gitignore') let updateAndShowSnaphots = require('./update-and-show-snapshots') -let rootPath = path.dirname(require.main.filename) - async function watchAndShowSnaphots (print, error, cwd, filter) { let ignored = ['.git', 'node_modules'] - if (fs.existsSync(`${ rootPath }/.gitignore`)) { + let rootPath = path.dirname(await pkgUp()) + let gitignorePath = `${ rootPath }/.gitignore` + + if (fs.existsSync(gitignorePath)) { ignored = [...new Set([ ...ignored, - ...parseGitignore(fs.readFileSync(`${ rootPath }/.gitignore`)).map( + ...parseGitignore(fs.readFileSync(gitignorePath)).map( p => /\/$/.test(p) ? p.replace(/\/$/, '') : p ) ])] diff --git a/yarn.lock b/yarn.lock index f083a0f..78afb06 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2196,6 +2196,13 @@ pkg-dir@^4.2.0: dependencies: find-up "^4.0.0" +pkg-up@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/pkg-up/-/pkg-up-3.1.0.tgz#100ec235cc150e4fd42519412596a28512a0def5" + integrity sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA== + dependencies: + find-up "^3.0.0" + please-upgrade-node@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942" From 872846488eb4b267ea2003476a6a56c38d2b1ea8 Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Fri, 21 Feb 2020 12:38:41 +0300 Subject: [PATCH 8/9] fix: Use command-line-args for process argv schema description and parsing --- bin.js | 63 ++++++++++++++++++++++++++++++++++++++-------------- package.json | 1 + yarn.lock | 27 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/bin.js b/bin.js index 64802de..bf250d7 100755 --- a/bin.js +++ b/bin.js @@ -1,6 +1,7 @@ #!/usr/bin/env node let chalk = require('chalk') +let argv = require('command-line-args') let showSnapshots = require('./show-snapshots') let updateAndShowSnaphots = require('./update-and-show-snapshots') @@ -9,6 +10,29 @@ let showVersion = require('./show-version') let showHelp = require('./show-help') let cwd = process.cwd() +let argsSchema = [ + { + name: 'version', + type: Boolean + }, + { + name: 'help', + type: Boolean + }, + { + name: 'update', + type: Boolean + }, + { + name: 'watch', + type: Boolean + }, + { + name: 'filter', + type: String, + defaultOption: true + } +] function error (message) { process.stderr.write(chalk.red(message) + '\n') @@ -19,23 +43,28 @@ function print (...lines) { } async function run () { - let arg = process.argv[2] || '' - let filter = process.argv[3] || '' - - if (arg === '--version') { - showVersion(print) - } else if (arg === '--help') { - showHelp(print) - } else if (arg === '--update') { - await updateAndShowSnaphots(print, cwd, filter) - } else if (arg === '--watch') { - await watchAndShowSnaphots(print, error, cwd, filter) - } else if (arg.startsWith('--')) { - error(`Unknown argument ${ arg }\n`) - showHelp(print) - process.exit(1) - } else { - await showSnapshots(print, cwd, arg) + try { + let args = argv(argsSchema) + + if (args.version) { + showVersion(print) + } else if (args.help) { + showHelp(print) + } else if (args.update) { + await updateAndShowSnaphots(print, cwd, args.filter) + } else if (args.watch) { + await watchAndShowSnaphots(print, error, cwd, args.filter) + } else { + await showSnapshots(print, cwd, args.filter) + } + } catch (e) { + if (e.name === 'UNKNOWN_OPTION') { + error(`Unknown argument ${ e.optionName }\n`) + showHelp(print) + process.exit(1) + } else { + throw e + } } } diff --git a/package.json b/package.json index 7e68453..f0af2b4 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "chalk": "^3.0.0", "chokidar": "^3.3.1", + "command-line-args": "^5.1.1", "globby": "^11.0.0", "parse-gitignore": "^1.0.1", "pkg-up": "^3.1.0" diff --git a/yarn.lock b/yarn.lock index 78afb06..bf0ac35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -194,6 +194,11 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" +array-back@^3.0.1: + version "3.1.0" + resolved "https://registry.yarnpkg.com/array-back/-/array-back-3.1.0.tgz#b8859d7a508871c9a7b2cf42f99428f65e96bfb0" + integrity sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q== + array-includes@^3.0.3: version "3.1.1" resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.1.1.tgz#cdd67e6852bdf9c1215460786732255ed2459348" @@ -465,6 +470,16 @@ combined-stream@^1.0.6, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" +command-line-args@^5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/command-line-args/-/command-line-args-5.1.1.tgz#88e793e5bb3ceb30754a86863f0401ac92fd369a" + integrity sha512-hL/eG8lrll1Qy1ezvkant+trihbGnaKaeEjj6Scyr3DN+RC7iQ5Rz84IeLERfAWDGo0HBSNAakczwgCilDXnWg== + dependencies: + array-back "^3.0.1" + find-replace "^3.0.0" + lodash.camelcase "^4.3.0" + typical "^4.0.0" + commander@^3.0.0: version "3.0.2" resolved "https://registry.yarnpkg.com/commander/-/commander-3.0.2.tgz#6837c3fb677ad9933d1cfba42dd14d5117d6b39e" @@ -1053,6 +1068,13 @@ fill-range@^7.0.1: dependencies: to-regex-range "^5.0.1" +find-replace@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/find-replace/-/find-replace-3.0.0.tgz#3e7e23d3b05167a76f770c9fbd5258b0def68c38" + integrity sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ== + dependencies: + array-back "^3.0.1" + find-up@^2.0.0, find-up@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7" @@ -2815,6 +2837,11 @@ type-fest@^0.8.1: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d" integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA== +typical@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/typical/-/typical-4.0.0.tgz#cbeaff3b9d7ae1e2bbfaf5a4e6f11eccfde94fc4" + integrity sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw== + uc.micro@^1.0.1, uc.micro@^1.0.5: version "1.0.6" resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac" From 64e1eb68c228e644e051ecd56c3cab550a9de384 Mon Sep 17 00:00:00 2001 From: Oleg Levshin Date: Fri, 21 Feb 2020 12:39:46 +0300 Subject: [PATCH 9/9] chore: Update output of --help command --- show-help.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/show-help.js b/show-help.js index 53b91b0..3db8ec4 100644 --- a/show-help.js +++ b/show-help.js @@ -10,6 +10,9 @@ module.exports = function showHelp (print) { '', b('Arguments:'), ' ' + y('--version') + ' Show version', - ' ' + y('--help') + ' Show this message' + ' ' + y('--help') + ' Show this message', + ' ' + y('--update') + ' Update snapshots and show them', + ' ' + y('--watch') + ' Watch for changes of a project\'s *.js files,' + + 'update snaphots and show them' ) }