Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --recursive option to doc sync command #313

Merged
merged 9 commits into from
Jul 9, 2021
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