{"size": 1923, "ext": "js", "max_stars_repo_path": "tasks/js/build.js", "max_stars_repo_name": "junaid-faryad/email_template", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "tasks/js/build.js", "max_issues_repo_name": "junaid-faryad/email_template", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "tasks/js/build.js", "max_forks_repo_name": "junaid-faryad/email_template", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.4714285714, "max_line_length": 102, "alphanum_fraction": 0.6328653146}
let argv = require('yargs').argv;
let bin = require('./bin');
let command = require('node-cmd');

let fs = require('fs');
let mqpacker = require('css-mqpacker');
let execSync = require('child_process').execSync;

let AfterWebpack = require('on-build-webpack');
let AfterJigsaw = require('./after-jigsaw');
let BrowserSync = require('browser-sync');
let BrowserSyncPlugin = require('browser-sync-webpack-plugin');
let Watch = require('webpack-watch');

let env = argv.e || argv.env || 'local';
let port = argv.p || argv.port || 3000;

let browserSyncInstance;
let config = JSON.parse(execSync('php ./tasks/php/config -e' + env));

if (!config) {
  console.log('\nBuild aborted: Jigsaw config is '+ typeof config +'.\n');
  process.exit();
}

let build_path = config.build && config.build.destination ? config.build.destination : 'build_' + env;
let prettyURLs = config.pretty == false ? '--pretty=false ' : '';

module.exports = {
  jigsaw: new AfterWebpack(() => {

    let mqCombined = mqpacker.pack(fs.readFileSync("source/css/main.css", "utf8")).css;
    fs.writeFileSync('source/css/main.css', mqCombined);

    command.get(bin.path() + ' build ' + prettyURLs + env, (error, stdout, stderr) => {
      console.log(error ? stderr : stdout);

      AfterJigsaw.processEmails(config);

      if (browserSyncInstance) {
        browserSyncInstance.reload();
      }
    });

  }),

  watch: (paths) => {
    return new Watch({
      options: { ignoreInitial: true },
      paths: paths,
    })
  },

  browserSync: (proxy) => {
    return new BrowserSyncPlugin({
      notify: false,
      port: port,
      proxy: proxy,
      tunnel: config.browsersync.tunnel || false,
      server: proxy ? null : { baseDir: build_path, directory: config.browsersync.listing || true },
    },
    {
      reload: false,
      callback: () => {
        browserSyncInstance = BrowserSync.get('bs-webpack-plugin');
      },
    })
  },
};
