Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more functional to docs command #448

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
55 changes: 51 additions & 4 deletions src/commands/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,57 @@ const fs = require('fs');
const path = require('path');

/**
* Command to create docs from .EO sources.
* Recursively reads all .xmir files from a directory.
* @param {string} dir - Directory path
* @return {string[]} Array of file paths
*/
function readXmirFilesRecursively(dir) {
const files = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...readXmirFilesRecursively(fullPath));
} else if (entry.name.endsWith('.xmir')) {
files.push(fullPath);
}
}

return files;
}

/**
* Command to generate documentation.
* @param {Hash} opts - All options
*/
module.exports = function(opts) {
const filePath = path.resolve('eodocs.html');
fs.writeFileSync(filePath, '');
};
try {
const inputDir = path.resolve(opts.target, '.eoc', '1-parse');
const outputDir = path.resolve(opts.target, 'docs');

fs.mkdirSync(outputDir, { recursive: true });

const xmirFiles = readXmirFilesRecursively(inputDir);

for (const xmirFile of xmirFiles) {
const relativePath = path.relative(inputDir, xmirFile);
const packagePath = path.dirname(relativePath).split(path.sep).join('.');
const outputPath = path.join(outputDir, `package_${packagePath}.html`);

fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, '');
}

const packagesPath = path.join(outputDir, 'packages.html');
fs.writeFileSync(packagesPath, '');

const cssPath = path.join(outputDir, 'styles.css');
fs.writeFileSync(cssPath, '');

console.info('Documentation generation completed in %s directory', outputDir);
} catch (error) {
console.error('Error generating documentation:', error);
throw error;
}
};
48 changes: 0 additions & 48 deletions test/commands/test_docs.js

This file was deleted.

76 changes: 76 additions & 0 deletions test/commands/test_zdocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2025 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {runSync} = require('../helpers');

describe('docs', function() {
const testDir = path.resolve('temp/test-docs-command');
const eocDir = path.join(testDir, '.eoc', '1-parse');
const docsDir = path.join(testDir, 'docs');

beforeEach(function() {
fs.rmSync(testDir, { recursive: true, force: true });
fs.mkdirSync(eocDir, { recursive: true });
});

/**
* Tests that the 'docs' command generates empty HTML files in the docs directory.
* @param {Mocha.Done} done - Mocha callback signaling asynchronous completion
*/
it('generates empty HTML files for packages', function(done) {
const samplePackageDir = path.join(eocDir, 'foo', 'bar');
fs.mkdirSync(samplePackageDir, { recursive: true });
const xmirFilePath = path.join(samplePackageDir, 'test.xmir');
fs.writeFileSync(xmirFilePath, '<program name="test" />');

runSync([
'docs',
'--verbose',
'-s', path.resolve(testDir, 'src'),
'-t', testDir,
]);

assert(fs.existsSync(docsDir), 'Expected the docs directory to be created but it is missing');

const generatedFile = path.join(docsDir, 'package_foo.bar.html');
assert(fs.existsSync(generatedFile), `Expected file ${generatedFile} but it was not created`);
const content = fs.readFileSync(generatedFile, 'utf8');
assert.strictEqual(content, '', 'Expected the generated file to be empty');

const packagesFile = path.join(docsDir, 'packages.html');
assert(fs.existsSync(packagesFile), `Expected file ${packagesFile} but it was not created`);
assert.strictEqual(
fs.readFileSync(packagesFile, 'utf8'), '', 'Expected packages.html to be empty'
);

const cssFile = path.join(docsDir, 'styles.css');
assert(fs.existsSync(cssFile), `Expected file ${cssFile} but it was not created`);
assert.strictEqual(fs.readFileSync(cssFile, 'utf8'), '', 'Expected styles.css to be empty');

done();
});
});
Loading