@travetto/doc 3.0.0-rc.2 → 3.0.0-rc.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.
@@ -1,4 +1,3 @@
1
- import { FsUtil } from '@travetto/boot';
2
1
  import { FileUtil } from './file';
3
2
 
4
3
  /**
@@ -9,26 +8,20 @@ export class ResolveUtil {
9
8
  static resolveRef<T>(title: string | T, file: string): { title: string | T, file: string, line: number } {
10
9
 
11
10
  let line = 0;
12
- const { resolved } = FileUtil.resolveFile(file);
13
- file = resolved;
11
+ const res = FileUtil.read(file);
12
+ file = res.file;
14
13
 
15
- if (!FsUtil.existsSync(file)) {
16
- throw new Error(`${file} is not a valid location`);
17
- } else {
18
- const res = FileUtil.read(file);
19
- file = res.file;
20
- if (typeof title == 'string') {
21
- if (res.content) {
22
- line = res.content.split(/\n/g)
23
- .findIndex(x => new RegExp(`(class|function)[ ]+${title}`).test(x));
24
- if (line < 0) {
25
- line = 0;
26
- } else {
27
- line += 1;
28
- }
29
- if (FileUtil.isDecorator(title, file)) {
30
- title = `@${title}`;
31
- }
14
+ if (typeof title == 'string') {
15
+ if (res.content) {
16
+ line = res.content.split(/\n/g)
17
+ .findIndex(x => new RegExp(`(class|function)[ ]+${title}`).test(x));
18
+ if (line < 0) {
19
+ line = 0;
20
+ } else {
21
+ line += 1;
22
+ }
23
+ if (FileUtil.isDecorator(title, file)) {
24
+ title = `@${title}`;
32
25
  }
33
26
  }
34
27
  }
package/src/util/run.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import { spawnSync } from 'child_process';
2
2
 
3
- import { PathUtil, ExecUtil, EnvUtil, ExecutionOptions, ExecutionState } from '@travetto/boot';
3
+ import { path } from '@travetto/manifest';
4
+ import { Env, ExecUtil, ExecutionOptions, ExecutionState } from '@travetto/base';
5
+ import { stripAnsiCodes } from '@travetto/terminal';
4
6
 
5
7
  export type RunConfig = {
6
8
  filter?: (line: string) => boolean;
7
- module?: 'base' | 'boot';
9
+ module?: string;
8
10
  env?: Record<string, string>;
9
11
  cwd?: string;
10
12
  };
@@ -16,7 +18,7 @@ type RunState = {
16
18
  };
17
19
 
18
20
  class DocState {
19
- baseline = new Date(`${new Date().getFullYear()}-03-14T00:00:00.000`).getTime();
21
+ baseline = new Date('2029-03-14T00:00:00.000').getTime();
20
22
  _s = 37;
21
23
  ids: Record<string, string> = {};
22
24
 
@@ -46,21 +48,16 @@ export class DocRunUtil {
46
48
 
47
49
  static runState(cmd: string, args: string[], config: RunConfig = {}): RunState {
48
50
  args = [...args];
49
- if (cmd.endsWith('.ts')) {
50
- const mod = config.module ?? 'base';
51
- args.unshift(require.resolve(`@travetto/${mod}/bin/main`), cmd);
52
- cmd = process.argv0;
53
- }
54
51
  return {
55
52
  cmd,
56
53
  args,
57
54
  opts: {
58
- cwd: config.cwd ?? PathUtil.cwd,
55
+ cwd: path.toPosix(config.cwd ?? path.cwd()),
59
56
  shell: '/bin/bash',
60
57
  env: {
61
- ...EnvUtil.getAll(),
62
- DEBUG: '',
63
- TRV_DEBUG: '0',
58
+ ...Env.export(/^(TRV_.*|NODE_.*|.*COLOR.*|PATH)$/),
59
+ DEBUG: '0',
60
+ ...(config.module ? { TRV_MANIFEST: config.module } : {}),
64
61
  ...(config.env ?? {})
65
62
  }
66
63
  }
@@ -71,11 +68,10 @@ export class DocRunUtil {
71
68
  * Clean run output
72
69
  */
