Skip to content

Commit

Permalink
feat: Adding publicPath option for npm publishConfig (#1010)
Browse files Browse the repository at this point in the history
* feat: Adding publicPath option for npm publishConfig

with docs and tests

* feat: Adding publicPath option for npm publishConfig

with docs and tests
  • Loading branch information
xllily committed Jun 5, 2023
1 parent 11709dc commit fef8566
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions docs/npm.md
Expand Up @@ -83,6 +83,20 @@ The default registry is [https://registry.npmjs.org][2]. The publish to another
}
```

## Config public path of registry

The default public path is `/package`. To customize an alternative path, update or set the
`publishConfig`. For example, if a third-party tool such as `Verdaccio` is used to build a private server to proxy
npm registry, then the URL address of the web user interface is `http://{{host}}-/web/detail/{{packageName}}`:

```json
{
"publishConfig": {
"publicPath": "/-/web/detail"
}
}
```

## Yarn

Using Yarn? It adds or overwrites global environment variable(s), causing authentication issues or not being able to
Expand Down
9 changes: 8 additions & 1 deletion lib/plugin/npm/npm.js
Expand Up @@ -13,6 +13,7 @@ const MANIFEST_PATH = './package.json';
const DEFAULT_TAG = 'latest';
const DEFAULT_TAG_PRERELEASE = 'next';
const NPM_BASE_URL = 'https://www.npmjs.com';
const NPM_PUBLIC_PATH = '/package';

const fixArgs = args => (args ? (typeof args === 'string' ? args.split(' ') : args) : []);

Expand Down Expand Up @@ -198,7 +199,8 @@ class npm extends Plugin {

getPackageUrl() {
const baseUrl = this.getRegistry() || NPM_BASE_URL;
return urlJoin(baseUrl, 'package', this.getName());
const publicPath = this.getPublicPath() || NPM_PUBLIC_PATH;
return urlJoin(baseUrl, publicPath, this.getName());
}

getRegistry() {
Expand All @@ -213,6 +215,11 @@ class npm extends Plugin {
return registries[0];
}

getPublicPath() {
const { publishConfig } = this.getContext();
return (publishConfig && publishConfig.publicPath) ?? ''
}

async guessPreReleaseTag() {
const [tag] = await this.getRegistryPreReleaseTags();
if (tag) {
Expand Down
17 changes: 17 additions & 0 deletions test/npm.js
Expand Up @@ -18,6 +18,23 @@ test('should return npm package url (custom registry)', t => {
t.is(npmClient.getPackageUrl(), 'https://registry.example.org/package/my-cool-package');
});

test('should return npm package url (custom publicPath)', t => {
const options = { npm: { name: 'my-cool-package', publishConfig: { publicPath: '/custom/public-path' } } };
const npmClient = factory(npm, { options });
t.is(npmClient.getPackageUrl(), 'https://www.npmjs.com/custom/public-path/my-cool-package');
});

test('should return npm package url (custom registry and publicPath)', t => {
const options = {
npm: {
name: 'my-cool-package',
publishConfig: { registry: 'https://registry.example.org/', publicPath: '/custom/public-path' }
}
};
const npmClient = factory(npm, { options });
t.is(npmClient.getPackageUrl(), 'https://registry.example.org/custom/public-path/my-cool-package');
});

test('should return default tag', async t => {
const npmClient = factory(npm);
const tag = await npmClient.resolveTag();
Expand Down

0 comments on commit fef8566

Please sign in to comment.