Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli update and typescript conversion wip (new branch name) #69

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
19 changes: 15 additions & 4 deletions command.js → command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
const spec = require('conventional-changelog-config-spec')
const { getConfiguration } = require('./lib/configuration')
const defaults = require('./defaults')
// @ts-nocheck
/*
I haven't been able to find API docs for yargs that explain the runtime semantics thoroughly enough to describe the types,
and the types give do not appear to agree with the usage below. This leads me to believe the types are either incorrect or incomplete,
and adding types to Yargs is outside of scope.

As a compromise, `argv` is manually typed, and the type must be updated as changes are made below. There is a
[separate isssue](https://github.com/absolute-version/commit-and-tag-version/issues/31) for refactoring the API to bring the CLI
options to parity with the JSON options, which would likely involve breaking changes.
*/

import spec from 'conventional-changelog-config-spec/versions/2.1.0/schema.json'
import { getConfiguration } from './lib/configuration'
import defaults from './defaults'

const yargs = require('yargs')
.usage('Usage: $0 [options]')
Expand Down Expand Up @@ -148,7 +159,7 @@ const yargs = require('yargs')
.wrap(97)

Object.keys(spec.properties).forEach((propertyKey) => {
const property = spec.properties[propertyKey]
const property = spec.properties[propertyKey as keyof typeof spec.properties]
helmturner marked this conversation as resolved.
Show resolved Hide resolved
yargs.option(propertyKey, {
type: property.type,
describe: property.description,
Expand Down
43 changes: 0 additions & 43 deletions defaults.js

This file was deleted.

85 changes: 85 additions & 0 deletions defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import spec from "conventional-changelog-config-spec/versions/2.1.0/schema.json";

type Defaults = Readonly<
helmturner marked this conversation as resolved.
Show resolved Hide resolved
{
infile: string;
firstRelease: boolean;
sign: boolean;
noVerify: boolean;
commitAll: boolean;
silent: boolean;
tagPrefix: string;
releaseCount: number;
scripts: {
prerelease?: string;
prebump?: string;
postbump?: string;
prechangelog?: string;
postchangelog?: string;
precommit?: string;
postcommit?: string;
pretag?: string;
posttag?: string;
};
skip: {
bump?: boolean;
changelog?: boolean;
commit?: boolean;
tag?: boolean;
};
dryRun: boolean;
tagForce: boolean;
gitTagFallback: boolean;
preset: string;
npmPublishHint: string | undefined;
packageFiles: readonly string[];
bumpFiles: readonly string[];
} & {
[key in keyof typeof spec.properties]: typeof spec.properties[key]["default"];
}
>;

const defaults = {
infile: "CHANGELOG.md",
firstRelease: false,
sign: false,
noVerify: false,
commitAll: false,
silent: false,
tagPrefix: "v",
releaseCount: 1,
scripts: {},
skip: {},
dryRun: false,
tagForce: false,
gitTagFallback: true,
preset: require.resolve('conventional-changelog-conventionalcommits'),
npmPublishHint: undefined,
/**
* Sets the default for `header` (provided by the spec) for backwards
* compatibility. This should be removed in the next major version.
*/
header:
"# Changelog\n\nAll notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.\n",
packageFiles: ["package.json", "bower.json", "manifest.json"],
bumpFiles: [
"package.json",
"bower.json",
"manifest.json",
"package-lock.json",
"npm-shrinkwrap.json",
],
} as const satisfies Partial<Readonly<Defaults>>;

/**
* Merge in defaults provided by the spec
*/
Object.keys(spec.properties).forEach((propertyKey) => {
const k: keyof typeof spec.properties =
propertyKey as keyof typeof spec.properties;
helmturner marked this conversation as resolved.
Show resolved Hide resolved
const property = spec.properties[k];
// @ts-expect-error - we know that the key exists
defaults[k] = property.default;
});

export default defaults as any as Defaults;
19 changes: 10 additions & 9 deletions lib/configuration.js → lib/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const path = require('path')
const findUp = require('find-up')
const { readFileSync } = require('fs')
import path from 'path'
import findUp from 'find-up'
import { readFileSync } from 'fs'
import { CatVConfig } from './opts'

const CONFIGURATION_FILES = [
'.versionrc',
'.versionrc.cjs',
'.versionrc.json',
'.versionrc.cjs',
'.versionrc.js'
]
] as const;

module.exports.getConfiguration = function () {
let config = {}
export async function getConfiguration () {
let config = {} as Partial<CatVConfig>
helmturner marked this conversation as resolved.
Show resolved Hide resolved
const configPath = findUp.sync(CONFIGURATION_FILES)
if (!configPath) {
return config
}
const ext = path.extname(configPath)
if (ext === '.js' || ext === '.cjs') {
const jsConfiguration = require(configPath)
const jsConfiguration = await import(configPath)
if (typeof jsConfiguration === 'function') {
config = jsConfiguration()
} else {
config = jsConfiguration
}
} else {
config = JSON.parse(readFileSync(configPath))
config = JSON.parse(readFileSync(configPath, 'utf-8'))
}

/**
Expand Down
122 changes: 122 additions & 0 deletions lib/opts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { getConfiguration as getConfigFile } from "../configuration";
import { isin } from "../../type-helpers";
import type { PrettyPrint } from "../../type-helpers";

type Release = "minor" | "major" | "patch";
type Task = "changelog" | "commit" | "tag";
type Hook =
| "prerelease"
| "prebump"
| "postbump"
| "prechangelog"
| "postchangelog"
| "precommit"
| "postcommit"
| "pretag"
| "posttag";

type TypePrefixes = Array<
(
| { section: string; hidden?: boolean | undefined }
| { hidden: true; section?: string | undefined }
) & { type: string }
>;

type ConfigFiles = Array<
| string
| { filename: string; type: "json" | "gradle" | "plain-text" }
| { filename: string; updater: string }
>;

/**
* __THIS SHOULD NOT CHANGE.__
* @deprecated
*
* The configuration options for `standard-version` as of version 9.5 (The last version prior to the fork; deprecated).
*/
export type SVConfig = {
helmturner marked this conversation as resolved.
Show resolved Hide resolved
packageFiles: ConfigFiles;
bumpFiles: ConfigFiles;
releaseAs: Release;
prerelease: string | boolean;
infile: string;
message: string;
firstRelease: boolean;
sign: boolean;
noVerify: boolean;
commitAll: boolean;
silent: boolean;
tagPrefix: string;
scripts: Record<Hook, string>;
skip: Record<Task, string>;
dryRun: boolean;
gitTagFallback: boolean;
path: string;
changelogHeader: string;
preset: string;
lernaPackage: string;
header: string;
types: TypePrefixes;
preMajor: boolean;
commitUrlFormat: string;
compareUrlFormat: string;
issueUrlFormat: string;
userUrlFormat: string;
releaseCommitMessageFormat: string;
issuePrefixes: string[];
};

/**
* The configuration object for commit-and-tag-version, which is a superset of the conventional-changelog-config-spec (as of version 2.1.0)
* This may or may not maintain backwards compatibility with standard-version (as of version 9.5.0).
*/
export type CatVConfig = PrettyPrint<
helmturner marked this conversation as resolved.
Show resolved Hide resolved
SVConfig & {
npmPublishHint: string;
releaseCount: number;
tagForce: boolean;
}
>;

/** The configuration options that are not supported by standard-version (as of version 9.5.0). */
const catVOnlyFeatures = [
"npmPublishHint",
"releaseCount",
"tagForce",
] as const satisfies ReadonlyArray<Exclude<keyof CatVConfig, keyof SVConfig>>;

export const getMergedConfig = async (
cwd?: string
): Promise<Partial<CatVConfig>> => {
const dir = cwd ?? process.cwd();
const json = (await import("path")).join(dir, "package.json");
const svConfig: SVConfig = (await import(json))["standard-version"];
helmturner marked this conversation as resolved.
Show resolved Hide resolved
const catvConfig: CatVConfig = (await import(json))["commit-and-tag-version"];

const config: Partial<CatVConfig> = {};

if (svConfig) {
for (const key in svConfig) {
// @ts-expect-error - We know this key SHOULD NOT be in svConfig, but we're going to allow it and warn the user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably avoid all the // @ts-expect-error directives by making this an Object.keys(svConfig).filter(() => /* etc */)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I don't believe .filter()acts as a type guard. There is a library, ts-reset, that does this, but it's not recommended for libraries because it pollutes the global scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, let's not pollute the global scope 😅

What I mean is:

Object.keys(svConfig)
  .filter((key) => catVOnlyFeatures.includes(key))
  .forEach((key) => console.warn(
          `The "${key}" option is a feature of commit-and-tag-version, and is not supported by standard-version.${"\n"}Please move this option to the 'commit-and-tag-version' key.${"\n"}In a future version, this will throw an error.`))

Copy link
Author

@helmturner helmturner Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your meaning, but what I'm saying is that - despite how one might expect .filter() and Object.keys() to work - the type of key remains as string throughout that pipeline. It is never narrowed to the union of literal strings that are keys of svConfig, which is what is needed to remove the type errors.

You aren't the only one to expect this behavior, though. I certainly did, as did many others. That's why I bring up ts-reset - it was written with the purpose of making some less intuitive behaviors act how you would expect, and I think this is one of the things it addresses (again, not advocating for it here lol).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow - ignore me. I feel dumb now! I think I understand what you mean. I've refactored like so:

export const getMergedConfig = async (
  cwd?: string
): Promise<Partial<Config>> => {
  const searchDir = cwd ?? process.cwd();
  const pkgJson = (await import("path")).join(searchDir, "package.json");
  const legacyConf: LegacyConfig = (await import(pkgJson))["standard-version"];
  const modernConf: Config = (await import(pkgJson))["commit-and-tag-version"];

  Object.keys(legacyConf ?? {}).forEach((key) => {
    if (catVOnlyFeatures.includes(key as any)) {
      console.warn(
        `The "${key}" option is a feature of commit-and-tag-version, and is not supported by standard-version.${"\n"}Please move this option to the 'commit-and-tag-version' key.${"\n"}In a future version, this will throw an error.`
      );
    }
    if (modernConf && isin(modernConf, key as any)) {
      console.warn(
        `"standard-version"."${key}" in package.json is being overridden by "commit-and-tag-version"."${key}". in package.json`
      );
    }
  });

  const configFromFile = await getConfigFromFile();
  return { ...(legacyConf ?? {}), ...(modernConf ?? {}), ...(configFromFile ?? {}) };
};

That about right?

if (catVOnlyFeatures.includes(key)) {
console.warn(
`The "${key}" option is a feature of commit-and-tag-version, and is not supported by standard-version.${"\n"}Please move this option to the 'commit-and-tag-version' key.${"\n"}In a future version, this will throw an error.`
helmturner marked this conversation as resolved.
Show resolved Hide resolved
);
}
if (isin(catvConfig, key)) {
console.warn(
`"standard-version"."${key}" in package.json is being overridden by "commit-and-tag-version"."${key}". in package.json`
);
}
// @ts-expect-error - We know better than TS here. This is valid.
config[key] = svConfig[key];
}
}
for (const key in catvConfig) {
// @ts-expect-error - We know better than TS here. This is valid.
config[key] = catvConfig[key];
helmturner marked this conversation as resolved.
Show resolved Hide resolved
}

const config2 = await getConfigFile();
helmturner marked this conversation as resolved.
Show resolved Hide resolved
return { ...config, ...config2 };
};
Loading