From d80084bfb7f87d359ce01b2820f63e4aeeefa0c1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 17 Nov 2020 17:41:55 +0100 Subject: [PATCH] Added @capire:registry --- .registry/server.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 16 ++++++++++ package.json | 1 + 3 files changed, 88 insertions(+) create mode 100644 .registry/server.js diff --git a/.registry/server.js b/.registry/server.js new file mode 100644 index 00000000..c1461016 --- /dev/null +++ b/.registry/server.js @@ -0,0 +1,71 @@ +const { exec } = require ('child_process') +const express = require ('express') +const fs = require ('fs') +const app = express() + +const { PORT=4444 } = process.env +const [,,port=PORT] = process.argv + +app.use('/-/:tarball', (req,res,next) => { + const url = decodeURIComponent(req.url) + console.debug ('GET', req.params) + try { + const { tarball } = req.params + const [, pkg ] = /^capire-(\w+)/.exec(tarball) + fs.lstat(tarball,(err => { + if (err) exec(`npm pack ../${pkg}`,next) + else next() + })) + } catch (e) { + console.error(e) + res.sendStatus(500) + } +}) + +app.use('/-', express.static(__dirname)) + +app.get('/*', (req,res)=>{ + const url = decodeURIComponent(req.url) + console.debug ('GET',url) + try { + const [, capire, pkg ] = /^\/(@capire)\/(\w+)/.exec(url) + const package = require (`${capire}/${pkg}/package.json`) + const tarball = `capire-${pkg}-${package.version}.tgz` + // https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md + res.json({ + "name": package.name, + "dist-tags": { + "latest": package.version + }, + "versions": { + [package.version]: { + "name": package.name, + "version": package.version, + "dist": { + "tarball": `http://localhost:${port}/-/${tarball}` + }, + } + }, + }) + } catch (e) { + console.error(e) + res.sendStatus(404) + } +}) + +app.listen(port, ()=>{ + console.log (`npm set @capire:registry=http://localhost:${port}`) + console.log (`@capire registry listening on http://localhost:${port}`) + exec(`npm set @capire:registry=http://localhost:${port}`) +}) + +const _exit = ()=>{ + console.log ('\nnpm conf rm @capire:registry') + exec('npm conf rm @capire:registry') + exec('rm *.tgz') + process.exit() +} +process.on ('SIGTERM',_exit) +process.on ('SIGHUP',_exit) +process.on ('SIGINT',_exit) +process.on ('SIGUSR2',_exit) diff --git a/README.md b/README.md index 01d8fdf1..9f7773c4 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,22 @@ npx jest > While mocha is a bit smaller and faster, jest runs tests in parallel and isolation, which allows to run all tests. +### Serve `npm` + +We've simple npm registry mock included which allows you to do an `npm install @capire/` anywhere locally. Use it as follows: + +1. Start the @capire registry: +```sh +npm run registry +``` +> While running this will have `@capire:registry=http://localhost:4444` set with npmrc. + +2. Install one of the @capire packages wherever you like, e.g.: +```sh +npm add @capire/common @capire/bookshop +``` + + ## Get Support Check out the documentation at [https://cap.cloud.sap](https://cap.cloud.sap).
diff --git a/package.json b/package.json index b783f428..21fbea51 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "sqlite3": "^5" }, "scripts": { + "registry": "cd .registry && node server.js", "bookshop": "cds watch bookshop", "fiori": "cds watch fiori", "media": "cds watch media",