0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

feat: add node api to launch verdaccio programmatically

This commit is contained in:
Juan Picado @jotadeveloper 2018-01-17 22:29:00 +01:00
parent d3186896d0
commit 046e8b1ae1
No known key found for this signature in database
GPG key ID: 18AC54485952D158
8 changed files with 102 additions and 22 deletions

21
docs/node-api.md Normal file
View 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');
});
});
```

View file

@ -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';

View file

@ -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
View file

@ -0,0 +1,5 @@
// @flow
import {startVerdaccio} from './lib/bootstrap';
export default startVerdaccio;

15
src/lib/bootstrap.js vendored
View file

@ -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',

View file

@ -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}');

60
test/unit/cli.spec.js Normal file
View 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();
});
});
});

View file

@ -33,7 +33,8 @@
"build",
"contributing",
"source-code",
"unit-testing"
"unit-testing",
"node-api"
],
"Guides": ["protect-your-dependencies"]
}