-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsofria_mediaId.js
More file actions
175 lines (155 loc) · 4.43 KB
/
Copy pathsofria_mediaId.js
File metadata and controls
175 lines (155 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const PromisePool = require("es6-promise-pool");
const Filesystem = require("./util/filesystem");
const crypto = require("crypto");
const path = require("path");
const fse = require("fs-extra");
const { Proskomma } = require("proskomma-core");
const NUM_USX_FILES_TO_PROCESS_ASYNC = 5;
const NUM_CHAPTERS_TO_PROCESS_ASYNC = 4;
if (global.crypto != "object") {
global.crypto = {
getRandomValues: (array) => {
if (crypto.webcrypto?.getRandomValues) {
return crypto.webcrypto.getRandomValues(array);
}
return crypto.randomBytes(array.length);
},
};
}
const zeroComplete = function (num, places) {
return String(num).padStart(places, "0");
};
const generateChapterContent = function (chapter, usxFile, jsonPathOutput, pk) {
return new Promise(function (resolve, reject) {
const chapterQuery = `{documents {sofria(indent: 2, chapter: ${chapter}) } }`;
const gqlObject = pk.gqlQuerySync(chapterQuery);
if (gqlObject?.data?.documents[0]) {
const chapterJson = gqlObject.data.documents[0].sofria;
writeUsxJsonFile(
{
name: usxFile.name,
chapter: chapter,
jsonContent: chapterJson,
},
jsonPathOutput
)
.then(() => resolve(true)) // Resolves the outer promise when writeUsxJsonFile completes
.catch(reject); // Propagates any error to the outer promise;
} else {
resolve(true);
}
});
};
const generateJsonContentByUSXFile = function (usxFile, jsonPathOutput) {
return new Promise(function (resolve, _) {
const content = fse.readFileSync(usxFile.fullpath).toString();
if (content !== "" && usxFile.suffix !== "") {
const pk = new Proskomma();
pk.importDocument(
{
lang: "xxx",
abbr: "",
},
usxFile.suffix,
content
);
const chaptersQuery = `{documents {cIndexes { chapter } } }`;
const chaptersResult = pk.gqlQuerySync(chaptersQuery);
const chapters = chaptersResult.data.documents[0].cIndexes.map(
(ci) => ci.chapter
);
let count = 0;
const promiseChapterProducer = function () {
if (count < chapters.length) {
const chapterToProcess = chapters[count];
++count;
return generateChapterContent(
chapterToProcess,
usxFile,
jsonPathOutput,
pk
);
} else {
return null;
}
};
const poolChapter = new PromisePool(
promiseChapterProducer,
NUM_CHAPTERS_TO_PROCESS_ASYNC
);
poolChapter.start().then(
function () {
console.info(
"Generate JSON =>",
`Complete USX File: ${usxFile.fullpath}`
);
},
function (error) {
console.error(
"Generate JSON =>",
`Error processing USX File ${usxFile.fullpath}` + error.message
);
}
);
}
resolve(true);
});
};
const writeUsxJsonFile = async function (usxFile, jsonPathOutput) {
const newFile = path.join(
jsonPathOutput,
`${usxFile.name}_${zeroComplete(usxFile.chapter, 3)}.json`
);
try {
await fse.outputFile(newFile, usxFile.jsonContent);
} catch (err) {
console.error(
"Generate JSON =>",
`Error it can not create file: ${newFile} `,
err
);
}
};
const run = async function (usxPathInput, jsonPathOutput) {
console.info("Generate JSON =>", "Generate JSON filese - Start process..");
const listFilesToProcess = await Filesystem.getListFileFromDirectory(
usxPathInput
);
const folderFile = path.join(jsonPathOutput);
await fse.mkdir(folderFile, {
recursive: true,
});
let count = 0;
const promiseFileProducer = function () {
if (count < listFilesToProcess.length) {
const usxFileToProcess = listFilesToProcess[count];
++count;
return generateJsonContentByUSXFile(
usxFileToProcess.file,
jsonPathOutput
);
} else {
return null;
}
};
const pool = new PromisePool(
promiseFileProducer,
NUM_USX_FILES_TO_PROCESS_ASYNC
);
pool.start().then(
function () {
console.info(
"Generate JSON =>",
`File list (${listFilesToProcess.length}) processing completed`
);
},
function (error) {
console.error(
"Generate JSON =>",
"Error processing file list" + error.message,
error
);
}
);
};
module.exports.run = run;