"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const ReplaySubject_1 = require("rxjs/ReplaySubject");
const fs = require('fs');
const archiver = require('archiver');
const replace = require('replace-in-file');
function jar(project, options) {
    const subject = new ReplaySubject_1.ReplaySubject();
    const packageOptions = require(path.resolve(project.root, 'package.json'));
    const outputPath = path.resolve(project.root, options.outputJarPath);
    if (!fs.existsSync(outputPath)) {
        fs.mkdirSync(outputPath);
    }
    const jarPath = path.resolve(outputPath, packageOptions.name + '.jar');
    replace.sync({
        files: path.resolve(options.inputJarPath, '/src/main/resources/META-INF/MANIFEST.MF'),
        from: /Bnd-LastModified: .*/,
        to: 'Bnd-LastModified: ' + Date.now()
    });
    replace.sync({
        files: path.resolve(options.inputJarPath, '/src/main/resources/META-INF/MANIFEST.MF'),
        from: /Bundle-Version: .*/,
        to: 'Bundle-Version: ' + packageOptions.version
    });
    const archive = archiver('zip');
    const output = fs.createWriteStream(jarPath);
    output.on('close', function () {
        subject.next(archive.pointer());
        subject.complete();
    });
    archive.on('error', (error) => {
        subject.error(error);
        subject.complete();
    });
    archive.pipe(output);
    archive.directory(options.inputJarPath + '/src/main/resources/', '/')
        .file(options.inputJarPath + '/src/main/resources/META-INF/MANIFEST.MF', { name: '/META-INF/MANIFEST.MF' })
        .directory(options.inputJarPath + '/dist', '/META-INF/resources');
    if (fs.existsSync(options.inputJarPath + '/build/classes/main')) {
        archive.directory(options.inputJarPath + '/build/classes/main', '/');
    }
    archive.finalize();
    return subject.asObservable();
}
exports.jar = jar;
//# sourceMappingURL=/home/ddavis/Projects/xtivia/speedray/speedray-cli/lib/speedray/jar.js.map
