Skip to content

Commit f7fc585

Browse files
committed
feat: modify the git tag naming rules of the scope package
1 parent f6efe3f commit f7fc585

5 files changed

Lines changed: 57 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ npm add @tomjs/release-cli -D
4545
```bash
4646
$ rc -h
4747

48-
4948
A CLI tool to automatically publish npm packages.
5049

5150
Usage
@@ -67,7 +66,7 @@ Options
6766
--tag <tag> Publish under a given dist-tag (default: "latest")
6867
--scoped-tag Use scoped package name as git tag
6968
--no-log Skips generating changelog
70-
--log-full Generate a full changelog and replace the existing content (default: false)
69+
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
7170
--no-log-commit Don't add git commit SHA and link to the changelog
7271
--no-log-compare Don't add git compare link to the changelog
7372
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,

README.zh_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Options
6666
--tag <tag> Publish under a given dist-tag (default: "latest")
6767
--scoped-tag Use scoped package name as git tag
6868
--no-log Skips generating changelog
69-
--log-full Generate a full changelog and replace the existing content (default: false)
69+
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
7070
--no-log-commit Don't add git commit SHA and link to the changelog
7171
--no-log-compare Don't add git compare link to the changelog
7272
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,

src/changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function runGenerateChangelog(opts: ReleaseOptions) {
2323

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

2828
const depVersions = await getDependencyVersions(opts);
2929

src/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Options
3333
--tag <tag> Publish under a given dist-tag (default: "latest")
3434
--scoped-tag Use scoped package name as git tag
3535
--no-log Skips generating changelog
36-
--log-full Generate a full changelog and replace the existing content (default: false)
36+
--log-full Generate a full changelog and replace the existing content, not recommended (default: false)
3737
--no-log-commit Don't add git commit SHA and link to the changelog
3838
--no-log-compare Don't add git compare link to the changelog
3939
--git-url Specify the git web url. If not specified, the configuration of git or package.json will be read,
@@ -173,6 +173,7 @@ if (flags.h) {
173173
config,
174174
cliOpts,
175175
) as ReleaseCLIOptions;
176+
logger.enableDebug(!!releaseOpts.verbose);
176177
logger.debug('merged options:', releaseOpts);
177178

178179
releaseOpts.cwd ||= CWD;

src/git.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,48 +119,83 @@ export async function getChangedPackageNames(opts: ReleaseOptions) {
119119
* @returns
120120
*/
121121
export function clearTagVersion(tag: string) {
122-
return tag.replace(/^v|^@.+?@|\w.+?@/, '');
122+
return tag.replace(/(.+(_|-|@))/, '').replace('v', '');
123123
}
124124

125-
export function getGitTagVersion(name: string, version: string, opts: ReleaseOptions) {
125+
function getGitTagPrefixLegacy(name: string, opts: ReleaseOptions) {
126126
const { isMonorepo, scopedTag } = opts;
127-
128127
if (isMonorepo) {
129128
const names = name.split('/');
130129
const pre = scopedTag ? name : names[names.length - 1];
131-
return `${pre}@${version}`;
130+
return `${pre}@`;
131+
}
132+
return 'v';
133+
}
134+
135+
export function getGitTagPrefix(name: string, opts: ReleaseOptions) {
136+
const { isMonorepo, scopedTag } = opts;
137+
if (isMonorepo) {
138+
const names = name.split('/');
139+
const pre = scopedTag ? name.replace('@', '').replace('/', '-') : names[names.length - 1];
140+
return `${pre}-v`;
132141
}
133-
return `v${version}`;
142+
return 'v';
143+
}
144+
145+
export function getGitTagVersion(name: string, version: string, opts: ReleaseOptions) {
146+
return getGitTagPrefix(name, opts) + version;
134147
}
135148

136149
/**
137150
* Get the latest tag of the packages.
138151
* @param pkgNames only include package names
139152
* @returns
140153
*/
141-
export async function getGitTags(pkgNames?: string[]) {
142-
pkgNames ||= [];
154+
export async function getGitTags(opts: ReleaseOptions) {
155+
const { isMonorepo, pkgs } = opts;
156+
const pkgNames = isMonorepo ? pkgs.map(s => s.name) : [];
157+
158+
// compatible with old version
159+
const prefixMap: Record<string, string> = {};
160+
if (isMonorepo) {
161+
pkgNames.forEach(name => {
162+
prefixMap[getGitTagPrefixLegacy(name, { isMonorepo, scopedTag: true } as ReleaseOptions)] =
163+
name;
164+
prefixMap[getGitTagPrefixLegacy(name, { isMonorepo, scopedTag: false } as ReleaseOptions)] =
165+
name;
166+
prefixMap[getGitTagPrefix(name, { isMonorepo, scopedTag: true } as ReleaseOptions)] = name;
167+
prefixMap[getGitTagPrefix(name, { isMonorepo, scopedTag: false } as ReleaseOptions)] = name;
168+
});
169+
}
170+
const prefixKeys = Object.keys(prefixMap);
143171

144172
const records = await run(
145173
`git for-each-ref --format="%(refname:short) %(creatordate)" refs/tags`,
146174
);
147175
const map: Record<string, GitTagInfo[]> = {};
148-
const add = (name: string, record: string) => {
176+
const add = (record: string) => {
149177
const [tag, ...times] = record.split(' ');
150-
let pkgName = name;
151-
if (name !== '_') {
152-
if (!pkgNames.includes(name)) {
153-
const n = pkgNames.find(s => s.endsWith(`/${name}`));
154-
if (n) {
155-
pkgName = n;
156-
}
178+
const version = clearTagVersion(tag);
179+
180+
let pkgName = '';
181+
182+
// single package
183+
if (tag === `v${version}` || tag === version) {
184+
pkgName = '_';
185+
} else {
186+
const prefix = prefixKeys.find(pre => tag === `${pre}${version}`);
187+
if (prefix) {
188+
pkgName = prefixMap[prefix];
157189
}
158190
}
191+
if (!pkgName) {
192+
return;
193+
}
159194

160195
map[pkgName] = (map[pkgName] || []).concat([
161196
{
162197
name: tag,
163-
version: clearTagVersion(tag),
198+
version,
164199
time: dayjs(times.join(' ')).format('YYYY-MM-DD'),
165200
},
166201
]);
@@ -170,12 +205,7 @@ export async function getGitTags(pkgNames?: string[]) {
170205
if (!record) {
171206
return;
172207
}
173-
if (record.includes('@')) {
174-
const i = record.lastIndexOf('@');
175-
add(record.substring(0, i), record);
176-
} else {
177-
add('_', record);
178-
}
208+
add(record);
179209
});
180210

181211
Object.keys(map).forEach(name => {

0 commit comments

Comments
 (0)