Skip to content

Commit

Permalink
envolve build_type to improve the performance of cortex watch
Browse files Browse the repository at this point in the history
  • Loading branch information
supersheep committed Jun 25, 2015
1 parent 2181441 commit 89ec5ad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
49 changes: 48 additions & 1 deletion lib/command/build.js
Expand Up @@ -30,18 +30,20 @@ build.run = function(options, callback) {
var tasks = options.install
? [
'simplely_read_cortex_json',
'prepare_build_type',
'run_preinstall_script',
'run_prebuild_script',
'build_process',
'server_link'
] : [
'read_cortex_json',
'clean_cortex_json',
'prepare_build_type',
'run_preinstall_script',
'run_prebuild_script',
// #478
// `cortex.main` and many other files might have not been generated before `cortex.scripts`
// so, we clean cortex_json
'clean_cortex_json',
'build_process',
'server_link',
'run_postbuild_script',
Expand All @@ -52,6 +54,29 @@ build.run = function(options, callback) {
}, callback);
};

build.prepare_build_type = function(options, callback){
var pkg = options.pkg;
var entries = pkg.entries;
var csses = pkg.css;

function hasFile(list){
return list.map(function(rel){
return node_path.join(options.cwd, rel);
}).indexOf(options.file) > -1;
}

if(hasFile(entries)){
options.build_type = 'js';
}else if(hasFile(csses)){
options.build_type = 'css';
}else if(isDir()){
options.build_type = 'dir';
}else{
options.build_type = 'full';
}

callback();
};

build.simplely_read_cortex_json = function (options, callback) {
// If install from cache, cortex.json will be enhanced json.
Expand Down Expand Up @@ -134,6 +159,10 @@ build.run_prebuild_script = function(options, callback) {
if (!options.prebuild) {
return callback(null);
}

if(options.build_type != 'full'){
return callback(null);
}
this.run_script('prebuild', options, callback);
};

Expand Down Expand Up @@ -442,6 +471,11 @@ build.build_modules = function(options, callback) {
var cwd = options.cwd;
var to = options.to;
var self = this;

if(options.build_type == 'js'){
entries = [node_path.relative(options.cwd, options.file)];
}

async.eachSeries(entries, function (entry, done) {
var from = node_path.join(cwd, entry);
builder({
Expand Down Expand Up @@ -480,15 +514,28 @@ build.build_modules = function(options, callback) {
}, callback);
};

build._is_type = function(build_type, type){
// build_type is from step prepare filelist
return build_type == 'full' || build_type == type;
};

build.copy_csses = function (options, callback) {
var css = options.pkg.css;
if (!css) {
return callback(null);
}

if(!this._is_type(options.build_type, 'css')){
return callback(null);
}

var self = this;
var to = options.to;

if(options.build_type == 'css'){
css = [node_path.relative(options.cwd, options.file)];
}

async.eachSeries(css, function (path, done) {
var from = options.cwd;
var css_path = node_path.join(from, path);
Expand Down
4 changes: 3 additions & 1 deletion lib/command/watch.js
Expand Up @@ -36,6 +36,7 @@ watch._createWatcher = function (options) {
// only emitted on master watcher process
})
.on('all', function(event, filepath) {
options.file = filepath;
var dir = node_path.dirname(filepath);
// cortex commander will santitize filepath to the right root directory of the repo
self._rebuild(options, dir);
Expand Down Expand Up @@ -210,7 +211,8 @@ watch._rebuild = function(options, cwd, init) {
'build',
// Use --force to prevent grunt task interrupt the current process
'--force',
'--cwd', cwd
'--cwd', cwd,
'--file', options.file
];

var prerelease = options.prerelease;
Expand Down
5 changes: 5 additions & 0 deletions lib/option/build.js
Expand Up @@ -18,6 +18,11 @@ exports.options = {
default: false
},

file: {
type: node_path,
default: ''
},

prebuild: {
enumerable: false,
type: Boolean,
Expand Down

0 comments on commit 89ec5ad

Please sign in to comment.