-
-
Save kentcdodds/2d44448a8997b9964b1be44cd294d1f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node | |
const path = require('path') | |
const inquirer = require('inquirer') | |
const replace = require('replace-in-file') | |
const isCI = require('is-ci') | |
const spawn = require('cross-spawn') | |
const fileGlob = process.argv[2] || 'src/**/*.*' | |
const files = path.isAbsolute(fileGlob) | |
? fileGlob | |
: path.join(process.cwd(), fileGlob) | |
if (isCI) { | |
console.log(`Not running autofill feedback as we're on CI`) | |
} else if (process.env.EMAIL) { | |
const email = process.env.EMAIL | |
console.log(`Autofilling email address to process.env.EMAIL: "${email}"`) | |
replaceEmail(email) | |
} else { | |
const tryAgain = `If you'd like to try again, run this command:\n\n npx "https://gist.github.com/kentcdodds/2d44448a8997b9964b1be44cd294d1f5"\n` | |
const prompt = inquirer.prompt([ | |
{ | |
name: 'email', | |
message: `What's your email address?`, | |
validate(val) { | |
if (val && !val.includes('@')) { | |
return 'email requires an @ sign...' | |
} | |
return true | |
}, | |
}, | |
]) | |
const timeoutId = setTimeout(() => { | |
console.log(`\n\nPrompt timed out. No worries. ${tryAgain}`) | |
prompt.ui.close() | |
}, 60000) | |
prompt.then(({email} = {}) => { | |
clearTimeout(timeoutId) | |
if (!email) { | |
console.log( | |
`Not autofilling email because none was provided. No worries. ${tryAgain}`, | |
) | |
return | |
} | |
replaceEmail(email) | |
}) | |
} | |
function replaceEmail(email) { | |
const options = { | |
files: [files], | |
from: /&em=(?!.*@)/, | |
to: `&em=${encodeURIComponent(email)}`, | |
} | |
replace(options) | |
.then(results => { | |
const changedFiles = results.filter(file => file.hasChanged) | |
console.log( | |
`Updated ${changedFiles.length} files with the email ${email}`, | |
) | |
if (changedFiles.length) { | |
console.log( | |
'Committing changes for you so your jest watch mode works nicely', | |
) | |
spawn.sync('git', ['commit', '-am', 'email autofill', '--no-verify'], { | |
stdio: 'inherit', | |
}) | |
} | |
}) | |
.catch(error => { | |
console.error('Failed to update files') | |
console.error(error.stack) | |
}) | |
} |
{ | |
"name": "autofill-feedback-email", | |
"version": "1.0.0", | |
"description": "I use this to automatically fill in email addresses in feedback forms throughout workshop material", | |
"bin": "./autofill-feedback-email.js", | |
"dependencies": { | |
"inquirer": "7.0.4", | |
"replace-in-file": "5.0.2", | |
"is-ci": "2.0.0", | |
"cross-spawn": "7.0.1" | |
} | |
} |
Thanks!
Hi, @kentcdodds π
I have run this script a couple of times and noticed, that further provided e-mails are appended to the prevous emails, not replaced. It could be nice to have this script to change a provided e-mail, for example, to fix a typo.
So we could grab a value too if provided:
- from: /&em=/,
+ from: /&em=.*$/,
Unfortunately that won't work because this is what it's replacing:
<div>
<span>After the instruction, if you want to remember what you've just learned, then </span>
<a rel="noopener noreferrer" target="_blank" href="https://ws.kcd.im/?ws=React%20Fundamentals%20%E2%9A%9B&e=01%3A%20Basic%20JavaScript-rendered%20Hello%20World&em=">
fill out the elaboration and feedback form.
</a>
</div>
That would replace the ">
as well...
Dealing with that is simple enough, except for the Testing React Apps is different because in that one you don't use the app, you open the markdown file directly:
https://ws.kcd.im/?ws=Testing%20React%20Applications%20%F0%9F%A7%90&e=01%3A%20simple%20test%20with%20ReactDOM&em=
There's probably some extra work we could put in here to make this handle those cases, but I have other priorities at the moment π
I just fixed the regex with a negative lookahead. So if em=
is followed by anything with an @
symbol, it won't match :)
Hi, I've used npm run setup
of epic react workshops more than 10 times, but I haven't yet managed to set up the email before it timed out... I always have a quick look and I think "it's still running" because the script did not exit yet Β―\_(γ)_/Β―
Would it be possible to read from some environment variable please? Something like kcd_feedback_email=... npm run setup
=>
const prompt = process.env.kcd_feedback_email ? Promise.resolve({email: process.env.kcd_feedback_email}) : ...
Based on this I think I'll go with EMAIL
:)
Hey @kentcdodds. Here's a grammar improvement: L56
Also, when I run this on react-performance, it says it updated 49 files, but only 7 files where actually changed and committed. π€
Cya soon in one of your workshops π