Skip to content

Commit

Permalink
refactor: parsePackageRow
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Jul 31, 2020
1 parent 7e12b92 commit 626d087
Show file tree
Hide file tree
Showing 28 changed files with 247 additions and 123 deletions.
4 changes: 3 additions & 1 deletion packages/@yarn-tool/yarnlock-diff/lib/diff-service.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference types="node" />
import { Diff } from "deep-diff";
import { Option } from "fp-ts/lib/Option";
export declare function buildDiff(oldYarnLockContent: (Buffer | string)[], newYarnLockContent: (Buffer | string)[]): Option<Diff<{}, {}>[]>;
import { ITSValueOrArray } from 'ts-type/lib/type/base';
import { IComputedPackage } from './diff-service/types';
export declare function buildDiff(oldYarnLockContent: ITSValueOrArray<Buffer | string>, newYarnLockContent: ITSValueOrArray<Buffer | string>): Option<Diff<IComputedPackage, IComputedPackage>[]>;
8 changes: 5 additions & 3 deletions packages/@yarn-tool/yarnlock-diff/lib/diff-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { fromNullable, Option } from "fp-ts/lib/Option";
import { yarnLockParse } from '@yarn-tool/yarnlock-parse/index';
import { computeHashmapOfPackageAndVersionList } from './diff-service/computeHashmapOfPackageAndVersionList';
import { buildComputedPackage } from './diff-service/buildComputedPackage';
import { ITSValueOrArray } from 'ts-type/lib/type/base';
import { IComputedPackage } from './diff-service/types';

