Skip to content

Commit

Permalink
feat: modify the git tag naming rules of the scope package
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgao365 committed May 28, 2024
1 parent f6efe3f commit f7fc585
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ npm add @tomjs/release-cli -D
```bash
$ rc -h


A CLI tool to automatically publish npm packages.

Usage
Expand All @@ -67,7 +66,7 @@ Options
--tag <tag> Publish under a given dist-tag (default: "latest")
--scoped-tag Use scoped package name as git tag
--no-log Skips generating changelog
--log-full Generate a full changelog and replace the existing content (default: false)
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
--no-log-commit Don't add git commit SHA and link to the changelog
--no-log-compare Don't add git compare link to the changelog
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,
Expand Down
2 changes: 1 addition & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Options
--tag <tag> Publish under a given dist-tag (default: "latest")
--scoped-tag Use scoped package name as git tag
--no-log Skips generating changelog
--log-full Generate a full changelog and replace the existing content (default: false)
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
--no-log-commit Don't add git commit SHA and link to the changelog
--no-log-compare Don't add git compare link to the changelog
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,
Expand Down
2 changes: 1 addition & 1 deletion src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function runGenerateChangelog(opts: ReleaseOptions) {

const { isMonorepo, pkgs } = opts;
const pkgNames = pkgs.map(s => s.name);
const pkgTags = await getGitTags(isMonorepo ? pkgNames : []);
const pkgTags = await getGitTags(opts);

const depVersions = await getDependencyVersions(opts);

Expand Down
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Options
--tag <tag> Publish under a given dist-tag (default: "latest")
--scoped-tag Use scoped package name as git tag
--no-log Skips generating changelog
--log-full Generate a full changelog and replace the existing content (default: false)
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
--no-log-commit Don't add git commit SHA and link to the changelog
--no-log-compare Don't add git compare link to the changelog
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,
Expand Down Expand Up @@ -173,6 +173,7 @@ if (flags.h) {
config,
cliOpts,
) as ReleaseCLIOptions;
logger.enableDebug(!!releaseOpts.verbose);
logger.debug('merged options:', releaseOpts);

releaseOpts.cwd ||= CWD;
Expand Down
74 changes: 52 additions & 22 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,48 +119,83 @@ export async function getChangedPackageNames(opts: ReleaseOptions) {
* @returns
*/
export function clearTagVersion(tag: string) {
return tag.replace(/^v|^@.+?@|\w.+?@/, '');
return tag.replace(/(.+(_|-|@))/, '').replace('v', '');
}

export function getGitTagVersion(name: string, version: string, opts: ReleaseOptions) {
function getGitTagPrefixLegacy(name: string, opts: ReleaseOptions) {
const { isMonorepo, scopedTag } = opts;

if (isMonorepo) {
const names = name.split('/');
const pre = scopedTag ? name : names[names.length - 1];
return `${pre}@${version}`;
return `${pre}@`;
}
return 'v';
}

export function getGitTagPrefix(name: string, opts: ReleaseOptions) {
const { isMonorepo, scopedTag } = opts;
if (isMonorepo) {
const names = name.split('/');
const pre = scopedTag ? name.replace('@', '').replace('/', '-') : names[names.length - 1];
return `${pre}-v`;
}
return `v${version}`;
return 'v';
}

export function getGitTagVersion(name: string, version: string, opts: ReleaseOptions) {
return getGitTagPrefix(name, opts) + version;
}

/**
* Get the latest tag of the packages.
* @param pkgNames only include package names
* @returns
*/
export async function getGitTags(pkgNames?: string[]) {
pkgNames ||= [];
export async function getGitTags(opts: ReleaseOptions) {
const { isMonorepo, pkgs } = opts;
const pkgNames = isMonorepo ? pkgs.map(s => s.name) : [];

// compatible with old version
const prefixMap: Record<string, string> = {};
if (isMonorepo) {
pkgNames.forEach(name => {
prefixMap[getGitTagPrefixLegacy(name, { isMonorepo, scopedTag: true } as ReleaseOptions)] =
name;
prefixMap[getGitTagPrefixLegacy(name, { isMonorepo, scopedTag: false } as ReleaseOptions)] =
name;
prefixMap[getGitTagPrefix(name, { isMonorepo, scopedTag: true } as ReleaseOptions)] = name;
prefixMap[getGitTagPrefix(name, { isMonorepo, scopedTag: false } as ReleaseOptions)] = name;
});
}
const prefixKeys = Object.keys(prefixMap);

const records = await run(
`git for-each-ref --format="%(refname:short) %(creatordate)" refs/tags`,
);
const map: Record<string, GitTagInfo[]> = {};
const add = (name: string, record: string) => {
const add = (record: string) => {
const [tag, ...times] = record.split(' ');
let pkgName = name;
if (name !== '_') {
if (!pkgNames.includes(name)) {
const n = pkgNames.find(s => s.endsWith(`/${name}`));
if (n) {
pkgName = n;
}
const version = clearTagVersion(tag);

let pkgName = '';

// single package
if (tag === `v${version}` || tag === version) {
pkgName = '_';
} else {
const prefix = prefixKeys.find(pre => tag === `${pre}${version}`);
if (prefix) {
pkgName = prefixMap[prefix];
}
}
if (!pkgName) {
return;
}

map[pkgName] = (map[pkgName] || []).concat([
{
name: tag,
version: clearTagVersion(tag),
version,
time: dayjs(times.join(' ')).format('YYYY-MM-DD'),
},
]);
Expand All @@ -170,12 +205,7 @@ export async function getGitTags(pkgNames?: string[]) {
if (!record) {
return;
}
if (record.includes('@')) {
const i = record.lastIndexOf('@');
add(record.substring(0, i), record);
} else {
add('_', record);
}
add(record);
});

Object.keys(map).forEach(name => {
Expand Down

0 comments on commit f7fc585

Please sign in to comment.