Skip to content

Commit

Permalink
chore: code adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
fireairforce committed Nov 23, 2021
1 parent 9179d0a commit 89f9007
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 205 deletions.
5 changes: 2 additions & 3 deletions packages/plugin-commands-installation/package.json
Expand Up @@ -82,8 +82,9 @@
"@pnpm/sort-packages": "workspace:2.1.3",
"@pnpm/store-connection-manager": "workspace:3.1.9",
"@pnpm/types": "workspace:7.6.0",
"@yarnpkg/core": "^2.4.0",
"@yarnpkg/core": "^3.2.0-rc.4",
"@yarnpkg/lockfile": "^1.1.0",
"@yarnpkg/parsers": "^2.5.0-rc.3",
"@zkochan/rimraf": "^2.1.1",
"@zkochan/table": "^1.0.0",
"@zkochan/which": "^2.0.3",
Expand All @@ -92,7 +93,6 @@
"enquirer": "^2.3.6",
"is-ci": "^3.0.0",
"is-subdir": "^1.1.1",
"js-yaml": "^4.1.0",
"load-json-file": "^6.2.0",
"mem": "^8.0.0",
"p-filter": "^2.1.0",
Expand All @@ -102,7 +102,6 @@
"ramda": "^0.27.1",
"read-ini-file": "^3.1.0",
"render-help": "^1.0.1",
"snyk-nodejs-lockfile-parser": "^1.37.1",
"version-selector-type": "^3.0.0"
},
"peerDependencies": {
Expand Down
11 changes: 4 additions & 7 deletions packages/plugin-commands-installation/src/import.ts
Expand Up @@ -18,11 +18,11 @@ import rimraf from '@zkochan/rimraf'
import loadJsonFile from 'load-json-file'
import renderHelp from 'render-help'
import { parse as parseYarnLock } from '@yarnpkg/lockfile'
import * as yarnCore from '@yarnpkg/core'
import { parseSyml } from '@yarnpkg/parsers'
import exists from 'path-exists'
import recursive from './recursive'
import { yarnLockFileKeyNormalizer } from 'snyk-nodejs-lockfile-parser/dist/parsers/yarn-utils'
import * as yarnCore from '@yarnpkg/core'
import { load, FAILSAFE_SCHEMA } from 'js-yaml';
import { yarnLockFileKeyNormalizer } from './yarnUtil'

interface NpmPackageLock {
dependencies: LockedPackagesMap
Expand Down Expand Up @@ -179,10 +179,7 @@ async function readYarnLockFile (dir: string) {

function parseYarn2Lock (lockFileContents: string): YarnLock2Struct {
// eslint-disable-line
const parseYarnLock: any = load(lockFileContents, {
json: true,
schema: FAILSAFE_SCHEMA
})
const parseYarnLock: any = parseSyml(lockFileContents);

delete parseYarnLock.__metadata
const dependencies: YarnPackgeLock = {}
Expand Down
89 changes: 89 additions & 0 deletions packages/plugin-commands-installation/src/yarnUtil.ts
@@ -0,0 +1,89 @@
/**
* https://github.com/snyk/nodejs-lockfile-parser/blob/master/lib/parsers/yarn-utils.ts
*/
import { structUtils } from '@yarnpkg/core';

const BUILTIN_PLACEHOLDER = 'builtin';
const MULTIPLE_KEYS_REGEXP = / *, */g;

export type ParseDescriptor = typeof structUtils.parseDescriptor;
export type ParseRange = typeof structUtils.parseRange;

const keyNormalizer = (
parseDescriptor: ParseDescriptor,
parseRange: ParseRange,
) => (rawDescriptor: string): string[] => {
// See https://yarnpkg.com/features/protocols
const descriptors: string[] = [rawDescriptor];
const descriptor = parseDescriptor(rawDescriptor);
const name = `${descriptor.scope ? '@' + descriptor.scope + '/' : ''}${
descriptor.name
}`;
const range = parseRange(descriptor.range);
const protocol = range.protocol;
switch (protocol) {
case 'npm:':
case 'file:':
descriptors.push(`${name}@${range.selector}`);
descriptors.push(`${name}@${protocol}${range.selector}`);
break;
case 'git:':
case 'git+ssh:':
case 'git+http:':
case 'git+https:':
case 'github:':
if (range.source) {
descriptors.push(
`${name}@${protocol}${range.source}${
range.selector ? '#' + range.selector : ''
}`,
);
} else {
descriptors.push(`${name}@${protocol}${range.selector}`);
}
break;
case 'patch:':
if (range.source && range.selector.indexOf(BUILTIN_PLACEHOLDER) === 0) {
descriptors.push(range.source);
} else {
descriptors.push(
`${name}@${protocol}${range.source}${
range.selector ? '#' + range.selector : ''
}`,
);
}
break;
case null:
case undefined:
if (range.source) {
descriptors.push(`${name}@${range.source}#${range.selector}`);
} else {
descriptors.push(`${name}@${range.selector}`);
}
break;
case 'http:':
case 'https:':
case 'link:':
case 'portal:':
case 'exec:':
case 'workspace:':
case 'virtual:':
default:
// For user defined plugins
descriptors.push(`${name}@${protocol}${range.selector}`);
break;
}
return descriptors;
};

export type YarnLockFileKeyNormalizer = (fullDescriptor: string) => Set<string>;

export const yarnLockFileKeyNormalizer = (
parseDescriptor: ParseDescriptor,
parseRange: ParseRange,
): YarnLockFileKeyNormalizer => (fullDescriptor: string) => {
const allKeys = fullDescriptor
.split(MULTIPLE_KEYS_REGEXP)
.map(keyNormalizer(parseDescriptor, parseRange));
return new Set<string>(allKeys.flat(5));
};

0 comments on commit 89f9007

Please sign in to comment.