message: chore(root): improve prepare-beta
  adds optional retarget scope via command line
  does not silently fail on errors
  applies feedback from review
  Ticket:
diff: @@ -3,7 +3,8 @@ import { readFileSync, readdirSync, writeFileSync, statSync } from 'fs';
  import * as path from 'path';
  let lernaModules: string[] = [];
  -const SCOPE = '@bitgo-beta';
  +let TARGET_SCOPE = '@bitgo-beta';
  +let filesChanged = 0;
  /**
  * Create a function which can run lerna commands
  @@ -32,7 +33,6 @@ const getLernaModules = async (): Promise<void> => {
  const walk = (dir: string): string[] => {
  let results: string[] = [];
  const ignoredFolders = [/modules\/bitgo\/example/, /node_modules/];
  - try {
  const list = readdirSync(dir);
  list.forEach((file) => {
  file = path.join(dir, file);
  @@ -49,21 +49,19 @@ const walk = (dir: string): string[] => {
  results.push(file);
  }
  });
  - } catch (error) {
  - console.error(`Error when walking dir ${dir}`, error);
  - }
  return results;
  };
  -const edit = (filePath:string): void => {
  +const changeScopeInFile = (filePath:string): void => {
  const oldContent = readFileSync(filePath, { encoding: 'utf8' });
  let newContent = oldContent;
  lernaModules.forEach((moduleName) => {
  - const newName = `${moduleName.replace('@bitgo/', `${SCOPE}/`)}`;
  + const newName = `${moduleName.replace('@bitgo/', `${TARGET_SCOPE}/`)}`;
  newContent = newContent.replace(new RegExp(moduleName, 'g'), newName);
  });
  if (newContent !== oldContent) {
  writeFileSync(filePath, newContent, { encoding: 'utf-8' });
  + ++filesChanged;
  }
  };
  @@ -73,7 +71,7 @@ const replacePackageScopes = () => {
  ...walk(path.join(__dirname, '../', 'modules')),
  ...walk(path.join(__dirname, '../', 'webpack')),
  ];
  - filePaths.forEach((file) => edit(file));
  + filePaths.forEach((file) => changeScopeInFile(file));
  };
  // modules/bitgo is the only package we publish without an `@bitgo` prefix, so
  @@ -81,17 +79,35 @@ const replacePackageScopes = () => {
  const replaceBitGoPackageScope = () => {
  const cwd = path.join(__dirname, '../', 'modules', 'bitgo');
  const json = JSON.parse(readFileSync(path.join(cwd, 'package.json'), { encoding: 'utf-8' }));
  - json.name = `${SCOPE}/bitgo`;
  + json.name = `${TARGET_SCOPE}/bitgo`;
  writeFileSync(
  path.join(cwd, 'package.json'),
  JSON.stringify(json, null, 2) + '\n'
  );
  };
  +const getArgs = () => {
  + const args = process.argv.slice(2) || [];
  + const scopeArg = args.find((arg) => arg.startsWith('scope='));
  + if (scopeArg) {
  + const split = scopeArg.split('=');
  + TARGET_SCOPE = split[1] || TARGET_SCOPE;
  + }
  + console.log(`Preparing to re-target to ${TARGET_SCOPE}`);
  +};
  +
  const main = async () => {
  + getArgs();
  await getLernaModules();
  replacePackageScopes();
  replaceBitGoPackageScope();
  + if (filesChanged) {
  + console.log(`Successfully re-targeted ${filesChanged} files.`);
  + process.exit(0);
  + } else {
  + console.error('No files were changed, something must have gone wrong.');
  + process.exit(1);
  + }
  };
  main();
  