73
70
  static cleanRunOutput(text: string, cfg: RunConfig): string {
74
- text = text.trim()
75
- // eslint-disable-next-line no-control-regex
76
- .replace(/\x1b\[[?]?[0-9]{1,2}[a-z]/gi, '')
77
- .replace(/[A-Za-z0-9_.\-\/\\]+\/travetto\/module\//g, '@trv:')
78
- .replace(new RegExp(PathUtil.cwd, 'g'), '.')
71
+ text = stripAnsiCodes(text.trim())
72
+ .replace(/^(.{1,4})?Compiling[.]*/, '') // Compiling message, remove
73
+ .replace(/[A-Za-z0-9_.\-\/\\]+\/travetto\/module\//g, '@travetto/')
74
+ .replace(new RegExp(path.cwd(), 'g'), '.')
79
75
  .replace(/([.]trv_cache)[_A-Za-z0-9]+/g, (_, b) => b)
80
76
  .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([.]\d{3})?Z?/g, this.#docState.getDate.bind(this.#docState))
81
77
  .replace(/\b[0-9a-f]{4}[0-9a-f\-]{8,40}\b/ig, this.#docState.getId.bind(this.#docState))
@@ -104,17 +100,19 @@ export class DocRunUtil {
104
100
  let final: string;
105
101
  try {
106
102
  const state = this.runState(cmd, args, config);
107
- const res = spawnSync(state.cmd, state.args, {
103
+ const spawnCfg = {
108
104
  ...state.opts,
109
105
  stdio: 'pipe' as const,
110
106
  encoding: 'utf8',
111
107
  maxBuffer: 1024 * 1024 * 20,
112
- });
108
+ } as const;
109
+
110
+ const res = spawnSync(state.cmd, state.args, spawnCfg);
113
111
 
114
112
  if (res.error) {
115
113
  throw res.error;
116
114
  }
117
- final = res.stdout.toString() || res.stderr.toString();
115
+ final = stripAnsiCodes(res.stdout.toString()).trim() || stripAnsiCodes(res.stderr.toString()).trim();
118
116
  } catch (err) {
119
117
  if (err instanceof Error) {
120
118
  console.log('Found!', cmd, args, '\n', err);
@@ -0,0 +1,84 @@
1
+ import fs from 'fs/promises';
2
+
3
+ import { PackageUtil, path, RootIndex } from '@travetto/manifest';
4
+ import { WatchUtil, ExecUtil, GlobalEnvConfig } from '@travetto/base';
5
+ import { CliCommand, OptionConfig, ListOptionConfig } from '@travetto/cli';
6
+
7
+ import { RenderUtil } from '../src/render/util';
8
+
9
+ type Options = {
10
+ input: OptionConfig<string>;
11
+ outputs: ListOptionConfig<string>;
12
+ watch: OptionConfig<boolean>;
13
+ stdout: OptionConfig<boolean>;
14
+ };
15
+
16
+ /**
17
+ * Command line support for generating module docs.
18
+ */
19
+ export class DocCommand extends CliCommand<Options> {
20
+ name = 'doc';
21
+
22
+ getOptions(): Options {
23
+ return {
24
+ input: this.option({ desc: 'Input File', def: 'DOC.ts' }),
25
+ outputs: this.listOption({ desc: 'Outputs', def: [] }),
26
+ watch: this.boolOption({ desc: 'Watch' }),
27
+ stdout: this.boolOption({ desc: 'Write to stdout', def: false })
28
+ };
29
+ }
30
+
31
+ envInit(): GlobalEnvConfig {
32
+ return {
33
+ debug: false,
34
+ set: {
35
+ TRV_CONSOLE_WIDTH: 140,
36
+ TRV_CLI_IPC: '',
37
+ FORCE_COLOR: 0,
38
+ TRV_LOG_PLAIN: true
39
+ }
40
+ };
41
+ }
42
+
43
+ async action(): Promise<void> {
44
+ const docFile = path.resolve(this.cmd.input);
45
+ if (!(await fs.stat(docFile).catch(() => false))) {
46
+ ExecUtil.returnResponse(`The input ${this.cmd.input} does not exist`, true);
47
+ return this.exit(1);
48
+ }
49
+
50
+ if (this.cmd.outputs.length === 0) {
51
+ const workspacePkg = PackageUtil.readPackage(RootIndex.manifest.workspacePath);
52
+ this.cmd.outputs = workspacePkg.travetto?.docOutputs ?? ['README.md'];
53
+ }
54
+
55
+ const outputs = this.cmd.outputs.map(output => [path.extname(output).substring(1), path.resolve(output)]);
56
+
57
+ // If specifying output
58
+ const write = async (): Promise<void> => {
59
+ RenderUtil.purge(docFile);
60
+ for (const [fmt, out] of outputs) {
61
+ const finalName = path.resolve(out);
62
+ const result = await RenderUtil.render(docFile, fmt);
63
+ if (this.cmd.stdout) {
64
+ process.stdout.write(result);
65
+ } else {
66
+ await fs.writeFile(finalName, result, 'utf8');
67
+ }
68
+ }
69
+ };
70
+
71
+ if (this.cmd.watch) {
72
+ await WatchUtil.watchFile(docFile, write);
73
+ } else {
74
+ try {
75
+ await write();
76
+ ExecUtil.returnResponse(`Wrote docs for ${this.cmd.input}`);
77
+ return this.exit(0);
78
+ } catch (err) {
79
+ ExecUtil.returnResponse(`${err}`, true);
80
+ return this.exit(1);
81
+ }
82
+ }
83
+ }
84
+ }
package/bin/cli-doc.ts DELETED
@@ -1,84 +0,0 @@
1
- import * as path from 'path';
2
- import * as fs from 'fs/promises';
3
-
4
- import { CliCommand, OptionConfig, ListOptionConfig } from '@travetto/cli/src/command';
5
- import { EnvInit } from '@travetto/base/bin/init';
6
- import { PathUtil } from '@travetto/boot';
7
-
8
- type Options = {
9
- input: OptionConfig<string>;
10
- output: ListOptionConfig<string>;
11
- format: OptionConfig<string>;
12
- watch: OptionConfig<boolean>;
13
- };
14
-
15
- /**
16
- * Command line support for generating module docs.
17
- */
18
- export class DocCommand extends CliCommand<Options> {
19
- name = 'doc';
20
-
21
- getOptions(): Options {
22
- return {
23
- input: this.option({ desc: 'Input File', def: 'doc.ts' }),
24
- output: this.listOption({ desc: 'Output files' }),
25
- format: this.option({ desc: 'Format', def: 'md' }),
26
- watch: this.boolOption({ desc: 'Watch' })
27
- };
28
- }
29
-
30
- async envInit(): Promise<void> {
31
- EnvInit.init({
32
- debug: '0',
33
- append: {
34
- TRV_SRC_LOCAL: 'doc',
35
- TRV_RESOURCES: 'doc/resources'
36
- },
37
- set: {
38
- TRV_CONSOLE_WIDTH: '140',
39
- TRV_CLI_JSON_IPC: '',
40
- TRV_COLOR: '0',
41
- TRV_LOG_PLAIN: '1'
42
- }
43
- });
44
- }
45
-
46
- async action(): Promise<void> {
47
- console.error(process.env);
48
-
49
- const { PhaseManager } = await import('@travetto/base');
50
- // Standard compile
51
- await PhaseManager.run('init');
52
-
53
- const { RenderUtil } = await import('../src/render/util');
54
-
55
- const docFile = PathUtil.resolveUnix(this.cmd.input);
56
-
57
- // If specifying output
58
- if (this.cmd.output.length) {
59
- const write = async (): Promise<void> => {
60
- RenderUtil.purge(docFile);
61
- for (const out of this.cmd.output) {
62
- const fmt = path.extname(out) ?? this.cmd.format;
63
- const finalName = await PathUtil.resolveUnix(out);
64
- const result = await RenderUtil.render(docFile, fmt);
65
- await fs.writeFile(finalName, result, 'utf8');
66
- }
67
- };
68
-
69
- if (this.cmd.watch) {
70
- const { WatchUtil } = await import('@travetto/watch');
71
- await WatchUtil.watchFile(docFile, write, true);
72
- } else {
73
- try {
74
- await write();
75
- } catch (err) {
76
- console.error(PathUtil.cwd, err);
77
- process.exit(1);
78
- }
79
- }
80
- } else {
81
- console.log(await RenderUtil.render(docFile, this.cmd.format));
82
- }
83
- }
84
- }