-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rename wallet files for better alignment (#98)
* fix: rename wallet files for better alignment Signed-off-by: Mirko Mollik <[email protected]> * add link checker script Signed-off-by: Mirko Mollik <[email protected]> * debug validation Signed-off-by: Mirko Mollik <[email protected]> * tmp Signed-off-by: Mirko Mollik <[email protected]> * tmp Signed-off-by: Mirko Mollik <[email protected]> * check cases when folder does not exist Signed-off-by: Mirko Mollik <[email protected]> --------- Signed-off-by: Mirko Mollik <[email protected]>
- Loading branch information
Showing
72 changed files
with
905 additions
and
714 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Link checker | ||
|
||
on: | ||
schedule: | ||
- cron: '0 0 * * 0' # Runs at midnight every Sunday | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
link-checker: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18 | ||
|
||
- name: Cache npm dependencies | ||
uses: actions/cache@v4 | ||
with: | ||
path: viewer/node_modules | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
restore-keys: | | ||
${{ runner.os }}-node- | ||
- name: Install dependencies | ||
run: cd viewer && npm install | ||
|
||
- name: Validate links | ||
run: cd viewer && node scripts/link-checker.mjs | ||
|
||
# Deploy to local repo | ||
- name: Deploy | ||
uses: s0/git-publish-subdir-action@develop | ||
env: | ||
REPO: self | ||
BRANCH: errors | ||
FOLDER: errors | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// this script checks if all the links in the json files are still reachable | ||
import { readdirSync, readFileSync, writeFileSync, mkdirSync, existsSync, rmSync } from 'fs'; | ||
import axios from 'axios'; | ||
import { join, dirname } from 'path'; | ||
|
||
let counter = 0; | ||
let validFiles = 0; | ||
let invalidFiles = 0; | ||
const errorLog = {}; | ||
|
||
async function isLinkReachable(url, filePath, jsonPath) { | ||
try { | ||
const response = await axios.get(url, { | ||
timeout: 10000, // 10 seconds timeout | ||
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; LinkChecker/1.0)' }, | ||
maxRedirects: 5 | ||
}); | ||
return response.status >= 200 && response.status < 400; | ||
} catch (error) { | ||
if (error.code === 'ECONNABORTED') { | ||
console.log(`Request timed out for URL: ${url} in file: ${filePath} at path: ${jsonPath}`); | ||
} else { | ||
console.log(`Error reaching URL: ${url} in file: ${filePath} at path: ${jsonPath} - ${error.message}`); | ||
} | ||
counter++; | ||
if (!errorLog[filePath]) { | ||
errorLog[filePath] = {}; | ||
} | ||
errorLog[filePath][jsonPath] = url; | ||
return false; | ||
} | ||
} | ||
|
||
async function checkLinksInObject(obj, filePath, currentPath = '') { | ||
const promises = []; | ||
let hasUnreachableLinks = false; | ||
|
||
function collectPromises(obj, path) { | ||
for (const key in obj) { | ||
const newPath = path ? `${path}.${key}` : key; | ||
if (typeof obj[key] === 'object' && obj[key] !== null) { | ||
collectPromises(obj[key], newPath); | ||
} else if (typeof obj[key] === 'string' && obj[key].includes('http')) { | ||
promises.push( | ||
isLinkReachable(obj[key], filePath, newPath).then(isReachable => { | ||
if (!isReachable) { | ||
console.log(`Unreachable link found in ${filePath} at path: ${newPath}: ${obj[key]}`); | ||
hasUnreachableLinks = true; | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
} | ||
|
||
collectPromises(obj, currentPath); | ||
await Promise.all(promises); | ||
|
||
return !hasUnreachableLinks; | ||
} | ||
|
||
async function validateFolder(folder) { | ||
if(!existsSync(folder)) { | ||
return; | ||
} | ||
const files = readdirSync(folder); | ||
const promises = files | ||
.filter(file => file.endsWith('.json')) | ||
.map(async file => { | ||
const content = JSON.parse(readFileSync(`${folder}/${file}`, 'utf8')); | ||
const isValid = await checkLinksInObject(content, `${folder}/${file}`); | ||
if (isValid) { | ||
validFiles++; | ||
} else { | ||
invalidFiles++; | ||
} | ||
}); | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
const folders = ['case-studies', 'wallets', 'dependencies']; | ||
|
||
(async () => { | ||
const errorsDir = '../errors'; | ||
if (!existsSync(errorsDir)) { | ||
mkdirSync(errorsDir); | ||
} else { | ||
// delete all files and subdirectories in the errors folder | ||
const files = readdirSync(errorsDir); | ||
for (const file of files) { | ||
rmSync(join(errorsDir, file), { recursive: true, force: true }); | ||
} | ||
} | ||
|
||
for (const folder of folders) { | ||
counter = 0; | ||
validFiles = 0; | ||
invalidFiles = 0; | ||
await validateFolder('../' + folder); | ||
console.log(`Total unreachable links in ${folder}: ${counter}`); | ||
console.log(`Valid JSON files in ${folder}: ${validFiles}`); | ||
console.log(`Invalid JSON files in ${folder}: ${invalidFiles}`); | ||
} | ||
|
||
console.log('\nError Log:'); | ||
for (const [filePath, errors] of Object.entries(errorLog)) { | ||
const relativePath = filePath.replace('../', ''); | ||
const errorFilePath = join(errorsDir, relativePath); | ||
const errorDir = dirname(errorFilePath); | ||
|
||
if (!existsSync(errorDir)) { | ||
mkdirSync(errorDir, { recursive: true }); | ||
} | ||
writeFileSync(errorFilePath, JSON.stringify(errors, null, 2)); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.