-
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.
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
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,33 @@ | ||
const { XMLDoc, XMLNode } = require('./xml.js'); | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
|
||
async function walk(dir) { | ||
let files = await fs.readdir(dir); | ||
files = await Promise.all(files.map(async file => { | ||
const filePath = path.join(dir, file); | ||
const stats = await fs.stat(filePath); | ||
if (stats.isDirectory()) return walk(filePath); | ||
else if(stats.isFile()) return filePath; | ||
})); | ||
|
||
return files.reduce((all, folderContents) => all.concat(folderContents), []); | ||
} | ||
|
||
walk('./site').then(async (files) => { | ||
let file = true; | ||
const map = new XMLDoc(); | ||
while (file = files.shift()) { | ||
const node = new XMLNode('x-file'); | ||
node.attrs['path'] = file.replace('site/', ''); | ||
map.appendChild(node); | ||
} | ||
try { | ||
await (fs.stat('./site/index.xml').catch((err) => {}).then((stats) => { | ||
fs.unlink('./site/index.xml').catch((err) => {}).then(() => console.log('Deleted existing map succesfully.')).finally(); | ||
})); | ||
} catch { | ||
} finally { | ||
fs.writeFile('./site/index.xml', map.toString(), 'utf8').catch((err) => console.error('Failed to write index.xml\n', err)).then(() => console.log('Wrote index.xml')); | ||
} | ||
}); |
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,30 @@ | ||
const XMLNode = function XMLNode(type) { | ||
this.children = new Array(); | ||
this.attrs = new Object(); | ||
this.type = type; | ||
this.root = false; | ||
Object.defineProperty(this, 'textContent', { | ||
set(value) { | ||
this.children = [String(value)]; | ||
return true; | ||
}, | ||
get() { | ||
return ''; | ||
} | ||
}); | ||
this.toString = function() { | ||
return `${(`<${this.type} ` + Object.entries(this.attrs).map(attr => `${attr[0]}="${attr[1]}"`)).trimEnd()}>${this.children.map(child => child.toString()).join('')}</${this.type}>` | ||
}; | ||
this.appendChild = function(...children) { | ||
this.children.push(...children); | ||
}; | ||
} | ||
const XMLDoc = function XMLDoc() { | ||
XMLNode.apply(this, ['xml']); | ||
this.root = true; | ||
this._toString = this.toString; | ||
this.toString = function() { | ||
return `<?xml version="1.0" encoding="utf-8"?>${this._toString()}`; | ||
}; | ||
}; | ||
module.exports = { XMLNode, XMLDoc }; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.