const fs = require('fs');
const path = require('path');
const execa = require('execa');
const { Script } = require('@beemo/core');

module.exports = class BuildScript extends Script {
  args() {
    return {
      string: ['workspaces'],
      default: {
        workspaces: '',
      },
    };
  }

  bootstrap() {
    this.task('Determining workspace arguments', this.determineArgs);
    this.task('Building CommonJS files', this.buildCjs);
    this.task('Building ECMAScript module files', this.buildEsm);

    if (!this.context.args.workspaces) {
      this.task('Generating TypeScript declarations', this.buildDeclarations).skip(
        this.context.args.noDts,
      );
    }
  }

  determineArgs(context) {
    const ignorePackages = [];
    const args = ['--silent'];

    if (!context.args.workspaces) {
      return args;
    }

    this.tool.getWorkspacePackages().forEach(pkg => {
      const srcPath = path.join(pkg.workspace.packagePath, 'src');

      if (!fs.existsSync(srcPath)) {
        ignorePackages.push(pkg.name);
      }
    });

    args.push(`--workspaces=${ignorePackages.length > 0 ? `!(${ignorePackages.join('|')})` : '*'}`);

    // eslint-disable-next-line no-param-reassign
    context.workspaceArgs = args;

    return args;
  }

  buildCjs(context) {
    return this.handleResponse(execa('beemo', ['babel', '--clean', ...context.workspaceArgs]));
  }

  buildEsm(context) {
    if (context.args.noEsm) {
      return Promise.resolve();
    }

    return this.handleResponse(
      execa('beemo', ['babel', '--clean', '--esm', ...context.workspaceArgs]),
    );
  }

  buildDeclarations() {
    return this.handleResponse(
      execa('beemo', ['typescript', '--declaration', '--emitDeclarationOnly']),
    );
  }

  handleResponse(promise) {
    return promise
      .then(response => {
        const out = response.stdout.trim();

        if (out) {
          this.tool.log(out);
        }

        return response;
      })
      .catch(error => {
        this.tool.logError(error.message);

        throw error;
      });
  }
};

@@ -506,52 +506,8 @@
 m);%0A
-%0A    if (!this.context.args.workspaces) %7B%0A  
     
@@ -581,62 +581,9 @@
 ons)
-.skip(%0A        this.context.args.noDts,%0A      );%0A    %7D
+;
 %0A  %7D
@@ -1517,19 +1517,158 @@
 rations(
-) %7B
+context) %7B%0A    if (context.args.workspaces %7C%7C context.args.referenceWorkspaces %7C%7C context.args.noDts) %7B%0A      return Promise.resolve();%0A    %7D%0A
 %0A    ret

Fix build script.