protected function compileFiles($files, $output)
{
$root = $webDir = realpath($this->jarves->getRootDir() . '/../') . '/';
$web = $root . 'web';
chdir($web);
$traceur = 'traceur';
//todo, make configurable
$args = array($traceur, '--out=traceur_compiled.js', '--source-maps', '--experimental');
$md5 = [];
foreach ($files as $file) {
$args[] = $file;
$md5[] = filemtime($file);
}
$md5 = md5(implode('.', $md5));
$needsCompilation = false;
if (file_exists($output)) {
$fh = fopen($output, 'r+');
if ($fh) {
fseek($fh, -32, SEEK_END);
$lastLine = fgets($fh);
$needsCompilation = $lastLine !== $md5;
fclose($fh);
}
} else {
$needsCompilation = true;
}
if ($needsCompilation) {
$builder = new ProcessBuilder($args);
$process = $builder->getProcess();
$process->run();
// var_dump($process->getErrorOutput());
// var_dump($process->getOutput());
// echo($process->getCommandLine());
// var_dump($process->isSuccessful());
// exit;
$isSuccessful = $process->isSuccessful();
if ($isSuccessful) {
$error = $process->getErrorOutput();
if (false !== strpos($error, 'Internal error Error')) {
$isSuccessful = false;
}
}
if ($isSuccessful) {
$this->webFilesystem->mkdir(dirname($output));
$this->webFilesystem->move('traceur_compiled.js', $output);
$mapName = explode('.', $output);
array_pop($mapName);
$mapName[] = 'map';
$mapName = implode('.', $mapName);
$contents = file_get_contents($output);
if (file_exists('traceur_compiled.map')) {
$this->webFilesystem->move('traceur_compiled.map', $mapName);
$contents = str_replace('//# sourceMappingURL=traceur_compiled.map', '//# sourceMappingURL=' . basename($mapName), $contents);
}
$contents .= "//compile-md5: {$md5}";
file_put_contents($output, $contents);
} else {
chdir($root);
throw new \Exception('Traceur Compile failed: ' . $process->getErrorOutput());
}
}
chdir($root);
}