Skip to content

Commit bbe18df

Browse files
authored
Improve code quality across JavaScript and TypeScript utilities (#3504)
1 parent 7869178 commit bbe18df

File tree

4 files changed

+63
-54
lines changed

4 files changed

+63
-54
lines changed

scripts/db_tools/duplicate-doc.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ ShareDB.types.register(OTJson0.type);
1717
async function run() {
1818
const ws = utils.createWS(connectionConfig);
1919
const conn = new ShareDB.Connection(ws);
20-
for (const docId of docIds) {
21-
const document = conn.get(collection, docId);
22-
await utils.fetchDoc(document);
23-
const docParts = docId.split(':');
24-
// Convert the book number part to book ID
25-
const bookId = Scr.Canon.bookNumberToId(parseInt(docParts[1]));
26-
const newDocId = `${docParts[0]}:${bookId}:${docParts[2]}:target`;
27-
const newDoc = conn.get(collection, newDocId);
28-
const newData = document.data;
29-
newData.dataId = newDocId;
30-
await utils.createDoc(newDoc, newData, OTJson0.type.name);
20+
try {
21+
for (const docId of docIds) {
22+
const document = conn.get(collection, docId);
23+
await utils.fetchDoc(document);
24+
const docParts = docId.split(':');
25+
// Convert the book number part to book ID
26+
const bookId = Scr.Canon.bookNumberToId(parseInt(docParts[1]));
27+
const newDocId = `${docParts[0]}:${bookId}:${docParts[2]}:target`;
28+
const newDoc = conn.get(collection, newDocId);
29+
const newData = document.data;
30+
newData.dataId = newDocId;
31+
await utils.createDoc(newDoc, newData, OTJson0.type.name);
32+
}
33+
} finally {
34+
conn.close();
3135
}
32-
conn.close();
3336
}
3437

3538
run();

scripts/db_tools/utils.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
const WebSocket = require('ws');
22

3+
// Configuration constants
4+
const devConfig = {
5+
dbLocation: 'mongodb://localhost:27017/xforge',
6+
wsConnectionString: 'ws://127.0.0.1:5003/?server=true',
7+
origin: 'http://localhost:5000'
8+
};
9+
10+
const qaConfig = {
11+
dbLocation: 'mongodb://localhost:4017/xforge',
12+
wsConnectionString: 'ws://127.0.0.1:4003/?server=true',
13+
origin: 'https://qa.scriptureforge.org'
14+
};
15+
16+
const liveConfig = {
17+
dbLocation: 'mongodb://localhost:3017/xforge',
18+
wsConnectionString: 'ws://127.0.0.1:3003/?server=true',
19+
origin: 'https://scriptureforge.org'
20+
};
21+
22+
const databaseConfigs = new Map();
23+
databaseConfigs.set('dev', devConfig);
24+
databaseConfigs.set('qa', qaConfig);
25+
databaseConfigs.set('live', liveConfig);
26+
27+
/** Enum of colors and their bash 256-colour values. */
28+
const colors = Object.freeze({
29+
darkGrey: 241,
30+
red: 196,
31+
lightBlue: 39,
32+
lightGreen: 48,
33+
yellow: 190,
34+
orange: 208
35+
});
36+
37+
let shouldUseColor = true;
38+
339
/**
440
* @param {ShareDB.Doc} doc The doc to fetch
541
*/
@@ -55,7 +91,7 @@ function deleteDoc(doc) {
5591
function createDoc(doc, data, type) {
5692
return new Promise((resolve, reject) => {
5793
doc.create(data, type, undefined, err => {
58-
if (err) {
94+
if (err != null) {
5995
reject(err);
6096
} else {
6197
resolve();
@@ -90,8 +126,6 @@ function fetchSnapshotByTimestamp(conn, collection, docId, time) {
90126
});
91127
}
92128

93-
let shouldUseColor = true;
94-
95129
/** Specify if `colored()` should use colouring. Nice for output to a dark terminal. Not nice in log files. */
96130
function useColor(ifUseColor) {
97131
shouldUseColor = ifUseColor;
@@ -113,20 +147,10 @@ function colored(colorCode, textToColor) {
113147
}
114148
}
115149

116-
/** Enum of colors and their bash 256-colour values. */
117-
const colors = Object.freeze({
118-
darkGrey: 241,
119-
red: 196,
120-
lightBlue: 39,
121-
lightGreen: 48,
122-
yellow: 190,
123-
orange: 208
124-
});
125-
126150
/**
127151
* @param {Object[]} ops List of operations, where an operation can be any object for the purposes of this function
128152
* (though in practice the definition of an operation should be much more limited; this function is just flexible).
129-
* @param {showAttributes} Describe some op attributes inline.
153+
* @param {boolean} showAttributes Describe some op attributes inline.
130154
*/
131155
function visualizeOps(ops, showAttributes = false) {
132156
const result = ops
@@ -157,29 +181,6 @@ function createWS(connectionConfig) {
157181
return new WebSocket(connectionConfig.wsConnectionString, [], { origin: connectionConfig.origin });
158182
}
159183

160-
const devConfig = {
161-
dbLocation: 'mongodb://localhost:27017/xforge',
162-
wsConnectionString: 'ws://127.0.0.1:5003/?server=true',
163-
origin: 'http://localhost:5000'
164-
};
165-
166-
const qaConfig = {
167-
dbLocation: 'mongodb://localhost:4017/xforge',
168-
wsConnectionString: 'ws://127.0.0.1:4003/?server=true',
169-
origin: 'https://qa.scriptureforge.org'
170-
};
171-
172-
const liveConfig = {
173-
dbLocation: 'mongodb://localhost:3017/xforge',
174-
wsConnectionString: 'ws://127.0.0.1:3003/?server=true',
175-
origin: 'https://scriptureforge.org'
176-
};
177-
178-
const databaseConfigs = new Map();
179-
databaseConfigs.set('dev', devConfig);
180-
databaseConfigs.set('qa', qaConfig);
181-
databaseConfigs.set('live', liveConfig);
182-
183184
module.exports = {
184185
fetchDoc,
185186
submitDocOp,

scripts/update_from_crowdin.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ async function copyFiles() {
113113
for await (const dirEntry of Deno.readDir(source)) {
114114
if (dirEntry.isFile) {
115115
const crowdinFileName = dirEntry.name;
116-
const localeCodeInCrowdinFile = crowdinFileName.match(localeRegex)![1];
116+
const localeMatch = crowdinFileName.match(localeRegex);
117+
if (localeMatch == null) {
118+
throw new Error(`Could not extract locale from file name: ${crowdinFileName}`);
119+
}
120+
121+
const localeCodeInCrowdinFile = localeMatch[1];
117122
const localeCodeWithHyphens = localeCodeInCrowdinFile.replaceAll("_", "-");
118123
const localeObject = locales.find(l => l.tags.includes(localeCodeWithHyphens));
119124
if (localeObject == null) continue;

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function supportedBrowser(): boolean {
4444
'samsung internet': '>=17.0'
4545
}
4646
});
47-
return isSupportedBrowser ? true : false;
47+
return isSupportedBrowser ?? false;
4848
}
4949

5050
export function isIosDevice(): boolean {
@@ -135,16 +135,16 @@ export function aspCultureCookieValue(language: string): string {
135135
*/
136136
export function getAspCultureCookieLanguage(cookie: string): string {
137137
const parts = cookie.split('|');
138-
let c: string;
139-
let uic: string;
138+
let c: string | undefined;
139+
let uic: string | undefined;
140140
parts.forEach(value => {
141141
if (value.startsWith('c=')) {
142142
c = value.slice('c='.length);
143143
} else if (value.startsWith('uic=')) {
144144
uic = value.slice('uic='.length);
145145
}
146146
});
147-
return uic! || c! || 'en';
147+
return uic ?? c ?? 'en';
148148
}
149149

150150
export function getI18nLocales(): Locale[] {

0 commit comments

Comments
 (0)