@travetto/doc 3.0.0-rc.2 → 3.0.0-rc.20

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,11 +1,14 @@
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>;
11
+ profiles?: string[];
9
12
  cwd?: string;
10
13
  };
11
14
 
@@ -16,7 +19,7 @@ type RunState = {
16
19
  };
17
20
 
18
21
  class DocState {
19
- baseline = new Date(`${new Date().getFullYear()}-03-14T00:00:00.000`).getTime();
22
+ baseline = new Date('2029-03-14T00:00:00.000').getTime();
20
23
  _s = 37;
21
24
  ids: Record<string, string> = {};
22
25
 
@@ -46,21 +49,19 @@ export class DocRunUtil {
46
49
 
47
50
  static runState(cmd: string, args: string[], config: RunConfig = {}): RunState {
48
51
  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
52
  return {
55
53
  cmd,
56
54
  args,
57
55
  opts: {
58
- cwd: config.cwd ?? PathUtil.cwd,
56
+ cwd: path.toPosix(config.cwd ?? path.cwd()),
59
57
  shell: '/bin/bash',
60
58
  env: {
61
- ...EnvUtil.getAll(),
62
- DEBUG: '',
63
- TRV_DEBUG: '0',
59
+ ...Env.export(/^(TRV_.*|NODE_.*|.*COLOR.*|PATH)$/),
60
+ DEBUG: '0',
61
+ TRV_MANIFEST: '',
62
+ TRV_BUILD: 'warn',
63
+ TRV_MODULE: config.module ?? '',
64
+ ...(config.profiles ? { TRV_PROFILES: config.profiles.join(' ') } : {}),
64
65
  ...(config.env ?? {})
65
66
  }
66
67
  }
@@ -71,12 +72,10 @@ export class DocRunUtil {
71
72
  * Clean run output
72
73
  */
73
74
  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'), '.')
79
- .replace(/([.]trv_cache)[_A-Za-z0-9]+/g, (_, b) => b)
75
+ text = stripAnsiCodes(text.trim())
76
+ .replace(/^(.{1,4})?Compiling[.]*/, '') // Compiling message, remove
77
+ .replace(/[A-Za-z0-9_.\-\/\\]+\/travetto\/module\//g, '@travetto/')
78
+ .replace(new RegExp(path.cwd(), 'g'), '.')
80
79
  .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
80
  .replace(/\b[0-9a-f]{4}[0-9a-f\-]{8,40}\b/ig, this.#docState.getId.bind(this.#docState))
82
81
  .replace(/(\d+[.]\d+[.]\d+)-(alpha|rc)[.]\d+/g, (all, v) => v);
@@ -104,17 +103,19 @@ export class DocRunUtil {
104
103
  let final: string;
105
104
  try {
106
105
  const state = this.runState(cmd, args, config);
107
- const res = spawnSync(state.cmd, state.args, {
106
+ const spawnCfg = {
108
107
  ...state.opts,
109
108
  stdio: 'pipe' as const,
110
109
  encoding: 'utf8',
111
110
  maxBuffer: 1024 * 1024 * 20,
112
- });
111
+ } as const;
112
+
113
+ const res = spawnSync(state.cmd, state.args, spawnCfg);
113
114
 
114
115
  if (res.error) {
115
116
  throw res.error;
116
117
  }
117
- final = res.stdout.toString() || res.stderr.toString();
118
+ final = stripAnsiCodes(res.stdout.toString()).trim() || stripAnsiCodes(res.stderr.toString()).trim();
118
119
  } catch (err) {
119
120
  if (err instanceof Error) {
120
121
  console.log('Found!', cmd, args, '\n', err);
@@ -0,0 +1,83 @@
1
+ import fs from 'fs/promises';
2
+
3
+ import { PackageUtil, path, RootIndex, watchFolders } from '@travetto/manifest';
4
+ import { 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
+ };
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
+ outputs: this.listOption({ desc: 'Outputs', def: [] }),
25
+ watch: this.boolOption({ desc: 'Watch' })
26
+ };
27
+ }
28
+
29
+ envInit(): GlobalEnvConfig {
30
+ return {
31
+ debug: false,
32
+ set: {
33
+ TRV_CONSOLE_WIDTH: 140,
34
+ TRV_CLI_IPC: '',
35
+ FORCE_COLOR: 0,
36
+ TRV_LOG_PLAIN: true
37
+ }
38
+ };
39
+ }
40
+
41
+ async action(): Promise<void> {
42
+ const docFile = path.resolve(this.cmd.input);
43
+ if (!(await fs.stat(docFile).catch(() => false))) {
44
+ console.error(`The input ${this.cmd.input} does not exist`);
45
+ return this.exit(1);
46
+ }
47
+
48
+ if (this.cmd.outputs.length === 0) {
49
+ const workspacePkg = PackageUtil.readPackage(RootIndex.manifest.workspacePath);
50
+ this.cmd.outputs = workspacePkg.travetto?.docOutputs ?? ['README.md'];
51
+ }
52
+
53
+ const outputs = this.cmd.outputs.map(output => output.includes('.') ? [path.extname(output).substring(1), path.resolve(output)] : [output, null] as const);
54
+
55
+ // If specifying output
56
+ const write = async (): Promise<void> => {
57
+ RenderUtil.purge(docFile);
58
+ for (const [fmt, out] of outputs) {
59
+ const result = await RenderUtil.render(docFile, fmt);
60
+ if (out) {
61
+ const finalName = path.resolve(out);
62
+ await fs.writeFile(finalName, result, 'utf8');
63
+ } else {
64
+ process.stdout.write(result);
65
+ }
66
+ }
67
+ };
68
+
69
+ if (this.cmd.watch) {
70
+ await watchFolders([path.dirname(docFile)], write, {
71
+ filter: ev => ev.action === 'update' && ev.file === docFile
72
+ });
73
+ } else {
74
+ try {
75
+ await write();
76
+ console.log(`Wrote docs for ${this.cmd.input}`);
77
+ } catch (err) {
78
+ console.error(err);
79
+ this.exit(1);
80
+ }
81
+ }
82
+ }
83
+ }
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
- }
File without changes