god-bless-your-code
Version:
Bless your code with ASCII gods, monsters, and mystical powers — your last line of defense against bugs and angry PMs.
66 lines (60 loc) • 2.54 kB
JavaScript
/*
┏┓ ┏┓
┏┛┻━━━━━┛┻┓
┃ ━ ┃
┃ ━┳┛ ┗┳━ ┃
┃ ┃
┃ ┻ ┃
┗━━┓ ┏━━┛
┃ ┃
┃ ┃ 神兽保佑
┃ ┃ 代码无BUG
┃ ┃
┃ ┗━━━━━━┓
┃ ┣┓
┃ ┏┛
┗┓┓┏━━━━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
Code is far away from bug
with the animal protecting.
*/
;
const node_fs = require('node:fs');
const node_path = require('node:path');
const node_url = require('node:url');
const constants = require('./constants.cjs');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
const __dirname$1 = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
const blessingNameToTextMap = {};
const blessingTextsDirPath = node_path.join(__dirname$1, 'blessing-texts');
function godBlessYourCode(code, codeType, options = {}) {
const { blessings = constants.defaultBlessings } = options;
const chosenBlessingName = Array.isArray(blessings)
? blessings[Math.floor(Math.random() * blessings.length)]
: blessings;
if (!chosenBlessingName)
return;
let blessingText = loadBlessingText(chosenBlessingName);
if (!blessingText)
return;
const wrap = (content) => {
switch (codeType) {
case 'css':
case 'js':
case 'ts': return `/*\n${content}\n*/`;
case 'html': return `<!--\n${content}\n-->`;
default: return `/*\n${content}\n*/`;
}
};
blessingText = wrap(blessingText);
return options.position === 'bottom' ? `${code}\n\n${blessingText}` : `${blessingText}\n\n${code}`;
}
function loadBlessingText(name) {
if (blessingNameToTextMap[name])
return blessingNameToTextMap[name];
return blessingNameToTextMap[name] = node_fs.readFileSync(node_path.join(blessingTextsDirPath, `${name}.txt`), 'utf-8').trimEnd();
}
exports.godBlessYourCode = godBlessYourCode;
exports.loadBlessingText = loadBlessingText;
//# sourceMappingURL=index.cjs.map