Skip to content

Commit

Permalink
remove false from version type
Browse files Browse the repository at this point in the history
* this has had no effect since sindresorhus#68
  • Loading branch information
tommy-mitchell committed Jan 26, 2024
1 parent 21b5c91 commit 6e178a9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 26 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@sindresorhus/tsconfig": "^5.0.0",
"@types/common-tags": "^1.8.4",
"@types/minimist": "^1.2.5",
"@types/node": "~18.18",
"@types/yargs-parser": "^21.0.3",
"ava": "^6.1.0",
"camelcase-keys": "^9.1.3",
"common-tags": "^2.0.0-alpha.1",
"cross-env": "^7.0.3",
"decamelize": "^6.0.0",
"decamelize-keys": "^2.0.1",
"dedent": "^1.5.1",
"delete_comments": "^0.0.2",
"execa": "^8.0.1",
"globby": "^14.0.0",
Expand All @@ -93,7 +92,7 @@
"tsd": "^0.30.4",
"tsimp": "^2.0.10",
"tslib": "^2.6.2",
"type-fest": "^4.10.0",
"type-fest": "^4.10.1",
"typescript": "~5.3.3",
"xo": "^0.56.0",
"yargs-parser": "^21.1.1"
Expand All @@ -110,7 +109,7 @@
},
"ava": {
"files": [
"test/*"
"test/*.ts"
],
"extensions": {
"ts": "module"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The description will be shown above your help text automatically.

##### version

Type: `string | boolean`\
Type: `string`\
Default: The package.json `"version"` property

Set a custom version output.
Expand Down
4 changes: 1 addition & 3 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,8 @@ export type Options<Flags extends AnyFlags = AnyFlags> = {

/**
Set a custom version output. Default: The package.json `"version"` property.
Set it to `false` to disable it altogether.
*/
readonly version?: string | false;
readonly version?: string;

/**
Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text.
Expand Down
31 changes: 13 additions & 18 deletions test/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,30 @@
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {execa, type ExecaChildProcess} from 'execa';
import {createTag, stripIndentTransformer, trimResultTransformer} from 'common-tags';
import {execa, type ExecaChildProcess, type Options as ExecaOptions} from 'execa';
import dedent from 'dedent';
import meow from '../source/index.js';
import type {Options} from '../source/types.js';

export const stripIndent = dedent;

export const __dirname = path.dirname(fileURLToPath(import.meta.url));

const getFixture = (fixture: string) => path.join(__dirname, 'fixtures', fixture);

declare function _spawnFixture(args: string[]): Promise<ExecaChildProcess>;
declare function _spawnFixture(fixture: string, args?: string[]): Promise<ExecaChildProcess>;
type SpawnFixture = typeof _spawnFixture;
export async function spawnFixture(args: string[]): Promise<ExecaChildProcess>;
export async function spawnFixture(fixture: string, args?: string[]): Promise<ExecaChildProcess>;
export async function spawnFixture(fixture: string, args?: string[], options?: ExecaOptions): Promise<ExecaChildProcess>; // eslint-disable-line @typescript-eslint/unified-signatures

// TODO: function overloading?
export const spawnFixture = async (fixture: string | string[] = 'fixture.ts', args: string[] = []) => {
export async function spawnFixture(fixtureOrArgs: string | string[] = 'fixture.ts', args: string[] = [], options: ExecaOptions = {}): Promise<ExecaChildProcess> {
// Allow calling with args first
if (Array.isArray(fixture)) {
args = fixture;
fixture = 'fixture.ts';
if (Array.isArray(fixtureOrArgs)) {
args = fixtureOrArgs;
fixtureOrArgs = 'fixture.ts';
}

return execa(getFixture(fixture), args);
};

// Use old behavior prior to zspecza/common-tags#165
export const stripIndent = createTag(
stripIndentTransformer(),
trimResultTransformer(),
);
return execa(getFixture(fixtureOrArgs), args, options);
}

export type MeowOptions = Omit<Options, 'importMeta'> & {importMeta?: ImportMeta};

Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/version/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env tsimp
import process from 'node:process';
import type {LiteralUnion} from 'type-fest';
import meow from '../../../source/index.js';

/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/consistent-type-definitions */
declare global {
namespace NodeJS {
interface ProcessEnv {
VERSION?: LiteralUnion<'false', string>;
}
}
}
/* eslint-enable @typescript-eslint/no-namespace, @typescript-eslint/consistent-type-definitions */

const version = (process.env.VERSION === 'false' ? false : process.env.VERSION) as string;

const options = {
importMeta: import.meta,
version,
autoVersion: !process.argv.includes('--no-auto-version'),
flags: {
showVersion: {type: 'boolean'},
},
} as const;

if (options.version === undefined) {
// @ts-expect-error: version should be deleted
delete options.version;
}

const cli = meow(options);

if (cli.flags.showVersion) {
cli.showVersion();
}

6 changes: 6 additions & 0 deletions test/fixtures/version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"version": "1.0.0",
"bin": "./fixture.ts",
"type": "module"
}
47 changes: 47 additions & 0 deletions test/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import test from 'ava';
import {spawnFixture, stripIndent} from './_utils.js';

const fixture = 'version/fixture.ts';

test('spawn cli and show version', async t => {
const {stdout} = await spawnFixture(fixture, ['--version']);
t.is(stdout, '1.0.0');
});

test('spawn cli and disabled autoVersion', async t => {
const {stdout} = await spawnFixture(['--version', '--no-auto-version']);

t.is(stdout, stripIndent`
version
autoVersion
meow
camelCaseOption
`);
});

test('spawn cli and not show version', async t => {
const {stdout} = await spawnFixture(['--version=beta']);

t.is(stdout, stripIndent`
version
meow
camelCaseOption
`);
});

test('custom version', async t => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const {stdout} = await spawnFixture(fixture, ['--version'], {env: {VERSION: 'beta'}});
t.is(stdout, 'beta');
});

test('version = false has no effect', async t => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const {stdout} = await spawnFixture(fixture, ['--version'], {env: {VERSION: 'false'}});
t.is(stdout, '');
});

test('manual showVersion', async t => {
const {stdout} = await spawnFixture(fixture, ['--show-version']);
t.is(stdout, '1.0.0');
});

0 comments on commit 6e178a9

Please sign in to comment.