Skip to content

Commit

Permalink
Make the build-docs command silently fail if no template exists
Browse files Browse the repository at this point in the history
  • Loading branch information
eaviles committed Oct 21, 2021
1 parent 3394fd3 commit 17c4b3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./shared/eslint.json",
"rules": {
"global-require": "off",
"no-bitwise": "off",
"no-console": "off",
"no-process-exit": "off"
}
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/build-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const { $ } = require('zx');
const { readFile, writeFile } = require('fs').promises;

const { loadConfig } = require('../utils');
const { fileExists, loadConfig } = require('../utils');

module.exports = {
command: path.parse(__filename).name,
Expand All @@ -17,6 +17,7 @@ module.exports = {
const { docs } = await loadConfig();
const { files, headingDepth, noGfm, template } = docs || {};
const templatePath = template || path.resolve(cwd, 'docs', 'templates', 'README.hbs');
if (!(await fileExists(templatePath))) return;
const templateData = await readFile(templatePath, 'utf8');
const cfg = {
files: (Array.isArray(files) && files) || ['./lib/**/*.js'],
Expand Down
22 changes: 20 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
const path = require('path');
const prettier = require('prettier');
const { cosmiconfig } = require('cosmiconfig');
const { readFile, writeFile } = require('fs').promises;
const { constants, promises } = require('fs');

const { name } = require('../package.json');

const { access, readFile, writeFile } = promises;
const { F_OK, R_OK } = constants;

/**
* Loads the configuration for this tool from the target package.json file.
*
Expand Down Expand Up @@ -61,4 +64,19 @@ async function storeJsonFile(file, data) {
return writeFile(filePath, fileData, 'utf8');
}

module.exports = { loadConfig, loadJsonFile, pathFromCwd, storeJsonFile };
/**
* Checks if the given file path exists and is readable.
*
* @param {string} file - The path of the file to check.
* @returns {Promise<boolean>}
*/
async function fileExists(file) {
try {
await access(file, F_OK | R_OK);
return true;
} catch {
return false;
}
}

module.exports = { fileExists, loadConfig, loadJsonFile, pathFromCwd, storeJsonFile };

0 comments on commit 17c4b3f

Please sign in to comment.