2014-12-22 20:50:10 -05:00
|
|
|
var util = require('./util');
|
|
|
|
var Q = require('q');
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
var through2 = require('through2');
|
|
|
|
var path = require('path');
|
|
|
|
var glob = require('glob');
|
2015-02-02 19:25:34 -05:00
|
|
|
var fs = require('fs');
|
2014-12-22 20:50:10 -05:00
|
|
|
|
|
|
|
module.exports = function(gulp, plugins, config) {
|
|
|
|
return function() {
|
|
|
|
var webFolders = [].slice.call(glob.sync(path.join(config.src, '*/web')));
|
|
|
|
return nextFolder();
|
|
|
|
|
|
|
|
function nextFolder() {
|
|
|
|
if (!webFolders.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-05 05:58:13 -05:00
|
|
|
var folder = path.resolve(path.join(webFolders.shift(), '..'));
|
2014-12-22 20:50:10 -05:00
|
|
|
var destFolder = path.resolve(path.join(config.dest, path.basename(folder)));
|
2015-04-06 17:48:07 -04:00
|
|
|
var pubMode = config.mode || 'release';
|
|
|
|
var pubArgs = ['build', '--mode', pubMode, '-o', destFolder];
|
|
|
|
return util.processToPromise(spawn(config.command, pubArgs, {
|
2014-12-22 20:50:10 -05:00
|
|
|
stdio: 'inherit',
|
|
|
|
cwd: folder
|
|
|
|
})).then(function() {
|
|
|
|
return replaceDartWithJsScripts(gulp, destFolder);
|
2015-02-02 19:25:34 -05:00
|
|
|
}).then(function() {
|
|
|
|
return removeWebFolder(gulp, destFolder);
|
2014-12-22 20:50:10 -05:00
|
|
|
}).then(nextFolder);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function replaceDartWithJsScripts(gulp, folder) {
|
|
|
|
return util.streamToPromise(gulp.src(path.join(folder, '**/*.html'))
|
|
|
|
.pipe(through2.obj(function(file, enc, done) {
|
|
|
|
var content = file.contents.toString();
|
|
|
|
content = content.replace(/\.dart/, '.dart.js');
|
|
|
|
content = content.replace(/application\/dart/, 'text/javascript');
|
|
|
|
file.contents = new Buffer(content);
|
|
|
|
this.push(file);
|
|
|
|
done();
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(folder)));
|
2015-02-02 19:25:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeWebFolder(gulp, folder) {
|
|
|
|
fs.renameSync(path.join(folder, 'web', 'src'), path.join(folder, 'src'));
|
|
|
|
fs.renameSync(path.join(folder, 'web', 'packages'), path.join(folder, 'packages'));
|
|
|
|
fs.rmdirSync(path.join(folder, 'web'));
|
|
|
|
return Q.resolve();
|
|
|
|
}
|