@travetto/doc 7.1.2 → 7.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/__index__.ts CHANGED
@@ -2,4 +2,5 @@ export { type JSXElement as DocJSXElement, isJSXElement as isDocJSXElement } fro
2
2
  export { c, d, JSXElementByFn as DocJSXElementByFn } from './src/jsx.ts';
3
3
  export { MODULES as module } from './src/mapping/module.ts';
4
4
  export { DocFileUtil } from './src/util/file.ts';
5
- export { DocRunUtil, COMMON_DATE } from './src/util/run.ts';
5
+ export { DocRunUtil, COMMON_DATE } from './src/util/run.ts';
6
+ export { PackageDocUtil } from './src/util/package.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/doc",
3
- "version": "7.1.2",
3
+ "version": "7.1.4",
4
4
  "description": "Documentation support for the Travetto framework",
5
5
  "keywords": [
6
6
  "docs",
@@ -24,12 +24,12 @@
24
24
  "directory": "module/doc"
25
25
  },
26
26
  "dependencies": {
27
- "@travetto/runtime": "^7.1.2",
27
+ "@travetto/runtime": "^7.1.4",
28
28
  "@types/prismjs": "^1.26.5",
29
29
  "prismjs": "^1.30.0"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/cli": "^7.1.2"
32
+ "@travetto/cli": "^7.1.4"
33
33
  },
