'use strict';

// hvigor/hvigor-config.json5 depends on this tgz, which is intentionally not
// tracked. Provision the matching plugin from this example's installed CLI.
const fs = require('fs');
const path = require('path');

const PLUGIN_FILE = 'rnoh-hvigor-plugin-0.3.0.tgz';
const projectDir = process.cwd();
const source = path.join(
  projectDir,
  'node_modules',
  '@react-native-oh',
  'react-native-harmony-cli',
  'harmony',
  PLUGIN_FILE,
);
const target = path.join(projectDir, 'harmony', 'hvigor', PLUGIN_FILE);

if (fs.existsSync(target)) {
  console.log(`[hvigor] keep existing ${path.relative(projectDir, target)}`);
  process.exit(0);
}
if (!fs.existsSync(source)) {
  console.log(`[hvigor] skip ${PLUGIN_FILE}: @react-native-oh/react-native-harmony-cli is not installed yet.`);
  process.exit(0);
}
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.copyFileSync(source, target);
console.log(`[hvigor] provisioned ${path.relative(projectDir, target)} from ${path.relative(projectDir, source)}`);