Skip to content

Commit

Permalink
Move some stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-kiss committed Aug 2, 2024
1 parent 88cfa58 commit 2983570
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
33 changes: 33 additions & 0 deletions build/index.js
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'));
}
});
30 changes: 30 additions & 0 deletions build/xml.js
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 };
1 change: 0 additions & 1 deletion index.html

This file was deleted.

File renamed without changes.

0 comments on commit 2983570

Please sign in to comment.