From 1d254a7aaad2450b291db0cb6bc23d5dcf0cf8f1 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 16 Dec 2016 13:08:21 -0500 Subject: [PATCH] [*] Add basic babel toolchain Summary: Adds babel to K2 Creates a simple build script so it'll run on prod. Test Plan: manual Reviewers: jackie, halla, juan Reviewed By: juan Differential Revision: https://phab.nylas.com/D3527 --- .babelrc | 6 +++ .gitignore | 2 + Dockerfile | 3 ++ package.json | 6 +++ ...sendmail-client.js => sendmail-client.es6} | 0 pm2-dev.yml | 2 + pm2-prod-api.yml | 2 +- pm2-prod-workers.yml | 2 +- scripts/build-n1-cloud.js | 51 +++++++++++++++++++ 9 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 .babelrc rename packages/isomorphic-core/src/{sendmail-client.js => sendmail-client.es6} (100%) create mode 100644 scripts/build-n1-cloud.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 000000000..d0b9571cf --- /dev/null +++ b/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + "electron" + ], + "sourceMaps": "inline" +} diff --git a/.gitignore b/.gitignore index 06289f5de..8ee4e8931 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +n1_cloud_dist + *.swp *~ .DS_Store diff --git a/Dockerfile b/Dockerfile index a3794f16e..f3775f855 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,9 @@ RUN npm install --production # to run this separately from npm postinstall due to permission issues. RUN node_modules/.bin/lerna bootstrap +# This uses babel to compile any es6 to stock js for plain node +RUN npm run build-n1-cloud + # External services run on port 80. Expose it. EXPOSE 5100 diff --git a/package.json b/package.json index b9557314b..44536cac6 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,11 @@ "description": "The local sync engine for Nylas N1", "main": "", "dependencies": { + "babel-cli": "6.18.0", + "babel-core": "6.20.0", + "babel-preset-electron": "^0.37.8", + "fs-extra": "1.0.0", + "glob": "7.1.1", "lerna": "2.0.0-beta.30", "pm2": "^1.1.3" }, @@ -19,6 +24,7 @@ "scripts": { "start": "pm2 stop all; pm2 delete all; pm2 start ./pm2-dev.yml --no-daemon", "stop": "pm2 stop all; pm2 delete all", + "build-n1-cloud": "node scripts/build-n1-cloud.js", "restart": "pm2 restart all", "postinstall": "lerna bootstrap" }, diff --git a/packages/isomorphic-core/src/sendmail-client.js b/packages/isomorphic-core/src/sendmail-client.es6 similarity index 100% rename from packages/isomorphic-core/src/sendmail-client.js rename to packages/isomorphic-core/src/sendmail-client.es6 diff --git a/pm2-dev.yml b/pm2-dev.yml index 10b5e9b65..17b755a1b 100644 --- a/pm2-dev.yml +++ b/pm2-dev.yml @@ -1,5 +1,6 @@ apps: - script : packages/cloud-api/app.js + interpreter : node_modules/.bin/babel-node watch : ["packages"] name : api env : @@ -13,6 +14,7 @@ apps: HONEY_DATASET: 'n1-cloud-staging' HONEY_WRITE_KEY: 'XXXXXXXXXXXXX' - script : packages/cloud-workers/app.js + interpreter : node_modules/.bin/babel-node watch : ["packages"] name : workers env : diff --git a/pm2-prod-api.yml b/pm2-prod-api.yml index 300f35c43..4f1005f4c 100644 --- a/pm2-prod-api.yml +++ b/pm2-prod-api.yml @@ -1,5 +1,5 @@ apps: - - script : packages/cloud-api/app.js + - script : n1_cloud_dist/cloud-api/app.js name : api instances: 0 exec_mode: cluster diff --git a/pm2-prod-workers.yml b/pm2-prod-workers.yml index f9af7e567..201e565a4 100644 --- a/pm2-prod-workers.yml +++ b/pm2-prod-workers.yml @@ -1,5 +1,5 @@ apps: - - script : packages/cloud-workers/app.js + - script : n1_cloud_dist/cloud-workers/app.js name : workers instances: 0 exec_mode: fork diff --git a/scripts/build-n1-cloud.js b/scripts/build-n1-cloud.js new file mode 100644 index 000000000..bde410131 --- /dev/null +++ b/scripts/build-n1-cloud.js @@ -0,0 +1,51 @@ +const fs = require('fs-extra'); +const glob = require('glob'); +const path = require('path'); +const babel = require('babel-core'); + +fs.removeSync("n1_cloud_dist") +fs.copySync("packages/cloud-api", "n1_cloud_dist/cloud-api") +fs.copySync("packages/cloud-workers", "n1_cloud_dist/cloud-workers") + +fs.copySync("packages/cloud-core", "n1_cloud_dist/cloud-core") +fs.copySync("packages/isomorphic-core", "n1_cloud_dist/isomorphic-core") + +glob.sync("n1_cloud_dist/**/*.es6", {absolute: true}).forEach((es6Path) => { + if (/(node_modules|\.js$)/.test(es6Path)) return + const outPath = es6Path.replace(path.extname(es6Path), '.js'); + console.log(`---> Compiling ${es6Path.slice(es6Path.indexOf("/n1_cloud_dist") + 15)}`) + + const res = babel.transformFileSync(es6Path, { + presets: ["electron"], + sourceMaps: true, + sourceRoot: '/', + sourceMapTarget: path.relative("n1_cloud_dist/", outPath), + sourceFileName: path.relative("n1_cloud_dist/", es6Path), + }); + + fs.writeFileSync(outPath, `${res.code}\n//# sourceMappingURL=${path.basename(outPath)}.map\n`); + fs.writeFileSync(`${outPath}.map`, JSON.stringify(res.map)); + fs.unlinkSync(es6Path); +}); + +// Lerna bootstrap creates symlinks. Unfortunately it creates absolute +// path symlinks that reference the pre-copied, uncompiled files. This +// does a direct copy for each of the leran bootstrap links to ensure we +// don't encounter symlink path problems on prod +// +// Fix cloud-core symlinks +fs.removeSync("n1_cloud_dist/cloud-core/node_modules/isomorphic-core") +fs.copySync("n1_cloud_dist/isomorphic-core", "n1_cloud_dist/cloud-core/node_modules/isomorphic-core") + +// Fix cloud-api symlinks +fs.removeSync("n1_cloud_dist/cloud-api/node_modules/isomorphic-core") +fs.removeSync("n1_cloud_dist/cloud-api/node_modules/cloud-core") +fs.copySync("n1_cloud_dist/isomorphic-core", "n1_cloud_dist/cloud-api/node_modules/isomorphic-core") +fs.copySync("n1_cloud_dist/cloud-core", "n1_cloud_dist/cloud-api/node_modules/cloud-core") + +// Fix cloud-workers symlinks +fs.removeSync("n1_cloud_dist/cloud-workers/node_modules/isomorphic-core") +fs.removeSync("n1_cloud_dist/cloud-workers/node_modules/cloud-core") +fs.copySync("n1_cloud_dist/isomorphic-core", "n1_cloud_dist/cloud-workers/node_modules/isomorphic-core") +fs.copySync("n1_cloud_dist/cloud-core", "n1_cloud_dist/cloud-workers/node_modules/cloud-core") +