{"size": 1521, "ext": "js", "max_stars_repo_path": "src/index.test.js", "max_stars_repo_name": "clearsummit/flow-copy-source", "max_stars_repo_stars_event_min_datetime": "2016-02-26T19:02:11.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-17T07:50:55.000Z", "max_issues_repo_path": "src/index.test.js", "max_issues_repo_name": "clearsummit/flow-copy-source", "max_issues_repo_issues_event_min_datetime": "2019-01-20T18:31:17.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T10:23:43.000Z", "max_forks_repo_path": "src/index.test.js", "max_forks_repo_name": "clearsummit/flow-copy-source", "max_forks_repo_forks_event_min_datetime": "2016-08-10T11:33:25.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-12T03:36:28.000Z"}
{"max_stars_count": 134, "max_issues_count": 11, "max_forks_count": 17, "avg_line_length": 31.6875, "max_line_length": 87, "alphanum_fraction": 0.6370808679}
'use strict';

const tmp = require('tmp');
const fs = require('fs-extra');
const path = require('path');

const flowCopySource = require('.');

let _tmpDirCleanupFns = [];

async function createTmpDir() {
  return new Promise((resolve, reject) => {
    tmp.dir({keep: false, unsafeCleanup: true}, (err, path, cleanupCallback) => {
      if (err) return reject(err);
      _tmpDirCleanupFns.push(cleanupCallback);
      resolve(path);
    });
  });
}

afterEach(() => {
  _tmpDirCleanupFns.forEach(fn => fn());
  _tmpDirCleanupFns.length = 0;
});

it('basic case', async () => {
  const dir = await createTmpDir();
  const srcDir = path.join(dir, 'src');
  const jsDir = path.join(dir, 'js');

  await fs.mkdir(srcDir);
  await fs.writeFile(path.join(srcDir, 'index.js'), 'let x = 1;\n');
  await fs.writeFile(path.join(srcDir, 'component.jsx'), 'let component = 1;\n');
  await fs.writeFile(path.join(srcDir, 'foo.mjs'), 'let foo = 1;\n');
  await fs.writeFile(path.join(srcDir, 'ignoreme.py'), 'x = 1\n');

  await flowCopySource([srcDir], jsDir);

  expect(new Set(await fs.readdir(jsDir)))
    .toEqual(new Set(['index.js.flow', 'component.jsx.flow', 'foo.mjs.flow']));
  expect(await fs.readFile(path.join(jsDir, 'index.js.flow'), {encoding: 'utf8'}))
    .toBe('let x = 1;\n');
  expect(await fs.readFile(path.join(jsDir, 'component.jsx.flow'), {encoding: 'utf8'}))
    .toBe('let component = 1;\n');
  expect(await fs.readFile(path.join(jsDir, 'foo.mjs.flow'), {encoding: 'utf8'}))
    .toBe('let foo = 1;\n');
});
