[*] 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
This commit is contained in:
Evan Morikawa 2016-12-16 13:08:21 -05:00
parent b17be6b873
commit 1d254a7aaa
9 changed files with 72 additions and 2 deletions

6
.babelrc Normal file
View file

@ -0,0 +1,6 @@
{
"presets": [
"electron"
],
"sourceMaps": "inline"
}

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
n1_cloud_dist
*.swp
*~
.DS_Store

View file

@ -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

View file

@ -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"
},

View file

@ -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 :

View file

@ -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

View file

@ -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

51
scripts/build-n1-cloud.js Normal file
View file

@ -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")