diff --git a/docs/node-api.md b/docs/node-api.md new file mode 100644 index 000000000..352ed0d3b --- /dev/null +++ b/docs/node-api.md @@ -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'); + }); + }); +``` diff --git a/index.js b/index.js index 7cb2c4d5c..86b45f8e8 100644 --- a/index.js +++ b/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'; diff --git a/package.json b/package.json index b5eac75d8..17a332cc0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "type": "git", "url": "git://github.com/verdaccio/verdaccio" }, - "main": "index.js", + "main": "build/index.js", "bin": { "verdaccio": "./bin/verdaccio" }, diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..858bce321 --- /dev/null +++ b/src/index.js @@ -0,0 +1,5 @@ +// @flow + +import {startVerdaccio} from './lib/bootstrap'; + +export default startVerdaccio; diff --git a/src/lib/bootstrap.js b/src/lib/bootstrap.js index ab9ab4057..5cdecc941 100644 --- a/src/lib/bootstrap.js +++ b/src/lib/bootstrap.js @@ -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', diff --git a/src/lib/cli.js b/src/lib/cli.js index 10761733d..61f5ab22d 100644 --- a/src/lib/cli.js +++ b/src/lib/cli.js @@ -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}'); diff --git a/test/unit/cli.spec.js b/test/unit/cli.spec.js new file mode 100644 index 000000000..545217f60 --- /dev/null +++ b/test/unit/cli.spec.js @@ -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(); + }); + + + }); + +}); diff --git a/website/sidebars.json b/website/sidebars.json index b53f60108..48ac8c15c 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -33,7 +33,8 @@ "build", "contributing", "source-code", - "unit-testing" + "unit-testing", + "node-api" ], "Guides": ["protect-your-dependencies"] }