export function buildDiff(
oldYarnLockContent: (Buffer | string)[],
newYarnLockContent: (Buffer | string)[],
): Option<Diff<{}, {}>[]>
oldYarnLockContent: ITSValueOrArray<Buffer | string>,
newYarnLockContent: ITSValueOrArray<Buffer | string>,
): Option<Diff<IComputedPackage, IComputedPackage>[]>
{
const oldPacakges = buildComputedPackage(oldYarnLockContent);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import { IComputedPackage } from './types';
export declare function buildComputedPackage(yarnLockContentList: (Buffer | string)[], alreadyComputedPackage?: IComputedPackage): IComputedPackage;
import { ITSValueOrArray } from 'ts-type/lib/type/base';
export declare function buildComputedPackage(yarnLockContentList: ITSValueOrArray<Buffer | string>, alreadyComputedPackage?: IComputedPackage): IComputedPackage;

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

Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { IComputedPackage } from './types';
import { yarnLockParse } from '@yarn-tool/yarnlock-parse/index';
import { computeHashmapOfPackageAndVersionList } from './computeHashmapOfPackageAndVersionList';
import { ITSValueOrArray } from 'ts-type/lib/type/base';

export function buildComputedPackage(yarnLockContentList: (Buffer | string)[], alreadyComputedPackage: IComputedPackage = {})
export function buildComputedPackage(yarnLockContentList: ITSValueOrArray<Buffer | string>, alreadyComputedPackage: IComputedPackage = {})
{
if (!Array.isArray(yarnLockContentList))
{
yarnLockContentList = [yarnLockContentList];
}

return yarnLockContentList
.map(v => yarnLockParse(v))
.reduce(computeHashmapOfPackageAndVersionList, alreadyComputedPackage);
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,54 @@ import {
isYarnLockParsedV1,
isYarnLockParsedV2,
} from '@yarn-tool/yarnlock-parse/index';
import { computeHashmapOfPackageAndVersionList as v1 } from './v1/computeHashmapOfPackageAndVersionList';
import { computeHashmapOfPackageAndVersionList as v2 } from './v2/computeHashmapOfPackageAndVersionList';

import { parsePackageRow as v1 } from './v1/parsePackageRow';
import { parsePackageRow as v2 } from './v2/parsePackageRow';

import { IComputedPackage } from './types';
import { array_unique_overwrite } from 'array-hyper-unique/index';
import { compareLoose } from 'semver';

export function computeHashmapOfPackageAndVersionList(alreadyComputedPackage: IComputedPackage,
parsedOldPackage: IYarnLockParsedV1 | IYarnLockParsedV2): IComputedPackage
parsedOldPackage: IYarnLockParsedV1 | IYarnLockParsedV2,
): IComputedPackage
{
let fn: (...argv) => {
name: string;
version: string;
};

if (isYarnLockParsedV1(parsedOldPackage))
{
return v1(alreadyComputedPackage, parsedOldPackage.data)
fn = v1
}
else if (isYarnLockParsedV2(parsedOldPackage))
{
return v2(alreadyComputedPackage, parsedOldPackage.data)
fn = v2
}
else
{
throw new TypeError(`can't detect yarn.lock version`)
}

throw new TypeError(`can't detect yarn.lock version`)
Object.entries(parsedOldPackage.data)
.forEach(([packageName, packageData]) =>
{
const result = fn(packageName, packageData);

if (typeof result === 'undefined' || result === null)
{
return;
}

alreadyComputedPackage[result.name] ??= [];
alreadyComputedPackage[result.name].push(result.version);

array_unique_overwrite(alreadyComputedPackage[result.name])

alreadyComputedPackage[result.name].sort(compareLoose);
})
;

return alreadyComputedPackage;
}
4 changes: 4 additions & 0 deletions packages/@yarn-tool/yarnlock-diff/lib/diff-service/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface IComputedPackage {
[packagename: string]: string[];
}
export interface IParsePackageRow {
name: string;
version: string;
}
6 changes: 6 additions & 0 deletions packages/@yarn-tool/yarnlock-diff/lib/diff-service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export interface IComputedPackage
{
[packagename: string]: string[];
}

export interface IParsePackageRow
{
name: string;
version: string;
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const PACKAGE_REGEX: RegExp;

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PACKAGE_REGEX = /(?<packageName>.*)@(?:(?<semverPin>[\^\$])?(?<major>\d)(?:\.(?<minor>\d))?(?:\.(?<patch>\d))?(?:-(?<prerelease>[0-9a-zA-Z-]+)(?:.(?<prereleaseVersion>[0-9a-zA-Z]+))?)?(?:\+(?<metadata>[0-9a-zA-Z-]+)(?:.(?<metadataVersion>[0-9a-zA-Z]+))?)?|\*)/;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IPackageData } from './types';
import { IParsePackageRow } from '../types';
export declare function parsePackageRow(packageName: string, packageData: IPackageData): IParsePackageRow;

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IPackageData } from './types';
import { PACKAGE_REGEX } from './const';
import { IParsePackageRow } from '../types';

export function parsePackageRow(packageName: string, packageData: IPackageData): IParsePackageRow
{
const regexResult = PACKAGE_REGEX.exec(packageName);

const packageNameWithoutVersion =
regexResult?.groups?.packageName;

if (packageNameWithoutVersion)
{
return {
name: packageNameWithoutVersion,
version: packageData.version,
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { IParsePackageRow } from '../types';
export declare function parsePackageRow(packageName: string, packageData: any): IParsePackageRow;

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IPackageData } from '../v1/types';
import { IParsePackageRow } from '../types';

export function parsePackageRow(packageName: string, packageData): IParsePackageRow
{
throw new Error(`not implemented`)
}
4 changes: 3 additions & 1 deletion packages/@yarn-tool/yarnlock-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"chalk": "^4.1.0",
"cli-table": "^0.3.1",
"deep-diff": "^1.0.2",
"fp-ts": "^2.7.1"
"fp-ts": "^2.7.1",
"semver": "^7.3.2",
"ts-type": "^1.2.32"
},
"devDependencies": {},
"publishConfig": {
Expand Down

0 comments on commit 626d087

Please sign in to comment.