const rollup = require('rollup');
const typescript = require('rollup-plugin-typescript2');
const rimraf = require('rimraf');
const { name: LIB_NAME } = require('../../package.json');
const cacheRoot = '.rollupcache';
const externalLibs = [
  'aurelia-router',
  'aurelia-pal',
  'aurelia-logging',
  'aurelia-framework',
  'aurelia-metadata',
  'aurelia-path',
  'aurelia-templating',
  'aurelia-dependency-injection',
  'aurelia-binding'
];

clean().then(build).then(generateDts);

/**
 * @type {() => Promise<Error | null>}
 */
function clean() {
  console.log('\n==============\nCleaning dist folder...\n==============');
  return new Promise(resolve => {
    rimraf('dist', (error) => {
      if (error) {
        throw error;
      }
      resolve();
    });
  });
}

function generateDts() {
  console.log('\n==============\nGenerating dts bundle...\n==============');
  return new Promise(resolve => {
    const ChildProcess = require('child_process');
    ChildProcess.exec('npm run build:dts', (err, stdout, stderr) => {
      if (err || stderr) {
        console.log('Generating dts error:');
        console.log(stderr);
      } else {
        console.log('Generated dts bundle successfully');
        console.log(stdout);
      }
      resolve();
    });
  });
};

function build() {
  console.log('\n==============\nBuidling...\n==============');
  return Promise.all([
    {
      input: `src/${LIB_NAME}.ts`,
      output: [
        { file: `dist/es2015/${LIB_NAME}.js`, format: 'es' }
      ],
      external: externalLibs,
      plugins: [
        typescript({
          tsconfigOverride: {
            compilerOptions: {
              target: 'es2015'
            }
          },
          cacheRoot: cacheRoot
        }),
      ]
    },
    {
      input: `src/${LIB_NAME}.ts`,
      output: [
        { file: `dist/commonjs/${LIB_NAME}.js`, format: 'cjs' },
        { file: `dist/amd/${LIB_NAME}.js`, format: 'amd', amd: { id: LIB_NAME } },
        { file: `dist/native-${LIB_NAME}/index.js`, format: 'es' }
      ],
      external: externalLibs,
      plugins: [
        typescript({
          useTsconfigDeclarationDir: true,
          tsconfigOverride: {
            compilerOptions: {
              target: 'es5'
            }
          },
          cacheRoot: cacheRoot
        }),
      ]
    }
  ].map(cfg => {
    return rollup
      .rollup(cfg)
      .then(bundle => Promise.all(cfg.output.map(o => bundle.write(o))));
  }));
};


@@ -1483,32 +1483,49 @@
 s%60, format: 'es'
+, sourcemap: true
  %7D%0A      %5D,%0A    
@@ -1883,16 +1883,33 @@
 t: 'cjs'
+, sourcemap: true
  %7D,%0A    
@@ -1983,16 +1983,33 @@
 B_NAME %7D
+, sourcemap: true
  %7D,%0A    
@@ -2033,16 +2033,24 @@
 /native-
+modules/
 $%7BLIB_NA
@@ -2056,14 +2056,8 @@
 AME%7D
-/index
 .js%60
@@ -2070,16 +2070,33 @@
 at: 'es'
+, sourcemap: true
  %7D%0A     

fix build