Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
43 changes: 43 additions & 0 deletions .github/workflows/rename-and-update.yml
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 "[email protected]"
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


2 changes: 1 addition & 1 deletion .github/workflows/updateCompletionTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ for (const problemCategory in PROBLEMS_OBJ) {
let filePath = nestedFilesInDir[dir].find((file) =>
file
.match(/[\w-]+\..+/)?.[0]
?.startsWith(problemNumber.toString())
?.startsWith(problemNumber)
);
if (filePath) {
problemRow.push(
Expand Down
151 changes: 151 additions & 0 deletions .github/workflows/updateSiteData.js
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!');
// });
97 changes: 97 additions & 0 deletions .github/workflows/verifySiteData.js
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');
Copy link
Owner Author

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.


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)
}
Loading