mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-04-01 02:42:23 -05:00
Merge pull request #491 from verdaccio/feature-node-api
refactor: add node api to launch verdaccio programmatically
This commit is contained in:
commit
015a0a7f29
11 changed files with 106 additions and 24 deletions
|
@ -10,7 +10,8 @@
|
|||
[](https://www.npmjs.org/package/verdaccio)
|
||||
[](https://gitter.im/verdaccio/)
|
||||
[](https://crowdin.com/project/verdaccio)
|
||||
[](https://david-dm.org/verdaccio/verdaccio)
|
||||
[](https://david-dm.org/verdaccio/verdaccio)[](https://codecov.io/gh/verdaccio/verdaccio)
|
||||
|
||||
|
||||
<p align="center"><img src="https://firebasestorage.googleapis.com/v0/b/jotadeveloper-website.appspot.com/o/verdaccio_long_video2.gif?alt=media&token=4d20cad1-f700-4803-be14-4b641c651b41"></p>
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ test:
|
|||
- yarn run test
|
||||
- nvm alias default 9
|
||||
- yarn run test
|
||||
- yarn run coverage:publish
|
||||
- echo "machine github.com login verdacciobot password $GITHUB_TOKEN" > ~/.netrc
|
||||
- cd website && yarn install && GIT_USER=verdacciobot USE_SSH=false yarn run publish-gh-pages
|
||||
deployment:
|
||||
|
|
21
docs/node-api.md
Normal file
21
docs/node-api.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
---
|
||||
id: node-api
|
||||
title: "Node API"
|
||||
---
|
||||
|
||||
Verdaccio can be invoqued programmatically.
|
||||
|
||||
## Usage
|
||||
|
||||
#### Programmatically
|
||||
|
||||
```js
|
||||
import startServer from 'verdaccio';
|
||||
|
||||
startServer(configJsonFormat, 6000, store, '1.0.0', 'verdaccio',
|
||||
(webServer, addrs, pkgName, pkgVersion) => {
|
||||
webServer.listen(addr.port || addr.path, addr.host, () => {
|
||||
console.log('verdaccio running');
|
||||
});
|
||||
});
|
||||
```
|
10
index.js
10
index.js
|
@ -1,9 +1 @@
|
|||
module.exports = require('./src/api/index');
|
||||
|
||||
/** package
|
||||
{ "name": "verdaccio",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {"js-yaml": "*"},
|
||||
"scripts": {"postinstall": "js-yaml package.yaml > package.json ; npm install"}
|
||||
}
|
||||
**/
|
||||
export {default as startVerdaccio} from './build/index';
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
"type": "git",
|
||||
"url": "git://github.com/verdaccio/verdaccio"
|
||||
},
|
||||
"main": "index.js",
|
||||
"main": "build/index.js",
|
||||
"bin": {
|
||||
"verdaccio": "./bin/verdaccio"
|
||||
},
|
||||
|
|
5
src/index.js
Normal file
5
src/index.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
// @flow
|
||||
|
||||
import {startVerdaccio} from './lib/bootstrap';
|
||||
|
||||
export default startVerdaccio;
|
15
src/lib/bootstrap.js
vendored
15
src/lib/bootstrap.js
vendored
|
@ -21,7 +21,7 @@ const logger = require('./logger');
|
|||
- localhost:5557
|
||||
@return {Array}
|
||||
*/
|
||||
function getListListenAddresses(argListen, configListen) {
|
||||
export function getListListenAddresses(argListen, configListen) {
|
||||
// command line || config file || default
|
||||
let addresses;
|
||||
if (argListen) {
|
||||
|
@ -62,13 +62,6 @@ function startVerdaccio(config, cliListen, configPath, pkgVersion, pkgName, call
|
|||
throw new Error('config file must be an object');
|
||||
}
|
||||
|
||||
if (!config.self_path) {
|
||||
config.self_path = Path.resolve(configPath);
|
||||
}
|
||||
if (!config.https) {
|
||||
config.https = {enable: false};
|
||||
}
|
||||
|
||||
const app = server(config);
|
||||
const addresses = getListListenAddresses(cliListen, config.listen);
|
||||
|
||||
|
@ -97,9 +90,9 @@ function unlinkAddressPath(addr) {
|
|||
}
|
||||
}
|
||||
|
||||
function displayHTTPSWarning(configPath) {
|
||||
function displayHTTPSWarning(storageLocation) {
|
||||
const resolveConfigPath = function(file) {
|
||||
return Path.resolve(Path.dirname(configPath), file);
|
||||
return Path.resolve(Path.dirname(storageLocation), file);
|
||||
};
|
||||
|
||||
logger.logger.fatal([
|
||||
|
@ -115,7 +108,7 @@ function displayHTTPSWarning(configPath) {
|
|||
' $ openssl x509 -req -in ' + resolveConfigPath('verdaccio-csr.pem') +
|
||||
' -signkey ' + resolveConfigPath('verdaccio-key.pem') + ' -out ' + resolveConfigPath('verdaccio-cert.pem'),
|
||||
'',
|
||||
'And then add to config file (' + configPath + '):',
|
||||
'And then add to config file (' + storageLocation + '):',
|
||||
' https:',
|
||||
' key: verdaccio-key.pem',
|
||||
' cert: verdaccio-cert.pem',
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
/* eslint no-sync:0 */
|
||||
/* eslint no-empty:0 */
|
||||
|
||||
import path from 'path';
|
||||
import {startVerdaccio, listenDefaultCallback} from './bootstrap';
|
||||
import findConfigFile from './config-path';
|
||||
|
||||
|
@ -50,6 +51,13 @@ try {
|
|||
verdaccioConfiguration = Utils.parseConfigFile(configPathLocation);
|
||||
process.title = verdaccioConfiguration.web && verdaccioConfiguration.web.title || 'verdaccio';
|
||||
|
||||
if (!verdaccioConfiguration.self_path) {
|
||||
verdaccioConfiguration.self_path = path.resolve(configPathLocation);
|
||||
}
|
||||
if (!verdaccioConfiguration.https) {
|
||||
verdaccioConfiguration.https = {enable: false};
|
||||
}
|
||||
|
||||
logger.logger.warn({file: configPathLocation}, 'config file - @{file}');
|
||||
} catch (err) {
|
||||
logger.logger.fatal({file: configPathLocation, err: err}, 'cannot open config file @{file}: @{!err.message}');
|
||||
|
|
|
@ -4,7 +4,7 @@ const assert = require('assert');
|
|||
const express = require('express');
|
||||
const request = require('request');
|
||||
const rimraf = require('rimraf');
|
||||
const verdaccio = require('../../');
|
||||
const verdaccio = require('../../src/api/index');
|
||||
const config = require('./partials/config');
|
||||
|
||||
const app = express();
|
||||
|
|
60
test/unit/cli.spec.js
Normal file
60
test/unit/cli.spec.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
import startServer from '../../src';
|
||||
import {getListListenAddresses} from '../../src/lib/bootstrap';
|
||||
import config from './partials/config';
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
|
||||
require('../../src/lib/logger').setup([]);
|
||||
|
||||
describe('startServer via API', () => {
|
||||
|
||||
describe('startServer launcher', () => {
|
||||
test('should provide all server data', (done) => {
|
||||
const store = path.join(__dirname, 'partials/store');
|
||||
|
||||
startServer(config, 6000, store, '1.0.0', 'verdaccio-test',
|
||||
(webServer, addrs, pkgName, pkgVersion) => {
|
||||
expect(webServer).toBeDefined();
|
||||
expect(addrs).toBeDefined();
|
||||
expect(addrs.proto).toBe('http');
|
||||
expect(addrs.host).toBe('localhost');
|
||||
expect(addrs.port).toBe('6000');
|
||||
expect(pkgName).toBeDefined();
|
||||
expect(pkgVersion).toBeDefined();
|
||||
expect(pkgVersion).toBe('1.0.0');
|
||||
expect(pkgName).toBe('verdaccio-test');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('should fails if config is missing', () => {
|
||||
expect(() => { return startServer() }).toThrow('config file must be an object');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('getListListenAddresses test', () => {
|
||||
test('should return by default 4873', () => {
|
||||
const addrs = getListListenAddresses()[0];
|
||||
|
||||
expect(addrs.proto).toBe('http');
|
||||
expect(addrs.host).toBe('localhost');
|
||||
expect(addrs.port).toBe('4873');
|
||||
});
|
||||
|
||||
test('should return a list of address and no cli argument provided', () => {
|
||||
const addrs = getListListenAddresses(null, ['1000', '2000']);
|
||||
|
||||
expect(_.isArray(addrs)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should return an address and no cli argument provided', () => {
|
||||
const addrs = getListListenAddresses(null, '1000');
|
||||
|
||||
expect(_.isArray(addrs)).toBeTruthy();
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
|
@ -33,7 +33,8 @@
|
|||
"build",
|
||||
"contributing",
|
||||
"source-code",
|
||||
"unit-testing"
|
||||
"unit-testing",
|
||||
"node-api"
|
||||
],
|
||||
"Guides": ["protect-your-dependencies"]
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue