Node.js API
Is ordú dénártha é Verdaccio atá ar fáil i do thimpeallacht nuair a shuiteálann tú an pacáiste go domhanda m. sh. npm i -g verdaccio
, ach is féidir leis a bheith spleách ar do thionscadal agus é a úsáid go ríomhchláraithe.
Ag baint úsáide as forc
ó mhodúl child_process
Ag baint úsáide as an dénártha is é an bealach is tapúla chun verdaccio a úsáid go ríomhchláraithe, ní mór duit _debug: true
a chur leis an gcomhad cumraíochta chun an córas teachtaireachtaí a chumasú, nuair a bheidh verdaccio réidh seolfaidh sé verdaccio_started
teaghrán mar theachtaireacht mar an sampla seo a leanas.
Má tá modúil ESM in úsáid agat, ní bheidh an
require
ar fáil.
export function runRegistry(args: string[] = [], childOptions: {}): Promise<ChildProcess> {
return new Promise((resolve, reject) => {
const childFork = fork(require.resolve('verdaccio/bin/verdaccio'), args, childOptions);
childFork.on('message', (msg: { verdaccio_started: boolean }) => {
if (msg.verdaccio_started) {
resolve(childFork);
}
});
childFork.on('error', (err: any) => reject([err]));
childFork.on('disconnect', (err: any) => reject([err]));
});
}
Is féidir leat an sampla iomlán a fheiceáil ar an stór seo.
https://github.com/juanpicado/verdaccio-fork
Ag baint úsáide as an API modúl
Gné ar fáil i v5.11.0
agus níos airde.
Ag baint úsáide as const verdaccio = need('verdaccio'); toisc nach bhfuil an modúl réamhshocraithe imchlúdaithe, tá sé i léig agus moltar úsáid
runServer
le haghaidh comhoiriúnacht amach anseo.
Tá trí bhealach ann chun é a úsáid:
- Gan ionchur, gheobhaidh sé an
config.yaml
mar a rithfeáverdaccio
sa chonsól. - Le cosán iomlán.
- Le réad (tá gabháil anseo, féach thíos).
const {runServer} = require('verdaccio');
const app = await runServer(); // default configuration
const app = await runServer('./config/config.yaml');
const app = await runServer({ configuration });
app.listen(4000, (event) => {
// do something
});
Le réad ní mór duit self_path
a chur leis, de láimh (níl sé go deas ach bheadh sé ina athrú briste anois é) ar v6 níl sé ag teastáil uait a thuilleadh.
const { runServer, parseConfigFile } = require('verdaccio');
const configPath = join(__dirname, './config.yaml');
const c = parseConfigFile(configPath);
// workaround
// on v5 the `self_path` still exists and will be removed in v6
c.self_path = 'foo';
runServer(c).then(() => {});
Gné ar fáil níos lú ná v5.11.0
.
Is bealach bailí é seo ach dírítear air le haghaidh eisiúintí amach anseo.
const fs = require('fs');
const path = require('path');
const verdaccio = require('verdaccio').default;
const YAML = require('js-yaml');
const getConfig = () => {
return YAML.load(fs.readFileSync(path.join(__dirname, 'config.yaml'), 'utf8'));
};
const cache = path.join(__dirname, 'cache');
const config = Object.assign({}, getConfig(), {
self_path: cache,
});
verdaccio(config, 6000, cache, '1.0.0', 'verdaccio', (webServer, addrs, pkgName, pkgVersion) => {
try {
webServer.unref();
webServer.listen(addrs.port || addrs.path, addrs.host, () => {
console.log('verdaccio running');
});
} catch (error) {
console.error(error);
}
});