import * as fs from 'fs';
import path from 'path';

const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const args = yargs(hideBin(process.argv)).argv;

export default () => {
  const workingDir = path.resolve(process.cwd());
  const settingsPath = path.resolve(workingDir, './src/settings.js');

  if (!fs.existsSync(settingsPath)) {
    return;
  }

  const settingsData = fs.readFileSync(settingsPath, {
    encoding: 'utf8',
    flag: 'r'
  });

  fs.writeFileSync(
    settingsPath,
    settingsData.replace(
      /commerceUrl: '.*'/,
      `commerceUrl: '${args.commerceUrl}'`
    )
  );

  console.log(
    '\x1b[32m%s\x1b[0m',
    `\n ✓ Commerce URL is set to ${args.commerceUrl}.\n\n`
  );
};
