You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-07-13 00:10:37 +02:00
Tools: Allow setting website build environment from config file
This commit is contained in:
@ -1,6 +1,10 @@
|
|||||||
const fs = require('fs-extra');
|
import { pathExistsSync, readFileSync } from 'fs-extra';
|
||||||
|
|
||||||
export async function credentialDir() {
|
// All these calls used to be async but certain scripts need to load config
|
||||||
|
// files early, so they've been converted to sync calls. Do not convert them
|
||||||
|
// back to async.
|
||||||
|
|
||||||
|
export function credentialDir() {
|
||||||
const username = require('os').userInfo().username;
|
const username = require('os').userInfo().username;
|
||||||
|
|
||||||
const toTry = [
|
const toTry = [
|
||||||
@ -11,23 +15,23 @@ export async function credentialDir() {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const dirPath of toTry) {
|
for (const dirPath of toTry) {
|
||||||
if (await fs.pathExists(dirPath)) return dirPath;
|
if (pathExistsSync(dirPath)) return dirPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Could not find credential directory in any of these paths: ${JSON.stringify(toTry)}`);
|
throw new Error(`Could not find credential directory in any of these paths: ${JSON.stringify(toTry)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function credentialFile(filename: string) {
|
export function credentialFile(filename: string) {
|
||||||
const rootDir = await credentialDir();
|
const rootDir = credentialDir();
|
||||||
const output = `${rootDir}/${filename}`;
|
const output = `${rootDir}/${filename}`;
|
||||||
if (!(await fs.pathExists(output))) throw new Error(`No such file: ${output}`);
|
if (!(pathExistsSync(output))) throw new Error(`No such file: ${output}`);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readCredentialFile(filename: string, defaultValue: string = '') {
|
export function readCredentialFile(filename: string, defaultValue: string = '') {
|
||||||
try {
|
try {
|
||||||
const filePath = await credentialFile(filename);
|
const filePath = credentialFile(filename);
|
||||||
const r = await fs.readFile(filePath);
|
const r = readFileSync(filePath);
|
||||||
// There's normally no reason to keep the last new line character and it
|
// There's normally no reason to keep the last new line character and it
|
||||||
// can cause problems in certain scripts, so trim it. Any other white
|
// can cause problems in certain scripts, so trim it. Any other white
|
||||||
// space should also not be relevant.
|
// space should also not be relevant.
|
||||||
@ -36,3 +40,16 @@ export async function readCredentialFile(filename: string, defaultValue: string
|
|||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function readCredentialFileJson<T>(filename: string, defaultValue: T = null): T {
|
||||||
|
const v = readCredentialFile(filename);
|
||||||
|
if (!v) return defaultValue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const o = JSON.parse(v);
|
||||||
|
return o;
|
||||||
|
} catch (error) {
|
||||||
|
error.message = `Could not parse JSON file ${filename}: ${error.message}`;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,14 +8,21 @@ import { MarkdownAndFrontMatter, stripOffFrontMatter } from './utils/frontMatter
|
|||||||
import { dirname, basename } from 'path';
|
import { dirname, basename } from 'path';
|
||||||
import { readmeFileTitle, replaceGitHubByWebsiteLinks } from './utils/parser';
|
import { readmeFileTitle, replaceGitHubByWebsiteLinks } from './utils/parser';
|
||||||
import { extractOpenGraphTags } from './utils/openGraph';
|
import { extractOpenGraphTags } from './utils/openGraph';
|
||||||
|
import { readCredentialFileJson } from '@joplin/lib/utils/credentialFiles';
|
||||||
|
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
|
|
||||||
|
interface BuildConfig {
|
||||||
|
env: Env;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildConfig = readCredentialFileJson<BuildConfig>('website-build.json', {
|
||||||
|
env: Env.Prod,
|
||||||
|
});
|
||||||
|
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const md5File = require('md5-file/promise');
|
const md5File = require('md5-file/promise');
|
||||||
|
|
||||||
const env = Env.Prod;
|
|
||||||
|
|
||||||
const docDir = `${dirname(dirname(dirname(dirname(__dirname))))}/joplin-website/docs`;
|
const docDir = `${dirname(dirname(dirname(dirname(__dirname))))}/joplin-website/docs`;
|
||||||
|
|
||||||
if (!pathExistsSync(docDir)) throw new Error(`Doc directory does not exist: ${docDir}`);
|
if (!pathExistsSync(docDir)) throw new Error(`Doc directory does not exist: ${docDir}`);
|
||||||
@ -25,7 +32,7 @@ const readmeDir = `${rootDir}/readme`;
|
|||||||
const mainTemplateHtml = readFileSync(`${websiteAssetDir}/templates/main-new.mustache`, 'utf8');
|
const mainTemplateHtml = readFileSync(`${websiteAssetDir}/templates/main-new.mustache`, 'utf8');
|
||||||
const frontTemplateHtml = readFileSync(`${websiteAssetDir}/templates/front.mustache`, 'utf8');
|
const frontTemplateHtml = readFileSync(`${websiteAssetDir}/templates/front.mustache`, 'utf8');
|
||||||
const plansTemplateHtml = readFileSync(`${websiteAssetDir}/templates/plans.mustache`, 'utf8');
|
const plansTemplateHtml = readFileSync(`${websiteAssetDir}/templates/plans.mustache`, 'utf8');
|
||||||
const stripeConfig = loadStripeConfig(env, `${rootDir}/packages/server/stripeConfig.json`);
|
const stripeConfig = loadStripeConfig(buildConfig.env, `${rootDir}/packages/server/stripeConfig.json`);
|
||||||
const partialDir = `${websiteAssetDir}/templates/partials`;
|
const partialDir = `${websiteAssetDir}/templates/partials`;
|
||||||
|
|
||||||
const discussLink = 'https://discourse.joplinapp.org/c/news/9';
|
const discussLink = 'https://discourse.joplinapp.org/c/news/9';
|
||||||
@ -82,7 +89,7 @@ async function getAssetUrls(): Promise<AssetUrls> {
|
|||||||
|
|
||||||
function defaultTemplateParams(assetUrls: AssetUrls): TemplateParams {
|
function defaultTemplateParams(assetUrls: AssetUrls): TemplateParams {
|
||||||
return {
|
return {
|
||||||
env,
|
env: buildConfig.env,
|
||||||
baseUrl,
|
baseUrl,
|
||||||
imageBaseUrl: `${baseUrl}/images`,
|
imageBaseUrl: `${baseUrl}/images`,
|
||||||
cssBaseUrl,
|
cssBaseUrl,
|
||||||
|
Reference in New Issue
Block a user