Security update for Core, with self-updated composer
[yaffs-website] / web / core / scripts / js / babel-es6-watch.js
index d614774f2914a8378662d457b8c92232d1725ad7..9b494822369b9190bc0f0fd4f65d9464dba6889f 100644 (file)
 
 const fs = require('fs');
 const path = require('path');
-const babel = require('babel-core');
 const chokidar = require('chokidar');
 
-// Logging human-readable timestamp.
-const log = function log(message) {
-  // eslint-disable-next-line no-console
-  console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`);
-};
-
-function addSourceMappingUrl(code, loc) {
-  return `${code}\n\n//# sourceMappingURL=${path.basename(loc)}`;
-}
+const changeOrAdded = require('./changeOrAdded');
+const log = require('./log');
 
+// Match only on .es6.js files.
 const fileMatch = './**/*.es6.js';
+// Ignore everything in node_modules
 const watcher = chokidar.watch(fileMatch, {
   ignoreInitial: true,
-  ignored: 'node_modules/**'
+  ignored: './node_modules/**'
 });
 
-const changedOrAdded = (filePath) => {
-  babel.transformFile(filePath, {
-    sourceMaps: true,
-    comments: false
-  }, (err, result) => {
-    const fileName = filePath.slice(0, -7);
-    // we've requested for a sourcemap to be written to disk
-    const mapLoc = `${fileName}.js.map`;
-
-    fs.writeFileSync(mapLoc, JSON.stringify(result.map));
-    fs.writeFileSync(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc));
-
-    log(`'${filePath}' has been changed.`);
-  });
-};
-
 const unlinkHandler = (err) => {
   if (err) {
     log(err);
   }
 };
 
+// Watch for filesystem changes.
 watcher
-  .on('add', filePath => changedOrAdded(filePath))
-  .on('change', filePath => changedOrAdded(filePath))
+  .on('add', changeOrAdded)
+  .on('change', changeOrAdded)
   .on('unlink', (filePath) => {
     const fileName = filePath.slice(0, -7);
     fs.stat(`${fileName}.js`, () => {