2015-05-28 18:54:54 -04:00
|
|
|
import fse = require('fs-extra');
|
|
|
|
import path = require('path');
|
|
|
|
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
|
2016-01-22 13:51:16 -05:00
|
|
|
import {AngularBuilderOptions} from './angular_builder';
|
|
|
|
|
2015-05-28 18:54:54 -04:00
|
|
|
var spawn = require('child_process').spawn;
|
2015-05-27 21:53:49 -04:00
|
|
|
var exec = require('child_process').exec;
|
2015-05-28 18:54:54 -04:00
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
function processToPromise(process: NodeJS.Process) {
|
2015-05-28 18:54:54 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-01-22 13:51:16 -05:00
|
|
|
process.on('close', function(code: number) {
|
2015-05-28 18:54:54 -04:00
|
|
|
if (code) {
|
|
|
|
reject(code);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class DartFormatter implements DiffingBroccoliPlugin {
|
|
|
|
private DARTFMT: string;
|
|
|
|
private verbose: boolean;
|
2015-06-08 17:52:05 -04:00
|
|
|
private firstBuild: boolean = true;
|
2015-05-28 18:54:54 -04:00
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
constructor(public inputPath: string, public cachePath: string, options: AngularBuilderOptions) {
|
2016-04-12 12:40:37 -04:00
|
|
|
if (!options.dartSDK) throw new Error("Missing Dart SDK");
|
2015-05-28 18:54:54 -04:00
|
|
|
this.DARTFMT = options.dartSDK.DARTFMT;
|
|
|
|
this.verbose = options.logs.dartfmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
rebuild(treeDiff: DiffResult): Promise<any> {
|
|
|
|
let args = ['-w'];
|
2015-06-08 17:52:05 -04:00
|
|
|
let argsLength = 2;
|
2016-01-22 13:51:16 -05:00
|
|
|
let argPackages: string[][] = [];
|
2015-06-08 17:52:05 -04:00
|
|
|
let firstBuild = this.firstBuild;
|
2016-04-12 12:40:37 -04:00
|
|
|
treeDiff.addedPaths.concat(treeDiff.changedPaths)
|
|
|
|
.forEach((changedFile) => {
|
|
|
|
let sourcePath = path.join(this.inputPath, changedFile);
|
|
|
|
let destPath = path.join(this.cachePath, changedFile);
|
|
|
|
if (!firstBuild && /\.dart$/.test(changedFile)) {
|
|
|
|
if ((argsLength + destPath.length + 2) >= 0x2000) {
|
|
|
|
// Win32 command line arguments length
|
|
|
|
argPackages.push(args);
|
|
|
|
args = ['-w'];
|
|
|
|
argsLength = 2;
|
|
|
|
}
|
|
|
|
args.push(destPath);
|
|
|
|
argsLength += destPath.length + 2;
|
|
|
|
}
|
|
|
|
fse.copySync(sourcePath, destPath);
|
|
|
|
});
|
2015-05-28 18:54:54 -04:00
|
|
|
treeDiff.removedPaths.forEach((removedFile) => {
|
|
|
|
let destPath = path.join(this.cachePath, removedFile);
|
|
|
|
fse.removeSync(destPath);
|
|
|
|
});
|
2015-05-27 21:53:49 -04:00
|
|
|
|
2015-06-08 17:52:05 -04:00
|
|
|
if (!firstBuild && args.length > 1) {
|
|
|
|
argPackages.push(args);
|
2015-05-27 21:53:49 -04:00
|
|
|
}
|
2015-06-08 17:52:05 -04:00
|
|
|
|
2016-01-22 13:51:16 -05:00
|
|
|
let execute = (args: string[]) => {
|
2016-05-20 19:06:52 -04:00
|
|
|
if (args.length < 2)
|
|
|
|
return Promise.resolve();
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
2016-01-22 13:51:16 -05:00
|
|
|
exec(this.DARTFMT + ' ' + args.join(' '), (err: Error, stdout: string, stderr: string) => {
|
2015-06-08 17:52:05 -04:00
|
|
|
if (this.verbose) {
|
|
|
|
console.log(stdout);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
console.error(shortenFormatterOutput(stderr));
|
|
|
|
reject('Formatting failed.');
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2015-05-27 21:53:49 -04:00
|
|
|
});
|
2015-06-08 17:52:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (firstBuild) {
|
|
|
|
// On firstBuild, format the entire cachePath
|
|
|
|
this.firstBuild = false;
|
|
|
|
return execute(['-w', this.cachePath]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(argPackages.map(execute));
|
2015-05-28 18:54:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default wrapDiffingPlugin(DartFormatter);
|
2015-05-27 21:53:49 -04:00
|
|
|
|
|
|
|
var ARROW_LINE = /^(\s+)\^+/;
|
|
|
|
var BEFORE_CHARS = 15;
|
|
|
|
var stripAnsi = require('strip-ansi');
|
2016-01-22 13:51:16 -05:00
|
|
|
function shortenFormatterOutput(formatterOutput: string) {
|
2015-05-27 21:53:49 -04:00
|
|
|
var lines = formatterOutput.split('\n');
|
2016-01-22 13:51:16 -05:00
|
|
|
var match: string, line: string;
|
2015-05-27 21:53:49 -04:00
|
|
|
for (var i = 0; i < lines.length; i += 1) {
|
|
|
|
line = lines[i];
|
|
|
|
if (match = stripAnsi(line).match(ARROW_LINE)) {
|
|
|
|
let leadingWhitespace = match[1].length;
|
|
|
|
let leadingCodeChars = Math.min(leadingWhitespace, BEFORE_CHARS);
|
|
|
|
lines[i] = line.substr(leadingWhitespace - leadingCodeChars);
|
|
|
|
lines[i - 1] = lines[i - 1].substr(leadingWhitespace - leadingCodeChars, 80) + '…';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines.join('\n');
|
|
|
|
}
|