-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update file names #1607
Merged
Merged
Update file names #1607
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55561bc
Update file names
neetcode-gh 5b3d951
Create daily workflow for `updateSiteData.js` and `verifySiteData.js`
Ahmad-A0 dc2bf85
Revert "Create daily workflow for `updateSiteData.js` and `verifySite…
Ahmad-A0 77d60ff
update GH base URL
Ahmad-A0 71a23e0
Merge branch 'main' into nc-refactor
neetcode-gh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create daily workflow for
updateSiteData.js
and verifySiteData.js
- Loading branch information
commit 5b3d9515a4323c7f87611c1c6715689c8cdbd050
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'; | ||
Ahmad-A0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this dependency may need to be added to your github actions.