diff --git a/.github/workflows/update-docs.yaml b/.github/workflows/update-docs.yaml new file mode 100644 index 000000000..b8a1f5be7 --- /dev/null +++ b/.github/workflows/update-docs.yaml @@ -0,0 +1,52 @@ +name: Update Docs + +on: + push: + branches: + - master + +jobs: + update-docs: + runs-on: ubuntu-latest + + permissions: + # Give the default GITHUB_TOKEN write permission to commit and push the + # added or changed files to the repository. + contents: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '16' # Update based on your project's requirements + + - name: Build + run: | + cd docs; + npm install + cd .. + ls -la + node docs/build.js + + - name: Deploy to Docs Branch + run: | + # Configure Git + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + + # Preserve built files + mv build .. + + # Switch to the docs branch + git checkout --orphan docs + git rm -rf . || true + cp -r ../build/* . + rm -rf ../build + + # Commit and push changes + git add . + git commit -m "Update docs on $(date)" + git push --force origin docs diff --git a/.gitignore b/.gitignore index a7988b367..f96db2252 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bin obj aoc-crypt.key docs/node_modules +build diff --git a/docs/build.js b/docs/build.js index 03a4ba8a5..56a8db2a9 100644 --- a/docs/build.js +++ b/docs/build.js @@ -109,7 +109,7 @@ function generateDayPicker(year, day, yearToDays) { return res; } const template = loadTemplate('docs/template.html'); - +const redirectTemplate = loadTemplate('docs/redirect_template.html') const yearToDays = {}; for (const { year, day } of findReadmes('.')) { @@ -123,10 +123,18 @@ for(const year of Object.keys(yearToDays)){ yearToDays[year] = yearToDays[year].sort((a,b) => a-b); } - +console.log(yearToDays); +const lastYear = Math.max(...Object.keys(yearToDays)) +const lastDay = Math.max(...yearToDays[lastYear]); copyDirectory('docs/static', 'build'); +const filledRedirectTemplate = fillTemplate(redirectTemplate, { + 'default-page-url': `https://aoc.csokavar.hu/${lastYear}/${lastDay}/`, +}); + +fs.writeFileSync(path.join('build', 'index.html'), filledRedirectTemplate); + // Iterate over readme.md files and print filled templates for (const { year, day, name, notes, code, illustration } of findReadmes('.')) { const filledHtml = fillTemplate(template, { @@ -143,3 +151,4 @@ for (const { year, day, name, notes, code, illustration } of findReadmes('.')) { fs.writeFileSync(path.join(dst, 'index.html'), filledHtml); fs.copyFileSync(illustration, path.join(dst, 'illustration.jpeg')); } + diff --git a/docs/static/index.html b/docs/redirect_template.html similarity index 72% rename from docs/static/index.html rename to docs/redirect_template.html index 7a1d07c70..ce6cd42cf 100644 --- a/docs/static/index.html +++ b/docs/redirect_template.html @@ -4,5 +4,7 @@ const url = new URL(window.location.href); if (url.search.match(/^\?(\d{4})\/\d{1,2}/)) { window.location = url.search.substring(1); - } + } else { + window.location = '{{default-page-url}}'; + }