Skip to content

Instantly share code, notes, and snippets.

@therewasaguy
Last active September 5, 2023 16:35
Show Gist options
  • Save therewasaguy/e6ee64ca3d46ed5cc88898196c77050f to your computer and use it in GitHub Desktop.
Save therewasaguy/e6ee64ca3d46ed5cc88898196c77050f to your computer and use it in GitHub Desktop.
watch p5, p5.js and p5.js-website repos and move a bunch of files around when changes are made
/*
This script watches p5.js-sound/src for changes,
then moves the built library to p5.js to generate documentation,
then moves the documentation and p5.sound library files to the p5.js-website repo.
Assumes a directory structure like...
./docwatch-website.js
./p5.js/
./p5.js-sound/
./p5.js-website/
Usage:
- Run `node docwatch-website.js` in the root directory.
*/
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
// repo locations
const p5 = path.join(__dirname, './p5.js');
const p5Sound = path.join(__dirname, './p5.js-sound');
const p5Website = path.join(__dirname, './p5.js-website');
watchP5Sound();
serveWebsite();
// watch p5.sound for changes...
fs.watchFile(`${p5Sound}/lib/p5.sound.js`, function() {
// once the compiled p5.js-sound has been moved to p5.js `addons`, generate documentation
copyFile(`${p5Sound}/lib/p5.sound.js`, `${p5}/lib/addons/p5.sound.js`)
.then(copyFile(`${p5Sound}lib/p5.sound.min.js`, `${p5}/src/assets/js/p5.sound.min.js`))
.then(copyFile(`${p5Sound}lib/p5.sound.js`, `${p5Website}/src/assets/js/p5.sound.js`))
.then(copyFile(`${p5Sound}lib/p5.sound.min.js`, `${p5Website}/src/assets/js/p5.sound.min.js`))
.then(generateDocs)
.then(copyFile(`${p5}/docs/reference/data.min.json`, `${p5Website}/dist/reference/data.min.json`))
.then(copyFile(`${p5}/docs/reference/data.json`, `${p5Website}/dist/reference/data.json`))
.then(function() {
console.log('done!');
})
.catch(function(err) {
console.error(err);
});
});
function copyFile(oldLocation, newLocation) {
const read = fs.createReadStream(oldLocation);
const write = fs.createWriteStream(newLocation);
return new Promise( function(resolve, reject) {
read.on('error', reject);
write.on('error', reject);
write.on('finish', resolve);
read.pipe(write);
}).catch(function(err) {
read.destroy();
write.end();
throw err;
});
}
function generateDocs() {
return new Promise(function(resolve, reject) {
console.log('running grunt yui:docs');
const gruntTask = spawn('grunt', ['yui:docs'], {
cwd: p5
});
gruntTask.stdout.on('data', function(data) {
console.log(`Generate Docs: ${data}`)
});
gruntTask.stderr.on('data', function(err) {
console.error(`Generate Docs: ${err}`);
});
gruntTask.stdout.on('finish', function(code, signal) {
if (!signal) {
return resolve(code);
} else {
console.error('child process exited with ' +
`code ${code} and signal ${signal}`);
return reject(code);
}
});
});
}
function watchP5Sound() {
console.log('p5Sound: watching src for changes');
const gruntTask = spawn('grunt', ['watch'], {
cwd: p5Sound
});
gruntTask.stdout.on('data', function(data) {
console.log(`p5Sound: ${data}`)
});
gruntTask.stderr.on('data', function(err) {
console.error(`p5Sound: ${err}`);
});
}
function serveWebsite() {
console.log('serving p5 website');
const gruntTask = spawn('grunt', ['server'], {
cwd: p5Website
});
gruntTask.stdout.on('data', function(data) {
console.log(`p5Website: ${data}`)
});
gruntTask.stderr.on('data', function(err) {
console.error(`p5Website: ${err}`);
});
}
console.log('watching files for changes', 'text-decoration: underline');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment