/*
 * SPDX-License-Identifier: EUPL-1.2
 * Authors: see README.md
 */

// Extract files that we need from the node_modules folder
// The extracted files are integrated to the repository, so production server don't need to
// have node installed

// Include fs and path module

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

const basePath = path.join(__dirname, '../')

function copySync(src, dest, opts) {
  if (fs.existsSync(src)) {
    fs.copySync(path.join(basePath, src), path.join(basePath, dest), opts)
  } else {
    console.log(`${src} is not existing !`)
  }
}

// function mergeFilesSync(sources, dest) {
//   const fullDest = path.join(basePath, dest)
//   if (!fs.existsSync(fullDest)) {
//     fs.mkdirSync(path.dirname(fullDest), { recursive: true })
//   } else {
//     fs.unlinkSync(fullDest)
//   }
//   sources.forEach((file) => {
//     const fullSrc = path.join(basePath, file)
//     if (!fs.existsSync(fullSrc)) {
//       console.log(`${fullSrc} is not existing !`)
//     } else {
//       fs.appendFileSync(fullDest, fs.readFileSync(fullSrc))
//     }
//   })
// }

// knacss
copySync(
  'node_modules/knacss/css/knacss-mini/knacss.css',
  'css/vendor/knacss/knacss.min.css',
  { overwrite: true }
)
copySync(
  'node_modules/knacss/LICENSE',
  'css/vendor/knacss/LICENSE',
  { overwrite: true }
)

// modernizr
const modernizr = require("modernizr")

modernizr.build({
  "minify": true,
  "options": [
    "setClasses"
  ],
  "feature-detects": [
    "test/requestanimationframe",
    "test/css/transitions",
    "test/dom/classlist"
  ]
}, function (result) {
  let src = 'js/vendor/modernizr'
  let currentPath = path.join(basePath, src)
  if (!fs.existsSync(currentPath)) {
    fs.mkdirSync(currentPath,{recursive :true})
  }
  fs.writeFileSync(path.join(currentPath, 'modernizr.min.js'), result);// the build
});
copySync(
  'node_modules/modernizr/LICENSE',
  'js/vendor/modernizr/LICENSE',
  { overwrite: true }
)

// example
// mergeFilesSync(
//   [
//     'node_modules/file1.js',
//     'node_modules/file2.js',
//   ],
//   'javascripts/vendor/output-file.js');
