"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const tape = require('tape');
const promisifyTape = require('tape-promise');
const test = promisifyTape(tape);
const path = require('path');
const src_1 = require('../src');
const prepare_1 = require('./support/prepare');
const exists = require('exists-file');
const existsSymlink = require('exists-link');
const testDefaults_1 = require('./support/testDefaults');
test('prune removes extraneous packages', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-negative@2.1.0'], testDefaults_1.default({ save: true }));
        yield src_1.installPkgs(['applyq@0.2.1'], testDefaults_1.default({ saveDev: true }));
        yield src_1.installPkgs(['fnumber@0.1.0'], testDefaults_1.default({ saveOptional: true }));
        yield src_1.installPkgs(['is-positive@2.0.0', '@zkochan/logger@0.1.0'], testDefaults_1.default());
        yield src_1.prune(testDefaults_1.default());
        const store = path.join(process.cwd(), 'node_modules/.store/nested');
        const modules = path.join(process.cwd(), 'node_modules');
        let stat = yield exists(path.join(store, 'is-positive@2.0.0'));
        t.ok(!stat, 'extraneous package is removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-positive'));
        t.ok(!stat, 'extraneous package is removed from node_modules');
        stat = yield exists(path.join(store, '@zkochan+logger@0.1.0'));
        t.ok(!stat, 'scoped extraneous package is removed from store');
        stat = yield existsSymlink(path.join(modules, '@zkochan/logger'));
        t.ok(!stat, 'scoped extraneous package is removed from node_modules');
        stat = yield exists(path.join(store, 'is-negative@2.1.0'));
        t.ok(stat, 'dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-negative'));
        t.ok(stat, 'dependency package is not removed from node_modules');
        stat = yield exists(path.join(store, 'applyq@0.2.1'));
        t.ok(stat, 'dev dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'applyq'));
        t.ok(stat, 'dev dependency package is not removed from node_modules');
        stat = yield exists(path.join(store, 'fnumber@0.1.0'));
        t.ok(stat, 'optional dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'fnumber'));
        t.ok(stat, 'optional dependency package is not removed from node_modules');
    });
});
test('prune removes only the specified extraneous packages', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-positive@2.0.0', 'is-negative@2.1.0'], testDefaults_1.default());
        yield src_1.prunePkgs(['is-positive'], testDefaults_1.default());
        const store = path.join(process.cwd(), 'node_modules/.store/nested');
        const modules = path.join(process.cwd(), 'node_modules');
        let stat = yield exists(path.join(store, 'is-positive@2.0.0'));
        t.ok(!stat, 'extraneous package is removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-positive'));
        t.ok(!stat, 'extraneous package is removed from node_modules');
        stat = yield exists(path.join(store, 'is-negative@2.1.0'));
        t.ok(stat, 'dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-negative'));
        t.ok(stat, 'dependency package is not removed from node_modules');
    });
});
test('prune throws error when trying to removes not an extraneous package', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-positive@2.0.0'], testDefaults_1.default({ save: true }));
        try {
            yield src_1.prunePkgs(['is-positive'], testDefaults_1.default());
            t.fail('prune had to fail');
        }
        catch (err) {
            t.equal(err['code'], 'PRUNE_NOT_EXTR', 'cannot prune non-extraneous package error thrown');
        }
    });
});
test('prune removes dev dependencies in production', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-positive@2.0.0'], testDefaults_1.default({ saveDev: true }));
        yield src_1.installPkgs(['is-negative@2.1.0'], testDefaults_1.default({ save: true }));
        yield src_1.installPkgs(['fnumber@0.1.0'], testDefaults_1.default({ saveOptional: true }));
        yield src_1.prune(testDefaults_1.default({ production: true }));
        const store = path.join(process.cwd(), 'node_modules/.store/nested');
        const modules = path.join(process.cwd(), 'node_modules');
        let stat = yield exists(path.join(store, 'is-positive@2.0.0'));
        t.ok(!stat, 'dev dependency package is removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-positive'));
        t.ok(!stat, 'dev dependency package is removed from node_modules');
        stat = yield exists(path.join(store, 'is-negative@2.1.0'));
        t.ok(stat, 'dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'is-negative'));
        t.ok(stat, 'dependency package is not removed from node_modules');
        stat = yield exists(path.join(store, 'fnumber@0.1.0'));
        t.ok(stat, 'optional dependency package is not removed from store');
        stat = yield existsSymlink(path.join(modules, 'fnumber'));
        t.ok(stat, 'optional dependency package is not removed from node_modules');
    });
});
//# sourceMappingURL=prune.js.map