Skip to content

Commit

Permalink
Add --recursive option to doc sync command (#313)
Browse files Browse the repository at this point in the history
* Add --recursive option to doc sync command

This option causes a simple recursive search of the supplied folder.

* Whoops! Typo

* docs: adding a pull request template

* feat: dropping support for node 10, expanding coverage to node 14/16 (#325)

* feat: dropping support for node 10

* test: fixing a broken test on node 14

* ci: updating the ci workflow to use node 12+

* ci: upgrading the codeql workflow to the latest way it configs

* chore(deps-dev): upgrading jest to v27

* chore(deps-dev): upgrading code styling dev deps

* chore(deps-dev): upgrading nock to the latest version

* chore: removing a swagger file that shouldnt be in the root dir?

* ci: updating codeql to run on `main` not `master`

* fix: removing the `main` declaration because it doesnt exist

* chore: directory spring cleaning (#327)

* feat: dropping support for node 10

* test: fixing a broken test on node 14

* ci: updating the ci workflow to use node 12+

* ci: upgrading the codeql workflow to the latest way it configs

* chore(deps-dev): upgrading jest to v27

* chore(deps-dev): upgrading code styling dev deps

* chore(deps-dev): upgrading nock to the latest version

* chore: removing a swagger file that shouldnt be in the root dir?

* ci: updating codeql to run on `main` not `master`

* chore: updating the license to be 2021

* chore: renaming the test directory to `__tests__`

* chore: various dotfile cleanup

* chore: moving the codebase into a `src/` directory structure

* Bugfix: Get rid of re-declaration

Co-authored-by: Jon Ursenbach <[email protected]>
  • Loading branch information
ollyfg and erunion authored Jul 9, 2021
1 parent e5c4768 commit 96ea0d8
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/cmds/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.args = [
type: String,
description: 'Project version',
},
{
name: 'recursive',
type: Boolean,
description: 'Search for files recursively',
},
{
name: 'folder',
type: String,
Expand All @@ -36,7 +41,7 @@ exports.args = [
];

exports.run = async function (opts) {
const { folder, key, version } = opts;
const { folder, key, version, recursive } = opts;

if (!key) {
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
Expand All @@ -50,7 +55,29 @@ exports.run = async function (opts) {
return Promise.reject(new Error(`No folder provided. Usage \`${config.cli} ${exports.usage}\`.`));
}

const files = fs.readdirSync(folder).filter(file => file.endsWith('.md') || file.endsWith('.markdown'));
// Find the files to sync, either recursively or not
let allFiles;
if (recursive) {
// A recursive function that returns a list of child file paths
const readdirRecursive = folderToSearch => {
const filesInFolder = fs.readdirSync(folderToSearch, { withFileTypes: true });
const files = filesInFolder
.filter(fileHandle => fileHandle.isFile())
.map(fileHandle => path.join(folderToSearch, fileHandle.name));
const folders = filesInFolder.filter(fileHandle => fileHandle.isDirectory());
const subFiles = [].concat(
...folders.map(fileHandle => readdirRecursive(path.join(folderToSearch, fileHandle.name)))
);
return [...files, ...subFiles];
};
// Pull off the leading subdirectory, to keep things consistant with below
allFiles = readdirRecursive(folder).map(file => file.replace(`${folder}${path.sep}`, ''));
} else {
allFiles = fs.readdirSync(folder);
}
// Strip out non-markdown files
const files = allFiles.filter(file => file.endsWith('.md') || file.endsWith('.markdown'));

if (files.length === 0) {
return Promise.reject(new Error(`We were unable to locate Markdown files in ${folder}.`));
}
Expand Down Expand Up @@ -94,8 +121,9 @@ exports.run = async function (opts) {
files.map(async filename => {
const file = await readFile(path.join(folder, filename), 'utf8');
const matter = frontMatter(file);
// Stripping the markdown extension from the filename and lowecasing to get the default slug
const slug = filename.replace(path.extname(filename), '').toLowerCase();

// Stripping the subdirectories and markdown extension from the filename and lowercasing to get the default slug.
const slug = path.basename(filename).replace(path.extname(filename), '').toLowerCase();
const hash = crypto.createHash('sha1').update(file).digest('hex');

return request
Expand Down

0 comments on commit 96ea0d8

Please sign in to comment.