-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use async file operations for helpers
- Loading branch information
1 parent
5ae1389
commit efa705e
Showing
48 changed files
with
616 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const app = require('./app.js') | ||
const configPaths = require('../config/paths.js') | ||
const PORT = process.env.PORT || configPaths.ports.app | ||
|
||
const app = require('./app.js')() | ||
const PORT = process.env.PORT || configPaths.ports.app | ||
|
||
app.listen(PORT, () => { | ||
app().then(server => server.listen(PORT, () => { | ||
console.log('Server started at http://localhost:' + PORT) | ||
}) | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
env: { | ||
jest: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
/* eslint-env jest */ | ||
|
||
const { toHaveNoViolations } = require('jest-axe') | ||
|
||
expect.extend(toHaveNoViolations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,120 @@ | ||
const fs = require('fs') | ||
const { readdir, readFile, stat } = require('fs/promises') | ||
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 {Promise<{ basename: string; stats: import('fs').Stats }[]>} entries | ||
*/ | ||
const getListing = async (directoryPath) => { | ||
const cache = getListing.cache ??= new Map() | ||
|
||
// Cache listings | ||
if (!cache.has(directoryPath)) { | ||
const listing = await readdir(directoryPath) | ||
|
||
// Loop through listing entries | ||
const entries = listing.map(async basename => { | ||
return { basename, stats: await stat(path.join(directoryPath, basename)) } | ||
}) | ||
|
||
// Resolve on completion | ||
cache.set(directoryPath, Promise.all(entries)) | ||
} | ||
|
||
return cache.get(directoryPath) | ||
} | ||
|
||
// 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 {Promise<string[]>} directories | ||
*/ | ||
const getDirectories = async (directoryPath) => { | ||
const cache = getDirectories.cache ??= new Map() | ||
|
||
// Cache directory-only listings | ||
if (!cache.has(directoryPath)) { | ||
const entries = await getListing(directoryPath) | ||
|
||
// Read the contents of a file from a given path | ||
const readFileContents = filePath => { | ||
return fs.readFileSync(filePath, 'utf8') | ||
cache.set(directoryPath, entries | ||
.filter(({ stats }) => stats.isDirectory()) | ||
.map(({ basename: directory }) => directory)) | ||
} | ||
|
||
return cache.get(directoryPath) | ||
} | ||
|
||
exports.readFileContents = readFileContents | ||
/** | ||
* Generate component list from source directory, excluding anything that's not | ||
* a directory (for example, .DS_Store files) | ||
* | ||
* @returns {Promise<string[]>} directories | ||
*/ | ||
const getAllComponents = async () => { | ||
return 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 } | ||
) | ||
} catch (error) { | ||
throw new Error(error) | ||
/** | ||
* Load component data | ||
* | ||
* @param {string} componentName - Component name | ||
* @returns {Promise<{ examples?: unknown[]; params?: unknown[] }>} Component data | ||
*/ | ||
const getComponentData = async componentName => { | ||
const cache = getComponentData.cache ??= new Map() | ||
|
||
// Cache component data | ||
if (!cache.has(componentName)) { | ||
let componentData = {} | ||
|
||
try { | ||
const yamlPath = path.join(configPaths.components, componentName, `${componentName}.yaml`) | ||
componentData = yaml.load(await readFile(yamlPath, 'utf8'), { json: true }) | ||
} catch (error) { | ||
throw new Error(error) | ||
} | ||
|
||
cache.set(componentName, componentData) | ||
} | ||
|
||
return cache.get(componentName) | ||
} | ||
|
||
exports.getComponentData = getComponentData | ||
const getFullPageExamples = async () => { | ||
if (!getFullPageExamples.cache) { | ||
const examplesDirectories = await getDirectories(path.resolve(configPaths.fullPageExamples)) | ||
|
||
const examples = await Promise.all( | ||
examplesDirectories.map(async folderName => { | ||
const templatePath = path.join(configPaths.fullPageExamples, folderName, 'index.njk') | ||
const { attributes } = fm(await readFile(templatePath, 'utf8')) | ||
|
||
return { | ||
name: folderName, | ||
path: folderName, | ||
...attributes | ||
} | ||
}) | ||
) | ||
|
||
getFullPageExamples.cache = examples | ||
.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1) | ||
} | ||
|
||
return getFullPageExamples.cache | ||
} | ||
|
||
exports.fullPageExamples = () => { | ||
return childDirectories(path.resolve(configPaths.fullPageExamples)) | ||
.map(folderName => ({ | ||
name: folderName, | ||
path: folderName, | ||
...fm(readFileContents(path.join(configPaths.fullPageExamples, folderName, 'index.njk'))).attributes | ||
})) | ||
.sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1) | ||
module.exports = { | ||
getAllComponents, | ||
getFullPageExamples, | ||
getComponentData, | ||
getDirectories, | ||
getListing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.