mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-07 22:45:23 +02:00
remove rcparser
This commit is contained in:
parent
f05006bf39
commit
0b25ffcdf6
2 changed files with 0 additions and 403 deletions
|
@ -1,131 +0,0 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
/**
|
||||
* The __dirname CommonJS variables are not available in ES modules.
|
||||
* https://nodejs.org/api/esm.html#no-__filename-or-__dirname
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const rcPath = path.resolve(__dirname, '../../../', './.codexdocsrc');
|
||||
|
||||
/**
|
||||
* @typedef {object} menu
|
||||
* @property {string} title - menu option title
|
||||
* @property {string} uri - menu option href
|
||||
*/
|
||||
interface Menu {
|
||||
title: string;
|
||||
uri: string;
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {object} RCData
|
||||
* @property {string} title - website title
|
||||
* @property {Menu[]} menu - options for website menu
|
||||
*/
|
||||
interface RCData {
|
||||
title: string;
|
||||
menu: Menu[];
|
||||
[key: string]: string | Menu[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @class RCParser
|
||||
* @classdesc Class to parse runtime configuration file for CodeX Docs engine
|
||||
*/
|
||||
export default class RCParser {
|
||||
/**
|
||||
* Default CodeX Docs configuration
|
||||
*
|
||||
* @static
|
||||
* @returns {{title: string, menu: Array}}
|
||||
*/
|
||||
public static get DEFAULTS():RCData {
|
||||
return {
|
||||
title: 'CodeX Docs',
|
||||
menu: [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and parse runtime configuration file
|
||||
*
|
||||
* @static
|
||||
* @returns {{title: string, menu: []}}
|
||||
*/
|
||||
public static getConfiguration(): RCData {
|
||||
if (!fs.existsSync(rcPath)) {
|
||||
return RCParser.DEFAULTS;
|
||||
}
|
||||
|
||||
const file = fs.readFileSync(rcPath, 'utf-8');
|
||||
const rConfig = RCParser.DEFAULTS;
|
||||
let userConfig;
|
||||
|
||||
try {
|
||||
userConfig = JSON.parse(file);
|
||||
} catch (e) {
|
||||
console.log('CodeX Docs rc file should be in JSON format.');
|
||||
|
||||
return RCParser.DEFAULTS;
|
||||
}
|
||||
|
||||
for (const option in userConfig) {
|
||||
if (Object.prototype.hasOwnProperty.call(userConfig, option)) {
|
||||
rConfig[option] = userConfig[option] || RCParser.DEFAULTS[option] || undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(rConfig.menu instanceof Array)) {
|
||||
console.log('Menu section in the rc file must be an array.');
|
||||
rConfig.menu = RCParser.DEFAULTS.menu;
|
||||
}
|
||||
|
||||
rConfig.menu = rConfig.menu.filter((option: string | Menu, i:number) => {
|
||||
i = i + 1;
|
||||
if (typeof option === 'string') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!option || option instanceof Array || typeof option !== 'object') {
|
||||
console.log(`Menu option #${i} in rc file must be a string or an object`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const { title, uri } = option;
|
||||
|
||||
if (!title || typeof title !== 'string') {
|
||||
console.log(`Menu option #${i} title must be a string.`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!uri || typeof uri !== 'string') {
|
||||
console.log(`Menu option #${i} uri must be a string.`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
rConfig.menu = rConfig.menu.map((option: string | Menu) => {
|
||||
if (typeof option === 'string') {
|
||||
return {
|
||||
title: option,
|
||||
/* Replace all non alpha- and numeric-symbols with '-' */
|
||||
uri: '/' + option.toLowerCase().replace(/[ -/:-@[-`{-~]+/, '-'),
|
||||
};
|
||||
}
|
||||
|
||||
return option;
|
||||
});
|
||||
|
||||
return rConfig;
|
||||
}
|
||||
}
|
|
@ -1,272 +0,0 @@
|
|||
import { expect } from 'chai';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from 'config';
|
||||
import sinon = require('sinon');
|
||||
|
||||
import rcParser from '../backend/utils/rcparser.js';
|
||||
|
||||
const rcPath = path.resolve(process.cwd(), config.get('rcFile'));
|
||||
|
||||
describe('RC file parser test', () => {
|
||||
afterEach(() => {
|
||||
if (fs.existsSync(rcPath)) {
|
||||
fs.unlinkSync(rcPath);
|
||||
}
|
||||
});
|
||||
|
||||
it('Default config', async () => {
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
});
|
||||
|
||||
it('Invalid JSON formatted config', () => {
|
||||
const invalidJson = '{title: "Codex Docs"}';
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, invalidJson, 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('CodeX Docs rc file should be in JSON format.')).to.be.true;
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(rcParser.DEFAULTS);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Normal config', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig).to.be.deep.equal(normalConfig);
|
||||
});
|
||||
|
||||
it('Missed title', () => {
|
||||
const normalConfig = {
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.menu).to.be.deep.equal(normalConfig.menu);
|
||||
expect(parsedConfig.title).to.be.equal(rcParser.DEFAULTS.title);
|
||||
});
|
||||
|
||||
it('Missed menu', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
});
|
||||
|
||||
it('Menu is not an array', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: {
|
||||
0: { title: 'Option 1', uri: '/option1' },
|
||||
1: { title: 'Option 2', uri: '/option2' },
|
||||
2: { title: 'Option 3', uri: '/option3' },
|
||||
},
|
||||
};
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu section in the rc file must be an array.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(rcParser.DEFAULTS.menu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option is a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
'Option 1',
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 1', uri: '/option-1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
});
|
||||
|
||||
it('Menu option is not a string or an object', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
[ { title: 'Option 1', uri: '/option1' } ],
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 in rc file must be a string or an object')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: [], uri: '/option1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 title must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option uri is undefined', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1' },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
|
||||
it('Menu option title is not a string', () => {
|
||||
const normalConfig = {
|
||||
title: 'Documentation',
|
||||
menu: [
|
||||
{ title: 'Option 1', uri: [] },
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
],
|
||||
};
|
||||
|
||||
const expectedMenu = [
|
||||
{ title: 'Option 2', uri: '/option2' },
|
||||
{ title: 'Option 3', uri: '/option3' },
|
||||
];
|
||||
const spy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(rcPath, JSON.stringify(normalConfig), 'utf8');
|
||||
|
||||
const parsedConfig = rcParser.getConfiguration();
|
||||
|
||||
expect(spy.calledOnce).to.be.true;
|
||||
expect(spy.calledWith('Menu option #1 uri must be a string.')).to.be.true;
|
||||
|
||||
expect(parsedConfig.title).to.be.equal(normalConfig.title);
|
||||
expect(parsedConfig.menu).to.be.deep.equal(expectedMenu);
|
||||
spy.restore();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue