Skip to content

Commit

Permalink
Merge pull request #13 from snyk/chore/more-types
Browse files Browse the repository at this point in the history
Chore/more types
  • Loading branch information
michael-go authored Aug 13, 2018
2 parents 6c5a766 + 4e08bcb commit 8135695
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 143 deletions.
33 changes: 23 additions & 10 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@ import * as fs from 'fs';
import * as path from 'path';
import * as _ from 'lodash';

export async function buildDepTree(targetFileRaw, lockFileRaw, options) {
export {
buildDepTree,
buildDepTreeFromFiles,
};

interface PkgTree {
name: string;
version: string;
dependencies?: {
[dep: string]: PkgTree;
};
}

async function buildDepTree(targetFileRaw: string, lockFileRaw: string): Promise<PkgTree> {

const lockFile = JSON.parse(lockFileRaw);
const targetFile = JSON.parse(targetFileRaw);
Expand All @@ -15,10 +28,10 @@ export async function buildDepTree(targetFileRaw, lockFileRaw, options) {
throw new Error("No 'dependencies' property in package-lock.json");
}

const depTree = {
const depTree: PkgTree = {
dependencies: {},
name: targetFile.name || undefined,
version: targetFile.version || undefined,
name: targetFile.name,
version: targetFile.version,
};

const topLevelDeps = Object.keys(targetFile.dependencies);
Expand All @@ -30,9 +43,9 @@ export async function buildDepTree(targetFileRaw, lockFileRaw, options) {
return depTree;
}

async function buildSubTreeRecursive(dep: string, depKeys: string[], lockFile: object) {
async function buildSubTreeRecursive(dep: string, depKeys: string[], lockFile: object): Promise<PkgTree> {

const depSubTree = {
const depSubTree: PkgTree = {
dependencies: {},
name: dep,
version: undefined,
Expand Down Expand Up @@ -74,7 +87,7 @@ function getDepPath(depKeys: string[]) {
return depPath;
}

export function buildDepTreeFromFiles(root, targetFilePath, lockFilePath, options) {
async function buildDepTreeFromFiles(root: string, targetFilePath: string, lockFilePath: string): Promise<PkgTree> {
if (!root || !lockFilePath || !lockFilePath) {
throw new Error('Missing required parameters for parseLockFile()');
}
Expand All @@ -89,8 +102,8 @@ export function buildDepTreeFromFiles(root, targetFilePath, lockFilePath, option
throw new Error(`LockFile package-lock.json not found at location: ${lockFileFullPath}`);
}

const targetFile = fs.readFileSync(targetFileFullPath);
const lockFile = fs.readFileSync(lockFileFullPath);
const targetFile = fs.readFileSync(targetFileFullPath, 'utf-8');
const lockFile = fs.readFileSync(lockFileFullPath, 'utf-8');

return buildDepTree(targetFile, lockFile, options);
return await buildDepTree(targetFile, lockFile);
}
103 changes: 4 additions & 99 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"test": "npm run lint && npm run unit-test",
"unit-test": "tap test/lib -R=spec --timeout=300 --node-path ts-node --test-file-pattern '/\\.[tj]s$/'",
"lint": "tslint lib/**/*.ts",
"lint": "tslint -p tsconfig.json",
"build": "tsc",
"build-watch": "tsc -w",
"prepare": "npm run build",
Expand All @@ -21,22 +21,26 @@
},
"author": "snyk.io",
"license": "Apache-2.0",
"engines": { "node" : ">=4" },
"files": ["bin", "dist"],
"engines": {
"node": ">=4"
},
"files": [
"bin",
"dist"
],
"homepage": "https://github.com/snyk/nodejs-lockfile-parser#readme",
"dependencies": {
"lodash": "4.17.10",
"path": "0.12.7",
"source-map-support": "^0.5.7"
},
"devDependencies": {
"@types/lodash": "^4.14.116",
"@types/node": "10.5.5",
"@types/sinon": "5.0.1",
"sinon": "6.1.4",
"semantic-release": "^15.9.3",
"tap": "github:snyk/node-tap#alternative-runtimes",
"ts-node": "7.0.0",
"tslint": "5.11.0",
"typescript": "3.0.1",
"semantic-release": "^15.9.3"
"typescript": "3.0.1"
}
}
3 changes: 0 additions & 3 deletions test/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// tslint:disable:max-line-length
// tslint:disable:object-literal-key-quotes
import {test} from 'tap';
import * as sinon from 'sinon';
import {buildDepTreeFromFiles} from '../../lib';
import * as fs from 'fs';

Expand All @@ -18,7 +17,6 @@ test('Parse npm package-lock.json', async (t) => {
`${__dirname}/fixtures/goof/`,
'package.json',
'package-lock.json',
null,
);

t.deepEqual(depTree, JSON.parse(expectedDepTree), 'Tree generated as expected');
Expand All @@ -29,6 +27,5 @@ test('Parse npm package-lock.json with missing dependency', async (t) => {
`${__dirname}/fixtures/goof/`,
'package.json',
'package-lock_missing_dep.json',
null,
), null, 'Error is thrown');
});
31 changes: 7 additions & 24 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@
"max-line-length": {
"options": [120]
},
"array-bracket-spacing": [2, "never"],
"comma-dangle": ["error", "always-multiline"],
"computed-property-spacing": [2, "never"],
"curly": 2,
"eqeqeq": [2, "smart"],
"indent": [
2,
2,
{
"SwitchCase": 1
}
],
"max-depth": [1, 3],
"max-len": [1, 80],
"max-statements": [1, 15],
"no-use-before-define": [2, "nofunc"],
"object-curly-spacing": [2, "never"],
"trailing-comma": [true, {"multiline": "always"}],
"triple-equals": true,
"curly": true,
"indent": [true, "spaces", 2],
"interface-name": false,
"no-use-before-declare": true,
"ordered-imports": false,
"quotemark": [true, "single", "avoid-escape", "avoid-template"],
"semi": [2, "always"],
"keyword-spacing": [2, {"before": true, "after": true}]
},
"jsRules": {
"max-line-length": {
"options": [120]
}
"semicolon": [true, "always"]
}
}

0 comments on commit 8135695

Please sign in to comment.