From 1bf535549a83dc02deae9f314eb7252a8b005dd3 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Tue, 19 Jan 2021 15:57:09 +0100
Subject: [PATCH] Reuse some rollup helper functions from the toolkit

---
 rollup.config.js | 66 +++---------------------------------------------
 rollup.utils.js  | 21 +++++++++++++++
 2 files changed, 25 insertions(+), 62 deletions(-)
 create mode 100644 rollup.utils.js

diff --git a/rollup.config.js b/rollup.config.js
index cd895e2..1f413ed 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -1,5 +1,4 @@
 import path from 'path';
-import fs from 'fs';
 import url from 'url';
 import glob from 'glob';
 import resolve from '@rollup/plugin-node-resolve';
@@ -14,8 +13,9 @@ import license from 'rollup-plugin-license';
 import del from 'rollup-plugin-delete';
 import emitEJS from 'rollup-plugin-emit-ejs'
 import {getBabelOutputPlugin} from '@rollup/plugin-babel';
-import selfsigned from 'selfsigned';
 import appConfig from './app.config.js';
+import {getPackagePath, getBuildInfo} from './vendor/toolkit/rollup.utils.js';
+import {generateTLSConfig} from './rollup.utils.js';
 
 // -------------------------------
 
@@ -70,64 +70,6 @@ if (config.nextcloudBaseURL) {
     config.nextcloudWebDavURL = '';
 }
 
-/**
- * Creates a server certificate and caches it in the .cert directory
- */
-function generateTLSConfig() {
-  fs.mkdirSync('.cert', {recursive: true});
-
-  if (!fs.existsSync('.cert/server.key') || !fs.existsSync('.cert/server.cert')) {
-    const attrs = [{name: 'commonName', value: 'dbp-dev.localhost'}];
-    const pems = selfsigned.generate(attrs, {algorithm: 'sha256', days: 9999});
-    fs.writeFileSync('.cert/server.key', pems.private);
-    fs.writeFileSync('.cert/server.cert', pems.cert);
-  }
-
-  return {
-    key: fs.readFileSync('.cert/server.key'),
-    cert: fs.readFileSync('.cert/server.cert')
-  }
-}
-
-function getBuildInfo() {
-    const child_process = require('child_process');
-    const url = require('url');
-
-    let remote = child_process.execSync('git config --get remote.origin.url').toString().trim();
-    let commit = child_process.execSync('git rev-parse --short HEAD').toString().trim();
-
-    let parsed = url.parse(remote);
-    // convert git urls
-    if (parsed.protocol === null) {
-        parsed = url.parse('git://' + remote.replace(":", "/"));
-        parsed.protocol = 'https:';
-    }
-    let newPath = parsed.path.slice(0, parsed.path.lastIndexOf('.'));
-    let newUrl = parsed.protocol + '//' + parsed.host + newPath + '/commit/' + commit;
-
-    return {
-        info: commit,
-        url: newUrl,
-        time: new Date().toISOString(),
-        env: appEnv
-    }
-}
-
-export async function getPackagePath(packageName, assetPath) {
-    const r = resolve();
-    const resolved = await r.resolveId(packageName);
-    let packageRoot;
-    if (resolved !== null) {
-        const id = (await r.resolveId(packageName)).id;
-        const packageInfo = r.getPackageInfoForId(id);
-        packageRoot = packageInfo.root;
-    } else {
-        // Non JS packages
-        packageRoot = path.dirname(require.resolve(packageName + '/package.json'));
-    }
-    return path.relative(process.cwd(), path.join(packageRoot, assetPath));
-}
-
 export default (async () => {return {
     input: (appEnv != 'test') ? [
       'src/' + pkg.name + '.js',
@@ -167,7 +109,7 @@ export default (async () => {return {
         }),
         consts({
           environment: appEnv,
-          buildinfo: getBuildInfo(),
+          buildinfo: getBuildInfo(appEnv),
           nextcloudBaseURL: config.nextcloudBaseURL,
         }),
         emitEJS({
@@ -194,7 +136,7 @@ export default (async () => {return {
             environment: appEnv,
             matomoUrl: config.matomoUrl,
             matomoSiteId: config.matomoSiteId,
-            buildInfo: getBuildInfo()
+            buildInfo: getBuildInfo(appEnv)
           }
         }),
         resolve({
diff --git a/rollup.utils.js b/rollup.utils.js
new file mode 100644
index 0000000..46b774a
--- /dev/null
+++ b/rollup.utils.js
@@ -0,0 +1,21 @@
+import fs from 'fs';
+import selfsigned from 'selfsigned';
+
+/**
+ * Creates a server certificate and caches it in the .cert directory
+ */
+export function generateTLSConfig() {
+    fs.mkdirSync('.cert', {recursive: true});
+  
+    if (!fs.existsSync('.cert/server.key') || !fs.existsSync('.cert/server.cert')) {
+      const attrs = [{name: 'commonName', value: 'dbp-dev.localhost'}];
+      const pems = selfsigned.generate(attrs, {algorithm: 'sha256', days: 9999});
+      fs.writeFileSync('.cert/server.key', pems.private);
+      fs.writeFileSync('.cert/server.cert', pems.cert);
+    }
+  
+    return {
+      key: fs.readFileSync('.cert/server.key'),
+      cert: fs.readFileSync('.cert/server.cert')
+    }
+  }
\ No newline at end of file
-- 
GitLab