Skip to content

Commit 495d80a

Browse files
committed
Add logger to rebuild vocabs and chore tasks #21 #5
1 parent 1ac4766 commit 495d80a

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/commonChoreForVocabs.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require("fs-extra")
22
const path = require('path');
33
const { gitHubApiHeaders } = require("./common.js")
44
const types = require("./types.js")
5+
const logger = require("./logger.js")
56

67

78
/** get all json data from dist/build folder
@@ -21,7 +22,7 @@ const readBuildDir = async () => {
2122

2223
return jsonObjects;
2324
} catch (err) {
24-
console.error('Error:', err);
25+
logger.error(`Error: ${err}`);
2526
throw err;
2627
}
2728
}
@@ -60,15 +61,15 @@ async function checkIfBranchExists(repository, ref) {
6061
.then(data => {
6162
const branchExists = data.some(branch => branch.name === branchName);
6263
if (branchExists) {
63-
console.log(`${repository}/${branchName} exists!`);
64+
// console.log(`${repository}/${branchName} exists!`);
6465
return true
6566
} else {
66-
console.log(`Branch "${branchName}" does not exist.`);
67+
logger.error(`Branch "${branchName}" does not exist.`);
6768
return false
6869
}
6970
})
7071
.catch(error => {
71-
console.error(`Fetch error for repo ${repository} and branch ${branchName}, ${error}`);
72+
logger.error(`Fetch error for repo ${repository} and branch ${branchName}, ${error}`);
7273
return false
7374
});
7475
return result

src/rebuildVocabs.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const fs = require("fs-extra")
2-
const path = require('path');
3-
const { securePayload, gitHubApiHeaders } = require("./common.js")
2+
const { securePayload } = require("./common.js")
43
const { readBuildDir, sortBuildInfo, checkIfBranchExists } = require("./commonChoreForVocabs.js")
54
const types = require("./types.js")
5+
const logger = require("./logger.js")
66
require("dotenv").config()
77

88
const { SECRET, BUILD_URL, REBUILD_MAX_ATTEMPTS } = process.env
@@ -46,7 +46,7 @@ const sendBuildRequest = async (buildInfo) => {
4646
const respBody = await response.text()
4747
// get url from response
4848
const responseUrl = protocolizeUrl(respBody.substring(17))
49-
console.log("Build Urls:", responseUrl)
49+
logger.info(`Build Urls: ${responseUrl}`)
5050
const url = new URL(responseUrl)
5151
const id = url.searchParams.get("id")
5252

@@ -56,7 +56,7 @@ const sendBuildRequest = async (buildInfo) => {
5656
ref: buildInfo.ref
5757
}
5858
} catch (error) {
59-
console.error("Error sending request", error)
59+
logger.error(`Error sending request ${error} `)
6060
return {
6161
id: null,
6262
repository: buildInfo.repository,
@@ -66,11 +66,7 @@ const sendBuildRequest = async (buildInfo) => {
6666
}
6767

6868
/**
69-
* @param {{
70-
* id: string
71-
* repository: string
72-
* ref: string
73-
* }} buildInfo
69+
* @param {types.BuildInfo} buildInfo
7470
*/
7571
const checkBuildStatus = (buildInfo) => {
7672
const maxAttempts = REBUILD_MAX_ATTEMPTS ? Number(REBUILD_MAX_ATTEMPTS) : 30
@@ -81,15 +77,18 @@ const checkBuildStatus = (buildInfo) => {
8177
const response = fs.readFileSync(`./dist/build/${buildInfo.id}.json`)
8278
/** @type {BuildInfo} */
8379
json = JSON.parse(response)
84-
if (json.status === "complete" || json.status === "error") {
85-
console.log(`${json.repository}, ${json.ref}: Finish with status: ${json.status} (ID: ${buildInfo.id})`)
80+
if (json.status === "complete") {
81+
logger.info(`${json.repository}, ${json.ref}: Finish with status: ${json.status} (ID: ${buildInfo.id})`)
82+
return
83+
} else if (json.status === "error") {
84+
logger.error(`${json.repository}, ${json.ref}: Finish with status: ${json.status} (ID: ${buildInfo.id})`)
8685
return
8786
} else {
8887
throw new Error("Not completed")
8988
}
9089
} catch (error) {
9190
if (attempts > maxAttempts || !buildInfo.id) {
92-
console.log(`${buildInfo.repository}, ${buildInfo.ref}: did not finish after ${attempts} attempts. Aborting. Error: ${error} (ID: ${buildInfo.id})`)
91+
logger.info(`${buildInfo.repository}, ${buildInfo.ref}: did not finish after ${attempts} attempts.Aborting.Error: ${error} (ID: ${buildInfo.id})`)
9392
return
9493
}
9594
setTimeout(() => {

0 commit comments

Comments
 (0)