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 7d066bc
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 217 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
29 changes: 13 additions & 16 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 All @@ -45,7 +45,7 @@ interface YarnLockPackage {
[name: string]: string
}
optionalDependencies?: {
[depName: string]: string;
[depName: string]: string
}
}
interface YarnPackgeLock {
Expand All @@ -60,7 +60,7 @@ enum YarnLockType {
// copy from yarn v1
interface YarnLock2Struct {
type: YarnLockType.yarn2
object: any;
object: YarnPackgeLock
}

export const rcOptionsTypes = cliOptionsTypes
Expand Down Expand Up @@ -178,33 +178,30 @@ async function readYarnLockFile (dir: string) {
}

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

// eslint-disable-next-line
const parseYarnLock: any = parseSyml(lockFileContents)

delete parseYarnLock.__metadata
const dependencies: YarnPackgeLock = {}

const { structUtils } = yarnCore
const { parseDescriptor, parseRange } = structUtils
const keyNormalizer = yarnLockFileKeyNormalizer(
parseDescriptor,
parseRange,
parseRange
)

Object.entries(parseYarnLock).forEach(
// eslint-disable-next-line
([fullDescriptor, versionData]: [string, any]) => {
keyNormalizer(fullDescriptor).forEach((descriptor) => {
dependencies[descriptor] = versionData;
dependencies[descriptor] = versionData
})
},
}
)
console.log('dependencies: ', dependencies);
return {
object: dependencies,
type: YarnLockType.yarn2
type: YarnLockType.yarn2,
}
}

Expand Down
90 changes: 90 additions & 0 deletions packages/plugin-commands-installation/src/yarnUtil.ts
@@ -0,0 +1,90 @@
/**
* 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(
// eslint-disable-next-line
`${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))
}
7 changes: 6 additions & 1 deletion packages/plugin-commands-installation/test/import.ts
Expand Up @@ -98,7 +98,12 @@ test('import from yarn2 lock file', async () => {
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()

expect(lockfile.packages).toMatchSnapshot();
expect(lockfile.packages).toHaveProperty(['/is-positive/1.0.0'])
expect(lockfile.packages).toHaveProperty(['/is-negative/1.0.0'])

// node_modules is not created
await project.has('balanced-match')
await project.has('brace-expansion')
})

test('import from npm-shrinkwrap.json', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/pnpm/bin/pnpm.cjs
Expand Up @@ -14,7 +14,7 @@ ${COMPATIBILITY_PAGE}`)
process.exit(1)
}

// require('../dist/pnpm.cjs')
require('../dist/pnpm.cjs')

// if you want to debug at your local env, you can use this
require('../lib/pnpm')
// require('../lib/pnpm')

0 comments on commit 7d066bc

Please sign in to comment.