@oclif/core 1.3.5 → 1.5.0

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/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.5.0](https://github.com/oclif/core/compare/v1.4.0...v1.5.0) (2022-03-02)
6
+
7
+
8
+ ### Features
9
+
10
+ * dir and file flags that validate existence and type ([#384](https://github.com/oclif/core/issues/384)) ([44dff41](https://github.com/oclif/core/commit/44dff41c5a3ffcdcbf2f10dcefb7c1ab233bfc4f))
11
+
12
+ ## [1.4.0](https://github.com/oclif/core/compare/v1.3.6...v1.4.0) (2022-03-01)
13
+
14
+
15
+ ### Features
16
+
17
+ * make global flags settable ([#385](https://github.com/oclif/core/issues/385)) ([e14061c](https://github.com/oclif/core/commit/e14061ca7e6a4c288eb50e0e9954b38e042682df))
18
+
19
+ ### [1.3.6](https://github.com/oclif/core/compare/v1.3.5...v1.3.6) (2022-02-28)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * parsing the default is wrong types ([ba08723](https://github.com/oclif/core/commit/ba087237773e6f4b3649d03dc88f693a22681de9))
25
+
5
26
  ### [1.3.5](https://github.com/oclif/core/compare/v1.3.4...v1.3.5) (2022-02-25)
6
27
 
7
28
 
package/lib/command.d.ts CHANGED
@@ -63,9 +63,11 @@ export default abstract class Command {
63
63
  * @param {Interfaces.LoadOptions} opts options
64
64
  */
65
65
  static run: Interfaces.Command.Class['run'];
66
- private static globalFlags;
66
+ protected static _globalFlags: Interfaces.FlagInput<any>;
67
+ static get globalFlags(): Interfaces.FlagInput<any>;
68
+ static set globalFlags(flags: Interfaces.FlagInput<any>);
67
69
  /** A hash of flags for the command */
68
- private static _flags;
70
+ protected static _flags: Interfaces.FlagInput<any>;
69
71
  static get flags(): Interfaces.FlagInput<any>;
70
72
  static set flags(flags: Interfaces.FlagInput<any>);
71
73
  id: string | undefined;
package/lib/command.js CHANGED
@@ -17,6 +17,12 @@ process.stdout.on('error', (err) => {
17
17
  return;
18
18
  throw err;
19
19
  });
20
+ const jsonFlag = {
21
+ json: Flags.boolean({
22
+ description: 'Format output as json.',
23
+ helpGroup: 'GLOBAL',
24
+ }),
25
+ };
20
26
  /**
21
27
  * An abstract class which acts as the base for each command
22
28
  * in your project.
@@ -33,11 +39,20 @@ class Command {
33
39
  this.debug = () => { };
34
40
  }
35
41
  }
42
+ static get globalFlags() {
43
+ return this._globalFlags;
44
+ }
45
+ static set globalFlags(flags) {
46
+ this._globalFlags = this.enableJsonFlag ?
47
+ Object.assign({}, jsonFlag, this.globalFlags, flags) :
48
+ Object.assign({}, this.globalFlags, flags);
49
+ }
36
50
  static get flags() {
37
51
  return this._flags;
38
52
  }
39
53
  static set flags(flags) {
40
- this._flags = this.enableJsonFlag ? Object.assign({}, Command.globalFlags, flags) : flags;
54
+ this.globalFlags = {};
55
+ this._flags = Object.assign({}, this.globalFlags, flags);
41
56
  }
42
57
  get ctor() {
43
58
  return this.constructor;
@@ -169,9 +184,3 @@ Command.run = async function (argv, opts) {
169
184
  const cmd = new this(argv, config);
170
185
  return cmd._run(argv);
171
186
  };
172
- Command.globalFlags = {
173
- json: Flags.boolean({
174
- description: 'Format output as json.',
175
- helpGroup: 'GLOBAL',
176
- }),
177
- };
package/lib/flags.d.ts CHANGED
@@ -10,6 +10,6 @@ declare const _enum: <T = string>(opts: EnumFlagOptions<T>) => OptionFlag<T>;
10
10
  export { _enum as enum };
11
11
  declare const stringFlag: Definition<string>;
12
12
  export { stringFlag as string };
13
- export { boolean, integer, url } from './parser';
13
+ export { boolean, integer, url, directory, file } from './parser';
14
14
  export declare const version: (opts?: Partial<BooleanFlag<boolean>>) => BooleanFlag<void>;
15
15
  export declare const help: (opts?: Partial<BooleanFlag<boolean>>) => BooleanFlag<void>;
package/lib/flags.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.help = exports.version = exports.url = exports.integer = exports.boolean = exports.string = exports.enum = exports.option = exports.build = void 0;
3
+ exports.help = exports.version = exports.file = exports.directory = exports.url = exports.integer = exports.boolean = exports.string = exports.enum = exports.option = exports.build = void 0;
4
4
  const Parser = require("./parser");
5
5
  function build(defaults) {
6
6
  return Parser.flags.build(defaults);
@@ -28,6 +28,8 @@ var parser_1 = require("./parser");
28
28
  Object.defineProperty(exports, "boolean", { enumerable: true, get: function () { return parser_1.boolean; } });
29
29
  Object.defineProperty(exports, "integer", { enumerable: true, get: function () { return parser_1.integer; } });
30
30
  Object.defineProperty(exports, "url", { enumerable: true, get: function () { return parser_1.url; } });
31
+ Object.defineProperty(exports, "directory", { enumerable: true, get: function () { return parser_1.directory; } });
32
+ Object.defineProperty(exports, "file", { enumerable: true, get: function () { return parser_1.file; } });
31
33
  const version = (opts = {}) => {
32
34
  return Parser.flags.boolean({
33
35
  description: 'Show CLI version.',
@@ -6,7 +6,7 @@ export { HelpOptions } from './help';
6
6
  export { Hook, Hooks } from './hooks';
7
7
  export { Manifest } from './manifest';
8
8
  export { S3Manifest } from './s3-manifest';
9
- export { ParserArg, Arg, ParseFn, ParserOutput, ParserInput, ArgToken, OptionalArg, FlagOutput, OutputArgs, OutputFlags, FlagUsageOptions, CLIParseErrorOptions, ArgInput, RequiredArg, Metadata, ParsingToken, FlagToken, List, ListItem, BooleanFlag, Flag, FlagBase, OptionFlag, Input, EnumFlagOptions, DefaultContext, Default, Definition, CompletableOptionFlag, Completion, CompletionContext, FlagInput, CompletableFlag, } from './parser';
9
+ export { ParserArg, Arg, ParseFn, ParserOutput, ParserInput, ArgToken, OptionalArg, FlagOutput, OutputArgs, OutputFlags, FlagUsageOptions, CLIParseErrorOptions, ArgInput, RequiredArg, Metadata, ParsingToken, FlagToken, List, ListItem, BooleanFlag, Flag, FlagBase, OptionFlag, Input, EnumFlagOptions, DefaultContext, Default, Definition, CompletableOptionFlag, Completion, CompletionContext, FlagInput, CompletableFlag, OptionFlagProps, } from './parser';
10
10
  export { PJSON } from './pjson';
11
11
  export { Plugin, PluginOptions, Options } from './plugin';
12
12
  export { Topic } from './topic';
@@ -7,6 +7,12 @@ export declare function build<T>(defaults: {
7
7
  export declare function build(defaults: Partial<OptionFlag<string>>): Definition<string>;
8
8
  export declare function boolean<T = boolean>(options?: Partial<BooleanFlag<T>>): BooleanFlag<T>;
9
9
  export declare const integer: Definition<number>;
10
+ export declare const directory: (opts?: {
11
+ exists?: boolean;
12
+ } & Partial<OptionFlag<string>>) => OptionFlag<string | undefined>;
13
+ export declare const file: (opts?: {
14
+ exists?: boolean;
15
+ } & Partial<OptionFlag<string>>) => OptionFlag<string | undefined>;
10
16
  /**
11
17
  * Initializes a string as a URL. Throws an error
12
18
  * if the string is not a valid URL.
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  // tslint:disable interface-over-type-literal
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.defaultFlags = exports.string = exports.option = exports.url = exports.integer = exports.boolean = exports.build = void 0;
4
+ exports.defaultFlags = exports.string = exports.option = exports.url = exports.file = exports.directory = exports.integer = exports.boolean = exports.build = void 0;
5
5
  const url_1 = require("url");
6
+ const fs = require("fs");
6
7
  function build(defaults) {
7
8
  return (options = {}) => {
8
9
  return {
@@ -32,6 +33,20 @@ exports.integer = build({
32
33
  return Number.parseInt(input, 10);
33
34
  },
34
35
  });
36
+ const directory = (opts = {}) => {
37
+ return build({
38
+ ...opts,
39
+ parse: async (input) => opts.exists ? dirExists(input) : input,
40
+ })();
41
+ };
42
+ exports.directory = directory;
43
+ const file = (opts = {}) => {
44
+ return build({
45
+ ...opts,
46
+ parse: async (input) => opts.exists ? fileExists(input) : input,
47
+ })();
48
+ };
49
+ exports.file = file;
35
50
  /**
36
51
  * Initializes a string as a URL. Throws an error
37
52
  * if the string is not a valid URL.
@@ -55,3 +70,21 @@ exports.string = stringFlag;
55
70
  exports.defaultFlags = {
56
71
  color: boolean({ allowNo: true }),
57
72
  };
73
+ const dirExists = async (input) => {
74
+ if (!fs.existsSync(input)) {
75
+ throw new Error(`No directory found at ${input}`);
76
+ }
77
+ if (!(await fs.promises.stat(input)).isDirectory()) {
78
+ throw new Error(`${input} exists but is not a directory`);
79
+ }
80
+ return input;
81
+ };
82
+ const fileExists = async (input) => {
83
+ if (!fs.existsSync(input)) {
84
+ throw new Error(`No file found at ${input}`);
85
+ }
86
+ if (!(await fs.promises.stat(input)).isFile()) {
87
+ throw new Error(`${input} exists but is not a file`);
88
+ }
89
+ return input;
90
+ };
@@ -8,5 +8,9 @@ export { flagUsages } from './help';
8
8
  export declare function parse<TFlags, TArgs extends {
9
9
  [name: string]: string;
10
10
  }>(argv: string[], options: Input<TFlags>): Promise<ParserOutput<TFlags, TArgs>>;
11
- declare const boolean: typeof flags.boolean, integer: import("../interfaces").Definition<number>, url: import("../interfaces").Definition<import("url").URL>;
12
- export { boolean, integer, url };
11
+ declare const boolean: typeof flags.boolean, integer: import("../interfaces").Definition<number>, url: import("../interfaces").Definition<import("url").URL>, directory: (opts?: {
12
+ exists?: boolean | undefined;
13
+ } & Partial<import("../interfaces").OptionFlag<string>>) => import("../interfaces").OptionFlag<string | undefined>, file: (opts?: {
14
+ exists?: boolean | undefined;
15
+ } & Partial<import("../interfaces").OptionFlag<string>>) => import("../interfaces").OptionFlag<string | undefined>;
16
+ export { boolean, integer, url, directory, file };
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // tslint:disable interface-over-type-literal
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.url = exports.integer = exports.boolean = exports.parse = exports.flagUsages = exports.flags = exports.args = void 0;
4
+ exports.file = exports.directory = exports.url = exports.integer = exports.boolean = exports.parse = exports.flagUsages = exports.flags = exports.args = void 0;
5
5
  const args = require("./args");
6
6
  exports.args = args;
7
7
  const deps_1 = require("./deps");
@@ -32,7 +32,9 @@ async function parse(argv, options) {
32
32
  return output;
33
33
  }
34
34
  exports.parse = parse;
35
- const { boolean, integer, url } = flags;
35
+ const { boolean, integer, url, directory, file } = flags;
36
36
  exports.boolean = boolean;
37
37
  exports.integer = integer;
38
38
  exports.url = url;
39
+ exports.directory = directory;
40
+ exports.file = file;
@@ -181,11 +181,7 @@ class Parser {
181
181
  this.metaData.flags[k] = { setFromDefault: true };
182
182
  // eslint-disable-next-line no-await-in-loop
183
183
  const defaultValue = (typeof flag.default === 'function' ? await flag.default({ options: flag, flags, ...this.context }) : flag.default);
184
- const parsedValue = flag.type === 'option' && flag.parse ?
185
- // eslint-disable-next-line no-await-in-loop
186
- await flag.parse(defaultValue, this.context) :
187
- defaultValue;
188
- flags[k] = parsedValue;
184
+ flags[k] = defaultValue;
189
185
  }
190
186
  }
191
187
  return flags;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oclif/core",
3
3
  "description": "base library for oclif CLIs",
4
- "version": "1.3.5",
4
+ "version": "1.5.0",
5
5
  "author": "Salesforce",
6
6
  "bugs": "https://github.com/oclif/core/issues",
7
7
  "dependencies": {