'use strict';

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

const generateFn = require('../lib/actions/generate');

const basePath = process.cwd();
const testBasePath = basePath + '/e2e_test/test_site';
const expectationDir = testBasePath + '/expectation';
const publicDir = testBasePath + '/public';
generateFn(testBasePath, false, (_, n) => {
  n();

  const result = fs.walkSync(publicDir);
  const expectation = fs.walkSync(expectationDir, []);

  assert(result.length === expectation.length, 'The number of files in the directories do not match');

  const resultFiles = result.reduce((map, filePath) => Object.assign(
    map,
    { [filePath]: fs.readFileSync(filePath, {encoding: 'utf8'}) }
  ), {});

  const expectedFiles = expectation.reduce((map, filePath) => Object.assign(
      map,
      { [filePath.replace(expectationDir, publicDir)]: fs.readFileSync(filePath, {encoding: 'utf8'}) }
  ), {});

  const keys = Object.keys(expectedFiles);
  keys.forEach((key) => {
    assert(expectedFiles[key] === resultFiles[key], `The files do not match. File was: ${key}`)
  });

  fs.removeSync(publicDir);
});

@@ -44,38 +44,8 @@
 ');%0A
-const path = require('path');%0A
 cons
@@ -685,32 +685,33 @@
 Sync(filePath, %7B
+ 
 encoding: 'utf8'
@@ -702,32 +702,33 @@
 encoding: 'utf8'
+ 
 %7D) %7D%0A  ), %7B%7D);%0A%0A
@@ -897,16 +897,17 @@
 ePath, %7B
+ 
 encoding
@@ -914,16 +914,17 @@
 : 'utf8'
+ 
 %7D) %7D%0A  )
@@ -1095,16 +1095,17 @@
 $%7Bkey%7D%60)
+;
 %0A  %7D);%0A%0A

fix linting errors