const fs = require('fs');
const archiver = require('archiver');

const packageJson = JSON.parse(fs.readFileSync('package.json'));

const releaseFolder = 'release';
if (!fs.existsSync(releaseFolder)) {
    fs.mkdirSync(releaseFolder);
}

const outputFile = `./${packageJson.name}.zip`;

const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(outputFile);

stream.on('close', () => {

    fs.rename(outputFile, `./release/${outputFile}`, err => {
        if (err) {
            console.error(`Error moving archive: ${err}`);
        } else {
        }
    });
});

archive.on('warning', err => {
    if (err.code === 'ENOENT') {
        console.warn(err);
    } else {
        throw err;
    }
});

archive.on('error', err => {
    throw err;
});

archive.pipe(stream);

archive.directory(releaseFolder, false);

archive.finalize();
