diff --git a/core/scripts/js/changeOrAdded.js b/core/scripts/js/changeOrAdded.js index e6d1a96..c791ee5 100644 --- a/core/scripts/js/changeOrAdded.js +++ b/core/scripts/js/changeOrAdded.js @@ -1,33 +1,16 @@ const fs = require('fs'); -const babel = require('babel-core'); const log = require('./log'); +const transpile = require('./transpile'); module.exports = (filePath) => { const moduleName = filePath.slice(0, -7); log(`'${filePath}' is being processed.`); // Transform the file. - // Check process.env.NODE_ENV to see if we should create sourcemaps. - babel.transformFile( - filePath, - { - sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false, - comments: false, - plugins: [ - ['add-header-comment', { - 'header': [ - `DO NOT EDIT THIS FILE.\nAll changes should be applied to ${filePath}\nSee the following change record for more information,\nhttps://www.drupal.org/node/2873849\n@preserve` - ] - }] - ] - }, - (err, result) => { - if (err) { - throw new Error(err); - } + transpile(filePath, function check(code) { const fileName = filePath.slice(0, -7); // Write the result to the filesystem. - fs.writeFile(`${fileName}.js`, result.code, () => { + fs.writeFile(`${fileName}.js`, code, () => { log(`'${filePath}' is finished.`); }); } diff --git a/core/scripts/js/check.js b/core/scripts/js/check.js index 0a34363..ce484ee 100644 --- a/core/scripts/js/check.js +++ b/core/scripts/js/check.js @@ -1,39 +1,20 @@ const fs = require('fs'); -const babel = require('babel-core'); const log = require('./log'); +const transpile = require('./transpile'); module.exports = (filePath) => { log(`'${filePath}' is being processed.`); - // Transform the file. - // Check process.env.NODE_ENV to see if we should create sourcemaps. - babel.transformFile( - filePath, - { - sourceMaps: process.env.NODE_ENV === 'development' ? 'inline' : false, - comments: false, - plugins: [ - ['add-header-comment', { - 'header': [ - `DO NOT EDIT THIS FILE.\nAll changes should be applied to ${filePath}\nSee the following change record for more information,\nhttps://www.drupal.org/node/2873849\n@preserve` - ] - }] - ] - }, - (err, result) => { - if (err) { - throw new Error(err); - } + transpile(filePath, function check(code) { const fileName = filePath.slice(0, -7); fs.readFile(`${fileName}.js`, function read(err, data) { - if (err) { - throw err; - } - if (result.code != data) { - log(`${fileName}.js`); - throw new Error(`'${filePath}' is not updated.`); - } + if (err) { + throw err; + } + if (code != data) { + log(`${fileName}.js`); + throw new Error(`'${filePath}' is not updated.`); + } }); - } - ); + }); } diff --git a/core/scripts/js/changeOrAdded.js b/core/scripts/js/transpile.js similarity index 66% copy from core/scripts/js/changeOrAdded.js copy to core/scripts/js/transpile.js index e6d1a96..b7e89ff 100644 --- a/core/scripts/js/changeOrAdded.js +++ b/core/scripts/js/transpile.js @@ -1,11 +1,7 @@ const fs = require('fs'); const babel = require('babel-core'); -const log = require('./log'); - -module.exports = (filePath) => { - const moduleName = filePath.slice(0, -7); - log(`'${filePath}' is being processed.`); +module.exports = (filePath, callback) => { // Transform the file. // Check process.env.NODE_ENV to see if we should create sourcemaps. babel.transformFile( @@ -25,11 +21,7 @@ module.exports = (filePath) => { if (err) { throw new Error(err); } - const fileName = filePath.slice(0, -7); - // Write the result to the filesystem. - fs.writeFile(`${fileName}.js`, result.code, () => { - log(`'${filePath}' is finished.`); - }); + callback(result.code); } ); }