-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add directory listing helpers
getDirectories()
, getFiles()
, `getL…
…isting()`
- Loading branch information
1 parent
7ab8f5b
commit 0a290cf
Showing
2 changed files
with
73 additions
and
22 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
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 | ||
} |
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