#!/usr/bin/env node

const babel = require('babel-core');
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const mkdir = require('mkdirp');
const { execSync } = require('child_process');
const rimraf = require('rimraf');

let packageNames = [];
if (process.argv.length < 3) {
  packageNames = glob.sync('*', { cwd: path.join(process.cwd(), 'packages') });
} else {
  packageNames.push(process.argv[2]);
}

packageNames.forEach((packageName) => {
  const babelrc = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'packages', packageName, '.babelrc')));
  const options = Object.assign(babelrc, {
    sourceMaps: 'inline',
  });

  rimraf.sync(path.join(process.cwd(), 'packages', packageName, 'build'));

  const globCommonOpts = {
    ignore: [
      '**/__tests__/**/*',
    ],
  };
  glob.sync(`${path.join(process.cwd(), 'packages', packageName)}/src/**/*.js`, globCommonOpts).forEach((filename) => {
    const outputFilename = filename.replace('src', 'build');
    console.log(`${filename.replace(process.cwd(), '')} -> ${outputFilename.replace(process.cwd(), '')}`);
    const { code } = babel.transformFileSync(filename, options);
    mkdir.sync(path.dirname(outputFilename));
    fs.writeFileSync(outputFilename, code, 'utf-8');
  });

  glob.sync(`${path.join(process.cwd(), 'packages', packageName)}/src/**/*.{html,hbs}`, globCommonOpts).forEach((filename) => {
    const outputFilename = filename.replace('src', 'build');
    console.log(`${filename.replace(process.cwd(), '')} -> ${outputFilename.replace(process.cwd(), '')}`);
    fs.createReadStream(filename).pipe(fs.createWriteStream(filename.replace('src', 'build')))
      .on('error', error => console.error(error));
  });

  if (packageName === 'gluestick-generators') {
    const command = './node_modules/.bin/flow-copy-source -v -i \'**/__tests__/**\' '
      + `./packages/${packageName}/src `
      + `./packages/${packageName}/build`;
    execSync(command);
  }
});

@@ -436,16 +436,247 @@
 2%5D);%0A%7D%0A%0A
+const getDestinationPath = (packageName, filename) =%3E %7B%0A  const pathToPackage = path.join(process.cwd(), 'packages', packageName);%0A  return path.join(pathToPackage, filename.replace(pathToPackage, '').replace('src', 'build'));%0A%7D;%0A%0A
 packageN
@@ -1200,39 +1200,48 @@
 e = 
-filename.replace('src', 'build'
+getDestinationPath(packageName, filename
 );%0A 
@@ -1676,39 +1676,48 @@
 e = 
-filename.replace('src', 'build'
+getDestinationPath(packageName, filename
 );%0A 

tweak build script (#1026)