Skip to content

Instantly share code, notes, and snippets.

@kwelch
Created December 5, 2018 21:21
Show Gist options
  • Save kwelch/70b7984e6d26f551c1ad7fe0b1ed329b to your computer and use it in GitHub Desktop.
Save kwelch/70b7984e6d26f551c1ad7fe0b1ed329b to your computer and use it in GitHub Desktop.
Rapid File Saver
var fs = require('fs');
var path = require('path');
const TEMP_FILE_PATH = path.resolve(__dirname, '../../src/playground/tempFile.js');
// Recommendation: Do not set lower than 200ms
// File changes that quickly will not allow webpack to finish compiling
const REWRITE_TIMEOUT_MIN = 200;
const REWRITE_TIMEOUT_MAX = 300;
const getRandomInRange = (min, max) => (Math.random() * (max - min) + min)
const getTimeout = () => getRandomInRange(REWRITE_TIMEOUT_MIN, REWRITE_TIMEOUT_MAX);
const FILE_VALUES = [
{name: 'add', content:'export default (a, b) => (a + b);'},
{name: 'subtract', content:'export default (a, b) => (a - b);'},
{name: 'divide', content:'export default (a, b) => (a / b);'},
{name: 'multiply', content:'export default (a, b) => (a * b);'},
];
let currentValue = 1;
const getValue = () => {
const value = FILE_VALUES[currentValue];
if (currentValue === FILE_VALUES.length-1) {
currentValue = 0;
} else {
currentValue++;
}
return value;
}
const writeToFile = () => {
const {name, content} = getValue();
console.log(`${new Date().toISOString()} -- WRITING (${name}) --`);
fs.writeFileSync(TEMP_FILE_PATH, content);
setTimeout(writeToFile, getTimeout());
}
writeToFile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment