From f80082bb42df2af3005754b8b92e102f7cb0e74c Mon Sep 17 00:00:00 2001
From: AlexKVal <alexkval@gmail.com>
Date: Fri, 9 Oct 2015 19:45:24 +0300
Subject: [PATCH] [fixed] bower building and publishing

---
 package.json             |  7 ++++-
 scripts/bower-prepare.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 scripts/bower-prepare.js

diff --git a/package.json b/package.json
index 62b481d..5b85df7 100644
--- a/package.json
+++ b/package.json
@@ -5,11 +5,12 @@
   "main": "./lib/index.js",
   "scripts": {
     "prepublish": "npm run build",
-    "build": "babel src --out-dir=lib && webpack && webpack -p",
+    "build": "babel src --out-dir=lib && webpack && webpack -p && npm run bower-prepare",
     "test": "npm run lint && karma start --single-run",
     "tdd": "karma start",
     "visual-test": "open http://localhost:8080/ && webpack-dev-server --config webpack.visual.config.babel.js",
     "lint": "eslint *.babel.js src tests",
+    "bower-prepare": "babel-node scripts/bower-prepare.js",
     "release": "release"
   },
   "repository": {
@@ -40,6 +41,7 @@
     "babel-eslint": "^4.1.3",
     "babel-loader": "^5.3.2",
     "bootstrap": "^3.3.5",
+    "colors": "^1.1.2",
     "css-loader": "^0.19.0",
     "es5-shim": "^4.1.14",
     "eslint": "1.6.x",
@@ -60,6 +62,7 @@
     "karma-webpack": "^1.7.0",
     "less": "^2.5.3",
     "less-loader": "^2.2.1",
+    "lodash": "^3.10.1",
     "mocha": "^2.3.3",
     "mt-changelog": "^0.6.2",
     "node-libs-browser": "^0.5.3",
@@ -69,6 +72,8 @@
     "react-dom": "^0.14.0",
     "react-router": "^1.0.0-rc3",
     "release-script": "^0.5.3",
+    "rimraf": "^2.4.3",
+    "shelljs": "^0.5.3",
     "style-loader": "^0.12.4",
     "url-loader": "^0.5.6",
     "webpack": "^1.12.2",
diff --git a/scripts/bower-prepare.js b/scripts/bower-prepare.js
new file mode 100644
index 0000000..02acfa4
--- /dev/null
+++ b/scripts/bower-prepare.js
@@ -0,0 +1,70 @@
+/* globals cat, config, cp, mkdir, rm, test */
+/* eslint curly: 0 */
+import 'colors';
+import 'shelljs/global';
+import path from 'path';
+import _ from 'lodash';
+import yargs from 'yargs';
+
+// do not die on errors
+config.fatal = false;
+
+//------------------------------------------------------------------------------
+// constants
+const repoRoot = path.resolve(__dirname, '../');
+const libFolder = path.join(repoRoot, 'lib');
+const bowerRoot = path.join(repoRoot, 'amd');
+const bowerTemplate = path.join(repoRoot, 'bower.template.json');
+const license = path.join(repoRoot, 'LICENSE');
+
+
+//------------------------------------------------------------------------------
+// command line options
+const argv = yargs
+  .usage('Usage: $0 [--verbose]')
+  .example('$0', 'Prepare bower package for releasing')
+  .option('verbose', {
+    demand: false,
+    default: false,
+    describe: 'Increased debug output'
+  })
+  .argv;
+
+if (argv.dryRun) console.log('DRY RUN'.magenta);
+
+config.silent = !argv.verbose;
+
+
+//------------------------------------------------------------------------------
+// functions
+function bower() {
+  console.log('Creating: '.cyan + 'bower package'.green);
+
+  rm('-rf', bowerRoot);
+  mkdir('-p', bowerRoot);
+
+  // generate bower.json from template
+  const pkg = JSON.parse(cat(path.join(repoRoot, 'package.json')));
+  const template = _.template(cat(bowerTemplate));
+  const bowerConfigObject = template({ pkg });
+  const json = JSON.stringify(JSON.parse(bowerConfigObject), null, 2); // proper formatting hack
+  json.to(path.join(bowerRoot, 'bower.json'));
+
+  // copy readme and license
+  const readmeBower = path.join(repoRoot, 'README.bower.md');
+  const readme = path.join(repoRoot, 'README.md');
+  if (test('-e', readmeBower)) {
+    cp(readmeBower, path.join(bowerRoot, 'README.md'));
+  } else {
+    cp(readme, bowerRoot);
+  }
+  if (test('-e', license)) cp(license, bowerRoot);
+
+  // copy distr files
+  cp('-r', libFolder, bowerRoot);
+
+  console.log('Created: '.cyan + 'bower package'.green);
+}
+
+//------------------------------------------------------------------------------
+bower();
