From c6149529b046526aabe73524ba3a19c5632ad3fb Mon Sep 17 00:00:00 2001
From: Alvaro Cabrera <pateketrueke@gmail.com>
Date: Sun, 15 Oct 2017 14:23:54 -0500
Subject: [PATCH] Upgrade build scripts

---
 build/dist.js  | 62 +++++++++++++++++++++++++-------------------------
 build/tsc.sh   | 12 ----------
 build/tsify.js |  9 --------
 package.json   |  3 +--
 yarn.lock      |  4 ++++
 5 files changed, 36 insertions(+), 54 deletions(-)
 delete mode 100755 build/tsc.sh
 delete mode 100644 build/tsify.js

diff --git a/build/dist.js b/build/dist.js
index 4d7f9ecd..9f20419f 100644
--- a/build/dist.js
+++ b/build/dist.js
@@ -14,18 +14,21 @@ var buildDir = __dirname,
     projectDir = path.join(__dirname, '..');
 
 var BANNER_TEXT = fs.readFileSync(path.join(buildDir, '.banner.txt')).toString();
+var LOCALE_TEXT = fs.readFileSync(path.join(buildDir, '.locale.js')).toString();
 
 var pkg = require(path.join(projectDir, 'package.json')),
-    bannerTemplate = template(BANNER_TEXT);
+    bannerTemplate = template(BANNER_TEXT),
+    localeTemplate = template(LOCALE_TEXT);
 
 var banner = bannerTemplate({ pkg: pkg, now: (new Date()).toISOString().replace('T', ' ') });
 
 // custom bundler
-function bundle(options, next) {
+function bundle(options) {
   var destFile = path.join(projectDir, 'dist', options.dest || '', options.id + '.js');
 
-  rollup.rollup({
+  return rollup.rollup({
     input: options.src,
+    onwarn() {},
     plugins: [
       {
         resolveId(importee, importer) {
@@ -93,44 +96,41 @@ function bundle(options, next) {
 
     // OK
     console.log('Bundle: ' + destFile);
-
-    next();
   })
   .catch(function(error) {
     console.log(error.stack);
   });
 }
 
-console.log('Building...');
-
-bundle({ id: pkg.name, src: path.join(projectDir, 'lib/index.js') }, function(err) {
-  if (err) {
-    throw err;
-    return;
-  }
-});
+Promise.resolve()
+.then(() => {
+  console.log('Building main library...');
 
-var outputs = [
-  { id: pkg.name, src: projectDir }
-];
+  return bundle({ id: pkg.name, src: path.join(projectDir, 'lib/index.js') });
+})
+.then(() => {
+  // proxied versions from faker's locales
+  var languages = glob.sync(path.join(require.resolve('faker'), '../locale/*.js'));
 
-// proxied versions from faker's locales
-var languages = glob.sync(path.join(require.resolve('faker'), '../locale/*.js'));
+  console.log('Preparing all sources...');
 
-languages.forEach(function(lang) {
-  outputs.push({ id: path.basename(lang, '.js'), dest: 'locale' });
-});
+  const tasks = [];
 
-console.log('Preparing all sources...');
+  languages.forEach(function(lang) {
+    fs.outputFileSync(path.join(projectDir, 'locale/' + path.basename(lang)), localeTemplate({ lang: path.basename(lang, '.js') }));
 
-(function next(err) {
-  if (err) {
-    throw err;
-  }
+    tasks.push(() => {
+      console.log('Building language ' + path.basename(lang, '.js') + '...');
 
-  var opts = outputs.shift();
+      return bundle({ id: path.basename(lang, '.js'), src: path.join(projectDir, 'locale/' + path.basename(lang)) });
+    });
+  });
 
-  if (opts) {
-    bundle(opts, next);
-  }
-})();
+  return tasks.reduce((prev, cur) => prev.then(() => cur()), Promise.resolve());
+})
+.then(() => {
+  console.log('Done.');
+})
+.catch(e => {
+  console.log(e.stack);
+});
diff --git a/build/tsc.sh b/build/tsc.sh
deleted file mode 100755
index 6f458856..00000000
--- a/build/tsc.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-
-# --out will not work with CommonJS, see https://github.com/Microsoft/TypeScript/issues/1544
-tsc --module commonjs ts/index.ts ts/api/extend.ts # add `--noImplicitAny` at some point
-mkdir -p lib/api lib/class lib/core lib/generators lib/types
-
-mv ts/api/*.js lib/api/
-mv ts/class/*.js lib/class/
-mv ts/core/*.js lib/core/
-mv ts/generators/*.js lib/generators/
-mv ts/types/*.js lib/types/
-mv ts/*.js lib/
diff --git a/build/tsify.js b/build/tsify.js
deleted file mode 100644
index 6fa31f28..00000000
--- a/build/tsify.js
+++ /dev/null
@@ -1,9 +0,0 @@
-var browserify = require('browserify');
-var tsify = require('tsify');
-
-browserify()
-  .add('ts/index.ts')
-  .plugin(tsify, { noImplicitAny: true })
-  .bundle()
-  .on('error', function (error) { console.error(error.toString()); })
-  .pipe(process.stdout);
diff --git a/package.json b/package.json
index b65e41b8..766ec55d 100644
--- a/package.json
+++ b/package.json
@@ -15,8 +15,6 @@
     "dist": "yarn build && yarn build:dist",
     "build": "tarima -qfe dist VERSION=`./build/VERSION`",
     "build:dist": "yarn build && node build/dist.js",
-    "tsc": "bash build/tsc.sh",
-    "tsify": "browserify ts/index.ts -p [ tsify ] > dist/temp-bundle.js",
     "typedoc": "typedoc --out docs/html ts/ --module commonjs",
     "graphviz": "madge lib --dot > structure.gv"
   },
@@ -81,6 +79,7 @@
     "seedrandom": "^2.4.3",
     "semver": "^5.3.0",
     "tarima": "^3.8.1",
+    "tslib": "^1.8.0",
     "tslint": "^4.0.2",
     "tv4": "^1.2.7",
     "typedoc": "^0.5.1",
diff --git a/yarn.lock b/yarn.lock
index c0e03338..90b46e28 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2215,6 +2215,10 @@ tryit@^1.0.1:
   version "1.0.3"
   resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb"
 
+tslib@^1.8.0:
+  version "1.8.0"
+  resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.0.tgz#dc604ebad64bcbf696d613da6c954aa0e7ea1eb6"
+
 tslint@^4.0.2:
   version "4.5.1"
   resolved "https://registry.yarnpkg.com/tslint/-/tslint-4.5.1.tgz#05356871bef23a434906734006fc188336ba824b"
-- 
GitLab