Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Dec 1, 2015
0 parents commit da1706a
Show file tree
Hide file tree
Showing 11 changed files with 11,083 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Forum 2015 - Hands-On WebAudio
=======================================================

Example application created during during the WebAudio Worshop at IRCAM in November 2015.

To setup and run the application, open a terminal and type the following commands:

```
cd path/to/forum-2015-hands-on-webaudio
npm install
npm run watch
```

_note: the example uses motion sensors to control synthesis parameters and will thus be more interesting on a mobile device_

Additionnal resources are listed in the `resources.pdf` file shipped with the project.
Binary file added assets/favicon.ico
Binary file not shown.
159 changes: 159 additions & 0 deletions bin/scripts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env node
var pkg = require('../package.json');
var childProcess = require('child_process');
var util = require('util');
// transpile stuff
var babel = require('babel');
var browserify = require('browserify');
var fse = require('fs-extra');
var nodeWatch = require('node-watch');
// http-server
var connect = require('connect');
var serveStatic = require('serve-static');
var serveFavicon = require('serve-favicon');
var portfinder = require('portfinder');


// CONFIG
// -----------------------------------------------
portfinder.basePort = 3000;
var srcDir = 'es6';
var distDir = 'dist';
var serverPort = 3000;
var bundleFile = 'bundle.js';

// options for babel
var babelOptions = {
sourceMap: 'inline',
modules: 'common',
optional: ['runtime']
};

// options for browserify
var browserifyOptions = {
debug: true
};

// colors for terminal logs
// cf. http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
var red = '\033[0;31m';
var green = '\033[0;32m';
var blue = '\033[0;34m';
var NC = '\033[0m'; // No Color

// COMMAND INTERPRETER
// -----------------------------------------------
var command = process.argv[2];

switch (command) {
case '--watch':
watch();
break;
case '--serve':
serve();
break;
case '--bundle':
bundle();
break;
case '--transpile':
transpileAll();
break;
}

// HELPERS
// -----------------------------------------------

// change filename from `src/` to `dist/`
function createTargetName(filename) {
return filename.replace(new RegExp('^' + srcDir), distDir);
}

// SCRIPTS
// -----------------------------------------------

function watch() {
nodeWatch(srcDir, function(filename) {
transpile(filename);
});

// middleware to browserify on 'bundle.js' request
serve(bundleMiddleWare);
}

function serve(middleware) {
var app = connect();

if (middleware) {
app.use(middleware);
}

app.use(serveFavicon('./assets/favicon.ico'));
app.use(serveStatic('.', { index: ['index.html'] }));

portfinder.getPort(function(err, port) {
if (err) { return console.log(util.format(red + '=> %s' + NC, err.message)); }

app.listen(port, function() {
console.log(util.format(blue + 'server listen at http://127.0.0.1:%s' + NC, port));
});
});
}

function bundleMiddleWare(req, res, next) {
if (req.url === '/' + bundleFile) {
bundle(next); // rebundle
} else {
next();
}
}

function bundle(next) {
var src = './' + pkg.main;
var target = bundleFile;
var bro = browserify(src, browserifyOptions);
var error = null; // prevent success message if an error occured in browserify

stream = fse.createWriteStream(target)
bro.bundle()
.on('error', function(err) {
error = err;
this.emit('end');
})
.pipe(stream);

stream.on('finish', function() {
if (error !== null) {
console.log(util.format(red + '=> %s' + NC, error.message));
} else {
console.log(util.format(green + '=> %s: successfully created' + NC, target));
if (next) { next(); }
}
});
}

function transpileAll() {
var cmd = 'find '+ srcDir +' -type f';

childProcess.exec(cmd , function(err, stdout, stderr) {
var fileList = stdout.split('\n');

fileList.forEach(function(file) {
if (!file) { return; }
transpile(file);
});
});
}

function transpile(src) {
var target = createTargetName(src);

babel.transformFile(src, babelOptions, function(err, res) {
if (err) { return console.log(util.format(red + '=> %s' + NC, err.message)); }

fse.outputFile(target, res.code, function(err, res) {
if (err) { return console.log(util.format(red + '=> %s' + NC, err.message)); }

console.log(util.format(green + '=> %s: successfully transpiled to %s' + NC, src, target));
});
});
}
Loading

0 comments on commit da1706a

Please sign in to comment.