forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create daily workflow for
updateSiteData.js
and verifySiteData.js
- Loading branch information
Showing
3 changed files
with
291 additions
and
0 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,43 @@ | ||
name: Rename and Update | ||
|
||
on: | ||
#push: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
jobs: | ||
Build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.head_ref }} | ||
fetch-depth: 1 | ||
|
||
- name: Use Node.js (dependency) | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
|
||
- name: Update Site Data | ||
run: node ./.github/workflows/updateSiteData.js; | ||
|
||
- name: Verify New Site Data | ||
run: node ./.github/workflows/verifySiteData.js; | ||
|
||
- name: Check for modified files | ||
id: git-check | ||
run: echo modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi) >> $GITHUB_OUTPUT | ||
|
||
- name: Push | ||
if: steps.git-check.outputs.modified == 'true' | ||
run: | | ||
git config --global user.email "71089234+Ahmad-A0@users.noreply.github.com" | ||
git config --global user.name "Bot-A0" | ||
git add . | ||
git commit -am "🌐 Update Site Data (🛠️ from Github Actions)" || true | ||
git push || git pull --rebase && git push | ||
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,151 @@ | ||
/** Script to update ./.problemSiteData.json contains code for solutions hosted on neetcode.io */ | ||
|
||
const fs = require('fs'); | ||
|
||
const PROBLEMS_OBJ = JSON.parse(fs.readFileSync('./.problemList.json', 'utf8')); | ||
const PROBLEMS_SITE_DATA = JSON.parse(fs.readFileSync('./.problemSiteData.json', 'utf8')); | ||
|
||
const languages = [ | ||
{ | ||
name: 'C', | ||
directory: 'c', | ||
extension: 'c' | ||
}, | ||
{ | ||
name: 'C++', | ||
directory: 'cpp', | ||
extension: 'cpp' | ||
}, | ||
{ | ||
name: 'C#', | ||
directory: 'csharp', | ||
extension: 'cs' | ||
}, | ||
{ | ||
name: 'Java', | ||
directory: 'java', | ||
extension: 'java' | ||
}, | ||
{ | ||
name: 'Python', | ||
directory: 'python', | ||
extension: 'py' | ||
}, | ||
{ | ||
name: 'JavaScript', | ||
directory: 'javascript', | ||
extension: 'js' | ||
}, | ||
{ | ||
name: 'TypeScript', | ||
directory: 'typescript', | ||
extension: 'ts' | ||
}, | ||
{ | ||
name: 'Go', | ||
directory: 'go', | ||
extension: 'go' | ||
}, | ||
{ | ||
name: 'Ruby', | ||
directory: 'ruby', | ||
extension: 'rb' | ||
}, | ||
{ | ||
name: 'Swift', | ||
directory: 'swift', | ||
extension: 'swift' | ||
}, | ||
{ | ||
name: 'Kotlin', | ||
directory: 'kotlin', | ||
extension: 'kt' | ||
}, | ||
{ | ||
name: 'Rust', | ||
directory: 'rust', | ||
extension: 'rs' | ||
}, | ||
{ | ||
name: 'Scala', | ||
directory: 'scala', | ||
extension: 'scala' | ||
}, | ||
] | ||
|
||
// Rename files to match leetcode url path, and normalize problem number to four digits. | ||
for (const lang of languages) { | ||
const langDir = lang.directory; | ||
const langExt = lang.extension; | ||
|
||
// Get list of all files in the current lang directory | ||
const files = fs.readdirSync(langDir, { withFileTypes: true }); | ||
console.log(`This many files in ${langDir}: ${files.length}`); | ||
|
||
let counter = 0; | ||
for (const category in PROBLEMS_OBJ) { | ||
for (const problem of PROBLEMS_OBJ[category]) { | ||
const url = problem[1]; | ||
|
||
// Use leetcode url path to rename each problem for consistency | ||
let problemName = problem[1].replace('https://leetcode.com/problems/', ''); | ||
problemName = problemName.replace('/', '').toLowerCase(); | ||
|
||
// Use problem number to find each problem | ||
const problemNumber = problem[2]; | ||
const newProblemNumber = updateProblemNumber(problem[2]); | ||
|
||
const foundFile = files.find(file => file.name.startsWith(`${problemNumber.toString()}-`)); | ||
if (foundFile && foundFile.isFile()) { | ||
// rename file to match leetcode url path | ||
const oldFile = `${langDir}/${foundFile.name}`; | ||
const newFile = `${langDir}/${newProblemNumber}-${problemName}.${langExt}`; | ||
fs.renameSync(oldFile, newFile); | ||
counter++; | ||
|
||
updateSiteData(url, `${newProblemNumber}-${problemName}`, langDir); | ||
} | ||
} | ||
} | ||
console.log(`Renamed ${counter} files in ${langDir}, which had ${files.length} total files.`); | ||
} | ||
|
||
// Add leading zeros to make four digits long (24 -> 0024) | ||
function updateProblemNumber(problemNumberInt) { | ||
let problemNumber = problemNumberInt.toString(); | ||
while (problemNumber.length < 4) { | ||
problemNumber = '0' + problemNumber; | ||
} | ||
return problemNumber; | ||
} | ||
|
||
function updateSiteData(problemUrl, newCodeLink, langName) { | ||
for (const p of PROBLEMS_SITE_DATA) { | ||
if (problemUrl.includes(p.link)) { | ||
p.code = newCodeLink; | ||
p[langName] = true; | ||
return; | ||
} | ||
} | ||
console.log(`Could not find ${problemUrl} in PROBLEMS_SITE_DATA.`); | ||
} | ||
|
||
|
||
fs.writeFile('./.problemSiteData.json', JSON.stringify(PROBLEMS_SITE_DATA), function (err) { | ||
if (err) throw err; | ||
console.log('Saved!'); | ||
}); | ||
|
||
|
||
/** Update problem numbers in .problemList.json */ | ||
|
||
// for (const category in PROBLEMS_OBJ) { | ||
// for (const problem of PROBLEMS_OBJ[category]) { | ||
// problem[2] = updateProblemNumber(problem[2]); | ||
// } | ||
// } | ||
|
||
// fs.writeFile('./.problemList.json', JSON.stringify(PROBLEMS_OBJ), function (err) { | ||
// if (err) throw err; | ||
// console.log('Saved!'); | ||
// }); |
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,97 @@ | ||
/** Script to verify code links in ./.problemSiteData.json */ | ||
|
||
const fs = require('fs'); | ||
const https = require('/opt/homebrew/lib/node_modules/sync-request'); | ||
|
||
const PROBLEMS_SITE_DATA = JSON.parse(fs.readFileSync('./.problemSiteData.json', 'utf8')); | ||
|
||
const languageMap = { | ||
c: { | ||
name: 'C', | ||
directory: 'c', | ||
extension: 'c' | ||
}, | ||
cpp: { | ||
name: 'C++', | ||
directory: 'cpp', | ||
extension: 'cpp' | ||
}, | ||
csharp: { | ||
name: 'C#', | ||
directory: 'csharp', | ||
extension: 'cs' | ||
}, | ||
java: { | ||
name: 'Java', | ||
directory: 'java', | ||
extension: 'java' | ||
}, | ||
python: { | ||
name: 'Python', | ||
directory: 'python', | ||
extension: 'py' | ||
}, | ||
javascript: { | ||
name: 'JavaScript', | ||
directory: 'javascript', | ||
extension: 'js' | ||
}, | ||
typescript: { | ||
name: 'TypeScript', | ||
directory: 'typescript', | ||
extension: 'ts' | ||
}, | ||
go: { | ||
name: 'Go', | ||
directory: 'go', | ||
extension: 'go' | ||
}, | ||
ruby: { | ||
name: 'Ruby', | ||
directory: 'ruby', | ||
extension: 'rb' | ||
}, | ||
swift: { | ||
name: 'Swift', | ||
directory: 'swift', | ||
extension: 'swift' | ||
}, | ||
kotlin: { | ||
name: 'Kotlin', | ||
directory: 'kotlin', | ||
extension: 'kt' | ||
}, | ||
rust: { | ||
name: 'Rust', | ||
directory: 'rust', | ||
extension: 'rs' | ||
}, | ||
scala: { | ||
name: 'Scala', | ||
directory: 'scala', | ||
extension: 'scala' | ||
}, | ||
}; | ||
|
||
const GITHUB_BASE_URL = 'https://github.com/neetcode-gh/leetcode/blob/nc-refactor'; | ||
|
||
let anyErrorCode = false; | ||
for (const problem of PROBLEMS_SITE_DATA) { | ||
for (const language in languageMap) { | ||
if (problem[language] !== true) continue; | ||
|
||
const { directory, extension } = languageMap[language]; | ||
const codeUrl = `${GITHUB_BASE_URL}/${directory}/${problem.code}.${extension}`; | ||
|
||
const res = https('GET', codeUrl).statusCode; | ||
if (res !== 200) { | ||
console.error(codeUrl) | ||
console.error(res) | ||
anyErrorCode = true; | ||
} | ||
} | ||
} | ||
|
||
if (anyErrorCode) { | ||
process.exitCode(1) | ||
} |