Skip to content

Commit cff6fa6

Browse files
committed
perf!: config and console in one flow to avoid unnecessary charges
1 parent 03d64a8 commit cff6fa6

File tree

20 files changed

+325
-261
lines changed

20 files changed

+325
-261
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,43 @@ generi.init().then(async () => {
7373
})
7474
```
7575

76-
### generi.json
76+
### Config
77+
78+
#### v1 (generi.json)
79+
80+
```json
81+
{
82+
"silent": false,
83+
"commits": "none",
84+
"tag": true,
85+
"version": true,
86+
"push": false,
87+
"publish": false,
88+
"release": false,
89+
"exclude": [" typo"],
90+
"prerelease": "beta",
91+
"packagePath": "package.json",
92+
"lernaPath": "lerna.json"
93+
}
94+
```
95+
96+
#### v2 (generi.config.ts)
97+
98+
```ts
99+
export default {
100+
silent: false,
101+
commits: "none",
102+
tag: true,
103+
version: true,
104+
push: false,
105+
publish: false,
106+
release: false,
107+
exclude: [" typo"],
108+
prerelease: "beta",
109+
packagePath: "package.json",
110+
lernaPath: "lerna.json"
111+
};
112+
```
77113

78114
##### `silent` Default: `false`
79115

generi.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
silent: false,
3+
commits: "none",
4+
tag: true,
5+
version: true,
6+
push: false,
7+
publish: false,
8+
release: false,
9+
exclude: [" typo"],
10+
prerelease: "beta",
11+
packagePath: "package.json",
12+
lernaPath: "lerna.json"
13+
};

generi.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"dependencies": {
5151
"bumpp": "^10.3.1",
52+
"c12": "^3.3.1",
5253
"consola": "^3.4.2",
5354
"destr": "^2.0.5",
5455
"execa": "^5.1.1",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/changelog.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { success } from './console';
21
import { commits, isTagCommit, setCommit, getTagCommit } from './git';
3-
import { Commit, GeneriEmoticon } from './types';
2+
import { Commit, GeneriEmoticon, GeneriOptions, Root } from './types';
43
import { setChangelog } from './utils';
5-
import { getGeneriConfig } from './generi';
64
import conventional from './defines/conventional-commits.json';
75

86
const getEmoji = (str: string): string => {
@@ -38,11 +36,10 @@ const setSubHeader = (commit: Commit) => {
3836
return '\n### ' + v + '\n\n';
3937
};
4038

41-
const setBasic = (commit: Commit) => {
42-
const generi = getGeneriConfig();
39+
const setBasic = (commit: Commit, config: GeneriOptions) => {
4340
let result: string[] | string;
4441

45-
if (generi.commits === 'conventional-commits') {
42+
if (config.commits === 'conventional-commits') {
4643
result = commit.summary
4744
.split(/:(.+)/)
4845
.filter((part) => part)
@@ -51,11 +48,11 @@ const setBasic = (commit: Commit) => {
5148
result = commit.summary;
5249
}
5350

54-
const sha = generi?.repository
55-
? ` - [[${commit.sha}](${generi.repository}/commit/${commit.sha})]`
51+
const sha = config?.repository
52+
? ` - [[${commit.sha}](${config.repository}/commit/${commit.sha})]`
5653
: '';
5754

58-
if (generi.commits === 'conventional-commits') {
55+
if (config.commits === 'conventional-commits') {
5956
if (!result[0] || !result[1]) return '';
6057

6158
return (
@@ -87,8 +84,7 @@ export const setActuallyTag = (tag: string) => {
8784
});
8885
};
8986

90-
export const createChangelog = (tag: string) => {
91-
const config = getGeneriConfig();
87+
export const createChangelog = (tag: string, { config, console }: Root) => {
9288
const exclude = config?.exclude ?? [' typo'];
9389

9490
let changelog = getChangelogHeader();
@@ -105,12 +101,12 @@ export const createChangelog = (tag: string) => {
105101
if (!isConventionalCommit(commit) && config.commits === 'conventional-commits')
106102
return;
107103

108-
changelog += setBasic(commit);
104+
changelog += setBasic(commit, config);
109105
});
110106

111107
setChangelog(changelog);
112108

113-
setCommit(config.tag ? tag : 'chore(generi): generate changelog.md');
109+
setCommit(config.tag ? tag : 'chore(generi): generate changelog.md', true, console);
114110

115-
success('Generate CHANGELOG.md');
111+
console.success('Generate CHANGELOG.md');
116112
};

src/commands/init.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
1-
import { getHeader, error } from '../console';
1+
import * as log from './log';
22
import { isGit, setVersion, setTag, initGit, setCommit, existsTag } from '../git';
33
import { createChangelog } from '../changelog';
4-
import { setGeneriConfig, getGeneriConfig, pkgConfig, lernaConfig } from '../generi';
5-
import * as log from './log';
6-
import generiDefault from '../defines/generi-default.json';
7-
import { getVersion, isChangesForCommit } from '../utils';
4+
import { getGeneri } from '../generi';
5+
import { exists, getVersion, isChangesForCommit } from '../utils';
86

97
export const setup = () => {
10-
const git = isGit();
11-
let version = getVersion();
8+
getGeneri().then(({ config, console }) => {
9+
const git = isGit();
10+
let version = getVersion(config);
1211

13-
getHeader('generi init');
12+
console.header('generi init');
1413

15-
if (getGeneriConfig()) error('<generi.json> exists!');
14+
if (exists('./generi.json'))
15+
console.warning(
16+
'generi.json file is not supported in v2. New format in documentation.'
17+
);
1618

17-
if (!git) initGit();
19+
if (!exists('./generi.config.ts') || !exists('./generi.config.js'))
20+
console.warning('generi.config was not found, default config loaded.');
1821

19-
isChangesForCommit(git);
22+
if (!git) initGit(console);
2023

21-
// @ts-expect-error
22-
setGeneriConfig(generiDefault);
24+
isChangesForCommit(git, console);
2325

24-
if (!existsTag()) {
25-
if (version) setTag(version);
26-
else {
27-
version = 'v0.1.0';
28-
setVersion(version, 'patch');
26+
if (!existsTag()) {
27+
if (version) setTag(version, console);
28+
else {
29+
version = 'v0.1.0';
30+
setVersion(version, { config, console }, 'patch');
31+
}
2932
}
30-
}
3133

32-
if (git) {
33-
setCommit('chore: generate generi.json');
34+
if (git) {
35+
setCommit('chore: generate generi.json', true, console);
3436

35-
log.setup('patch', { header: true, git: { prerelease: undefined } });
37+
log.setup('patch', { header: true, git: { prerelease: undefined } });
3638

37-
return;
38-
}
39+
return;
40+
}
3941

40-
if (!version) {
41-
error(`${pkgConfig} or ${lernaConfig} it was not found.`);
42+
if (!version) {
43+
console.error(`${config.packagePath} or ${config.lernaPath} it was not found.`);
4244

43-
return;
44-
}
45+
return;
46+
}
4547

46-
setVersion(version, 'minor');
48+
setVersion(version, { config, console }, 'minor');
4749

48-
createChangelog(version);
50+
createChangelog(version, { config, console });
4951

50-
setTag(version);
52+
setTag(version, console);
53+
});
5154
};

src/commands/log.ts

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,98 @@
11
import { createChangelog } from '../changelog';
2-
import { GitNewTag, LogOptions } from '../types';
2+
import type { GeneriConsole, GitNewTag, GitPrerelease, LogOptions } from '../types';
33
import { lastTag, setVersion, setTag, newCommits, isValidTag, pushCommits } from '../git';
4-
import { success, error, getHeader } from '../console';
5-
import {
6-
existsConfig,
7-
getFile,
8-
getLernaRoot,
9-
getPackageRoot,
10-
isPrerelease,
11-
} from '../utils';
12-
import { getGeneriConfig } from '../generi';
4+
import { exists, existsConfig, getFile, getFileRoot, isPrerelease } from '../utils';
5+
import { getGeneri } from '../generi';
136
import { publish } from '../npm';
147
import { nextTag } from '../tag';
158
import { release } from '../release';
169
import { destr } from 'destr';
1710

18-
const validateLog = (tag: GitNewTag) => {
11+
const validateLog = (tag: GitNewTag, console: GeneriConsole) => {
1912
const commits = newCommits();
2013

2114
if (!existsConfig()) {
22-
error('Generi not exists! Use <generi init> command instead.');
23-
return false;
15+
console.error('Generi not exists! Use <generi init> command instead.');
2416
}
2517

2618
if (commits.length < 1) {
27-
error('There are no valid commits to create a new release.');
28-
return false;
19+
console.error('There are no valid commits to create a new release.');
2920
}
3021

3122
if (!isValidTag(tag)) {
32-
error('Invalid Tag. Use patch|prepatch|minor|preminor|major|premajor');
33-
return false;
23+
console.error('Invalid Tag. Use patch|prepatch|minor|preminor|major|premajor');
3424
}
3525

3626
return true;
3727
};
3828

3929
export const setup = (tag: GitNewTag, options: LogOptions) => {
40-
const config = getGeneriConfig();
30+
getGeneri().then(({ config, console }) => {
31+
if (!tag) {
32+
console.error('Insert valid git tag.');
4133

42-
if (!tag) {
43-
error('Insert valid git tag.');
34+
return;
35+
}
4436

45-
return;
46-
}
37+
if (options.header) console.header(`generi log ${tag}`);
4738

48-
if (options.header) getHeader(`generi log ${tag}`);
39+
if (!validateLog(tag, console)) return;
4940

50-
if (!validateLog(tag)) return;
41+
if (exists('./generi.json'))
42+
console.warning(
43+
'generi.json file is not supported in v2. New format in documentation.'
44+
);
5145

52-
const lerna = getFile(getLernaRoot());
53-
const pkg = getFile(getPackageRoot());
46+
if (!exists('./generi.config.ts') || !exists('./generi.config.js'))
47+
console.warning('generi.config was not found, default config loaded.');
5448

55-
if (!lerna && !pkg) {
56-
error(
57-
'No configuration file was found. If you use a different path for <lerna.json> or <package.json>, look in the documentation on our github for the packagePath or lernaPath option.'
58-
);
59-
}
49+
const lerna = getFile(getFileRoot(config.lernaPath));
50+
const pkg = getFile(getFileRoot(config.packagePath));
6051

61-
let target = lerna ? lerna : pkg;
52+
if (!lerna && !pkg) {
53+
console.error(
54+
'No configuration file was found. If you use a different path for <lerna.json> or <package.json>, look in the documentation on our github for the packagePath or lernaPath option.'
55+
);
56+
}
6257

63-
const version = destr<Record<string, any>>(target)?.version;
64-
if (!version) error('version in lerna.json or package.json not found!');
65-
const last = 'v' + version;
58+
let target = lerna ? lerna : pkg;
6659

67-
const prerelease = isPrerelease(tag)
68-
? (options?.git?.prerelease ?? config.prerelease ?? 'beta')
69-
: undefined;
60+
const version = destr<Record<string, any>>(target)?.version;
61+
if (!version) console.error('version in lerna.json or package.json not found!');
62+
const last = 'v' + version;
7063

71-
const next = nextTag({
72-
last,
73-
tag,
74-
prerelease,
75-
});
64+
const prerelease = isPrerelease(tag)
65+
? ((options?.git?.prerelease ?? config.prerelease ?? 'beta') as GitPrerelease)
66+
: undefined;
7667

77-
if (!next) {
78-
error('Unable to create a required tag!');
79-
return;
80-
}
68+
const next = nextTag(
69+
{
70+
last,
71+
tag,
72+
prerelease,
73+
},
74+
console
75+
);
8176

82-
if (config.version) {
83-
success(`${last} to ${next} (${tag.toUpperCase()})`);
77+
if (!next) {
78+
console.error('Unable to create a required tag!');
79+
return;
80+
}
8481

85-
setVersion(next, tag, prerelease);
86-
}
82+
if (config.version) {
83+
console.success(`${last} to ${next} (${tag.toUpperCase()})`);
8784

88-
createChangelog(!config.version ? lastTag() : next);
85+
setVersion(next, { console, config }, tag, prerelease);
86+
}
8987

90-
if (config.tag && (!options.init || !lerna)) setTag(next);
88+
createChangelog(!config.version ? lastTag() : next, { config, console });
9189

92-
pushCommits();
90+
if (config.tag && (!options.init || !lerna)) setTag(next, console);
9391

94-
if (config?.release) release(next, true);
92+
pushCommits({ config, console });
9593

96-
if (config?.publish) publish(next, lerna, next);
94+
if (config?.release) release(next, true, console);
95+
96+
if (config?.publish) publish(next, console, lerna, next);
97+
});
9798
};

0 commit comments

Comments
 (0)