/*jslint node: true, nomen: true, sloppy: true*/

var cp = require('child_process'),
    fs = require('fs'),
    tests,
    command,
    exitCode = 0;

// Execute requirejs optimizer
cp.exec('node ' + __dirname + '/../vendor/r.js/dist/r.js -o ' + __dirname + '/Classify.build.js', function (error, stdout, stderr) {

    // Print success or error
    if (error !== null) {
        console.error(stderr);
        exitCode = 1;
    } else {
        console.log(stdout);
    }

    // Remove other files
    var files = fs.readdirSync(__dirname + '/../dist'),
        distDir = __dirname + '/../dist/';

    files.forEach(function (file) {

        var code;

        if (file.substr(file.length - 3) === '.js' && file !== 'Classify.js') {
            console.log('Deleting ' + file);
            code = fs.unlinkSync(distDir + file);

            if (code === 0) {
                console.error('Unable to delete file.');
                exitCode = 1;
            }
        }
    });

    // Exit if something went badly
    if (exitCode) {
        process.exit(exitCode);
    }

    // Run tests
    command = 'mocha -R list ' + __dirname + '/../test/Classify.test.js';

    if (process.platform === 'win32') {
        tests = cp.spawn('cmd', ['/s', '/c', command], { customFds: [0, 1, 2] });
    } else {
        tests = cp.spawn('sh', ['-c', command], { customFds: [0, 1, 2] });
    }
    tests.on('exit', function (code) {

        if (code !== 0) {
            process.exit(1);
        } else {
            process.exit(0);
        }
    });
});
@@ -142,18 +142,198 @@
 Code = 0
+,%0A    distDir = __dirname + '/../dist/',%0A    files;%0A%0A// Delete previous files%0Afiles = fs.readdirSync(distDir);%0Afiles.forEach(function (file) %7B%0A    fs.unlinkSync(distDir + file);%0A%7D)
 ;%0A
-
 %0A// Exec
@@ -814,28 +814,8 @@
 ) %7B%0A
-%0A        var code;%0A%0A
     
@@ -906,222 +906,38 @@
     
-console.log('Deleting ' + file);%0A            code = fs.unlinkSync(distDir + file);%0A%0A            if (code === 0) %7B%0A                console.error('Unable to delete file.');%0A                exitCode = 1;%0A            %7D
+fs.unlinkSync(distDir + file);
 %0A   
@@ -1514,11 +1514,12 @@
     %7D);%0A
-
 %7D);
+%0A

Build script now delete all previous files in dist directory.