Skip to content

Commit

Permalink
Merge branch 'master' into ms/markdown-tables-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Slezak committed Sep 26, 2023
2 parents 666dfad + 625bfa5 commit 5a89786
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/*
docs/script/.env
node_modules/*
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 20230925 - Order references/guides script

Added script, located in `docs/script/` directory to quickly update order of references and guides based on `.md` files. For more information please check [Update-Order-Standard-Work.md](automation%2FUpdate-Order-Standard-Work.md) under `Update Order of Docs - AUTOMATIC` section.

## 20230823 - New Endpoints

### Introduced new endpoints and related object schemas
Expand Down Expand Up @@ -327,4 +331,4 @@ The following endpoints in the OpenAPI document found in the `paths` object are
`/v1/category-object`
`/v1/metadata-schema-object`
`/v1/location-object`
`/v1/qualification-object`
`/v1/qualification-object`
7 changes: 7 additions & 0 deletions automation/Update-Order-Standard-Work.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ By performing these actions, you will be able to use the `rdme docs ./guides --v

## Update Order of Docs

**AUTOMATIC**
1. Go to root folder
2. *[if you have not already]* Install node modules. `npm install`
3. *[if you have not already]* Copy `docs/script/.env.example` to `docs/script/.env` and fill out the file with credentials.
4. Run `npm run re-order -- -- --version=vXXXXXXXX` from root folder (where vXXXXXXXX is project version).

**MANUALLY:**
Go To Postman and run two collections (make sure to configure authentication and headers as noted below):

1. Update _Guides_: run `Readme-Voucherify-Docs-Guides.postman_collection.json`
Expand Down
1 change: 1 addition & 0 deletions docs/script/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README_IO_AUTH=rdme_x......
101 changes: 101 additions & 0 deletions docs/script/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const fetch = require("node-fetch");
const fsPromises = require("fs/promises");
const fs = require("fs");
const path = require("path");
require("dotenv").config();

const { version } = require("minimist")(process.argv.slice(2));

if (!version) {
console.log(
"`version` argument was not provided :/, next time try add `-- --version=************` at the end of file execution command"
);
return;
}

if (process.env.README_IO_AUTH?.length < 10) {
console.log("`README_IO_AUTH` was not provided in `.env` file :/");
return;
}
const main = async () => {
const basePath = path.join(__dirname, "..");
const pathsToFiles = await getFiles(basePath);
const dataToProcess = [];
for (const pathToFile of pathsToFiles) {
const data = await fsPromises.readFile(pathToFile, { encoding: "utf8" });
const slug = data.match(/slug: .*/)?.[0]?.split?.("slug: ")?.[1];
const order = parseInt(
data.match(/order: .*/)?.[0]?.split?.("order: ")?.[1]
);
if (!slug || isNaN(order)) {
throw new Error("Invalid slug or order in " + pathToFile);
}
dataToProcess.push({ slug, order, pathToFile });
}
for (const chunk of chunkArray(dataToProcess, 6)) {
await asyncMap(chunk, updateDoc);
}
console.log("Done!");
};
const updateDoc = async ({ slug, order, pathToFile }) => {
const options = {
method: "PUT",
headers: {
"x-readme-version": version,
authorization: "Basic " + btoa(process.env.README_IO_AUTH + ":"),
"content-type": "application/json",
accept: "application/json",
},
body: JSON.stringify({ order }),
};

const response = await fetch(
`https://dash.readme.com/api/v1/docs/${slug}`,
options
);

const responseJSON = await response.json();
if (responseJSON.error) {
console.log({ filePath: pathToFile, response });
throw new Error(responseJSON.error);
}

if (order === responseJSON.order) {
console.log(`Updated successfully ${pathToFile}!`);
} else {
console.log(`Not updated ${pathToFile}!`);
}

return responseJSON;
};

const getFiles = async (path) => {
const pathsToFiles = [];
const items = await fsPromises.readdir(path, {
withFileTypes: true,
});
for (const item of items) {
const itemPath = path + `/${item.name}`;
if (item.isDirectory() && !itemPath.endsWith(".bin")) {
(await getFiles(itemPath)).forEach((value) => {
pathsToFiles.push(value);
});
continue;
}
if (itemPath.endsWith(".md")) {
pathsToFiles.push(itemPath);
}
}
return pathsToFiles;
};

const asyncMap = (arr, asyncFn) => {
return Promise.all(arr.map(asyncFn));
};

const chunkArray = (list, chunkSize) =>
[...Array(Math.ceil(list.length / chunkSize))].map((_) =>
list.splice(0, chunkSize)
);

main();
11 changes: 11 additions & 0 deletions docs/script/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0.0",
"scripts": {
"start": "node index.js"
}
}
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0.0",
"scripts": {
"re-order": "npm run start --prefix docs/script"
},
"dependencies": {
"dotenv": "^16.3.1",
"minimist": "^1.2.8",
"node-fetch": "^2.7.0"
}
}

0 comments on commit 5a89786

Please sign in to comment.