Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SonoIo committed Sep 25, 2015
0 parents commit f83e509
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EVE - Command line tool

## Install

```
$ npm install -g git+https://<your-username>:<password>@bitbucket.org/mbaggio/eve.git#master
```

## File limit

If `EMFILE` file is triggered use the command `ulimit -n 2048` to fix it.
94 changes: 94 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

var exorcist = require('exorcist')
var browserify = require('browserify');
var fs = require('fs');
var path = require('path');
var UglifyJS = require('uglify-js');


module.exports = function(mode, unit, done) {
var time = process.hrtime();
var pkg = require(path.resolve('package.json'));

mode = mode || 'dev';
var components = pkg['eve-components'] || [];
var entryJsFile;
var outputJsFile;
var mapFile;
var debug;

switch (mode) {
case 'dist':
case 'dev':
entryJsFile = path.join('lib', 'app.js');
outputJsFile = path.join('build', 'assets', 'js', 'bundle.js');
debug = mode === 'dev';
break;
case 'test':
entryJsFile = path.join('test', unit, 'app.js');
outputJsFile = path.join('test', unit, 'bundle.js');
debug = true;
break;
default:
return done(new Error('Unknown mode ' + mode));
}

mapFile = outputJsFile + '.map';

var b = browserify({ debug: debug });
b.transform('brfs', {
basedir: path.resolve(__dirname, "node_modules")
});

components.forEach(function (aComponent) {
b.require(path.resolve(aComponent.path), aComponent.options);
});

b.require(path.resolve(entryJsFile), { entry: true } );

b.bundle(function (err, buf) {
if (err) return done(err);
done(null, beautifyTime(process.hrtime(time)), mode);
})
.on("end", function() {
if ( !debug ){
setTimeout(function(){
var src = fs.readFileSync( outputJsFile, { encoding: 'utf8'} ); // buf.toString('utf8');
var srcMinify = UglifyJS.minify(
src,
{
fromString: true
}
);

if ( srcMinify ) {
fs.writeFileSync(outputJsFile, srcMinify.code );
} else {
done(new Error('Minify error'));
}

if ( fs.existsSync(mapFile) )
fs.unlinkSync( mapFile );

}, 2000);
}
})
.pipe(exorcist(mapFile))
.pipe(fs.createWriteStream(outputJsFile, 'utf8'));


function beautifyTime(diff) {
var out = '';
if (diff[0] > 0) {
out += diff[0];
out += '.';
out += parseInt( diff[1] / 1e6 ) + 's'
}
else {
out += parseInt( diff[1] / 1e6 ) + 'ms'
}
return out;
};


}
156 changes: 156 additions & 0 deletions lib/eve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env node

var program = require('commander');
var colors = require('colors');
var path = require('path');
var pkg = require('../package.json');
var exec = require('child_process').exec;
var ets = require('ets');

program
.version(pkg.version);

// http://patorjk.com/software/taag/#p=display&f=Slant&t=EVE
var logo = '' +
' _______ ________\n' +
' / ____/ | / / ____/\n' +
' / __/ | | / / __/ \n' +
' / /___ | |/ / /___ \n' +
'/_____/ |___/_____/ \n';

console.log(logo.cyan);
console.log('In case of EMFILE error type: ulimit -n 2048'.gray);


program
.command('build [mode] [unit]')
.description('Compila l\'applicazione')
.action(function (mode, unit) {
var build = require('./build');
var scss = require('./scss');
build(mode, unit, function (err, time, mode) {
if (err) return handleError(err, 'JS');
console.log('JS builded with mode '.gray + mode.cyan + '...'.gray + time.green + '\u0007');
});
scss(mode, function (err, time) {
if (err) return handleError(err, 'SCSS');
console.log('SCSS builded...'.gray + time.green + '\u0007');
});
});

