Skip to content

Commit

Permalink
feat: add package.json parser #20
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Apr 23, 2020
1 parent 7a20ec0 commit 494eb67
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 157 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@
"@types/jest": "^25.2.1",
"@types/jest-when": "^2.7.1",
"@types/node": "^13.13.2",
"@types/node-fetch": "^2.5.6",
"@types/node-fetch": "^2.5.7",
"@types/pacote": "^11.1.0",
"@types/semver": "^7.1.0",
"@types/shell-quote": "^1.6.1",
"@types/tmp": "^0.1.0",
"@types/yaml": "^1.2.0",
"@types/yaml": "^1.9.7",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
Expand Down
4 changes: 2 additions & 2 deletions src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { packageInfoModulesBinder } from '../utils/packageInfo';
import { nodeModulesBinder } from './nodeModulesContainer';
import { enginesResolveModulesBinder } from '../resolvers/enginesResolver';
import { loggerModuleBinder } from '../utils/logger';
import { packageJsonParserModulesBinder } from '../utils/packageJsonParser';
import { manifestParserModulesBinder } from '../utils/manifestParser';

export const container = new Container({
skipBaseClassChecks: true,
Expand All @@ -35,7 +35,7 @@ const binders: Binder[] = [
packageInfoModulesBinder,
enginesResolveModulesBinder,
loggerModuleBinder,
packageJsonParserModulesBinder,
manifestParserModulesBinder,
];

container.load(
Expand Down
2 changes: 1 addition & 1 deletion src/dependencyChecker/interfaces/IDependencyChecker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Moment } from 'moment';
import { DependencyType } from '../../utils/packageJsonParser';
import { DependencyType } from '../../utils/manifestParser';

export interface IDependencyCheckerRunOptions {
pkg: {
Expand Down
62 changes: 62 additions & 0 deletions src/utils/manifestParser/impl/manifestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { injectable } from 'inversify';
import { ILoggerFactory } from '../../logger';
import { ILogger } from '../../logger/interfaces/ILogger';
import { IManifestParser, IManifestParserOptions } from '../interfaces/IManifestParser';
import { IPredicate } from '../../predicateBuilder/predicateBuilder';
import { DependencyType, IDependency, IPackageData } from '../types';

@injectable()
export class ManifestParser extends IManifestParser {
private readonly logger: ILogger;

constructor(loggerFactory: ILoggerFactory) {
super();
this.logger = loggerFactory.getLogger(`Manifest Parser`);
}

public async parse({ manifest, include, predicate }: IManifestParserOptions): Promise<IPackageData> {
this.logger.info(`Reading manifest information`);
const dependencies = new Set<IDependency>();
const depTypeHandler = this.getDepTypeHandler(dependencies, predicate);
depTypeHandler(include.dev, manifest.devDependencies, DependencyType.DEV, `dev`);
depTypeHandler(include.prod, manifest.dependencies, DependencyType.PROD, `production`);
depTypeHandler(include.peer, manifest.peerDependencies, DependencyType.PEER, `peer`);
depTypeHandler(include.optional, manifest.optionalDependencies, DependencyType.OPTIONAL, `optional`);
this.logger.info(`Going to check ${dependencies.size} dependencies`);
return {
dependencies,
};
}

private getDepTypeHandler(deps: Set<IDependency>, predicate: IPredicate) {
return (
included: boolean,
currentDeps: undefined | Record<string, string>,
type: DependencyType,
typeStr: string
): void => {
if (included) {
if (currentDeps) {
let count = 0;
for (const [name, semver] of Object.entries(currentDeps)) {
if (predicate(name)) {
count++;
deps.add({
name,
semver,
dependencyType: type,
});
} else {
this.logger.info(`Skipping ${name}`);
}
}
this.logger.info(`Found ${count} ${typeStr} dependencies`);
} else {
this.logger.info(`No ${typeStr} dependencies defined`);
}
} else {
this.logger.info(`Skipping ${typeStr} dependencies`);
}
};
}
}
43 changes: 43 additions & 0 deletions src/utils/manifestParser/impl/npmGlobalManifestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { injectable } from 'inversify';
import { ILoggerFactory } from '../../logger';
import { ILogger } from '../../logger/interfaces/ILogger';
import { INpmGlobalManifestParser, INpmGlobalManifestParserOptions } from '../interfaces/INpmGlobalManifestParser';
import { DependencyType, IDependency, IPackageData } from '../types';

@injectable()
export class NpmGlobalManifestParser extends INpmGlobalManifestParser {
private readonly logger: ILogger;

constructor(loggerFactory: ILoggerFactory) {
super();
this.logger = loggerFactory.getLogger(`Npm Global Manifest Parser`);
}

public async parse({ manifest, predicate }: INpmGlobalManifestParserOptions): Promise<IPackageData> {
this.logger.info(`Reading npm global manifest information`);
const dependencies = new Set<IDependency>();
const currentDeps = manifest.dependencies;
if (currentDeps) {
let count = 0;
for (const [name, info] of Object.entries(currentDeps)) {
if (predicate(name)) {
count++;
dependencies.add({
name,
semver: info.version,
dependencyType: DependencyType.GLOBAL,
});
} else {
this.logger.info(`Skipping ${name}`);
}
}
this.logger.info(`Found ${count} global dependencies`);
} else {
this.logger.info(`No global dependencies found`);
}
this.logger.info(`Going to check ${dependencies.size} dependencies`);
return {
dependencies,
};
}
}
15 changes: 15 additions & 0 deletions src/utils/manifestParser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { interfaces } from 'inversify';
import Bind = interfaces.Bind;
import { IManifestParser } from './interfaces/IManifestParser';
import { ManifestParser } from './impl/manifestParser';
import { INpmGlobalManifestParser } from './interfaces/INpmGlobalManifestParser';
import { NpmGlobalManifestParser } from './impl/npmGlobalManifestParser';

export const manifestParserModulesBinder = (bind: Bind): void => {
bind<IManifestParser>(IManifestParser).to(ManifestParser).inSingletonScope();
bind<INpmGlobalManifestParser>(INpmGlobalManifestParser).to(NpmGlobalManifestParser).inSingletonScope();
};

export * from './interfaces/IManifestParser';
export * from './interfaces/INpmGlobalManifestParser';
export * from './types';
18 changes: 18 additions & 0 deletions src/utils/manifestParser/interfaces/IManifestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IPredicate } from '../../predicateBuilder/predicateBuilder';
import { Manifest } from 'pacote';
import { IPackageData } from '../types';

export interface IManifestParserOptions {
manifest: Manifest;
include: {
prod: boolean;
dev: boolean;
optional: boolean;
peer: boolean;
};
predicate: IPredicate;
}

export abstract class IManifestParser {
public abstract async parse(options: IManifestParserOptions): Promise<IPackageData>;
}
15 changes: 15 additions & 0 deletions src/utils/manifestParser/interfaces/INpmGlobalManifestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IPackageData } from '../types';
import { IPredicate } from '../../predicateBuilder/predicateBuilder';

export interface INpmGlobalManifest {
dependencies?: Record<string, { version: string }>;
}

export interface INpmGlobalManifestParserOptions {
manifest: INpmGlobalManifest;
predicate: IPredicate;
}

export abstract class INpmGlobalManifestParser {
public abstract async parse(options: INpmGlobalManifestParserOptions): Promise<IPackageData>;
}
17 changes: 17 additions & 0 deletions src/utils/manifestParser/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum DependencyType {
PROD = `production`,
DEV = `development`,
PEER = `peer`,
OPTIONAL = `optional`,
GLOBAL = `global`,
}

export interface IDependency {
semver: string;
name: string;
dependencyType: DependencyType;
}

export interface IPackageData {
dependencies: Set<IDependency>;
}
78 changes: 0 additions & 78 deletions src/utils/packageJsonParser/impl/packageJsonParser.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/utils/packageJsonParser/index.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/utils/packageJsonParser/interfaces/IPackageJsonParser.ts

This file was deleted.

25 changes: 25 additions & 0 deletions test/src/utils/manifestParser/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BindingTypes, testBindings } from '../../../common/testers/bindingTester';
import {
IManifestParser,
INpmGlobalManifestParser,
manifestParserModulesBinder,
} from '../../../../src/utils/manifestParser';
import { ManifestParser } from '../../../../src/utils/manifestParser/impl/manifestParser';
import { NpmGlobalManifestParser } from '../../../../src/utils/manifestParser/impl/npmGlobalManifestParser';

testBindings({
name: `manifest parser module container`,
binderFn: manifestParserModulesBinder,
bindings: [
{
binder: IManifestParser,
binded: ManifestParser,
type: BindingTypes.SINGELTON,
},
{
binder: INpmGlobalManifestParser,
binded: NpmGlobalManifestParser,
type: BindingTypes.SINGELTON,
},
],
});
15 changes: 0 additions & 15 deletions test/src/utils/packageJsonParser/index.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"checkJs": false,
"allowJs": false,
"strict": true,
"skipLibCheck": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedLocals": true,
Expand Down
Loading

0 comments on commit 494eb67

Please sign in to comment.