-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add --update and --watch options #4
Changes from 4 commits
cad21c4
f234880
b71cd24
1cd5934
59983d2
47b16de
177f61a
8728464
64e1eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
ai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (arg === '--help') { | ||
showHelp(print) | ||
} else if (arg.startsWith('--')) { | ||
return | ||
} | ||
|
||
if (arg === '--update') { | ||
await updateAndShowSnaphots(print, cwd, filter) | ||
return | ||
} | ||
|
||
if (arg === '--watch') { | ||
(await watchAndShowSnaphots(print, cwd, filter)) | ||
.on('error', err => error(err.stack || err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s refactor this code to something more readable. We even pass |
||
return | ||
} | ||
|
||
if (arg.startsWith('--')) { | ||
error(`Unknown argument ${ arg }\n`) | ||
showHelp(print) | ||
process.exit(1) | ||
} else { | ||
await showSnapshots(print, process.cwd(), arg) | ||
} | ||
|
||
await showSnapshots(print, cwd, arg) | ||
} | ||
|
||
run().catch(e => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]>", | ||
"license": "MIT", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s remove this note as well |
||
|
||
let spawned = spawn('npx', ['jest', '-u'], { | ||
cwd, stdio: 'inherit' | ||
}) | ||
|
||
spawned.on('exit', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need |
||
print(chalk.green('\nSnapshots updated! \n')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we do not need this note |
||
|
||
await showSnapshots(print, cwd, filter) | ||
}) | ||
} | ||
|
||
module.exports = updateAndShowSnaphots |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
let fs = require('fs') | ||
let chokidar = require('chokidar') | ||
let parseGitignore = require('parse-gitignore') | ||
|
||
let updateAndShowSnaphots = require('./update-and-show-snapshots') | ||
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can believe that The only way to find the project root is to find |
||
p => /\/$/.test(p) ? p.replace(/\/$/, '') : p | ||
) | ||
])] | ||
} | ||
|
||
let watcher = chokidar.watch('**/*.js', { | ||
cwd, ignored | ||
}) | ||
|
||
return watcher | ||
.on('change', async () => { | ||
await updateAndShowSnaphots(print, cwd, filter) | ||
}) | ||
} | ||
|
||
module.exports = watchAndShowSnaphots |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filter can be passed before
--update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are shure about give ability to set filter before args?
For example, all CLIs that working with paths accept it as last argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap. User may call
npx print-snapshots server
, found problem, press arrow up to load latest command in terminal and add--watch
in the end.By the way, did we forget about changing
--help
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, i will fix it and update help output. Thank you!