Skip to content

Commit

Permalink
find best registry
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Oct 20, 2022
1 parent 1976435 commit 4e95818
Show file tree
Hide file tree
Showing 15 changed files with 366 additions and 212 deletions.
6 changes: 0 additions & 6 deletions data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1420,12 +1420,6 @@
"scope": "application",
"description": "Winblend option of notification window, neovim only."
},
"npm.binPath": {
"type": "string",
"scope": "application",
"default": "npm",
"description": "Command or absolute path to npm or yarn."
},
"outline.autoPreview": {
"type": "boolean",
"scope": "application",
Expand Down
11 changes: 0 additions & 11 deletions doc/coc-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Inlay hint |coc-config-inlayHint|
Links |coc-config-links|
List |coc-config-list|
Notification |coc-config-notification|
Npm |coc-config-npm|
Outline |coc-config-outline|
Pull diagnostics |coc-config-pullDiagnostic|
Refactor |coc-config-refactor|
Expand Down Expand Up @@ -790,16 +789,6 @@ Notification~

Scope: `application`, default: `30`

------------------------------------------------------------------------------
Npm~
*coc-config-npm*
"npm.binPath" *coc-config-npm-binPath*

Command or absolute path to npm or yarn for global extension
install/uninstall.

Scope: `application`, default: `"npm"`

------------------------------------------------------------------------------
Outline~
*coc-config-outline*
Expand Down
8 changes: 2 additions & 6 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,8 @@ Use |:CocInstall| to install coc extensions from vim's command line.

To make coc.nvim install extensions on startup, use |g:coc_global_extensions|.

To use package manager other than npm (like `yarn` or `pnpm`), use
|coc-config-npm-binPath|.

To customize npm registry for coc.nvim add `coc.nvim:registry` in your
`~/.npmrc`, like:
To customize node modules registry for coc.nvim add `coc.nvim:registry` in
your `~/.npmrc`, like:
>
coc.nvim:registry=https://registry.mycompany.org/
<
Expand Down Expand Up @@ -3645,7 +3642,6 @@ extensions *coc-list-extensions*
- 'enable' enable extension.
- 'lock' lock/unlock extension to current version.
- 'doc' view extension's README doc.
- 'fix' fix dependencies in terminal buffer.
- 'reload' reload extension.
- 'uninstall' uninstall extension.

Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/client/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('publish configuration feature', () => {
it('should send configuration for specific sections', async () => {
let client: LanguageClient
let called = false
client = createClient(['coc.preferences', 'npm', 'unknown'], {
client = createClient(['coc.preferences', 'http', 'unknown'], {
workspace: {
didChangeConfiguration: (sections, next) => {
called = true
Expand All @@ -139,12 +139,12 @@ describe('publish configuration feature', () => {
return changed != null
}, true)
expect(changed.settings.coc).toBeDefined()
expect(changed.settings.npm).toBeDefined()
expect(changed.settings.http).toBeDefined()
let { configurations } = workspace
configurations.updateMemoryConfig({ 'npm.binPath': 'cnpm' })
configurations.updateMemoryConfig({ 'http.proxyStrictSSL': false })
await helper.waitValue(() => {
return changed.settings.npm?.binPath
}, 'cnpm')
return changed.settings.http?.proxyStrictSSL
}, false)
await client.stop()
})

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/configuration/configurations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ describe('Configurations', () => {
modifyConfiguration: async () => {}
})
disposables.push(configurations)
configurations.extendsDefaults({ 'npm.binPath': 'cnpm' }, 'test')
configurations.extendsDefaults({ 'http.proxyStrictSSL': false }, 'test')
let o = configurations.configuration.defaults.contents
expect(o.npm.binPath).toBe('npm')
expect(o.http.proxyStrictSSL).toBe(true)
})

it('should update configuration', async () => {
Expand Down
93 changes: 64 additions & 29 deletions src/__tests__/modules/extensionDependency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@ import path from 'path'
import tar from 'tar'
import { URL } from 'url'
import { v4 as uuid } from 'uuid'
import { checkFileSha1, DependenciesInstaller, DependencyItem, findItem, getModuleInfo, getRegistries, getVersion, readDependencies, shouldRetry, untar, validVersionInfo, VersionInfo } from '../../extension/dependency'
import { checkFileSha1, DependencySession, DependenciesInstaller, DependencyItem, findItem, getModuleInfo, getVersion, readDependencies, shouldRetry, untar, validVersionInfo, VersionInfo } from '../../extension/dependency'
import { Dependencies } from '../../extension/installer'
import { CancellationError } from '../../util/errors'
import { loadJson, remove, writeJson } from '../../util/fs'
import helper, { getPort } from '../helper'

process.env.NO_PROXY = '*'

describe('utils', () => {
it('should getRegistries', () => {
let u = new URL('https://registry.npmjs.org')
expect(getRegistries(u).length).toBe(2)
u = new URL('https://registry.yarnpkg.com')
expect(getRegistries(u).length).toBe(2)
u = new URL('https://example.com')
expect(getRegistries(u).length).toBe(3)
})

it('should check valid versionInfo', async () => {
expect(validVersionInfo(null)).toBe(false)
expect(validVersionInfo({ name: 3 })).toBe(false)
Expand Down Expand Up @@ -194,15 +186,16 @@ describe('DependenciesInstaller', () => {
})
}

function create(root?: string, onMessage?: (msg: string) => void): DependenciesInstaller {
function create(root: string | undefined, directory: string, onMessage?: (msg: string) => void): DependenciesInstaller {
if (!root) {
root = path.join(os.tmpdir(), uuid())
fs.mkdirSync(root)
dirs.push(root)
}
let registry = new URL(`http://127.0.0.1:${httpPort}`)
onMessage = onMessage ?? function() {}
return new DependenciesInstaller(registry, root, onMessage)
let session = new DependencySession(registry, root)
return session.createInstaller(directory, onMessage)
}

function createVersion(name: string, version: string, dependencies?: Dependencies): VersionInfo {
Expand Down Expand Up @@ -252,8 +245,50 @@ describe('DependenciesInstaller', () => {
}))
}

it('should throw on cancel', async () => {
let root = path.join(os.tmpdir(), uuid())
fs.mkdirSync(root)
dirs.push(root)
let registry = new URL(`http://127.0.0.1:${httpPort}`)
let session = new DependencySession(registry, root)
let directory = path.join(os.tmpdir(), uuid())
dirs.push(directory)
writeJson(path.join(directory, 'package.json'), { dependencies: { foo: '>= 0.0.1' } })
let other = path.join(os.tmpdir(), uuid())
dirs.push(other)
writeJson(path.join(other, 'package.json'), { dependencies: { bar: '>= 0.0.1' } })
let one = session.createInstaller(directory, () => {})
let two = session.createInstaller(other, () => {})
let spy = jest.spyOn(one, 'fetchInfos').mockImplementation(() => {
return new Promise((resolve, reject) => {
one.token.onCancellationRequested(() => {
clearTimeout(timer)
reject(new CancellationError())
})
let timer = setTimeout(() => {
resolve()
}, 500)
})
})
let p = one.installDependencies()
let err
two.installDependencies().catch(error => {
err = error
})
await helper.wait(30)
session.cancel()
let fn = async () => {
await p
}
await expect(fn()).rejects.toThrow(Error)
spy.mockRestore()
await helper.waitValue(() => {
return err != null
}, true)
})

it('should retry fetch', async () => {
let install = create()
let install = create(undefined, '')
let fn = async () => {
await install.fetch(new URL('/', url), { timeout: 10 }, 3)
}
Expand All @@ -264,7 +299,7 @@ describe('DependenciesInstaller', () => {
})

it('should cancel request', async () => {
let install = create()
let install = create(undefined, '')
let p = install.fetch(new URL('/slow', url), {}, 1)
await helper.wait(10)
let fn = async () => {
Expand All @@ -275,7 +310,7 @@ describe('DependenciesInstaller', () => {
})

it('should throw when unable to load info', async () => {
let install = create()
let install = create(undefined, '')
let fn = async () => {
await install.loadInfo(url, 'foo', 10)
}
Expand All @@ -288,14 +323,14 @@ describe('DependenciesInstaller', () => {

it('should fetchInfos', async () => {
addJsonData()
let install = create()
let install = create(undefined, '')
await install.fetchInfos({ a: '^0.0.1' })
expect(install.resolvedInfos.size).toBe(4)
})

it('should linkDependencies', async () => {
addJsonData()
let install = create()
let install = create(undefined, '')
await install.fetchInfos({ a: '^0.0.1' })
let items: DependencyItem[] = []
install.linkDependencies(undefined, items)
Expand All @@ -305,7 +340,7 @@ describe('DependenciesInstaller', () => {
})

it('should retry download', async () => {
let install = create()
let install = create(undefined, '')
let fn = async () => {
await install.download(new URL('res', url), 'res', '', 3, 10)
}
Expand All @@ -323,7 +358,7 @@ describe('DependenciesInstaller', () => {
})

it('should throw when unable to resolve version', async () => {
let install = create()
let install = create(undefined, '')
expect(() => {
install.resolveVersion('foo', '^1.0.0')
}).toThrow()
Expand Down Expand Up @@ -359,12 +394,12 @@ describe('DependenciesInstaller', () => {
shasum: 'bf0d88712fc3dbf6e3ab9a6968c0b4232779dbc4',
version: '0.0.2'
})
let install = create()
let install = create(undefined, '')
let dest = path.join(install.modulesRoot, '.cache')
fs.mkdirSync(dest, { recursive: true })
let tarfile = path.resolve(__dirname, '../test.tar.gz')
fs.copyFileSync(tarfile, path.join(dest, `foo.0.0.1.tgz`))
let res = await install.downloadItems(items)
let res = await install.downloadItems(items, 1)
expect(res.size).toBe(2)
})

Expand All @@ -378,35 +413,35 @@ describe('DependenciesInstaller', () => {
shasum: 'badsum',
version: '0.0.2'
})
let install = create()
let install = create(undefined, '')
let fn = async () => {
await install.downloadItems(items)
await install.downloadItems(items, 2)
}
await expect(fn()).rejects.toThrow(Error)
})

it('should no nothing if no dependencies', async () => {
let msg: string
let install = create(undefined, s => {
msg = s
})
let directory = path.join(os.tmpdir(), uuid())
let file = path.join(directory, 'package.json')
writeJson(file, { dependencies: {} })
await install.installDependencies(directory)
let install = create(undefined, directory, s => {
msg = s
})
await install.installDependencies()
expect(msg).toMatch('No dependencies')
fs.rmSync(directory, { recursive: true })
})

it('should install dependencies ', async () => {
createFiles = true
addJsonData()
let install = create()
let directory = path.join(os.tmpdir(), uuid())
fs.mkdirSync(directory, { recursive: true })
let file = path.join(directory, 'package.json')
let install = create(undefined, directory)
writeJson(file, { dependencies: { a: '^0.0.1' } })
await install.installDependencies(directory)
await install.installDependencies()
let folder = path.join(directory, 'node_modules')
let res = fs.readdirSync(folder)
expect(res).toEqual(['a', 'b', 'c', 'd'])
Expand Down
19 changes: 2 additions & 17 deletions src/__tests__/modules/extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { v4 as uuid } from 'uuid'
import which from 'which'
import commandManager from '../../commands'
import extensions, { Extensions, toUrl } from '../../extension'
import { writeFile, writeJson } from '../../util/fs'
Expand Down Expand Up @@ -37,7 +36,8 @@ describe('extensions', () => {
expect(extensions.onDidActiveExtension).toBeDefined()
expect(extensions.onDidUnloadExtension).toBeDefined()
expect(extensions.schemes).toBeDefined()
expect(extensions.creteInstaller('npm', 'id')).toBeDefined()
let res = await extensions.creteInstaller('id')
expect(res).toBeDefined()
})

it('should get extensions stat', async () => {
Expand Down Expand Up @@ -94,21 +94,6 @@ describe('extensions', () => {
s.mockRestore()
})

it('should use absolute path for npm', async () => {
let res = extensions.npm
expect(path.isAbsolute(res)).toBe(true)
})

it('should not throw when npm not found', async () => {
let spy = jest.spyOn(which, 'sync').mockImplementation(() => {
throw new Error('not executable')
})
let res = extensions.npm
expect(res).toBeNull()
await extensions.updateExtensions()
spy.mockRestore()
})

it('should get all extensions', () => {
let list = extensions.all
expect(Array.isArray(list)).toBe(true)
Expand Down
Loading

0 comments on commit 4e95818

Please sign in to comment.