Skip to content

Commit

Permalink
Merge pull request #44 from Rich-Harris/prettier
Browse files Browse the repository at this point in the history
run prettier
  • Loading branch information
Rich-Harris authored Nov 11, 2017
2 parents de60e34 + f77288e commit 379255d
Show file tree
Hide file tree
Showing 101 changed files with 2,609 additions and 1,868 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"semi": [ 2, "always" ],
"keyword-spacing": [ 2, { "before": true, "after": true } ],
"space-before-blocks": [ 2, "always" ],
"space-before-function-paren": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": [ 0 ]
},
Expand Down
20 changes: 8 additions & 12 deletions bin/buble
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
var minimist = require( 'minimist' );
var minimist = require('minimist');

var command = minimist( process.argv.slice( 2 ), {
var command = minimist(process.argv.slice(2), {
alias: {
// Short options
h: 'help',
Expand All @@ -15,14 +15,10 @@ var command = minimist( process.argv.slice( 2 ), {
}
});

if ( command.help || ( process.argv.length <= 2 && process.stdin.isTTY ) ) {
require( './showHelp' )();
}

else if ( command.version ) {
console.log( 'Bublé version ' + require( '../package.json' ).version );
}

else {
require( './runBuble' )( command );
if (command.help || (process.argv.length <= 2 && process.stdin.isTTY)) {
require('./showHelp')();
} else if (command.version) {
console.log('Bublé version ' + require('../package.json').version); // eslint-disable-line no-console
} else {
require('./runBuble')(command);
}
56 changes: 34 additions & 22 deletions bin/handleError.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
var chalk = require( 'chalk' );
var chalk = require('chalk');

function print(msg) {
console.error(chalk.red(msg)); // eslint-disable-line no-console
}

var handlers = {
MISSING_INPUT_OPTION: function () {
console.error( chalk.red( 'You must specify an --input (-i) option' ) );
MISSING_INPUT_OPTION: () => {
print('You must specify an --input (-i) option');
},

MISSING_OUTPUT_DIR: function () {
console.error( chalk.red( 'You must specify an --output (-o) option when compiling a directory of files' ) );
MISSING_OUTPUT_DIR: () => {
print(
'You must specify an --output (-o) option when compiling a directory of files'
);
},

MISSING_OUTPUT_FILE: function () {
console.error( chalk.red( 'You must specify an --output (-o) option when creating a file with a sourcemap' ) );
MISSING_OUTPUT_FILE: () => {
print(
'You must specify an --output (-o) option when creating a file with a sourcemap'
);
},

ONE_AT_A_TIME: function ( err ) {
console.error( chalk.red( 'Bublé can only compile one file/directory at a time' ) );
ONE_AT_A_TIME: () => {
print('Bublé can only compile one file/directory at a time');
},

DUPLICATE_IMPORT_OPTIONS: function ( err ) {
console.error( chalk.red( 'use --input, or pass input path as argument – not both' ) );
DUPLICATE_IMPORT_OPTIONS: () => {
print('use --input, or pass input path as argument – not both');
},

BAD_TARGET: function ( err ) {
console.error( chalk.red( 'illegal --target option' ) );
BAD_TARGET: () => {
print('illegal --target option');
}
};

module.exports = function handleError ( err ) {
module.exports = function handleError(err) {
var handler;

if ( handler = handlers[ err && err.code ] ) {
handler( err );
if ((handler = handlers[err && err.code])) {
handler(err);
} else {
if ( err.snippet ) console.error( chalk.red( '---\n' + err.snippet ) );
console.error( chalk.red( err.message || err ) );
if (err.snippet) print('---\n' + err.snippet);
print(err.message || err);

if ( err.stack ) {
console.error( chalk.grey( err.stack ) );
if (err.stack) {
console.error(chalk.grey(err.stack)); // eslint-disable-line no-console
}
}

console.error( 'Type ' + chalk.cyan( 'buble --help' ) + ' for help, or visit https://buble.surge.sh/guide/' );
console.error( // eslint-disable-line no-console
'Type ' +
chalk.cyan('buble --help') +
' for help, or visit https://buble.surge.sh/guide/'
);

process.exit( 1 );
process.exit(1);
};
137 changes: 69 additions & 68 deletions bin/runBuble.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
var fs = require( 'fs' );
var path = require( 'path' );
var buble = require( '../dist/buble.deps.js' );
var handleError = require( './handleError.js' );
var fs = require('fs');
var path = require('path');
var buble = require('../dist/buble.deps.js');
var handleError = require('./handleError.js');
var EOL = require('os').EOL;

function compile ( from, to, command, options ) {
function compile(from, to, command, options) {
try {
var stats = fs.statSync( from );
if ( stats.isDirectory() ) {
compileDir( from, to, command, options );
var stats = fs.statSync(from);
if (stats.isDirectory()) {
compileDir(from, to, command, options);
} else {
compileFile( from, to, command, options );
compileFile(from, to, command, options);
}
} catch ( err ) {
handleError( err );
} catch (err) {
handleError(err);
}
}

function compileDir ( from, to, command, options ) {
if ( !command.output ) handleError({ code: 'MISSING_OUTPUT_DIR' });
function compileDir(from, to, command, options) {
if (!command.output) handleError({ code: 'MISSING_OUTPUT_DIR' });

try {
fs.mkdirSync( to )
} catch ( e ) {
if ( e.code !== 'EEXIST' ) throw e
fs.mkdirSync(to);
} catch (e) {
if (e.code !== 'EEXIST') throw e;
}

fs.readdirSync( from ).forEach( function ( file ) {
compile( path.resolve( from, file ), path.resolve( to, file ), command, options );
fs.readdirSync(from).forEach(function(file) {
compile(path.resolve(from, file), path.resolve(to, file), command, options);
});
}

function compileFile ( from, to, command, options ) {
var ext = path.extname( from );
function compileFile(from, to, command, options) {
var ext = path.extname(from);

if ( ext !== '.js' && ext !== '.jsm' && ext !== '.es6' && ext !== '.jsx') return;
if (ext !== '.js' && ext !== '.jsm' && ext !== '.es6' && ext !== '.jsx')
return;

if ( to ) {
var extTo = path.extname( to );
to = to.slice( 0, -extTo.length ) + '.js';
if (to) {
var extTo = path.extname(to);
to = to.slice(0, -extTo.length) + '.js';
}

var source = fs.readFileSync( from, 'utf-8' );
var result = buble.transform( source, {
var source = fs.readFileSync(from, 'utf-8');
var result = buble.transform(source, {
target: options.target,
transforms: options.transforms,
source: from,
Expand All @@ -52,35 +53,35 @@ function compileFile ( from, to, command, options ) {
namedFunctionExpressions: options.namedFunctionExpressions
});

write( result, to, command );
write(result, to, command);
}

function write ( result, to, command ) {
if ( command.sourcemap === 'inline' ) {
function write(result, to, command) {
if (command.sourcemap === 'inline') {
result.code += EOL + '//# sourceMappingURL=' + result.map.toUrl();
} else if ( command.sourcemap ) {
if ( !to ) {
} else if (command.sourcemap) {
if (!to) {
handleError({ code: 'MISSING_OUTPUT_FILE' });
}

result.code += EOL + '//# sourceMappingURL=' + path.basename( to ) + '.map';
fs.writeFileSync( to + '.map', result.map.toString() );
result.code += EOL + '//# sourceMappingURL=' + path.basename(to) + '.map';
fs.writeFileSync(to + '.map', result.map.toString());
}

if ( to ) {
fs.writeFileSync( to, result.code );
if (to) {
fs.writeFileSync(to, result.code);
} else {
console.log( result.code ); // eslint-disable-line no-console
console.log(result.code); // eslint-disable-line no-console
}
}

module.exports = function ( command ) {
if ( command._.length > 1 ) {
module.exports = function(command) {
if (command._.length > 1) {
handleError({ code: 'ONE_AT_A_TIME' });
}

if ( command._.length === 1 ) {
if ( command.input ) {
if (command._.length === 1) {
if (command.input) {
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
}

Expand All @@ -91,58 +92,58 @@ module.exports = function ( command ) {
target: {},
transforms: {},
jsx: command.jsx,
objectAssign: command.objectAssign === true ? "Object.assign" : command.objectAssign,
namedFunctionExpressions: command["named-function-expr"] !== false
objectAssign:
command.objectAssign === true ? 'Object.assign' : command.objectAssign,
namedFunctionExpressions: command['named-function-expr'] !== false
};

if ( command.target ) {
if ( !/^(?:(\w+):([\d\.]+),)*(\w+):([\d\.]+)$/.test( command.target ) ) {
if (command.target) {
if (!/^(?:(\w+):([\d\.]+),)*(\w+):([\d\.]+)$/.test(command.target)) {
handleError({ code: 'BAD_TARGET' });
}

command.target.split( ',' )
.map( function ( target ) {
return target.split( ':' );
command.target
.split(',')
.map(function(target) {
return target.split(':');
})
.forEach( function ( pair ) {
options.target[ pair[0] ] = pair[1];
.forEach(function(pair) {
options.target[pair[0]] = pair[1];
});
}

if ( command.yes ) {
command.yes.split( ',' ).forEach( function ( transform ) {
options.transforms[ transform ] = true;
if (command.yes) {
command.yes.split(',').forEach(function(transform) {
options.transforms[transform] = true;
});
}

if ( command.no ) {
command.no.split( ',' ).forEach( function ( transform ) {
options.transforms[ transform ] = false;
if (command.no) {
command.no.split(',').forEach(function(transform) {
options.transforms[transform] = false;
});
}

if ( command.input ) {
compile( command.input, command.output, command, options );
}

else {
if (command.input) {
compile(command.input, command.output, command, options);
} else {
process.stdin.resume();
process.stdin.setEncoding( 'utf8' );
process.stdin.setEncoding('utf8');

var source = '';

process.stdin.on( 'data', function ( chunk ) {
process.stdin.on('data', function(chunk) {
source += chunk;
});

process.stdin.on( 'end', function () {
options.source = command.input = "stdin";
process.stdin.on('end', function() {
options.source = command.input = 'stdin';
options.file = command.output;
try {
var result = buble.transform( source, options );
write( result, command.output, command );
} catch ( err ) {
handleError( err );
var result = buble.transform(source, options);
write(result, command.output, command);
} catch (err) {
handleError(err);
}
});
}
Expand Down
17 changes: 10 additions & 7 deletions bin/showHelp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
var fs = require( 'fs' );
var path = require( 'path' );
var fs = require('fs');
var path = require('path');

module.exports = function () {
fs.readFile( path.join( __dirname, 'help.md' ), function ( err, result ) {
module.exports = function() {
fs.readFile(path.join(__dirname, 'help.md'), (err, result) => {
var help;

if ( err ) throw err;
if (err) throw err;

help = result.toString().replace( '<%= version %>', require( '../package.json' ).version );
console.log( '\n' + help + '\n' );
help = result
.toString()
.replace('<%= version %>', require('../package.json').version);

console.log('\n' + help + '\n'); // eslint-disable-line no-console
});
};
Loading

0 comments on commit 379255d

Please sign in to comment.