const fs = require('fs');
const path = require('path');

const rootDir = path.join(process.cwd());
const extractFrom = 'lib/';

if (fs.existsSync(extractFrom)) {
  const files = fs
    .readdirSync(extractFrom)
    .map((filePath) => ({
      absolutePath: path.resolve(rootDir, extractFrom, filePath),
      symlinkPath: path.resolve(rootDir, filePath.replace(extractFrom, ''))
    }));

  const createSymlink = function createSymlink(file) {
    if (!fs.existsSync(file.absolutePath)) {
      console.info(`skipped file: ${file.absolutePath} does not exist`);
      return;
    }

    if (fs.existsSync(file.symlinkPath)) {
      fs.unlinkSync(file.symlinkPath);
      console.info(`removed symlink: ${file.symlinkPath} exists`);
    }

    fs.symlinkSync(file.absolutePath, file.symlinkPath);
    console.info(`created symlink: ${file.symlinkPath} of ${file.absolutePath}`);
  };


  files.forEach(createSymlink);
}
