@oclif/plugin-command-snapshot 5.0.10 → 5.1.1

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.
@@ -10,6 +10,5 @@ export default class SchemaCompare extends SnapshotCommand {
10
10
  filepath: import("@oclif/core/lib/interfaces/parser.js").OptionFlag<string, import("@oclif/core/lib/interfaces/parser.js").CustomOptions>;
11
11
  };
12
12
  run(): Promise<SchemaComparison>;
13
- private generateLatestSchema;
14
13
  private readExistingSchema;
15
14
  }
@@ -6,8 +6,8 @@ import * as fs from 'node:fs';
6
6
  import * as path from 'node:path';
7
7
  import * as semver from 'semver';
8
8
  import SnapshotCommand from '../../snapshot-command.js';
9
- import { getKeyNameFromFilename } from '../../util.js';
10
- import { SchemaGenerator, getAllFiles } from './generate.js';
9
+ import { GLOB_PATTERNS, getAllFiles, getKeyNameFromFilename } from '../../util.js';
10
+ import { SchemaGenerator } from './generate.js';
11
11
  function isNumber(n) {
12
12
  return Number.isInteger(Number(n));
13
13
  }
@@ -25,6 +25,19 @@ export default class SchemaCompare extends SnapshotCommand {
25
25
  }),
26
26
  };
27
27
  async run() {
28
+ const strategy = typeof this.config.pjson.oclif?.commands === 'string' ? 'pattern' : this.config.pjson.oclif?.commands?.strategy;
29
+ const commandsDir = typeof this.config.pjson.oclif?.commands === 'string'
30
+ ? this.config.pjson.oclif?.commands
31
+ : this.config.pjson.oclif?.commands?.target;
32
+ const commandGlobs = typeof this.config.pjson.oclif?.commands === 'string'
33
+ ? GLOB_PATTERNS
34
+ : this.config.pjson.oclif?.commands?.globPatterns;
35
+ if (strategy === 'single') {
36
+ this.error('This command is not supported for single command CLIs');
37
+ }
38
+ if (strategy === 'explicit') {
39
+ this.error('This command is not supported for explicit command discovery');
40
+ }
28
41
  const { flags } = await this.parse(SchemaCompare);
29
42
  try {
30
43
  fs.accessSync(flags.filepath);
@@ -34,7 +47,8 @@ export default class SchemaCompare extends SnapshotCommand {
34
47
  return [];
35
48
  }
36
49
  const existingSchema = this.readExistingSchema(flags.filepath);
37
- const latestSchema = await this.generateLatestSchema();
50
+ const generator = new SchemaGenerator({ base: this, commandGlobs, commandsDir });
51
+ const latestSchema = await generator.generate();
38
52
  this.debug('existingSchema', existingSchema);
39
53
  this.debug('latestSchema', latestSchema);
40
54
  const changes = diff(latestSchema, existingSchema);
@@ -89,15 +103,11 @@ export default class SchemaCompare extends SnapshotCommand {
89
103
  }
90
104
  }
91
105
  this.log();
92
- const bin = process.platform === 'win32' ? 'bin\\dev.cmd' : 'bin/dev';
106
+ const bin = process.platform === 'win32' ? 'bin\\dev.cmd' : 'bin/dev.js';
93
107
  this.log('If intended, please update the schema file(s) and run again:', chalk.bold(`${bin} ${toConfiguredId('schema:generate', this.config)}`));
94
108
  process.exitCode = 1;
95
109
  return changes;
96
110
  }
