diff --git a/lib/file-helper.js b/lib/file-helper.js index a3efbf50fc8..6a11f0e0983 100644 --- a/lib/file-helper.js +++ b/lib/file-helper.js @@ -1,45 +1,83 @@ -const fs = require('fs') +const { readdirSync, readFileSync, statSync } = require('fs') const path = require('path') const yaml = require('js-yaml') const fm = require('front-matter') const configPaths = require('../config/paths.js') -const childDirectories = dir => { - return fs.readdirSync(dir) - .filter(file => fs.statSync(path.join(dir, file)).isDirectory()) +/** + * Directory listing for path + * + * @param {string} directoryPath + * @returns {{ basename: string; stats: import('fs').Stats }[]} entries + */ +const getListing = (directoryPath) => { + const listing = readdirSync(directoryPath) + + // Loop through listing entries + return listing.map(basename => ({ + basename, stats: statSync(path.join(directoryPath, basename)) + })) } -// Generate component list from source directory, excluding anything that's not -// a directory (for example, .DS_Store files) -exports.allComponents = childDirectories(configPaths.components) +/** + * Directory listing (directories only) + * + * @param {string} directoryPath + * @returns {string[]} directories + */ +const getDirectories = (directoryPath) => { + const entries = getListing(directoryPath) -// Read the contents of a file from a given path -const readFileContents = filePath => { - return fs.readFileSync(filePath, 'utf8') + return entries + .filter(({ stats }) => stats.isDirectory()) + .map(({ basename: directory }) => directory) } -exports.readFileContents = readFileContents +/** + * Directory listing (files only) + * + * @param {string} directoryPath + * @returns {string[]} directories + */ +const getFiles = (directoryPath) => { + const entries = getListing(directoryPath) + + return entries + .filter(({ stats }) => stats.isFile()) + .map(({ basename: file }) => file) +} + +// Generate component list from source directory, excluding anything that's not +// a directory (for example, .DS_Store files) +const allComponents = getDirectories(configPaths.components) const getComponentData = componentName => { try { const yamlPath = path.join(configPaths.components, componentName, `${componentName}.yaml`) return yaml.load( - fs.readFileSync(yamlPath, 'utf8'), { json: true } + readFileSync(yamlPath, 'utf8'), { json: true } ) } catch (error) { throw new Error(error) } } -exports.getComponentData = getComponentData - -exports.fullPageExamples = () => { - return childDirectories(path.resolve(configPaths.fullPageExamples)) +const fullPageExamples = () => { + return getDirectories(path.resolve(configPaths.fullPageExamples)) .map(folderName => ({ name: folderName, path: folderName, - ...fm(readFileContents(path.join(configPaths.fullPageExamples, folderName, 'index.njk'))).attributes + ...fm(readFileSync(path.join(configPaths.fullPageExamples, folderName, 'index.njk'), 'utf8')).attributes })) .sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1) } + +module.exports = { + allComponents, + fullPageExamples, + getComponentData, + getDirectories, + getFiles, + getListing +} diff --git a/tasks/gulp/__tests__/after-build-dist.test.js b/tasks/gulp/__tests__/after-build-dist.test.js index aab60dceea7..cfd9e14829a 100644 --- a/tasks/gulp/__tests__/after-build-dist.test.js +++ b/tasks/gulp/__tests__/after-build-dist.test.js @@ -1,8 +1,9 @@ +const { readFile } = require('fs/promises') const path = require('path') -const lib = require('../../../lib/file-helper') -const configPaths = require('../../../config/paths.js') const recursive = require('recursive-readdir') +const configPaths = require('../../../config/paths.js') + describe('dist/', () => { const version = require(path.join('../../../', configPaths.package, 'package.json')).version @@ -54,7 +55,11 @@ describe('dist/', () => { }) describe(`govuk-frontend-${version}.min.css`, () => { - const stylesheet = lib.readFileContents(path.join(configPaths.dist, `govuk-frontend-${version}.min.css`)) + let stylesheet + + beforeAll(async () => { + stylesheet = await readFile(path.join(configPaths.dist, `govuk-frontend-${version}.min.css`), 'utf8') + }) it('should not contain current media query displayed on body element', () => { expect(stylesheet).not.toMatch(/body:before{content:/) @@ -66,7 +71,11 @@ describe('dist/', () => { }) describe(`govuk-frontend-ie8-${version}.min.css`, () => { - const stylesheet = lib.readFileContents(path.join(configPaths.dist, `govuk-frontend-ie8-${version}.min.css`)) + let stylesheet + + beforeAll(async () => { + stylesheet = await readFile(path.join(configPaths.dist, `govuk-frontend-ie8-${version}.min.css`), 'utf8') + }) it('should not contain current media query displayed on body element', () => { expect(stylesheet).not.toMatch(/body:before{content:/) @@ -74,7 +83,11 @@ describe('dist/', () => { }) describe(`govuk-frontend-${version}.min.js`, () => { - const javascript = lib.readFileContents(path.join(configPaths.dist, `govuk-frontend-${version}.min.js`)) + let javascript + + beforeAll(async () => { + javascript = await readFile(path.join(configPaths.dist, `govuk-frontend-${version}.min.js`), 'utf8') + }) it('should have the correct version name', () => { expect(javascript).toBeTruthy()