Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions convert/convertBadges.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { copyFile, existsSync, mkdirSync, rmSync, writeFileSync } from 'fs';
import { copyFile, existsSync, writeFileSync } from 'fs';
import path from 'path';
import { ConfigTaskOutput } from './convertConfig';
import { createOutputDir, deleteOutputDir } from './fileUtils';
import { Promisable, Task, TaskOutput } from './Task';

export async function convertBadges(
Expand All @@ -10,15 +11,11 @@ export async function convertBadges(
) {
const dstBadgeDir = path.join('src/gen-assets', 'badges');
if (!configData.data.mainFeatures['share-apple-app-link']) {
if (existsSync(dstBadgeDir)) {
rmSync(dstBadgeDir, { recursive: true });
}
deleteOutputDir(dstBadgeDir);
return;
}

if (!existsSync(dstBadgeDir)) {
mkdirSync(dstBadgeDir);
}
createOutputDir(dstBadgeDir);

// Badge languages from config
const languages = Object.keys(configData.data.interfaceLanguages!.writingSystems);
Expand Down
27 changes: 7 additions & 20 deletions convert/convertBooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { freeze, postQueries, queries } from '../sab-proskomma-tools';
import { SABProskomma } from '../src/lib/sab-proskomma';
import type { ConfigTaskOutput } from './convertConfig';
import { convertMarkdownsToMilestones } from './convertMarkdown';
import { createHashedFile, getHashedNameFromContents } from './fileUtils';
import { createHashedFile, createOutputDir, getHashedNameFromContents } from './fileUtils';
import { hasAudioExtension, hasImageExtension } from './stringUtils';
import { Promisable, Task, TaskOutput } from './Task';
import { verifyGlossaryEntries } from './verifyGlossaryEntries';
Expand Down Expand Up @@ -460,13 +460,9 @@ export async function convertBooks(
htmlBooks[context.docSet] = [];
if (collection.books.find((b) => b.format === 'html')) {
const illPath = join('static', 'illustrations');
if (!fs.existsSync(illPath)) {
fs.mkdirSync(illPath, { recursive: true });
}
createOutputDir(illPath);
const collPath = join('static', 'collections', collection.id);
if (!fs.existsSync(collPath)) {
fs.mkdirSync(collPath, { recursive: true });
}
createOutputDir(collPath);
}
for (const book of collection.books) {
let bookConverted = false;
Expand Down Expand Up @@ -538,26 +534,17 @@ export async function convertBooks(
catalogEntries.push(pk.gqlQuery(queries.catalogQuery({ cv: true })));
//check if folder exists for collection
const collPath = path.join('src/gen-assets', 'collections', context.bcId);
if (!fs.existsSync(collPath)) {
if (verbose) console.log('creating: ' + collPath);
fs.mkdirSync(collPath, { recursive: true });
}
createOutputDir(collPath);
//add quizzes path if necessary
if (quizzes[context.docSet].length > 0) {
const qPath = path.join('src/gen-assets', 'collections', context.bcId, 'quizzes');
if (!fs.existsSync(qPath)) {
if (verbose) console.log('creating: ' + qPath);
fs.mkdirSync(qPath, { recursive: true });
}
createOutputDir(qPath);
}
}
//write catalog entries
const entries = await Promise.all(catalogEntries);
const catalogPath = path.join('src/gen-assets', 'collections', 'catalog');
if (!fs.existsSync(catalogPath)) {
if (verbose) console.log('creating: ' + catalogPath);
fs.mkdirSync(catalogPath, { recursive: true });
}
createOutputDir(catalogPath);
entries.forEach((entry) => {
fs.writeFileSync(
path.join(catalogPath, entry.data.docSets[0].id + '.json'),
Expand Down Expand Up @@ -820,7 +807,7 @@ function convertScriptureBook(
const bookFullDir = path.join(context.dataDir, 'books-full', context.bcId);
const bookPath = path.join(bookFullDir, file);
console.log(`Writing file: ${bookPath}`);
fs.mkdirSync(bookFullDir, { recursive: true });
createOutputDir(bookFullDir);
fs.writeFileSync(bookPath, content);
}
const selectors = { lang: context.lang, abbr: context.bcId };
Expand Down
10 changes: 4 additions & 6 deletions convert/convertContents.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import { ScriptureConfig } from '$config';
import jsdom from 'jsdom';
import { ConfigTaskOutput, parseLangAttribute } from './convertConfig';
import { createHashedFile } from './fileUtils';
import { createHashedFile, createOutputDir, deleteOutputDir } from './fileUtils';
import { Task, TaskOutput } from './Task';

type ContentItem = {
Expand Down Expand Up @@ -80,11 +80,9 @@ export function convertContents(
const destDir = path.join('static', 'contents');
const hasContentsDir = existsSync(contentsDir);
if (hasContentsDir) {
mkdirSync(destDir, { recursive: true });
createOutputDir(destDir);
} else {
if (existsSync(destDir)) {
rmSync(destDir, { recursive: true });
}
deleteOutputDir(destDir);
}

const contentsFile = path.join(dataDir, 'contents.xml');
Expand Down
2 changes: 1 addition & 1 deletion convert/convertFirebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copyFile, existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { Task, TaskOutput } from './Task';

Expand Down
10 changes: 3 additions & 7 deletions convert/convertPlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import type { ScriptureConfig } from '$config';
import { PlanDataItem, PlansData } from '../src/lib/data/plansData';
import { ConfigTaskOutput } from './convertConfig';
import { createOutputDir, deleteOutputDir } from './fileUtils';
import { Task, TaskOutput } from './Task';

function changeFileExtension(filename: string, ext: string): string {
Expand All @@ -24,14 +25,9 @@ export function convertPlans(
const plansDir = path.join(dataDir, 'plans');
const destDir = path.join('src/gen-assets', 'plans');
if (existsSync(plansDir)) {
if (existsSync(destDir)) {
rmSync(destDir, { recursive: true });
}
mkdirSync(destDir);
createOutputDir(destDir);
} else {
if (existsSync(destDir)) {
rmSync(destDir, { recursive: true });
}
deleteOutputDir(destDir);
}

const files: any[] = [];
Expand Down
7 changes: 3 additions & 4 deletions convert/convertReverseIndex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, readFileSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import path from 'path';
import type { DictionaryConfig } from '$config';
import { createOutputDir } from './fileUtils';
import { FileContent, Task, TaskOutput } from './Task';

interface ReversalEntry {
Expand Down Expand Up @@ -38,9 +39,7 @@ export function convertReverseIndex(
throw new Error(`Required reversal index not found: ${indexFilePath}`);
}

if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
createOutputDir(outputDir);

const content = readFileSync(indexFilePath, 'utf-8');
const indexEntries = content
Expand Down
5 changes: 3 additions & 2 deletions convert/convertSQLite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { copyFile, mkdirSync } from 'fs';
import { copyFile } from 'fs';
import path from 'path';
import { createOutputDir } from './fileUtils';
import { Task, TaskOutput } from './Task';

export interface SQLiteTaskOutput extends TaskOutput {
Expand All @@ -13,7 +14,7 @@ export function convertSQLite(dataDir: string, verbose: number) {
const srcFileWasm = path.join('node_modules/sql.js/dist', 'sql-wasm.wasm');
const dstDirWasm = path.join('src/gen-assets', 'wasm');
const dstFileWasm = path.join(dstDirWasm, 'sql-wasm.wasm');
mkdirSync(dstDirWasm, { recursive: true });
createOutputDir(dstDirWasm);
copyFile(srcFileWasm, dstFileWasm, function (err: any) {
if (err) throw err;
if (verbose) console.log(`Copied ${srcFileWasm} to ${dstFileWasm}`);
Expand Down
9 changes: 4 additions & 5 deletions convert/convertStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { copyFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { ConfigTaskOutput } from 'convertConfig';
import { createOutputDir } from './fileUtils';
import { compareVersions } from './stringUtils';
import { Task, TaskOutput } from './Task';

Expand All @@ -18,7 +19,7 @@ export interface StylesTaskOutput extends TaskOutput {
export function convertStyles(dataDir: string, configData: ConfigTaskOutput, verbose: number) {
const srcDir = path.join(dataDir, 'styles');
const dstDir = path.join('src/gen-assets', 'styles');
mkdirSync(dstDir, { recursive: true });
createOutputDir(dstDir);

readdirSync(srcDir).forEach((file) => {
const srcFile = path.join(srcDir, file);
Expand Down Expand Up @@ -83,9 +84,7 @@ export function convertStyles(dataDir: string, configData: ConfigTaskOutput, ver
if (existsSync(path.join(dataDir, 'cloud', fontName))) {
finalPath = 'cloud';
const targetDir = path.join('src/gen-assets', 'cloud');
if (!existsSync(targetDir)) {
mkdirSync(targetDir, { recursive: true });
}
createOutputDir(targetDir);
copyFileSync(
path.join(dataDir, 'cloud', fontName),
path.join(targetDir, fontName)
Expand Down
15 changes: 14 additions & 1 deletion convert/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from 'crypto';
import { copyFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { copyFileSync, existsSync, mkdirSync, readFileSync, rmdirSync, writeFileSync } from 'fs';
import { basename, extname, join } from 'path';

export function getHashedName(dataDir: string, src: string) {
Expand Down Expand Up @@ -62,3 +62,16 @@ export function createHashedFileFromContents(

return hashedPath;
}

export function createOutputDir(dirPath: string) {
if (existsSync(dirPath)) {
rmdirSync(dirPath, { recursive: true });
}
mkdirSync(dirPath, { recursive: true });
}

export function deleteOutputDir(dirPath: string) {
if (existsSync(dirPath)) {
rmdirSync(dirPath, { recursive: true });
}
}