97
- async generateLatestSchema() {
98
- const generator = new SchemaGenerator(this);
99
- return generator.generate();
100
- }
101
111
  readExistingSchema(filePath) {
102
112
  const contents = fs.readdirSync(filePath);
103
113
  const folderIsVersioned = contents.every((c) => semver.valid(c));
@@ -8,12 +8,19 @@ export type Schemas = {
8
8
  hooks: SchemasMap;
9
9
  };
10
10
  export type GenerateResponse = string[];
11
- export declare function getAllFiles(dirPath: string, ext: string, allFiles?: string[]): string[];
11
+ type SchemaGenerateOptions = {
12
+ base: SnapshotCommand;
13
+ commandGlobs?: string[];
14
+ commandsDir?: string;
15
+ ignoreVoid?: boolean;
16
+ };
12
17
  export declare class SchemaGenerator {
13
18
  private base;
14
- private ignoreVoid;
15
19
  private classToId;
16
- constructor(base: SnapshotCommand, ignoreVoid?: boolean);
20
+ private commandGlobs;
21
+ private commandsDir;
22
+ private ignoreVoid;
23
+ constructor(options: SchemaGenerateOptions);
17
24
  generate(): Promise<Schemas>;
18
25
  private determineCommandId;
19
26
  private generateSchema;
@@ -43,3 +50,4 @@ export default class SchemaGenerate extends SnapshotCommand {
43
50
  };
44
51
  run(): Promise<GenerateResponse>;
45
52
  }
53
+ export {};
@@ -1,35 +1,22 @@
1
1
  import { Flags } from '@oclif/core';
2
2
  import chalk from 'chalk';
3
+ import { globbySync } from 'globby';
3
4
  import * as fs from 'node:fs';
4
5
  import * as path from 'node:path';
5
6
  import { createGenerator } from 'ts-json-schema-generator';
6
7
  import SnapshotCommand from '../../snapshot-command.js';
7
- import { getSchemaFileName } from '../../util.js';
8
- export function getAllFiles(dirPath, ext, allFiles = []) {
9
- let files = [];
10
- try {
11
- files = fs.readdirSync(dirPath);
12
- }
13
- catch { }
14
- for (const file of files) {
15
- const fPath = path.join(dirPath, file);
16
- if (fs.statSync(fPath).isDirectory()) {
17
- allFiles = getAllFiles(fPath, ext, allFiles);
18
- }
19
- else if (file.endsWith(ext)) {
20
- allFiles.push(fPath);
21
- }
22
- }
23
- return allFiles;
24
- }
8
+ import { GLOB_PATTERNS, getAllFiles, getSchemaFileName } from '../../util.js';
25
9
  export class SchemaGenerator {
26
10
  base;
27
- ignoreVoid;
28
11
  classToId = {};
29
- // eslint-disable-next-line no-useless-constructor
30
- constructor(base, ignoreVoid = true) {
31
- this.base = base;
32
- this.ignoreVoid = ignoreVoid;
12
+ commandGlobs;
13
+ commandsDir;
14
+ ignoreVoid;
15
+ constructor(options) {
16
+ this.base = options.base;
17
+ this.ignoreVoid = options.ignoreVoid ?? true;
18
+ this.commandsDir = options.commandsDir;
19
+ this.commandGlobs = options.commandGlobs ?? GLOB_PATTERNS;
33
20
  }
34
21
  async generate() {
35
22
  for (const cmd of this.base.commands) {
@@ -81,7 +68,11 @@ export class SchemaGenerator {
81
68
  }
82
69
  }
83
70
  getAllCmdFiles() {
84
- const { rootDir } = this.getDirs();
71
+ const { outDir, rootDir } = this.getDirs();
72
+ if (this.commandsDir) {
73
+ const commandsSrcDir = path.resolve(this.base.config.root, this.commandsDir).replace(outDir, rootDir);
74
+ return globbySync(this.commandGlobs, { absolute: true, cwd: commandsSrcDir });
75
+ }
85
76
  return getAllFiles(path.join(rootDir, 'commands'), '.ts');
86
77
  }
87
78
  getAllHookFiles() {
@@ -180,8 +171,21 @@ export default class SchemaGenerate extends SnapshotCommand {
180
171
  }),
181
172
  };
182
173
  async run() {
174
+ const strategy = typeof this.config.pjson.oclif?.commands === 'string' ? 'pattern' : this.config.pjson.oclif?.commands?.strategy;
175
+ const commandsDir = typeof this.config.pjson.oclif?.commands === 'string'
176
+ ? this.config.pjson.oclif?.commands
177
+ : this.config.pjson.oclif?.commands?.target;
178
+ const commandGlobs = typeof this.config.pjson.oclif?.commands === 'string'
179
+ ? GLOB_PATTERNS
180
+ : this.config.pjson.oclif?.commands?.globPatterns;
181
+ if (strategy === 'single') {
182
+ this.error('This command is not supported for single command CLIs');
183
+ }
184
+ if (strategy === 'explicit') {
185
+ this.error('This command is not supported for explicit command discovery');
186
+ }
183
187
  const { flags } = await this.parse(SchemaGenerate);
184
- const generator = new SchemaGenerator(this, flags.ignorevoid);
188
+ const generator = new SchemaGenerator({ base: this, commandGlobs, commandsDir, ignoreVoid: flags.ignorevoid });
185
189
  const schemas = await generator.generate();
186
190
  const directory = flags.filepath.replace('{version}', this.config.version);
187
191
  fs.mkdirSync(directory, { recursive: true });
package/lib/util.d.ts CHANGED
@@ -10,3 +10,5 @@ export declare const getSchemaFileName: (cmdId: string) => string;
10
10
  * @returns {string} - command ID
11
11
  */
12
12
  export declare const getKeyNameFromFilename: (file: string) => string;
13
+ export declare const getAllFiles: (dirPath: string, ext: string, allFiles?: string[]) => string[];
14
+ export declare const GLOB_PATTERNS: string[];
package/lib/util.js CHANGED
@@ -1,3 +1,5 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
1
3
  /**
2
4
  * Get the file name for a given command ID replacing "-" with "__" and ":" with "-"
3
5
  * @param cmdId - command ID
@@ -13,3 +15,19 @@ export const getSchemaFileName = (cmdId) => {
13
15
  * @returns {string} - command ID
14
16
  */
15
17
  export const getKeyNameFromFilename = (file) => file.replaceAll('-', ':').replaceAll('__', '-').replace('.json', '');
18
+ export const getAllFiles = (dirPath, ext, allFiles = []) => safeReadDirSync(dirPath)
19
+ .flatMap((f) => f.isDirectory() ? getAllFiles(path.join(dirPath, f.name), ext, allFiles) : path.join(dirPath, f.name))
20
+ .filter((f) => f.endsWith(ext));
21
+ const safeReadDirSync = (dirPath) => {
22
+ try {
23
+ // TODO: use recursive option when available in Node 20
24
+ return fs.readdirSync(dirPath, { withFileTypes: true });
25
+ }
26
+ catch {
27
+ return [];
28
+ }
29
+ };
30
+ export const GLOB_PATTERNS = [
31
+ '**/*.+(js|cjs|mjs|ts|tsx|mts|cts)',
32
+ '!**/*.+(d.ts|test.ts|test.js|spec.ts|spec.js|d.mts|d.cts)?(x)',
33
+ ];