Utility functions for working with the file system in Clay. This module will assume that the process which is running will be triggered from a directory that contains a package.json file and /components directory.
Node 26.12.1 or later.
npm install --save amphora-fs
- fileExists (code|tests|docs) - test if a file exists
- getComponentModule (code|tests|docs) - get a component's
model.js - getComponentName (code|tests|docs) - get a component's name, validating that it exists in your Clay instance
- getComponentPackage (code|tests|docs) - get a npm component's
package.json - getComponentPath (code|tests|docs) - get a component's filepath
- getComponents (code|tests|docs) - get a list of all components in your Clay instance
- getFiles (code|tests|docs) - get files (that aren't test or documentation) in a directory
- getFolders (code|tests|docs) - get folders in a directory
- getIndices (code|tests|docs) - get all component references in an object
- getLayoutModule (code|tests|docs) - get a layout's
model.js - getLayoutName (code|tests|docs) - get a layout's name, validating that it exists in your Clay instance
- getLayoutPath (code|tests|docs) - get a layout's filepath
- getLayouts (code|tests|docs) - get a list of all layouts in your Clay instance
- getSchemaPath (code|tests|docs) - get the path for a layout's / component's schema
- getYaml (code|tests|docs) - get and parse yaml files, including env variables (for
config.ymlandlocal.yml) - isDirectory (code|tests|docs) - determine if a path is a directory
- readFilePromise (code|tests|docs) -
fs.readFilethat returns a promise - tryRequire (code|tests|docs) - attempt to
require()a file, with graceful fallback
- The lists and files are read once.
getComponents,getLayouts,getFiles,getFolders,isDirectory,getYaml,tryRequire,getComponentModuleandgetComponentPackageremember what they found for each argument, including that something was not found, so a file added later is not seen. Each one has acache(aMap) that can be cleared. getYaml(path)readspath.yaml, orpath.ymlif there is no.yamlor it is empty. It isnullif there is no file (undefinedfor a missingconfigorlocal), and throws, naming the file, if the yaml is not valid.- In
configandlocalfiles,${NAME}is replaced by the environment variableNAME, which must be set and not empty. - Yaml is read with js-yaml 3 (
safeLoad): numbers by YAML 1.1 rules (0777is 511,12:30is 750,yesis a string), merge keys, timestamps, no js types.
npm test
runs ESLint and the tests (node --test), and checks that every function still gives what amphora-fs 2.0.0 gave on a project that looks like a Clay site (test/golden.test.js).