CI: Trying to fix website builder

pull/7973/head
Laurent Cozic 2023-04-04 17:01:34 +02:00
parent 572701d9a0
commit 659c851960
2 changed files with 22 additions and 21 deletions

View File

@ -21,6 +21,10 @@ export function credentialDir() {
throw new Error(`Could not find credential directory in any of these paths: ${JSON.stringify(toTry)}`);
}
export const hasCredentialFile = (filename: string) => {
return pathExistsSync(filename);
};
export function credentialFile(filename: string) {
const rootDir = credentialDir();
const output = `${rootDir}/${filename}`;

View File

@ -1,5 +1,5 @@
import * as fs from 'fs-extra';
import { readCredentialFile } from '@joplin/lib/utils/credentialFiles';
import { pathExists, readFile, writeFile, unlink, stat, createWriteStream } from 'fs-extra';
import { hasCredentialFile, readCredentialFile } from '@joplin/lib/utils/credentialFiles';
import { execCommand as execCommand2, commandToString } from '@joplin/utils';
const fetch = require('node-fetch');
@ -23,7 +23,7 @@ export interface GitHubRelease {
async function insertChangelog(tag: string, changelogPath: string, changelog: string, isPrerelease: boolean, repoTagUrl: string = '') {
repoTagUrl = repoTagUrl || 'https://github.com/laurent22/joplin/releases/tag';
const currentText = await fs.readFile(changelogPath, 'UTF-8');
const currentText = await readFile(changelogPath, 'UTF-8');
const lines = currentText.split('\n');
const beforeLines = [];
@ -80,7 +80,7 @@ export async function completeReleaseWithChangelog(changelogPath: string, newVer
const newChangelog = await insertChangelog(newTag, changelogPath, changelog, isPreRelease, repoTagUrl);
await fs.writeFile(changelogPath, newChangelog);
await writeFile(changelogPath, newChangelog);
console.info('');
console.info('Verify that the changelog is correct:');
@ -95,8 +95,8 @@ export async function completeReleaseWithChangelog(changelogPath: string, newVer
async function loadGitHubUsernameCache() {
const path = `${__dirname}/github_username_cache.json`;
if (await fs.pathExists(path)) {
const jsonString = await fs.readFile(path, 'utf8');
if (await pathExists(path)) {
const jsonString = await readFile(path, 'utf8');
return JSON.parse(jsonString);
}
@ -105,7 +105,7 @@ async function loadGitHubUsernameCache() {
async function saveGitHubUsernameCache(cache: any) {
const path = `${__dirname}/github_username_cache.json`;
await fs.writeFile(path, JSON.stringify(cache));
await writeFile(path, JSON.stringify(cache));
}
// Returns the project root dir
@ -173,22 +173,21 @@ export function toSystemSlashes(path: string) {
}
export async function setPackagePrivateField(filePath: string, value: any) {
const text = await fs.readFile(filePath, 'utf8');
const text = await readFile(filePath, 'utf8');
const obj = JSON.parse(text);
if (!value) {
delete obj.private;
} else {
obj.private = true;
}
await fs.writeFile(filePath, JSON.stringify(obj, null, 2), 'utf8');
await writeFile(filePath, JSON.stringify(obj, null, 2), 'utf8');
}
export async function downloadFile(url: string, targetPath: string) {
const https = require('https');
const fs = require('fs');
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(targetPath);
const file = createWriteStream(targetPath);
https.get(url, (response: any) => {
if (response.statusCode !== 200) reject(new Error(`HTTP error ${response.statusCode}`));
response.pipe(file);
@ -222,10 +221,8 @@ export function fileSha256(filePath: string) {
}
export async function unlinkForce(filePath: string) {
const fs = require('fs-extra');
try {
await fs.unlink(filePath);
await unlink(filePath);
} catch (error) {
if (error.code === 'ENOENT') return;
throw error;
@ -233,10 +230,8 @@ export async function unlinkForce(filePath: string) {
}
export function fileExists(filePath: string) {
const fs = require('fs-extra');
return new Promise((resolve, reject) => {
fs.stat(filePath, (error: any) => {
stat(filePath, (error: any) => {
if (!error) {
resolve(true);
} else if (error.code === 'ENOENT') {
@ -326,7 +321,10 @@ export function patreonOauthToken() {
}
export function githubOauthToken() {
return readCredentialFile('github_oauth_token.txt');
const filename = 'github_oauth_token.txt';
if (hasCredentialFile(filename)) return readCredentialFile(filename);
if (process.env.JOPLIN_GITHUB_OAUTH_TOKEN) return process.env.JOPLIN_GITHUB_OAUTH_TOKEN;
throw new Error(`Cannot get Oauth token. Neither ${filename} nor the env variable JOPLIN_GITHUB_OAUTH_TOKEN are present`);
}
// Note that the GitHub API releases/latest is broken on the joplin-android repo
@ -422,12 +420,11 @@ export function isMac() {
}
export async function insertContentIntoFile(filePath: string, markerOpen: string, markerClose: string, contentToInsert: string) {
const fs = require('fs-extra');
let content = await fs.readFile(filePath, 'utf-8');
let content = await readFile(filePath, 'utf-8');
// [^]* matches any character including new lines
const regex = new RegExp(`${markerOpen}[^]*?${markerClose}`);
content = content.replace(regex, markerOpen + contentToInsert + markerClose);
await fs.writeFile(filePath, content);
await writeFile(filePath, content);
}
export function dirname(path: string) {