Skip to content

Commit

Permalink
Add directory listing helpers getDirectories(), getFiles(), `getL…
Browse files Browse the repository at this point in the history
…isting()`

Preparation for tests that need to filter components by certain files, for example “All components with `${component}.mjs`”
  • Loading branch information
colinrotherham committed Sep 30, 2022
1 parent 679fe5e commit 64dc1b9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
72 changes: 55 additions & 17 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 18 additions & 5 deletions tasks/gulp/__tests__/after-build-dist.test.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:/)
Expand All @@ -66,15 +71,23 @@ 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:/)
})
})

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()
Expand Down

0 comments on commit 64dc1b9

Please sign in to comment.