Skip to content

Commit

Permalink
feat(core)!: remove option to configure doc file names and extension (#…
Browse files Browse the repository at this point in the history
…124)

doc file for components always needs to be called README.md
  • Loading branch information
mgrsskls committed Jul 19, 2022
1 parent 2c284cd commit 4fb64b7
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 157 deletions.
4 changes: 0 additions & 4 deletions docs/docs/configuration/default-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ Please refer to the [available options](/configuration/options/) for a full expl
"name": "index",
"extension": "css"
},
"docs": {
"name": "README",
"extension": "md"
},
"info": {
"name": "info",
"extension": "json"
Expand Down
13 changes: 0 additions & 13 deletions docs/docs/configuration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,6 @@ default:

_**Note:** You can use `"<component>"` for `name` if the file should have the same name as the component folder._

### `docs`

default:

```json
{
"extension": "md",
"name": "README"
}
```

### `info`

default:

```json
Expand Down
4 changes: 0 additions & 4 deletions packages/core/lib/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
"name": "index",
"extension": "css"
},
"docs": {
"name": "README",
"extension": "md"
},
"info": {
"name": "info",
"extension": "json"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/generator/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ module.exports = async function componentGenerator(cliParams, config) {
componentName
)}.${filesConfig.templates.extension}`,
mocks: `${filesConfig.mocks.name}.${filesConfig.mocks.extension}`,
docs: `${filesConfig.docs.name}.${filesConfig.docs.extension}`,
docs: "README.md",
info: `${filesConfig.info.name}.${filesConfig.info.extension}`,
css: `${helpers.getResolvedFileName(
filesConfig.css.name,
Expand Down
34 changes: 9 additions & 25 deletions packages/core/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,11 @@ module.exports = {
/**
* Accepts a template file path and returns the path to the corresponding documentation file
*
* @param {object} app - the express instance
* @param {string} filePath - file path to a template file
* @returns {string} file path to the corresponding doc file
*/
getDocumentationPathFromTemplatePath: function (app, filePath) {
return filePath.replace(
path.basename(filePath),
`${app.get("config").files.docs.name}.${
app.get("config").files.docs.extension
}`
);
getDocumentationPathFromTemplatePath: function (filePath) {
return filePath.replace(path.basename(filePath), "README.md");
},

/**
Expand Down Expand Up @@ -196,14 +190,11 @@ module.exports = {
/**
* Accepts a file path and checks if it is a documentation file
*
* @param {object} app - the express instance
* @param {string} filePath - path to any type of file
* @returns {boolean} is true if the given file is a doc file
*/
fileIsDocumentationFile: function (app, filePath) {
return (
path.extname(filePath) === `.${app.get("config").files.docs.extension}`
);
fileIsDocumentationFile: function (filePath) {
return path.extname(filePath) === ".md";
},

/**
Expand Down Expand Up @@ -427,23 +418,16 @@ module.exports = {
}
},

docFileIsIndexFile(app, fileName) {
docFileIsIndexFile(fileName) {
const baseName = path.basename(fileName);
const extname = path.extname(fileName);
const docsExtension = app.get("config").files.docs.extension;

if (extname !== `.${docsExtension}`) return false;

if (baseName === `${app.get("config").files.docs.name}.${docsExtension}`)
return true;

if (baseName === `index.${docsExtension}`) return true;
if (extname !== ".md") return false;
if (baseName === "README.md") return true;
if (baseName === "index.md") return true;

const dirParts = path.dirname(fileName).split(path.sep);
if (
dirParts[dirParts.length - 1] ===
path.basename(fileName, `.${docsExtension}`)
)
if (dirParts[dirParts.length - 1] === path.basename(fileName, ".md"))
return true;

return false;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/init/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function checkIfRequestedFileIsDocFile(app, component) {
if (!component) return false;

const { fileContents } = app.get("state");
const docsExtension = app.get("config").files.docs.extension;
const docsExtension = "md";

if (component.endsWith(app.get("config").files.docs.extension)) {
if (component.endsWith(docsExtension)) {
const var1 = path.join(
process.cwd(),
app.get("config").components.folder,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/init/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function updateFileContents(app, events) {
if (
helpers.fileIsTemplateFile(app, changedPath) ||
helpers.fileIsDataFile(app, changedPath) ||
helpers.fileIsDocumentationFile(app, changedPath) ||
helpers.fileIsDocumentationFile(changedPath) ||
helpers.fileIsInfoFile(app, changedPath) ||
helpers.fileIsSchemaFile(app, changedPath)
) {
Expand Down Expand Up @@ -225,7 +225,7 @@ async function handleFileChange() {
// updated file is a doc file
} else if (
triggeredEvents.some(({ changedPath }) =>
helpers.fileIsDocumentationFile(appInstance, changedPath)
helpers.fileIsDocumentationFile(changedPath)
)
) {
const hasBeenAdded = !Object.keys(
Expand Down
5 changes: 1 addition & 4 deletions packages/core/lib/render/menu/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ function shouldRenderWithToggle(app, directory) {

if (
directory.children.filter((child) =>
child.fullPath
? path.extname(child.fullPath) ===
`.${app.get("config").files.docs.extension}`
: false
child.fullPath ? path.extname(child.fullPath) === ".md" : false
).length > 0
)
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/render/views/iframe/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = async function renderIframeComponent({
const componentJson = await getComponentData(app, file);
const componentDocumentation =
app.get("state").fileContents[
helpers.getDocumentationPathFromTemplatePath(app, templateFilePath)
helpers.getDocumentationPathFromTemplatePath(templateFilePath)
];
const componentInfo =
app.get("state").fileContents[
Expand Down
9 changes: 3 additions & 6 deletions packages/core/lib/render/views/iframe/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = async function renderIframeDocs({
cookies,
}) {
const componentDocumentation = app.get("state").fileContents[fullPath];
const componentName = getHeadlineFromFileName(app, file);
const componentName = getHeadlineFromFileName(file);
const { ui } = app.get("config");
const themeMode = getThemeMode(app, cookies);
const componentTextDirection = getComponentTextDirection(app, cookies);
Expand Down Expand Up @@ -64,7 +64,7 @@ module.exports = async function renderIframeDocs({
* @param {string} file
* @returns {string}
*/
function getHeadlineFromFileName(app, file) {
function getHeadlineFromFileName(file) {
let fileName = file;

if (fileName.startsWith("/")) {
Expand All @@ -74,10 +74,7 @@ function getHeadlineFromFileName(app, file) {
if (file.endsWith("README.md") || file.endsWith("index.md")) {
fileName = path.dirname(fileName);
} else {
fileName = path.basename(
fileName,
`.${app.get("config").files.docs.extension}`
);
fileName = path.basename(fileName, ".md");
}

return fileName.replaceAll("-", " ");
Expand Down
5 changes: 1 addition & 4 deletions packages/core/lib/render/views/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const {
module.exports = async function renderIframeIndex({ app, res, cb, cookies }) {
const documentation =
app.get("state").fileContents[
helpers.getFullPathFromShortPath(
app,
`README.${app.get("config").files.docs.extension}`
)
helpers.getFullPathFromShortPath(app, "README.md")
];

const { ui } = app.get("config");
Expand Down
7 changes: 2 additions & 5 deletions packages/core/lib/render/views/main/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ module.exports = async function renderMainDocs({
}

const dirname = path.dirname(file);
const basename = path.basename(
file,
`.${app.get("config").files.docs.extension}`
);
const basename = path.basename(file, ".md");

await res.render(
"main.hbs",
Expand All @@ -53,7 +50,7 @@ module.exports = async function renderMainDocs({
? "/show-{{component}}.html"
: "/show?file={{component}}",
iframeSrc,
requestedComponent: (helpers.docFileIsIndexFile(app, file)
requestedComponent: (helpers.docFileIsIndexFile(file)
? dirname
: path.join(dirname, basename)
).slice(1),
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/state/file-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function getFilePaths(app) {
`${files.info.name}.${files.info.extension}`,
`data.${files.mocks.extension}`,
]) ||
helpers.fileIsDocumentationFile(app, res)
helpers.fileIsDocumentationFile(res)
) {
return res;
} else {
Expand Down Expand Up @@ -189,7 +189,7 @@ async function readFile(app, fileName) {
result = fs.readFileSync(fileName, { encoding: "utf8" });
} else if (path.extname(fileName) === ".yaml") {
result = getYamlFileContent(app, fileName);
} else if (helpers.fileIsDocumentationFile(app, fileName)) {
} else if (helpers.fileIsDocumentationFile(fileName)) {
result = getConvertedMarkdownFileContent(fileName);
} else if (
helpers.fileIsDataFile(app, fileName) &&
Expand Down
16 changes: 5 additions & 11 deletions packages/core/lib/state/menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getComponentFiles(app, directory) {

if (helpers.fileIsTemplateFile(app, baseName)) return true;

return helpers.docFileIsIndexFile(app, child.path);
return helpers.docFileIsIndexFile(child.path);
});
}

Expand Down Expand Up @@ -74,15 +74,12 @@ function getDataForLinkedDirectory(app, directory) {
function getDataForDocumentationFile(app, file) {
const shortPath = helpers
.getShortPathFromFullPath(app, file.path)
.replace(`.${app.get("config").files.docs.extension}`, "");
.replace(".md", "");
const normalizedShortPath = helpers.normalizeString(shortPath);

return {
type: file.type,
name: path.basename(
file.name,
`.${app.get("config").files.docs.extension}`
),
name: path.basename(file.name, ".md"),
fullPath: file.path,
shortPath,
normalizedShortPath,
Expand Down Expand Up @@ -176,13 +173,10 @@ function getMenu(app) {
}
array.push(restructured);
} else if (
helpers.fileIsDocumentationFile(app, item.path) &&
helpers.fileIsDocumentationFile(item.path) &&
!item.path.endsWith("index.md") &&
!item.path.endsWith("README.md") &&
path.basename(
item.path,
`.${app.get("config").files.docs.extension}`
) !==
path.basename(item.path, ".md") !==
path.dirname(item.path).split("/")[
path.dirname(item.path).split("/").length - 1
]
Expand Down
8 changes: 3 additions & 5 deletions packages/core/lib/state/source-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ function getSourceTree(app) {
{
attributes: ["type"],
extensions: new RegExp(
`.(${app.get("config").files.css.extension}|${
app.get("config").files.docs.extension
}|${app.get("config").files.js.extension}|${
app.get("config").files.mocks.extension
}|${
`.(${app.get("config").files.css.extension}|md|${
app.get("config").files.js.extension
}|${app.get("config").files.mocks.extension}|${
app.get("config").files.schema.extension
}|${helpers.getSingleFileExtension(
app.get("config").files.templates.extension
Expand Down
18 changes: 2 additions & 16 deletions packages/core/tests/lib/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,11 @@ describe("lib/helpers", () => {

describe("fileIsDocumentationFile()", () => {
test("returns true if file is a docs file", () => {
expect(
helpers.fileIsDocumentationFile(
app,
`foo/${app.get("config").files.docs.name}.${
app.get("config").files.docs.extension
}`
)
).toBe(true);
expect(helpers.fileIsDocumentationFile("foo/README.md")).toBe(true);
});

test("returns true if file is not a docs file", () => {
expect(
helpers.fileIsDocumentationFile(
app,
`foo/${app.get("config").files.docs.name}.${
app.get("config").files.docs.name
}!`
)
).toBe(false);
expect(helpers.fileIsDocumentationFile("foo/README.md!")).toBe(false);
});
});

Expand Down
Loading

0 comments on commit 4fb64b7

Please sign in to comment.