Skip to content

Commit d95b01a

Browse files
author
Mila Votradovec
committed
feat: export file and parsing interface + readibility improvements
1 parent 69290b1 commit d95b01a

File tree

1 file changed

+57
-58
lines changed

1 file changed

+57
-58
lines changed

lib/index.ts

+57-58
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,7 @@ import * as fs from 'fs';
33
import * as path from 'path';
44
import * as _ from 'lodash';
55

6-
export default function parseLockFile(root, targetFilePath, lockFilePath, options) {
7-
if (!root || !lockFilePath || !lockFilePath) {
8-
throw new Error('Missing required parameters for parseLockFile()');
9-
}
10-
// TODO: validate only valid options were passed in
11-
12-
const targetFileFullPath = path.resolve(root, targetFilePath);
13-
const lockFileFullPath = path.resolve(root, lockFilePath);
14-
15-
if (!fs.existsSync(targetFilePath)) {
16-
throw new Error(`Target file package.json not found at location: ${targetFileFullPath}`);
17-
}
18-
if (!fs.existsSync(lockFilePath)) {
19-
throw new Error(`LockFile package-lock.json not found at location: ${lockFileFullPath}`);
20-
}
21-
22-
const targetFile = fs.readFileSync(targetFileFullPath);
23-
const lockFile = fs.readFileSync(lockFileFullPath);
24-
25-
return buildDepTree(targetFile, lockFile, options);
26-
}
27-
28-
async function buildDepTree(targetFileRaw, lockFileRaw, options) {
6+
export async function buildDepTree(targetFileRaw, lockFileRaw, options) {
297

308
const lockFile = JSON.parse(lockFileRaw);
319
const targetFile = JSON.parse(targetFileRaw);
@@ -43,49 +21,48 @@ async function buildDepTree(targetFileRaw, lockFileRaw, options) {
4321
version: targetFile.version || undefined,
4422
};
4523

46-
const fullDepList = lockFile.dependencies;
4724
const topLevelDeps = Object.keys(targetFile.dependencies);
4825

4926
await Promise.all(topLevelDeps.map(async (dep) => {
50-
depTree.dependencies[dep] = await buildSubTreeRecursive(dep, []);
27+
depTree.dependencies[dep] = await buildSubTreeRecursive(dep, [], lockFile);
5128
}));
5229

5330
return depTree;
31+
}
32+
33+
async function buildSubTreeRecursive(dep: string, depKeys: string[], lockFile: object) {
5434

55-
async function buildSubTreeRecursive(dep: string, depKeys: string[]) {
56-
57-
const depSubTree = {
58-
dependencies: {},
59-
name: dep,
60-
version: undefined,
61-
};
62-
63-
// Get path to the nested dependencies from list ['package1', 'package2']
64-
// to ['dependencies', 'package1', 'dependencies', 'package2', 'dependencies']
65-
const depPath = getDepPath(depKeys);
66-
// try to get list of deps on the path
67-
const deps = _.get(lockFile, depPath);
68-
69-
// If exists and looked-up dep is there
70-
if (deps && deps[dep]) {
71-
// update the tree
72-
depSubTree.version = deps[dep].version;
73-
// repeat the process for dependencies of looked-up dep
74-
const newDeps = deps[dep].requires && Object.keys(deps[dep].requires) || [];
75-
await Promise.all(newDeps.map(async (subDep) => {
76-
depSubTree.dependencies[subDep] = await buildSubTreeRecursive(subDep, [...depKeys, dep]);
77-
}));
78-
return depSubTree;
79-
} else {
80-
// tree was walked to the root and dependency was not found
81-
if (!depKeys.length) {
82-
throw new Error(`Dependency ${dep} was not found in package-lock.json.
83-
Your package.json and package-lock.json are probably out of sync.
84-
Please run npm install and try to parse the log again.`);
85-
}
86-
// dependency was not found on a current path, remove last key (move closer to the root) and try again
87-
return buildSubTreeRecursive(dep, depKeys.slice(0, -1));
35+
const depSubTree = {
36+
dependencies: {},
37+
name: dep,
38+
version: undefined,
39+
};
40+
41+
// Get path to the nested dependencies from list ['package1', 'package2']
42+
// to ['dependencies', 'package1', 'dependencies', 'package2', 'dependencies']
43+
const depPath = getDepPath(depKeys);
44+
// try to get list of deps on the path
45+
const deps = _.get(lockFile, depPath);
46+
47+
// If exists and looked-up dep is there
48+
if (deps && deps[dep]) {
49+
// update the tree
50+
depSubTree.version = deps[dep].version;
51+
// repeat the process for dependencies of looked-up dep
52+
const newDeps = deps[dep].requires ? Object.keys(deps[dep].requires) : [];
53+
await Promise.all(newDeps.map(async (subDep) => {
54+
depSubTree.dependencies[subDep] = await buildSubTreeRecursive(subDep, [...depKeys, dep], lockFile);
55+
}));
56+
return depSubTree;
57+
} else {
58+
// tree was walked to the root and dependency was not found
59+
if (!depKeys.length) {
60+
throw new Error(`Dependency ${dep} was not found in package-lock.json.
61+
Your package.json and package-lock.json are probably out of sync.
62+
Please run npm install and try to parse the log again.`);
8863
}
64+
// dependency was not found on a current path, remove last key (move closer to the root) and try again
65+
return buildSubTreeRecursive(dep, depKeys.slice(0, -1), lockFile);
8966
}
9067
}
9168

@@ -96,3 +73,25 @@ function getDepPath(depKeys: string[]) {
9673

9774
return depPath;
9875
}
76+
77+
export function buildDepTreeFromFiles(root, targetFilePath, lockFilePath, options) {
78+
if (!root || !lockFilePath || !lockFilePath) {
79+
throw new Error('Missing required parameters for parseLockFile()');
80+
}
81+
// TODO: validate only valid options were passed in
82+
83+
const targetFileFullPath = path.resolve(root, targetFilePath);
84+
const lockFileFullPath = path.resolve(root, lockFilePath);
85+
86+
if (!fs.existsSync(targetFilePath)) {
87+
throw new Error(`Target file package.json not found at location: ${targetFileFullPath}`);
88+
}
89+
if (!fs.existsSync(lockFilePath)) {
90+
throw new Error(`LockFile package-lock.json not found at location: ${lockFileFullPath}`);
91+
}
92+
93+
const targetFile = fs.readFileSync(targetFileFullPath);
94+
const lockFile = fs.readFileSync(lockFileFullPath);
95+
96+
return buildDepTree(targetFile, lockFile, options);
97+
}

0 commit comments

Comments
 (0)