-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(scripts): 📝 update docs script for v2 syntax
- Loading branch information
1 parent
09a710b
commit caba05b
Showing
19 changed files
with
654 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,6 @@ yarn.lock | |
dist | ||
__js | ||
templates | ||
docs | ||
/docs | ||
CHANGELOG.md | ||
.yalc |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const path = require("path"); | ||
const outdent = require("outdent"); | ||
const { Project, ts } = require("ts-morph"); | ||
|
||
const { addMdContent } = require("./utils"); | ||
const { getPublicFiles, sortSourceFiles } = require("../utils/index"); | ||
|
||
// eslint-disable-next-line no-useless-escape | ||
const COMPOSITION_ADD_FLAG = /\<\!\-\- INJECT_COMPOSITION (.*) \-\-\>/m; | ||
|
||
const addComposition = docsTemplate => { | ||
return addMdContent( | ||
docsTemplate, | ||
COMPOSITION_ADD_FLAG, | ||
(line, regexMatched) => { | ||
const composites = getComposition( | ||
path.join(process.cwd(), regexMatched[1]), | ||
); | ||
|
||
return getMarkdown(composites); | ||
}, | ||
); | ||
}; | ||
|
||
module.exports = { addComposition }; | ||
|
||
function getComposition(rootPath) { | ||
const project = new Project({ | ||
tsConfigFilePath: path.join(process.cwd(), "tsconfig.json"), | ||
addFilesFromTsConfig: false, | ||
}); | ||
|
||
const publicPaths = Object.values(getPublicFiles(rootPath)); | ||
const sourceFiles = project.addSourceFilesAtPaths(publicPaths); | ||
project.resolveSourceFileDependencies(); | ||
|
||
let compose = {}; | ||
|
||
sortSourceFiles(sourceFiles).forEach(sourceFile => { | ||
sourceFile.forEachDescendant(node => { | ||
const kind = node.getKind(); | ||
|
||
if (kind === ts.SyntaxKind.TypeAliasDeclaration) { | ||
const typeAliasDeclaration = node | ||
.getChildrenOfKind(ts.SyntaxKind.Identifier)[0] | ||
.getText(); | ||
|
||
const isOptionsType = /.+Options$/.test(typeAliasDeclaration); | ||
|
||
if (isOptionsType) { | ||
const identifiers = node.getDescendantsOfKind( | ||
ts.SyntaxKind.Identifier, | ||
); | ||
|
||
const options = identifiers | ||
.map(identifier => { | ||
const identifierText = identifier.getText(); | ||
|
||
if ( | ||
/.+Options$/.test(identifierText) && | ||
identifierText !== typeAliasDeclaration | ||
) { | ||
return identifierText; | ||
} | ||
return null; | ||
}) | ||
.filter(Boolean); | ||
|
||
const module = typeAliasDeclaration.replace(/Options$/, ""); | ||
|
||
const composition = options.map(option => { | ||
return `use${option.replace(/Options$/, "")}`; | ||
}); | ||
|
||
compose[module] = composition; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
return compose; | ||
} | ||
|
||
function getMarkdown(compose) { | ||
const formatter = new Intl.ListFormat("en", { | ||
style: "long", | ||
type: "conjunction", | ||
}); | ||
const content = Object.keys(compose).map(moduleName => { | ||
const module = compose[moduleName]; | ||
|
||
const composition = formatter.format( | ||
module.map(item => { | ||
return "`" + item + "`"; | ||
}), | ||
); | ||
|
||
return outdent` | ||
- ${moduleName} uses ${composition} | ||
`; | ||
}); | ||
|
||
return outdent` | ||
## Composition | ||
${content.join("\n")}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const strip = require("strip-comments"); | ||
const prettier = require("prettier/standalone"); | ||
const parserBabel = require("prettier/parser-babel"); | ||
|
||
const prettierConfig = require("../../.prettierrc.js"); | ||
const { joinCwd, extractCode } = require("../utils/common-utils"); | ||
const { addMdContent } = require("./utils"); | ||
|
||
// eslint-disable-next-line no-useless-escape | ||
const CODE_EXAMPLE_FLAG = /\<\!\-\- IMPORT_EXAMPLE (.*) \-\-\>/m; | ||
|
||
const addExamples = docsTemplate => { | ||
return addMdContent(docsTemplate, CODE_EXAMPLE_FLAG, (line, regexMatched) => { | ||
const importString = regexMatched[1]; | ||
const importPath = joinCwd(importString); | ||
const prettifiedCode = prettier.format(strip(extractCode(importPath)), { | ||
parser: "babel", | ||
plugins: [parserBabel], | ||
...prettierConfig, | ||
}); | ||
return ["```js", "\n", prettifiedCode, "```"].join(""); | ||
}); | ||
}; | ||
|
||
module.exports = { addExamples }; |
Oops, something went wrong.