Skip to content

Instantly share code, notes, and snippets.

@therewasaguy
Last active September 5, 2023 16:34
Show Gist options
  • Save therewasaguy/1e397ff8762323372c2d32fd93bafb91 to your computer and use it in GitHub Desktop.
Save therewasaguy/1e397ff8762323372c2d32fd93bafb91 to your computer and use it in GitHub Desktop.
p5.sound --> p5.js documentation watch script
/*
Documentation Watcher
This assumes a directory structure like...
./docwatch.js
./p5.js/ <repo>
./p5.js-sound/ <repo>
Usage:
- Run `node docwatch.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');
watchP5Sound();
watchDocs();
// 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.js`, `${p5}/docs/reference/assets/js/p5.sound.js`))
.then(generateDocs)
.then(function() {
console.log('copied p5.sound and generated docs!');
})
.catch(function(err) {
console.error(err);
});
});
fs.watchFile(`${p5Sound}/lib/p5.sound.min.js`, function() {
copyFile(`${p5Sound}/lib/p5.sound.min.js`, `${p5}/lib/addons/p5.sound.min.js`)
.then(copyFile(`${p5Sound}/lib/p5.sound.min.js`, `${p5}/docs/reference/assets/js/p5.sound.min.js`))
.then(function() {
console.log('copied p5.min!');
})
.catch(function(err) {
console.error(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 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 watchP5Sound() {
console.log('p5Sound: watching src for changes');
const gruntTask = spawn('grunt', ['dev'], {
cwd: p5Sound
});
gruntTask.stdout.on('data', function(data) {
console.log(`p5Sound: ${data}`)
});
gruntTask.stderr.on('data', function(err) {
console.error(`p5Sound: ${err}`);
});
}
function watchDocs() {
const gruntTask = spawn('grunt', ['yui:dev'], {
cwd: p5
});
gruntTask.stdout.on('data', function(data) {
console.log(`p5 yui:dev: ${data}`)
});
gruntTask.stderr.on('data', function(err) {
console.error(`p5 yui:dev: ${err}`);
});
}
console.log('watching files for changes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment