Skip to content

Commit

Permalink
Refactor and optimize bin/yarn.js (#3482)
Browse files Browse the repository at this point in the history
* Refactor and optimize yarn.js

* Refactor and add `watch-modern` task
  • Loading branch information
KishanBagaria authored and bestander committed May 25, 2017
1 parent 848139f commit 9acdd68
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
10 changes: 5 additions & 5 deletions bin/yarn.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var semver = require('semver');
var ver = process.versions.node;
ver = ver.split('-')[0]; // explode and truncate tag from version #511

var path = null;
var dirPath = null;

if (semver.satisfies(ver, '>=5.0.0')) {
path = '../lib/cli/index.js';
dirPath = '../lib';
} else if (semver.satisfies(ver, '>=4.0.0')) {
path = '../lib-legacy/cli/index.js';
dirPath = '../lib-legacy';
} else {
console.log(require('chalk').red('Node version ' + ver + ' is not supported, please use Node.js 4.0 or higher.'));
process.exit(1);
Expand All @@ -27,7 +27,7 @@ if (semver.satisfies(ver, '>=5.7.0')) {

// ensure cache directory exists
var mkdirp = require('mkdirp');
var constants = require('../lib-legacy/constants');
var constants = require(dirPath + '/constants');
mkdirp.sync(constants.MODULE_CACHE_DIRECTORY);

module.exports = require(path);
module.exports = require(dirPath + '/cli');
43 changes: 24 additions & 19 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,39 @@ const fs = require('fs');

const babelRc = JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc'), 'utf8'));

function build(lib, opts) {
return gulp.src('src/**/*')
.pipe(plumber({
errorHandler(err) {
gutil.log(err.stack);
},
}))
.pipe(newer(lib))
.pipe(gulpif(argv.sourcemaps, sourcemaps.init()))
.pipe(babel(opts))
.pipe(gulpif(argv.sourcemaps, sourcemaps.write('.')))
.pipe(gulp.dest(lib));
}
const build = (lib, opts) =>
gulp.src('src/**/*')
.pipe(plumber({
errorHandler(err) {
gutil.log(err.stack);
},
}))
.pipe(newer(lib))
.pipe(gulpif(argv.sourcemaps, sourcemaps.init()))
.pipe(babel(opts))
.pipe(gulpif(argv.sourcemaps, sourcemaps.write('.')))
.pipe(gulp.dest(lib));

gulp.task('default', ['build']);

gulp.task('build', ['build-modern', 'build-legacy']);

gulp.task('build-modern', () => {
return build('lib', babelRc.env.node5);
});
gulp.task('build-modern', () =>
build('lib', babelRc.env.node5)
);

gulp.task('build-legacy', () => {
return build('lib-legacy', babelRc.env['pre-node5']);
});
gulp.task('build-legacy', () =>
build('lib-legacy', babelRc.env['pre-node5'])
);

gulp.task('watch', ['build'], () => {
watch('src/**/*', () => {
gulp.start('build');
});
});

gulp.task('watch-modern', ['build-modern'], () => {
watch('src/**/*', () => {
gulp.start('build-modern');
});
});

0 comments on commit 9acdd68

Please sign in to comment.