Skip to content

Commit

Permalink
move skipParseOptions and customPriorities to features
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed May 9, 2023
1 parent 1a856b1 commit c12806a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 75 deletions.
21 changes: 14 additions & 7 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,24 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
const actualOptions = Array.isArray(args) ? (options as O) : args;
const actualFeatures = Array.isArray(args) ? features : (options as F);

this.options = actualOptions;
this._initOptions = { ...actualOptions };
// Load parameters
this._args = actualArgs;
this.options = actualOptions;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
this.features = actualFeatures ?? ({} as F);

// Initialize properties
this._options = {};
this._arguments = [];
this._prompts = [];

// Parse parameters
this._initOptions = { ...actualOptions };
this._namespace = actualOptions.namespace;
this._namespaceId = requireNamespace(actualOptions.namespace);
this._customPriorities = actualOptions.customPriorities;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
this.features = actualFeatures ?? ({} as F);
this._customPriorities = this.features?.customPriorities;
this.features.skipParseOptions = this.features.skipParseOptions ?? this.options.skipParseOptions;
this.features.customPriorities = this.features.customPriorities ?? this.options.customPriorities;

this.option('help', {
type: Boolean,
Expand Down Expand Up @@ -476,7 +483,7 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
this._options[specName] = spec;
}

if (!this.options.skipParseOptions) {
if (!this.features.skipParseOptions) {
this.parseOptions();
}

Expand Down Expand Up @@ -517,7 +524,7 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
...config,
});

if (!this.options.skipParseOptions) {
if (!this.features.skipParseOptions) {
this.parseOptions();
}

Expand Down
16 changes: 12 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export type BaseFeatures = FeaturesApi & {

/** Provides a custom commit task. */
customCommitTask?: boolean | ((...args: any[]) => void | Promise<void>);

/** Disable args/options parsing. Whenever options/arguments are provided parsed like using commander based parsing. */
skipParseOptions?: boolean;

/** Custom priorities for more fine tuned workflows. */
customPriorities?: Priority[];
};

export type BaseOptions = OptionsApi & {
Expand All @@ -98,17 +104,19 @@ export type BaseOptions = OptionsApi & {

askAnswered?: boolean;

skipParseOptions?: boolean;

localConfigOnly?: boolean;

skipCache?: boolean;

skipLocalCache?: boolean;

customPriorities?: Priority[];

description?: string;

/** @deprecated moved to features */
skipParseOptions?: boolean;

/** @deprecated moved to features */
customPriorities?: Priority[];
};

export type ArgumentSpec = {
Expand Down
110 changes: 58 additions & 52 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,36 +1481,41 @@ describe('Base', () => {

beforeEach(function () {
TestGenerator = class extends Base {
constructor(args, options) {
super(args, {
...options,
customPriorities: [
...(options.customPriorities || []),
{
// Change priority prompting to be queue before writing for this generator.
// If we change defaults priorities in the future, the order of custom priorities will keep the same.
priorityName: 'prompting',
before: 'writing',
},
{
priorityName: 'prePrompting1',
before: 'prompting',
},
{
priorityName: 'preConfiguring1',
before: 'preConfiguring2',
queueName: 'common#preConfiguring1',
once: true,
},
{
priorityName: 'preConfiguring2',
before: 'configuring',
},
{
priorityName: 'afterEnd',
},
],
});
constructor(args, options, features) {
super(
args,
{
...options,
},
{
customPriorities: [
...(features?.customPriorities || []),
{
// Change priority prompting to be queue before writing for this generator.
// If we change defaults priorities in the future, the order of custom priorities will keep the same.
priorityName: 'prompting',
before: 'writing',
},
{
priorityName: 'prePrompting1',
before: 'prompting',
},
{
priorityName: 'preConfiguring1',
before: 'preConfiguring2',
queueName: 'common#preConfiguring1',
once: true,
},
{
priorityName: 'preConfiguring2',
before: 'configuring',
},
{
priorityName: 'afterEnd',
},
],
},
);
}
};
});
Expand Down Expand Up @@ -1674,19 +1679,24 @@ describe('Base', () => {
it('error is thrown with duplicate custom queue', function () {
const TestGenerator = class extends Base {
constructor(args, options) {
super(args, {
...options,
customPriorities: [
{
priorityName: 'beforePrompting',
before: 'prompting',
},
{
priorityName: 'beforePrompting',
before: 'prompting',
},
],
});
super(
args,
{
...options,
},
{
customPriorities: [
{
priorityName: 'beforePrompting',
before: 'prompting',
},
{
priorityName: 'beforePrompting',
before: 'prompting',
},
],
},
);
}
};

Expand Down Expand Up @@ -1850,15 +1860,11 @@ describe('Base', () => {
});

it('should return any public member when tasksMatchingPriority is true', function () {
const gen = new TestGen(
[],
{
namespace: 'foo',
env,
},
{ tasksMatchingPriority: true },
);
assert.deepStrictEqual(gen.getTaskNames(), ['default', 'customPriority']);
const Gen = helpers.createDummyGenerator(TestGen, {
default() {},
customPriority() {},
});
assert.deepStrictEqual(new Gen({ env }).getTaskNames(), ['default', 'customPriority']);
});
});
});
18 changes: 6 additions & 12 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ export default class BaseTest extends Base {
Partial<Pick<Base['options'], 'namespace' | 'resolved'>>,
features?: Base['features'],
);
constructor(args: any, options: any, features?: any) {
args = Array.isArray(args)
? args
: { ...args, resolved: args?.resolved ?? __filename, namespace: args?.namespace ?? 'yeoman:testnamespace' };
options = Array.isArray(args)
? {
...options,
resolved: options?.resolved ?? __filename,
namespace: options?.namespace ?? 'yeoman:testnamespace',
}
: options;
super(args, options, features);
constructor(...args: any[]) {
const optIndex = Array.isArray(args[0]) ? 1 : 0;
args[optIndex] = args[optIndex] ?? {};
args[optIndex].resolved = args[optIndex].resolved ?? __filename;
args[optIndex].namespace = args[optIndex].namespace ?? 'yeoman:testnamespace';
super(...args);
}
}

0 comments on commit c12806a

Please sign in to comment.