const fs = require('fs');

function DtsBundlePlugin() { }

DtsBundlePlugin.prototype.apply = function(compiler)  {
    compiler.plugin('after-emit', (compilation, callback) => {
        var dts = require('dts-bundle');

        fs.access('src/nonstop.d.ts', fs.constants.F_OK, function(err) {
            if(err) {
                callback();
            } else {
                dts.bundle({
                    name: 'nonstop',
                    main: 'src/nonstop.d.ts',
                    out: '../dist/nonstop.d.ts',
                    removeSource: true,
                    outputAsModuleFolder: true
                });
        
                callback();
            }
        });
    });
};

module.exports = DtsBundlePlugin;