Skip to content

Commit

Permalink
feat: prepare for yarn v2 feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dkontorovskyy committed Apr 28, 2020
1 parent fb0f1dd commit 6013a18
Show file tree
Hide file tree
Showing 43 changed files with 10 additions and 39,904 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ dist
.eslintcache
*.log
/package-lock.json
.idea
23 changes: 1 addition & 22 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from './parsers';
import { PackageLockParser } from './parsers/package-lock-parser';
import { YarnLockParser } from './parsers/yarn-lock-parse';
import { Yarn2LockParser } from './parsers/yarn2-lock-parse';
import getRuntimeVersion from './get-node-runtime-version';
import {
UnsupportedRuntimeError,
Expand Down Expand Up @@ -64,18 +63,6 @@ async function buildDepTree(
);
}
break;
case LockfileType.yarn2:
// parsing yarn.lock is supported for Node.js v10 and higher
if (getRuntimeVersion() >= 10) {
lockfileParser = new Yarn2LockParser();
} else {
throw new UnsupportedRuntimeError(
'Parsing `yarn.lock` is not ' +
'supported on Node.js version less than 10. Please upgrade your ' +
'Node.js environment or use `package-lock.json`',
);
}
break;
default:
throw new InvalidUserInputError(
'Unsupported lockfile type ' +
Expand Down Expand Up @@ -113,15 +100,7 @@ async function buildDepTreeFromFiles(
if (_.endsWith(lockFilePath, 'package-lock.json')) {
lockFileType = LockfileType.npm;
} else if (_.endsWith(lockFilePath, 'yarn.lock')) {
if (
fs.existsSync(
path.resolve(root, lockFilePath.replace('yarn.lock', '.yarnrc.yml')),
)
) {
lockFileType = LockfileType.yarn2;
} else {
lockFileType = LockfileType.yarn;
}
lockFileType = LockfileType.yarn;
} else {
throw new InvalidUserInputError(
`Unknown lockfile ${lockFilePath}. ` +
Expand Down
4 changes: 1 addition & 3 deletions lib/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as _ from 'lodash';
import { PackageLock } from './package-lock-parser';
import { YarnLock } from './yarn-lock-parse';
import { InvalidUserInputError } from '../errors';
import { Yarn2Lock } from './yarn2-lock-parse';

export interface Dep {
name: string;
Expand Down Expand Up @@ -65,7 +64,6 @@ export enum Scope {
export enum LockfileType {
npm = 'npm',
yarn = 'yarn',
yarn2 = 'yarn2',
}

export interface LockfileParser {
Expand All @@ -78,7 +76,7 @@ export interface LockfileParser {
) => Promise<PkgTree>;
}

export type Lockfile = PackageLock | YarnLock | Yarn2Lock;
export type Lockfile = PackageLock | YarnLock;

export function parseManifestFile(manifestFileContents: string): ManifestFile {
try {
Expand Down
4 changes: 2 additions & 2 deletions lib/parsers/yarn-lock-parse-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { config } from '../config';

const EVENT_PROCESSING_CONCURRENCY = 5;

export type YarnLockFileTypes = LockfileType.yarn | LockfileType.yarn2;
export type YarnLockFileTypes = LockfileType.yarn;

export interface YarnLockDeps {
[depName: string]: YarnLockDep;
Expand Down Expand Up @@ -112,7 +112,7 @@ export abstract class YarnLockParseBase<T extends YarnLockFileTypes>
const dependency = lockFile.object[depKey];
if (!dependency) {
if (strict) {
throw new OutOfSyncError(queueItem.tree.name!, 'yarn');
throw new OutOfSyncError(queueItem.tree.name!, this.type);
}
if (!queueItem.tree.labels) {
queueItem.tree.labels = {};
Expand Down
22 changes: 5 additions & 17 deletions lib/parsers/yarn-lock-parse.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
import * as yarnLockfileParser from '@yarnpkg/lockfile';

import { LockfileType } from './';
import getRuntimeVersion from '../get-node-runtime-version';
import { InvalidUserInputError, UnsupportedRuntimeError } from '../errors';
import { InvalidUserInputError } from '../errors';
import { YarnLockBase } from './yarn-lock-parse-base';
import { YarnLockParseBase } from './yarn-lock-parse-base';

export type YarnLock = YarnLockBase<LockfileType.yarn>;

export class YarnLockParser extends YarnLockParseBase<LockfileType.yarn> {
private yarnLockfileParser: any;

constructor() {
super(LockfileType.yarn);
// @yarnpkg/lockfile doesn't work with Node.js < 6 and crashes just after
// the import, so it has to be required conditionally
// more details at https://github.com/yarnpkg/yarn/issues/6304
if (getRuntimeVersion() < 6) {
throw new UnsupportedRuntimeError(
'yarn.lock parsing is supported for ' + 'Node.js v6 and higher.',
);
}
this.yarnLockfileParser = require('@yarnpkg/lockfile');
}

public parseLockFile(lockFileContents: string): YarnLock {
try {
const yarnLock: YarnLock = this.yarnLockfileParser.parse(
lockFileContents,
);
const yarnLock: YarnLock = yarnLockfileParser.parse(lockFileContents);
yarnLock.dependencies = yarnLock.object;
yarnLock.type = LockfileType.yarn;
return yarnLock;
} catch (e) {
throw new InvalidUserInputError(
'yarn.lock parsing failed with an ' + `error: ${e.message}`,
`yarn.lock parsing failed with an error: ${e.message}`,
);
}
}
Expand Down
87 changes: 0 additions & 87 deletions lib/parsers/yarn-utils.ts

This file was deleted.

55 changes: 0 additions & 55 deletions lib/parsers/yarn2-lock-parse.ts

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
],
"homepage": "https://github.com/snyk/nodejs-lockfile-parser#readme",
"dependencies": {
"@yarnpkg/core": "^2.0.0-rc.26",
"@yarnpkg/lockfile": "^1.0.2",
"event-loop-spinner": "^1.1.0",
"graphlib": "^2.1.5",
Expand All @@ -39,8 +38,7 @@
"snyk-config": "^3.0.0",
"source-map-support": "^0.5.7",
"tslib": "^1.9.3",
"uuid": "^3.3.2",
"yaml": "^1.9.2"
"uuid": "^3.3.2"
},
"devDependencies": {
"@types/graphlib": "^2.1.4",
Expand Down
1 change: 0 additions & 1 deletion test/lib/fixtures/cyclic-dep-simple/yarn2/.yarnrc.yml

This file was deleted.

23 changes: 0 additions & 23 deletions test/lib/fixtures/cyclic-dep-simple/yarn2/yarn.lock

This file was deleted.

1 change: 0 additions & 1 deletion test/lib/fixtures/dev-deps-only/yarn2/.yarnrc.yml

This file was deleted.

29 changes: 0 additions & 29 deletions test/lib/fixtures/dev-deps-only/yarn2/yarn.lock

This file was deleted.

1 change: 0 additions & 1 deletion test/lib/fixtures/empty-dev-deps/yarn2/.yarnrc.yml

This file was deleted.

20 changes: 0 additions & 20 deletions test/lib/fixtures/empty-dev-deps/yarn2/yarn.lock

This file was deleted.

1 change: 0 additions & 1 deletion test/lib/fixtures/external-tarball/yarn2/.yarnrc.yml

This file was deleted.

Loading

0 comments on commit 6013a18

Please sign in to comment.