Skip to content

Commit

Permalink
Merge branch 'rebuilds-caching' into rebuilds-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
rogermparent authored Apr 16, 2020
2 parents f1892f6 + 3a20681 commit 7f7120f
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@
"remark-html": "^11.0.1",
"stylelint": "^13.3.0",
"stylelint-config-standard": "^20.0.0",
"typescript": "^3.8.3"
"typescript": "^3.8.3",
"xml2json": "^0.12.0"
},
"husky": {
"hooks": {
Expand Down
85 changes: 85 additions & 0 deletions scripts/page-data-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const xmlParser = require('xml2json')
const path = require('path')
const fs = require('fs')
const { parse } = require('url')

const rootDir = path.join(__dirname, '..')
const exceptionFolders = ['index', '404', '404.html']

const getPagesListFromManifest = () => {
const sitemapXml = fs.readFileSync(path.join(rootDir, 'public/sitemap.xml'))
const sitemap = JSON.parse(xmlParser.toJson(sitemapXml))
return sitemap.urlset.url.map(u => u.loc)
}

const handlePageParts = (accumulator, parts) => {
const [current, ...restParts] = parts

if (!current) {
return
}

if (!accumulator[current]) {
accumulator[current] = {}
}

handlePageParts(accumulator[current], restParts)
}

const buildTree = pages => {
const pagesPathnames = pages.map(url => parse(url).pathname)
const pagesParts = pagesPathnames.map(path =>
path.replace('/', '').split('/')
)
const tree = {}

pagesParts.forEach(pageParts => handlePageParts(tree, pageParts))
exceptionFolders.forEach(exception => (tree[exception] = {}))
return tree
}

const deleteDir = dir => {
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach(item => {
const currentPath = path.join(dir, item)
if (fs.lstatSync(currentPath).isDirectory()) {
deleteFolderRecursive(currentPath)
} else {
fs.unlinkSync(currentPath)
}
})
fs.rmdirSync(dir)
}
}

const checkPageDataPath = (dir, pagesTree) => {
const dirItems = fs.readdirSync(dir)

dirItems.forEach(item => {
const itemPath = path.join(dir, item)
const itemStat = fs.statSync(itemPath)

if (itemStat.isDirectory()) {
if (!pagesTree[item]) {
console.warn(
`deleting page-data folder for outdated page at ${itemPath}`
)
return deleteDir(itemPath)
} else {
checkPageDataPath(itemPath, pagesTree[item])
}
}
})
}

const cleanUpPageData = () => {
const pages = getPagesListFromManifest()
const pagesTree = buildTree(pages)
const pagesDataRoot = path.join(rootDir, 'public/page-data')

checkPageDataPath(pagesDataRoot, pagesTree)
}

module.exports = {
cleanUpPageData
}
67 changes: 61 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8247,6 +8247,21 @@ hmac-drbg@^1.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.1"

[email protected]:
version "5.0.4"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-5.0.4.tgz#0f7fa270a1cafeb364a4b2ddfaa33f864e4157da"
integrity sha512-Alr4ZQgoMlnere5FZJsIyfIjORBqZll5POhDsF4q64dPuJR6rNxXdDxtHSQq8OXRurhmx+PWYEE8bXRROY8h0w==

[email protected]:
version "6.1.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-6.1.3.tgz#73b7d33952e01fe27a38b0457294b79dd8da242c"
integrity sha512-YXXAAhmF9zpQbC7LEcREFtXfGq5K1fmd+4PHkBq8NUqmzW3G+Dq10bI/i0KucLRwss3YYFQ0fSfoxBZYiGUqtQ==

hoek@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb"
integrity sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA==

hogan.js@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/hogan.js/-/hogan.js-3.0.2.tgz#4cd9e1abd4294146e7679e41d7898732b02c7bfd"
Expand Down Expand Up @@ -9421,6 +9436,13 @@ isarray@^2.0.1:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.5.tgz#8af1e4c1221244cc62459faf38940d4e644a5723"
integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==

[email protected]:
version "3.2.0"
resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.2.0.tgz#59310a021931a9fb06bbb51e155ce0b3f236832c"
integrity sha512-zKqkK+O+dGqevc93KNsbZ/TqTUFd46MwWjYOoMrjIMZ51eU7DtQG3Wmd9SQQT7i7RVnuTPEiYEWHU3MSbxC1Tg==
dependencies:
punycode "2.x.x"

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -9895,6 +9917,15 @@ [email protected]:
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217"
integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=

joi@^13.1.2:
version "13.7.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-13.7.0.tgz#cfd85ebfe67e8a1900432400b4d03bbd93fb879f"
integrity sha512-xuY5VkHfeOYK3Hdi91ulocfuFopwgbSORmIwzcwHKESQhC7w1kD5jaVSPnqDxS2I8t3RZ9omCKAxNwXN5zG1/Q==
dependencies:
hoek "5.x.x"
isemail "3.x.x"
topo "3.x.x"

jpeg-js@^0.3.4:
version "0.3.7"
resolved "https://registry.yarnpkg.com/jpeg-js/-/jpeg-js-0.3.7.tgz#471a89d06011640592d314158608690172b1028d"
Expand Down Expand Up @@ -11285,7 +11316,7 @@ name-all-modules-plugin@^1.0.1:
resolved "https://registry.yarnpkg.com/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz#0abfb6ad835718b9fb4def0674e06657a954375c"
integrity sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w=

nan@^2.12.1:
nan@^2.12.1, nan@^2.13.2:
version "2.14.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
Expand Down Expand Up @@ -11397,6 +11428,14 @@ node-eta@^0.9.0:
resolved "https://registry.yarnpkg.com/node-eta/-/node-eta-0.9.0.tgz#9fb0b099bcd2a021940e603c64254dc003d9a7a8"
integrity sha1-n7CwmbzSoCGUDmA8ZCVNwAPZp6g=

node-expat@^2.3.18:
version "2.3.18"
resolved "https://registry.yarnpkg.com/node-expat/-/node-expat-2.3.18.tgz#d9e6949cecda15e131f14259b73dc7b9ed7bc560"
integrity sha512-9dIrDxXePa9HSn+hhlAg1wXkvqOjxefEbMclGxk2cEnq/Y3U7Qo5HNNqeo3fQ4bVmLhcdt3YN1TZy7WMZy4MHw==
dependencies:
bindings "^1.5.0"
nan "^2.13.2"

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"
Expand Down Expand Up @@ -13283,16 +13322,16 @@ [email protected]:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=

[email protected], punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

punycode@^1.2.4, punycode@^1.3.2:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
integrity sha1-wNWmOycYgArY4esPpSachN1BhF4=

punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

q@^1.1.2:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
Expand Down Expand Up @@ -16112,6 +16151,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

[email protected]:
version "3.0.3"
resolved "https://registry.yarnpkg.com/topo/-/topo-3.0.3.tgz#d5a67fb2e69307ebeeb08402ec2a2a6f5f7ad95c"
integrity sha512-IgpPtvD4kjrJ7CRA3ov2FhWQADwv+Tdqbsf1ZnPUSAtCJ9e1Z44MmoSGDXGk4IppoZA7jd/QRkNddlLJWlUZsQ==
dependencies:
hoek "6.x.x"

tough-cookie@^2.3.3, tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -17400,6 +17446,15 @@ xml2js@^0.4.5:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

xml2json@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/xml2json/-/xml2json-0.12.0.tgz#b2ae450b267033b76d896f86e022fa7bff678572"
integrity sha512-EPJHRWJnJUYbJlzR4pBhZODwWdi2IaYGtDdteJi0JpZ4OD31IplWALuit8r73dJuM4iHZdDVKY1tLqY2UICejg==
dependencies:
hoek "^4.2.1"
joi "^13.1.2"
node-expat "^2.3.18"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
Expand Down

0 comments on commit 7f7120f

Please sign in to comment.