#!/usr/bin/env node

const spawn = require('cross-spawn');
const chalk = require('chalk');

const args = process.argv.slice(2);

const scriptIndex = args.findIndex(
  (x) =>
    x === 'clean-package' ||
    x === 'clean-all' ||
    x === 'audit-package' ||
    x === 'build-package' ||
    x === 'build-package-pr-scan' ||
    x === 'build-package-sub' ||
    x === 'build-element' ||
    x === 'build-remote' ||
    x === 'build-deps' ||
    x === 'build-all' ||
    x === 'build-app' ||
    x === 'serve-app' ||
    x === 'serve-element' ||
    x === 'serve-remote' ||
    x === 'serve-mock' ||
    x === 'lint-app' ||
    x === 'start-symfony' ||
    x === 'a11y-app' ||
    x === 'commit' ||
    x === 'publish' ||
    x === 'publish-all' ||
    x === 'generate-translations' ||
    x === 'generate-sprite' ||
    x === 'test-package' ||
    x === 'csdr-app' ||
    x === 'csdr-app-stats' ||
    x === 'csdr-app-package-init' ||
    x === 'csdr-init' ||
    x === 'csdr-upgrade-deps' ||
    x === 'csdr-serve-app' ||
    x === 'release-package' ||
    x === 'release-package-standalone' ||
    x === 'release-remote-standalone' ||
    x === 'release-backend' ||
    x === 'release-app' ||
    x === 'release-app-group' ||
    x === 'release-app-standalone' ||
    x === 'generate-translations' ||
    x === 'csdr-sync' ||
    x === 'csdr-cli' ||
    x === 'csdr-stats' ||
    x === 'generate-app-metadata' ||
    x === 'help' ||
    x === 'i18n-translate' ||
    x === 'inject-config-app' ||
    x === 'version' ||
    x === 'bundle-analyze-remote'
);
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];

switch (script) {
    case 'clean-package':
    case 'clean-all':
    case 'audit-package':
    case 'build-package':
    case 'build-package-pr-scan':
    case 'build-package-sub':
    case 'build-element':
    case 'build-remote':
    case 'build-deps':
    case 'build-all':
    case 'build-app':
    case 'serve-app':
    case 'serve-element':
    case 'serve-remote':
    case 'serve-mock':
    case 'lint-app':
    case 'start-symfony':
    case 'a11y-app':
    case 'commit':
    case 'publish':
    case 'publish-all':
    case 'test-package':
    case 'generate-translations':
    case 'generate-sprite':
    case 'csdr-app':
    case 'csdr-app-stats':
    case 'csdr-app-package-init':
    case 'csdr-init':
    case 'csdr-upgrade-deps':
    case 'csdr-serve-app':
    case 'release-package':
    case 'release-package-standalone':
    case 'release-remote-standalone':
    case 'release-backend':
    case 'release-app':
    case 'release-app-group':
    case 'release-app-standalone':
    case 'csdr-sync':
    case 'csdr-cli':
    case 'csdr-stats':
    case 'generate-app-metadata':
    case 'help':
    case 'i18n-translate':
    case 'inject-config-app':
    case 'version':
    case 'bundle-analyze-remote': {
        const result = spawn.sync(
            'node',
            nodeArgs
                .concat(require.resolve('./scripts/' + script))
                .concat(args.slice(scriptIndex + 1)),
            { stdio: 'inherit' }
        );
        if (result.signal) {
            if (result.signal === 'SIGKILL') {
                console.log(
                    'The build failed because the process exited too early. '+
                    'This probably means the system ran out of memory or someone called ' +
                    '`kill -9` on the process.'
                );
            } else if(result.signal === 'SIGTERM') {
                console.log(
                    'The build failed because the process exited too early. ' +
                    'Someone might have called `kill` or `killall`, or the system could ' +
                    'be shutting down.'
                )
            }
            process.exit(1);
        }
        process.exit(result.status);
        break;
    }
    default:
        console.log(chalk.red('Unknown script "' + script + '".'));
        break;
}
