Merge branch 'renovate/all-minor-patch' into 'main'
[ProtonMail-WebClient.git] / packages / pack / lib / config.js
blobdfc135a0b3a56be633705e70d45151789d19c516
1 const path = require('path');
2 const dedent = require('dedent');
3 const execa = require('execa');
5 const readJSON = (file) => {
6 const fileName = `${file}.json`;
7 try {
8 return require(path.join(process.cwd(), fileName));
9 } catch {
10 return undefined;
14 const getGitBranch = () => {
15 try {
16 const { stdout = '' } = execa.sync('git describe --all', { shell: true });
17 return stdout.trim();
18 } catch (e) {
19 return '';
23 const getGitCommitHash = () => {
24 try {
25 const { stdout = '' } = execa.sync('git rev-parse HEAD', { shell: true });
26 return stdout.trim();
27 } catch (e) {
28 return '';
32 const getGitTagVersion = (applicationName) => {
33 try {
34 const { stdout = '' } = execa.sync(`git describe --abbrev=0 --match=${applicationName}*`, { shell: true });
35 return stdout.trim();
36 } catch (e) {
37 return '';
41 /**
42 * Clean ex. proton-mail@4.x.x to 4.x.x
44 const getVersionNumberFromTag = (tag) => {
45 return tag.replace(/[^@]*@/g, '');
48 const APP_CONFIG_JSON = readJSON('appConfig') || {};
50 const LOCALES = (() => {
51 try {
52 return require(path.join(process.cwd(), 'locales', 'config', 'locales.json'));
53 } catch (e) {
54 return {};
56 })();
58 const ENV_CONFIG = Object.keys(APP_CONFIG_JSON).reduce(
59 (acc, key) => {
60 if (key === 'appConfig') {
61 acc.app = APP_CONFIG_JSON[key];
62 return acc;
64 const { api, secure } = APP_CONFIG_JSON[key];
65 if (api) {
66 acc.api[key] = api;
68 if (secure) {
69 acc.secure[key] = secure;
71 return acc;
73 { api: {}, secure: {}, app: {} }
76 const API_TARGETS = {
77 prod: 'https://mail.proton.me',
78 localhost: 'https://localhost',
79 proxy: '/api',
80 ...ENV_CONFIG.api,
83 const getApi = (value) => {
84 // We can do --api=https://mail.proton.me/api and it's only for dev, so we can stop here
85 if (value.startsWith('http') || value.startsWith('/api')) {
86 return value;
88 return API_TARGETS[value] || API_TARGETS.prod;
91 const getConfigData = ({ api, sso, apiProxy, publicPath, version }) => {
92 const pkg = require(path.join(process.cwd(), 'package.json'));
93 const appName = pkg.name;
94 if (!appName) {
95 throw new Error('Missing app name');
98 const isProduction = process.env.NODE_ENV === 'production';
100 const appData = {
101 appName,
102 version:
103 version || getVersionNumberFromTag(process.env.CI_COMMIT_TAG || getGitTagVersion(appName)) || '5.0.999.999',
104 locales: LOCALES,
105 api,
106 sso,
107 apiProxy,
108 sentryDsn: isProduction ? ENV_CONFIG.app.sentry || '' : '',
109 sentryDesktopDsn: isProduction ? ENV_CONFIG.app.sentryDesktop || '' : '',
110 publicPath,
113 const buildData = {
114 version: appData.version,
115 commit: process.env.CI_COMMIT_SHA || getGitCommitHash(),
116 branch: process.env.CI_COMMIT_REF_NAME || getGitBranch(),
117 date: new Date().toGMTString(),
120 return {
121 appData,
122 buildData,
126 const getConfigFile = ({ buildData, appData }) => {
127 return dedent`
128 export const CLIENT_TYPE = ${ENV_CONFIG.app.clientType || 1};
129 export const CLIENT_SECRET = '${ENV_CONFIG.app.clientSecret || ''}';
130 export const APP_VERSION = '${buildData.version}';
131 export const COMMIT = '${buildData.commit}';
132 export const BRANCH = '${buildData.branch}';
133 export const DATE_VERSION = '${buildData.date}';
134 export const APP_NAME = '${appData.appName}';
135 export const API_URL = '${(!appData.apiProxy && appData.api) || '/api'}';
136 export const SSO_URL = '${appData.sso || ''}';
137 export const LOCALES = ${JSON.stringify(LOCALES)};
138 export const VERSION_PATH = '${appData.publicPath}assets/version.json';
139 export const SENTRY_DSN = '${appData.sentryDsn}';
140 export const SENTRY_DESKTOP_DSN = '${appData?.sentryDesktopDsn ?? ''}';
144 module.exports = {
145 getConfigData,
146 getConfigFile,
147 getApi,