generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
3,358 additions
and
618 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
@cds-models/ | ||
.temp/ | ||
cache/ | ||
dist/ | ||
|
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,44 @@ | ||
import { MarkdownRenderer } from 'vitepress' | ||
import { execSync } from 'node:child_process' | ||
import { dirname, join, relative, resolve } from 'node:path' | ||
import { existsSync } from 'node:fs' | ||
|
||
type mdItEnv = { frontmatter: Record<string, any>, path: string, realPath: string } | ||
const modelOut = '@cds-models' | ||
|
||
/** | ||
* Runs cds-typer for all .md pages conifgured with `typedModel` frontmatter entries | ||
*/ | ||
export function install(md: MarkdownRenderer) { | ||
const fence = md.renderer.rules.fence | ||
md.renderer.rules.fence = (tokens, idx, options, env: mdItEnv, ...args) => { | ||
const typedModels = env.frontmatter.typedModels as Record<string,string>|undefined | ||
if (typedModels) { | ||
const mdDir = dirname(env.realPath ?? env.path) | ||
for (const modelKey in typedModels) { | ||
const modelPath = typedModels[modelKey] | ||
|
||
const srcDir = join(mdDir, modelPath) | ||
if (!existsSync(srcDir)) throw new Error(`${srcDir} does not exist. Check the '${modelPath}' path in frontmatter.`) | ||
|
||
runTyper(srcDir, modelOut) | ||
// replace all `%resolved:%` placeholders, e.g. `%resolved:bookshop%` -> `tools/assets/bookshop/@cds-models/*` | ||
const resolvedPath = resolve(mdDir, modelPath, modelOut, '*') | ||
tokens[idx].content = tokens[idx].content.replaceAll(`%resolved:${modelKey}%`, resolvedPath) | ||
} | ||
} | ||
|
||
return fence!(tokens, idx, options, env, ...args) | ||
} | ||
} | ||
|
||
function runTyper(srcDir:string, out:string) { | ||
const outPath = resolve(srcDir, out) | ||
// If target dir exists, stop here. Delta compilation is done through cds-typer ion VS Code. | ||
if (existsSync(outPath)) return | ||
|
||
const label = '✓ Running cds-typer in ' + relative(process.cwd(), srcDir) | ||
console.time(label) | ||
execSync(`npm exec --prefix ${srcDir} -- cds-typer '*' --outputDirectory ${out}`, {cwd: srcDir}) | ||
console.timeEnd(label) | ||
} |
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
Oops, something went wrong.