"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 fs = require('fs');
const exists = require('exists-file');
const existsSymlink = require('exists-link');
const prepare_1 = require('./support/prepare');
const testDefaults_1 = require('./support/testDefaults');
const src_1 = require('../src');
test('uninstall package with no dependencies', 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.uninstall(['is-negative'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/is-negative@2.1.0'));
        t.ok(!stat, 'is-negative is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative'));
        t.ok(!stat, 'is-negative is removed from node_modules');
        const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
        const dependencies = JSON.parse(pkgJson).dependencies;
        const expectedDeps = {};
        t.deepEqual(dependencies, expectedDeps, 'is-negative has been removed from dependencies');
    });
});
test('uninstall scoped package', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['@zkochan/logger@0.1.0'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['@zkochan/logger'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/@zkochan+logger@0.1.0'));
        t.ok(!stat, '@zkochan/logger is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', '@zkochan/logger'));
        t.ok(!stat, '@zkochan/logger is removed from node_modules');
        const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
        const dependencies = JSON.parse(pkgJson).dependencies;
        const expectedDeps = {};
        t.deepEqual(dependencies, expectedDeps, '@zkochan/logger has been removed from dependencies');
    });
});
test('uninstall tarball dependency', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['http://registry.npmjs.org/is-array/-/is-array-1.0.1.tgz'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['is-array'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/is-array-1.0.1#a83102a9c117983e6ff4d85311fb322231abe3d6'));
        t.ok(!stat, 'is-array is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'is-array'));
        t.ok(!stat, 'is-array is removed from node_modules');
        const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
        const dependencies = JSON.parse(pkgJson).dependencies;
        const expectedDeps = {};
        t.deepEqual(dependencies, expectedDeps, 'is-array has been removed from dependencies');
    });
});
test('uninstall package with dependencies and do not touch other deps', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-negative@2.1.0', 'camelcase-keys@3.0.0'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['camelcase-keys'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/camelcase-keys@2.1.0'));
        t.ok(!stat, 'camelcase-keys is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys'));
        t.ok(!stat, 'camelcase-keys is removed from node_modules');
        stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/camelcase@3.0.0'));
        t.ok(!stat, 'camelcase is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase'));
        t.ok(!stat, 'camelcase is removed from node_modules');
        stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/map-obj@1.0.1'));
        t.ok(!stat, 'map-obj is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj'));
        t.ok(!stat, 'map-obj is removed from node_modules');
        stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/is-negative@2.1.0'));
        t.ok(stat, 'is-negative is not removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative'));
        t.ok(stat, 'is-negative is not removed from node_modules');
        const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
        const dependencies = JSON.parse(pkgJson).dependencies;
        const expectedDeps = {
            'is-negative': '^2.1.0'
        };
        t.deepEqual(dependencies, expectedDeps, 'camelcase-keys has been removed from dependencies');
    });
});
test('uninstall package with its bin files', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['sh-hello-world@1.0.0'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['sh-hello-world'], testDefaults_1.default({ save: true }));
        // check for both a symlink and a file because in some cases the file will be a proxied not symlinked
        let stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world'));
        t.ok(!stat, 'sh-hello-world is removed from .bin');
        stat = yield exists(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world'));
        t.ok(!stat, 'sh-hello-world is removed from .bin');
    });
});
test('keep dependencies used by others', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['hastscript@3.0.0', 'camelcase-keys@3.0.0'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['camelcase-keys'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/camelcase-keys@2.1.0'));
        t.ok(!stat, 'camelcase-keys is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys'));
        t.ok(!stat, 'camelcase-keys is removed from node_modules');
        stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/camelcase@3.0.0'));
        t.ok(stat, 'camelcase is not removed from store');
        stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/map-obj@1.0.1'));
        t.ok(!stat, 'map-obj is removed from store');
        stat = yield existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj'));
        t.ok(!stat, 'map-obj is removed from node_modules');
        const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
        const dependencies = JSON.parse(pkgJson).dependencies;
        const expectedDeps = {
            'hastscript': '^3.0.0'
        };
        t.deepEqual(dependencies, expectedDeps, 'camelcase-keys has been removed from dependencies');
    });
});
test('keep dependency used by package', function (t) {
    return __awaiter(this, void 0, void 0, function* () {
        prepare_1.default();
        yield src_1.installPkgs(['is-not-positive@1.0.0', 'is-positive@3.1.0'], testDefaults_1.default({ save: true }));
        yield src_1.uninstall(['is-not-positive'], testDefaults_1.default({ save: true }));
        let stat = yield exists(path.join(process.cwd(), 'node_modules/.store/nested/is-positive@3.1.0'));
        t.ok(stat, 'is-positive is not removed from store');
    });
});
//# sourceMappingURL=uninstall.js.map