34
34
  "peerDependenciesMeta": {
35
35
  "@travetto/cli": {
package/src/jsx.ts CHANGED
@@ -5,6 +5,7 @@ import type { MODULES } from './mapping/module.ts';
5
5
  import type { CodeProps, RunConfig } from './util/types.ts';
6
6
 
7
7
  import { createElement, type JSXElement, type JSXComponentFunction as CompFn } from '../support/jsx-runtime.ts';
8
+ import { PackageDocUtil } from './util/package.ts';
8
9
 
9
10
  type InstallProps = { title: string, pkg: string };
10
11
  type ExecProps = { title: string, cmd: string, args?: string[], config?: RunConfig & { formatCommand?(cmd: string, args: string[]): string } };
@@ -105,4 +106,5 @@ export const d = {
105
106
  field: (name: string) => createElement(c.Field, { name }),
106
107
  library: (name: keyof typeof LIBRARIES) => createElement(c.Library, { name }),
107
108
  module: (name: keyof typeof MODULES) => createElement(c.Module, { name }),
109
+ get trv() { return PackageDocUtil.getPackageCommand('trv'); },
108
110
  };
@@ -5,7 +5,8 @@ export const LIBRARIES = {
5
5
  Node: { title: 'Node', href: 'https://nodejs.org' },
6
6
  TravettoPlugin: { title: 'VSCode plugin', href: 'https://marketplace.visualstudio.com/items?itemName=arcsine.travetto-plugin' },
7
7
  Npm: { title: 'Npm', href: 'https://docs.npmjs.com/downloading-and-installing-node-js-and-npm' },
8
- Yarn: { title: 'Yarn', href: 'https://yarnpg.com' },
8
+ Yarn: { title: 'Yarn', href: 'https://yarnpkg.com' },
9
+ Pnpm: { title: 'Pnpm', href: 'https://pnpm.io/' },
9
10
  Eslint: { title: 'ESLint', href: 'https://eslint.org/' },
10
11
  Rollup: { title: 'Rollup', href: 'https://rollupjs.org/' },
11
12
  TSConfig: { title: 'TS Config', href: 'https://www.typescriptlang.org/docs/handbook/tsconfig-json.html' },
@@ -3,7 +3,7 @@ import path from 'node:path';
3
3
  import { PackageUtil } from '@travetto/manifest';
4
4
  import { castTo, RuntimeIndex } from '@travetto/runtime';
5
5
 
6
- import { type JSXElementByFn, c } from '../jsx.ts';
6
+ import { type JSXElementByFn, c, d } from '../jsx.ts';
7
7
  import { DocResolveUtil, type ResolvedCode, type ResolvedRef, type ResolvedSnippetLink } from '../util/resolve.ts';
8
8
  import { DocRunUtil } from '../util/run.ts';
9
9
  import { createElement, JSXRuntimeTag } from '../../support/jsx-runtime.ts';
@@ -56,7 +56,7 @@ export class RenderContext {
56
56
  * Get rebuilt comment
57
57
  */
58
58
  get rebuildStamp(): string {
59
- return `Please modify ${this.file.replace(this.repoRoot, this.baseUrl)} and execute "npx trv doc" to rebuild`;
59
+ return `Please modify ${this.file.replace(this.repoRoot, this.baseUrl)} and execute "${d.trv} doc" to rebuild`;
60
60
  }
61
61
 
62
62
  /**
@@ -11,6 +11,7 @@ import { LIBRARIES } from '../mapping/library.ts';
11
11
  import type { RenderContext } from './context.ts';
12
12
  import { DocResolveUtil } from '../util/resolve.ts';
13
13
  import type { JSXElement } from '../../support/jsx-runtime.ts';
14
+ import { PackageDocUtil } from '../util/package.ts';
14
15
 
15
16
  const ESCAPE_ENTITIES: Record<string, string> = { '<': '&lt;', '>': '&gt;', '&': '&amp;', '{': "{{'{'}}", '}': "{{'}'}}" };
16
17
  const ENTITY_REGEX = new RegExp(`[${Object.keys(ESCAPE_ENTITIES).join('')}]`, 'gm');
@@ -52,11 +53,7 @@ export const Html: RenderProvider<RenderContext> = {
52
53
  },
53
54
  Install: async ({ context, node }) => {
54
55
  const highlighted = highlight(`
55
- npm install ${node.props.pkg}
56
-
57
- # or
58
-
59
- yarn add ${node.props.pkg}
56
+ ${PackageDocUtil.getInstallInstructions(node.props.pkg, true)}
60
57
  `, 'bash');
61
58
 
62
59
  return `\n
@@ -9,6 +9,7 @@ import { MODULES } from '../mapping/module.ts';
9
9
  import { LIBRARIES } from '../mapping/library.ts';
10
10
  import type { RenderContext } from './context.ts';
11
11
  import { DocResolveUtil } from '../util/resolve.ts';
12
+ import { PackageDocUtil } from '../util/package.ts';
12
13
 
13
14
  export const Markdown: RenderProvider<RenderContext> = {
14
15
  ext: 'md',
@@ -55,11 +56,7 @@ export const Markdown: RenderProvider<RenderContext> = {
55
56
  Install: async ({ context, node }) =>
56
57
  `\n\n**Install: ${node.props.title}**
57
58
  \`\`\`bash
58
- npm install ${node.props.pkg}
59
-
60
- # or
61
-
62
- yarn add ${node.props.pkg}
59
+ ${PackageDocUtil.getInstallInstructions(node.props.pkg, true)}
63
60
  \`\`\`
64
61
  `,
65
62
  Code: async ({ context, node, props }) => {
@@ -0,0 +1,46 @@
1
+ import { PACKAGE_MANAGERS, type NodePackageManager } from '@travetto/manifest';
2
+ import { Runtime } from '@travetto/runtime';
3
+
4
+ export class PackageDocUtil {
5
+ /**
6
+ * Get an the command for executing a package level binary
7
+ */
8
+ static getPackageCommand(pkg: string, args: string[] = [], manager?: NodePackageManager): string {
9
+ switch (manager ?? Runtime.workspace.manager) {
10
+ case 'npm':
11
+ case 'yarn': return `npx ${pkg} ${args.join(' ')}`.trim();
12
+ case 'pnpm': return `pnpm ${pkg} ${args.join(' ')}`.trim();
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Get an the command for executing a package level binary
18
+ */
19
+ static getWorkspaceInitCommand(manager?: NodePackageManager): string {
20
+ switch (manager ?? Runtime.workspace.manager) {
21
+ case 'npm': return 'npm init -f';
22
+ case 'yarn': return 'yarn init -y';
23
+ case 'pnpm': return 'pnpm init -y';
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Get an install command for a given npm module
29
+ */
30
+ static getInstallCommand(pkg: string, production = false, manager?: NodePackageManager): string {
31
+ switch (manager ?? Runtime.workspace.manager) {
32
+ case 'npm': return `npm install ${production ? '' : '--save-dev '}${pkg}`;
33
+ case 'yarn': return `yarn add ${production ? '' : '--dev '}${pkg}`;
34
+ case 'pnpm': return `pnpm add ${production ? '' : '--dev '}${pkg}`;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Get install example for a given package
40
+ */
41
+ static getInstallInstructions(pkg: string, production = false): string {
42
+ return PACKAGE_MANAGERS
43
+ .map(manager => this.getInstallCommand(pkg, production, manager.type))
44
+ .join('\n\n# or\n\n');
45
+ }
46
+ }
package/src/util/run.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import os from 'node:os';
2
2
  import util from 'node:util';
3
- import { spawn, type ChildProcess } from 'node:child_process';
3
+ import { spawn } from 'node:child_process';
4
4
  import path from 'node:path';
5
5
 
6
6
  import { Env, ExecUtil, Runtime, RuntimeIndex } from '@travetto/runtime';
@@ -62,41 +62,37 @@ export class DocRunUtil {
62
62
  .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([.]\d{3})?Z?/g, this.#docState.getDate.bind(this.#docState))
63
63
  .replace(/\b[0-9a-f]{4}[0-9a-f\-]{8,40}\b/ig, this.#docState.getId.bind(this.#docState))
64
64
  .replace(/(\d+[.]\d+[.]\d+)-(alpha|rc)[.]\d+/g, (all, value) => value);
65
- if (config.filter) {
66
- text = text.split(/\n/g).filter(config.filter).join('\n');
67
- }
68
- if (config.rewrite) {
69
- text = config.rewrite(text);
65
+ if (config.filter || config.rewrite) {
66
+ text = text.split(/\n/g)
67
+ .filter(line => config.filter?.(line) ?? true)
68
+ .map(line => config.rewrite?.(line) ?? line)
69
+ .join('\n');
70
70
  }
71
71
  return text;
72
72
  }
73
73
 
74
- /**
75
- * Spawn command with appropriate environment, and cwd
76
- */
77
- static spawn(cmd: string, args: string[], config: RunConfig = {}): ChildProcess {
78
- return spawn(cmd, args, {
79
- cwd: config.workingDirectory ?? this.workingDirectory(config),
80
- env: {
81
- ...process.env,
82
- ...Env.DEBUG.export(false),
83
- ...Env.TRV_CLI_IPC.export(undefined),
84
- ...Env.TRV_MANIFEST.export(''),
85
- ...Env.TRV_BUILD.export('none'),
86
- ...Env.TRV_ROLE.export(undefined),
87
- ...Env.TRV_MODULE.export(config.module ?? ''),
88
- ...config.env
89
- }
90
- });
91
- }
92
-
93
74
  /**
94
75
  * Run command synchronously and return output
95
76
  */
96
77
  static async run(cmd: string, args: string[], config: RunConfig = {}): Promise<string> {
97
78
  let final: string;
98
79
  try {
99
- const subProcess = this.spawn(cmd, args, config);
80
+ const spawnCmd = config.spawn ?? spawn;
81
+ const subProcess = spawnCmd(cmd, args, {
82
+ ...config,
83
+ cwd: config.workingDirectory ?? this.workingDirectory(config),
84
+ env: {
85
+ ...process.env,
86
+ ...Env.DEBUG.export(false),
87
+ ...Env.TRV_CLI_IPC.export(undefined),
88
+ ...Env.TRV_MANIFEST.export(''),
89
+ ...Env.TRV_BUILD.export('none'),
90
+ ...Env.TRV_ROLE.export(undefined),
91
+ ...Env.TRV_MODULE.export(config.module ?? ''),
92
+ ...config.env
93
+ }
94
+ });
95
+
100
96
  const result = await ExecUtil.getResult(subProcess, { catch: true });
101
97
  if (!result.valid) {
102
98
  throw new Error(result.stderr);
package/src/util/types.ts CHANGED
@@ -1,9 +1,12 @@
1
+ import type { ChildProcess, SpawnOptions } from 'node:child_process';
2
+
1
3
  export type RunConfig = {
2
4
  filter?: (line: string) => boolean;
3
- rewrite?: (text: string) => string;
5
+ rewrite?: (line: string) => string;
4
6
  module?: string;
5
7
  env?: Record<string, string>;
6
8
  workingDirectory?: string;
9
+ spawn?: (cmd: string, args: string[], options: SpawnOptions) => ChildProcess;
7
10
  };
8
11
 
9
12
  export type CodeProps = { title?: string, src: string | Function, language?: string, outline?: boolean, startRe?: RegExp, endRe?: RegExp };
@@ -1,6 +1,5 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
- import { spawn } from 'node:child_process';
4
3
 
5
4
  import { PackageUtil } from '@travetto/manifest';
6
5
  import { ExecUtil, Env, Runtime, WatchUtil } from '@travetto/runtime';
@@ -51,7 +50,7 @@ export class DocCommand implements CliCommandShape {
51
50
  const [first, ...args] = process.argv.slice(2).filter(arg => !/(-w|--watch)/.test(arg));
52
51
  await WatchUtil.watchCompilerEvents('change', async ({ file }) => {
53
52
  if (file === this.input) {
54
- const subProcess = spawn(process.argv0, [Runtime.trvEntryPoint, first, ...args], {
53
+ const subProcess = ExecUtil.spawnPackageCommand('trv', [first, ...args], {
55
54
  cwd: Runtime.mainSourcePath,
56
55
  env: { ...process.env, ...Env.TRV_QUIET.export(true) },
57
56
  stdio: 'inherit'