program
.command('start')
.description('Esegue l\'applicazione')
.option('-p --port <port>', 'Port')
.option('-a --app [platform]', 'App platform')
.action(function (options) {
var watch = require('./watch');
var build = require('./build');
var scss = require('./scss');
var express = require('./express');

express(options, function (err, port) {
console.log('Server started at port '.grey + (port + '').cyan);
});

watch(function (isJs, isCss) {
if (isJs) {
build('dev', null, function (err, time, mode) {
if (err) return handleError(err, 'JS');
prepareApp(function(err, platform) {
if (err) return handleError(err, 'CORDOVA');
console.log('JS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});
}
if (isCss) {
scss('dev', function (err, time) {
if (err) return handleError(err, 'SCSS');
prepareApp(function(err, platform) {
if (err) return handleError(err, 'CORDOVA');
console.log('SCSS builded...'.gray + time.green + '\u0007');
if (platform)
console.log(' Cordova prepared...'.gray + platform.green);
});
});
}
});

function prepareApp(done) {
if (!options.app) return done(null, false);
var command = 'cordova prepare';
var params = {
cwd: path.join(process.cwd(), 'app'),
};
if (options.app !== true)
command += ' ' + options.app;
exec('cordova prepare', params, function(err) {
done(err, options.app === true ? 'all' : options.app);
});
}
});

program
.command('test <unit>')
.description('Testa l\'applicazione')
.option('-p --port <port>', 'Port', 5001)
.action(function (unit, options) {
var watch = require('./watch');
var build = require('./build');
var scss = require('./scss');
var express = require('./express');
options.rootPath = path.join('test', unit);

express(options, function (err, port) {
console.log('Test server started at port '.grey + (port + '').cyan + ' for '.grey + unit.cyan + ' unit'.grey);
});

watch(function (isJs, isCss) {
if (isJs) {
build('test', unit, function (err, time, mode) {
if (err) return handleError(err, 'JS');
console.log('JS builded...'.gray + time.green + '\u0007');
});
}
if (isCss) {
scss('dev', function (err, time) {
if (err) return handleError(err, 'SCSS');
console.log('SCSS builded...'.gray + time.green + '\u0007');
});
}
});
});

program
.command('extract')
.description('Estrae le stringhe dall\'applicazione e le restituisce in un file per la traduzione')
.option('-d, --dir <directory, directory, ...>', 'Directories', split)
.option('-o, --out <file>', 'Output file')
.action(function (options) {
console.log('\n\nPer avere più funzionalità installare:\n\n npm install ets -g\n'.cyan);
ets.extract(options);
});

program.parse(process.argv);

if (!program.args.length) {
program.help();
}


function handleError(err, title) {
if (title) {
console.error(title.red + ' error'.red);
}
var output = '| '.red + err.toString().replace(/\n/g, '\n' + '| '.red);
console.error(output);
console.error('');
}


function split(str){
return str.split(",");
}

18 changes: 18 additions & 0 deletions lib/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var express = require('express');
var path = require('path');

module.exports = function (options, done) {
options = options || {};
var assetsPath = options.assetsPath || path.join('build', 'assets');
var rootPath = options.rootPath || path.join('build');
var stylesPath = options.stylesPath || path.join('styles');
var port = options.port || process.env.PORT || 5000;
var app = express();
app.use('/assets', express.static(path.resolve(assetsPath)));
app.use('/styles', express.static(path.resolve(stylesPath)));
app.use('/', express.static(path.resolve(rootPath)));
app.listen(port);
done(null, port);
};


9 changes: 9 additions & 0 deletions lib/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var exorcist = require('exorcist')
var browserify = require('browserify');
var fs = require('fs');
var path = require('path');


module.exports = function(mode, unit, done) {

}
57 changes: 57 additions & 0 deletions lib/scss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var sass = require('node-sass');
var path = require('path');
var fs = require('fs');

module.exports = function (mode, done) {
var time = process.hrtime();

mode = mode || 'dev';

var outFile = path.resolve(path.join('build', 'assets', 'css', 'style.css'));
var mapFile = path.resolve(path.join('build', 'assets', 'css', 'style.css.map'));

sass.render({
file: path.resolve(path.join('styles', 'style.scss')),
sourceMap: mode != 'dist',
outFile: outFile,
outputStyle: mode == 'dist' ? 'compressed' : null,
success: function(result) {
fs.writeFile(outFile, result.css, function (err) {
if (err) return done(err);
if (mode == 'dist') {
if (fs.existsSync(mapFile))
fs.unlinkSync(mapFile);
return done(null, beautifyTime(process.hrtime(time)));
}
fs.writeFile(mapFile, result.map, function (err) {
if (err) return done(err);
done(null, beautifyTime(process.hrtime(time)));
});
});
},
error: function(err) {
err.toString = function () {
var out = '';
out += 'line ' + this.line + ', column ' + this.column + ' \n';
out += this.message
return out;
};
done(err);
}
// includePaths: [ 'lib/', 'mod/' ],
});

function beautifyTime(diff) {
var out = '';
if (diff[0] > 0) {
out += diff[0];
out += '.';
out += parseInt( diff[1] / 1e6 ) + 's'
}
else {
out += parseInt( diff[1] / 1e6 ) + 'ms'
}
return out;
};

};
Loading

0 comments on commit f83e509

Please sign in to comment.