Comparing version 0.0.1 to 0.1.0
import { Module, Walker } from 'flora-colossus'; | ||
export declare type ShouldKeepModuleTest = (module: Module, isDevDep: boolean) => boolean; | ||
export declare type ModuleMap = Map<string, Module>; | ||
export declare class DestroyerOfModules { | ||
@@ -9,9 +10,8 @@ private walker; | ||
walker?: Walker; | ||
shouldKeepModuleTest: ShouldKeepModuleTest; | ||
shouldKeepModuleTest?: ShouldKeepModuleTest; | ||
}); | ||
destroyModule(modulePath: string, moduleMap: ModuleMap): Promise<void>; | ||
collectKeptModules(): Promise<ModuleMap>; | ||
destroy(): Promise<void>; | ||
private shouldKeepModule(module); | ||
destroyModule(modulePath: string, moduleMap: { | ||
[path: string]: Module; | ||
}): Promise<void>; | ||
destroy(): Promise<void>; | ||
} |
@@ -57,7 +57,2 @@ "use strict"; | ||
} | ||
DestroyerOfModules.prototype.shouldKeepModule = function (module) { | ||
var isDevDep = module.depType === flora_colossus_1.DepType.DEV; | ||
var shouldKeep = this.shouldKeepFn ? this.shouldKeepFn(module, isDevDep) : !isDevDep; | ||
return shouldKeep; | ||
}; | ||
DestroyerOfModules.prototype.destroyModule = function (modulePath, moduleMap) { | ||
@@ -69,9 +64,10 @@ return __awaiter(this, void 0, void 0, function () { | ||
case 0: | ||
module = moduleMap[modulePath]; | ||
if (!(module && this.shouldKeepModule(module))) return [3 /*break*/, 13]; | ||
module = moduleMap.get(modulePath); | ||
if (!module) return [3 /*break*/, 13]; | ||
nodeModulesPath = path.resolve(modulePath, 'node_modules'); | ||
return [4 /*yield*/, fs.pathExists(nodeModulesPath)]; | ||
case 1: | ||
if (!(_d.sent())) | ||
if (!(_d.sent())) { | ||
return [2 /*return*/]; | ||
} | ||
_i = 0; | ||
@@ -119,3 +115,3 @@ return [4 /*yield*/, fs.readdir(nodeModulesPath)]; | ||
}; | ||
DestroyerOfModules.prototype.destroy = function () { | ||
DestroyerOfModules.prototype.collectKeptModules = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -128,10 +124,26 @@ var modules, moduleMap, _i, modules_1, module_1; | ||
modules = _a.sent(); | ||
moduleMap = {}; | ||
moduleMap = new Map(); | ||
for (_i = 0, modules_1 = modules; _i < modules_1.length; _i++) { | ||
module_1 = modules_1[_i]; | ||
moduleMap[module_1.path] = module_1; | ||
if (this.shouldKeepModule(module_1)) { | ||
moduleMap.set(module_1.path, module_1); | ||
} | ||
} | ||
return [4 /*yield*/, this.destroyModule(this.walker.getRootModule(), moduleMap)]; | ||
return [2 /*return*/, moduleMap]; | ||
} | ||
}); | ||
}); | ||
}; | ||
DestroyerOfModules.prototype.destroy = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = this.destroyModule; | ||
_b = [this.walker.getRootModule()]; | ||
return [4 /*yield*/, this.collectKeptModules()]; | ||
case 1: return [4 /*yield*/, _a.apply(this, _b.concat([_c.sent()]))]; | ||
case 2: | ||
_a.sent(); | ||
_c.sent(); | ||
return [2 /*return*/]; | ||
@@ -142,2 +154,7 @@ } | ||
}; | ||
DestroyerOfModules.prototype.shouldKeepModule = function (module) { | ||
var isDevDep = module.depType === flora_colossus_1.DepType.DEV; | ||
var shouldKeep = this.shouldKeepFn ? this.shouldKeepFn(module, isDevDep) : !isDevDep; | ||
return shouldKeep; | ||
}; | ||
return DestroyerOfModules; | ||
@@ -144,0 +161,0 @@ }()); |
{ | ||
"name": "galactus", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Prunes dependencies from your package", | ||
@@ -9,3 +9,5 @@ "main": "lib/index.js", | ||
"prepublish": "npm run build", | ||
"test": "mocha --require ts-node/register test/*_spec.ts" | ||
"lint": "tslint src/**/*.ts test/**/*.ts", | ||
"mocha": "nyc mocha test/*_spec.ts", | ||
"test": "npm run lint && npm run mocha" | ||
}, | ||
@@ -23,5 +25,9 @@ "keywords": [ | ||
"@types/mocha": "^2.2.45", | ||
"@types/tempy": "^0.1.0", | ||
"chai": "^4.1.2", | ||
"mocha": "^4.1.0", | ||
"nyc": "^11.4.1", | ||
"tempy": "^0.2.1", | ||
"ts-node": "^4.1.0", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.4.2" | ||
@@ -33,3 +39,13 @@ }, | ||
"fs-extra": "^4.0.0" | ||
}, | ||
"nyc": { | ||
"extension": [ | ||
".ts" | ||
], | ||
"instrument": true, | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"sourceMap": true | ||
} | ||
} |
@@ -35,2 +35,9 @@ Galactus | ||
optional dependencies. You can override this behavior by providing a | ||
`shouldKeepModuleTest` function in the constructor. | ||
`shouldKeepModuleTest` function in the constructor. | ||
#### `destroyer.collectKeptModules()` | ||
Returns a `Promise` of a `ModuleMap` (a `Map` of paths to `Module`s). The | ||
`Promise` resolves when the walker finishes walking the module tree. The | ||
`ModuleMap` only contains the `Module`s that would be kept by a call | ||
to `destroy()`. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
67915
225
43
12