"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getPaths = getPaths;

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireDefault(require("path"));

var _child_process = require("child_process");

var _utils = require("../utils");

var _getCliPath = require("./getCliPath");

var _logger = require("../logger");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const options = (0, _utils.getOptions)(); // const args = process.argv.slice(3);
// NOTE: need to discuss about in below codes,
//  when we run webpack the  usable options  that comes  for npm run not  working  as automatical
//  we are use this option always --disable_watch=true reason for now no need for  

const {
  app: {
    outputFolder
  }
} = options;
const {
  cssSelectorZipPath
} = options.impactService;
const zipname = cssSelectorZipPath ? _path.default.parse(cssSelectorZipPath).name : null;

const outputFolderLocation = _path.default.join(process.cwd(), outputFolder);

function execSyncDefalut(command) {
  (0, _logger.messageLogger)(' command running ==>', command);
  return (0, _child_process.execSync)(command, {
    stdio: 'inherit'
  });
}

const isQuiet = 'q';
const webpack = (0, _getCliPath.getCliPath)('webpack'); // messageLogger('webpack === > ', webpack, fs.existsSync(webpack));

if (_fs.default.existsSync(outputFolderLocation)) {
  execSyncDefalut(`rm -rf ${outputFolder}`);
  (0, _logger.messageLogger)(`previous ${outputFolder} removed`);
}

if (_fs.default.existsSync(_path.default.join(process.cwd(), 'build.zip'))) {
  execSyncDefalut('rm build.zip');
  (0, _logger.messageLogger)('previous build.zip removed');
} // execSyncDefalut(`${webpack  } --version`);
// execSyncDefalut(`${webpack} --config ${require.resolve('../configs/webpack.dev.config.js')}`);


(0, _logger.messageLogger)('webpack compilation starts...');
const startTime = Date.now(); // const result = spawnSync(
//   webpack,
//   ['--config', require.resolve('../configs/webpack.dev.config.js')].concat(
//     args
//   ),
//   { stdio: 'inherit' }
// );
// --disable_watch=true

const result = execSyncDefalut(`${webpack} --config ${require.resolve('../configs/webpack.dev.config.js')} ${process.argv.slice(2).map(o => o.replace(/(.*?)=(.*)/, '$1="$2"')).join(' ')} `);
result && (0, _logger.messageLogger)(result);

if (result && result.stderr) {
  throw result.stderr;
}

(0, _logger.messageLogger)(`compailation done in ${Date.now() - startTime}ms`);

if (zipname) {
  execSyncDefalut(`zip -r${isQuiet} build.zip ${outputFolder}/*`);
  (0, _logger.messageLogger)('build.zip file created');
  const result = execSyncDefalut(`zip -r${isQuiet} ${cssSelectorZipPath} ${zipname}/*`);
  execSyncDefalut(`rm -rf ${zipname}`);

  if (result && result.stderr) {
    (0, _logger.messageLogger)('zip  file not  created', cssSelectorZipPath);
    console.error(result.stderr);
  } else {
    (0, _logger.messageLogger)('zip  file  created', cssSelectorZipPath);
  }
} // npm run start --app:domain=tsi --impact:cssbountry="{$@&&@$}" --disable-watch --dev-cache --cssselector_zip=css-source-map.zip
// npm run start --app_domain=tsi --impact_cssbountry="{$@&&@$}" --disable-watch --dev-cache --cssselector_zip=css-source-map.zip


(0, _logger.messageLogger)(`compailation done in ${Date.now() - startTime}ms`);

function getPaths() {
  return {
    zipname,
    cssSelectorZipPath,
    outputFolderLocation
  };
}