Skip to content

Commit

Permalink
function update
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Dec 27, 2024
1 parent e2c7808 commit c863f71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 7 additions & 4 deletions scripts/build-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const TARGET_DIR = 'pages';
const capitalizeTags = ['table', 'tr', 'td', 'th', 'thead', 'tbody'];

// Check if target directory doesn't exist then create it
/* istanbul ignore next */
if (!fs.existsSync(TARGET_DIR)) {
fs.mkdirSync(TARGET_DIR, { recursive: true });
function ensureDirectoryExists(directory) {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
}

ensureDirectoryExists(TARGET_DIR);

function capitalizeJsxTags(content) {
return content.replace(/<\/?(\w+)/g, function (match, letter) {
if (capitalizeTags.includes(letter.toLowerCase())) {
Expand Down Expand Up @@ -58,4 +61,4 @@ function copyAndRenameFiles(srcDir, targetDir) {

copyAndRenameFiles(SRC_DIR, TARGET_DIR);

module.exports = {copyAndRenameFiles,capitalizeJsxTags}
module.exports = { copyAndRenameFiles,capitalizeJsxTags, ensureDirectoryExists }
11 changes: 10 additions & 1 deletion tests/build-pages.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const path = require('path');
const { capitalizeJsxTags, copyAndRenameFiles } = require('../scripts/build-pages');
const { capitalizeJsxTags, copyAndRenameFiles, ensureDirectoryExists } = require('../scripts/build-pages');

describe('capitalizeJsxTags', () => {
test('should capitalize JSX tags', () => {
Expand Down Expand Up @@ -45,4 +45,13 @@ describe('copyAndRenameFiles', () => {
expect(targetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
expect(nestedTargetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
});

test('should create a directory if it does not exist', () => {
const TEST_DIR = 'testDir';

expect(fs.existsSync(TEST_DIR)).toBe(false);
ensureDirectoryExists(TEST_DIR);
expect(fs.existsSync(TEST_DIR)).toBe(true);
});

});

0 comments on commit c863f71

Please sign in to comment.