@programinglive/commiter 1.2.12 → 1.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,258 +1,258 @@
1
- #!/usr/bin/env node
2
-
3
- const { spawnSync } = require('child_process');
4
- const fs = require('fs');
5
- const path = require('path');
6
-
7
- const { updateReleaseNotes } = require('./update-release-notes.cjs');
8
-
9
- const fsFokPreloadPath = path.join(__dirname, 'preload', 'fs-f-ok.cjs');
10
- const FS_FOK_PRELOAD_FLAG = buildPreloadFlag(fsFokPreloadPath);
11
-
12
- require('./preload/fs-f-ok.cjs');
13
-
14
- const VALID_RELEASE_TYPES = new Set([
15
- 'major',
16
- 'minor',
17
- 'patch',
18
- 'premajor',
19
- 'preminor',
20
- 'prepatch',
21
- 'prerelease'
22
- ]);
23
-
24
- const SEMVER_REGEX = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
25
-
26
- const LOCKFILE_PREFERENCES = [
27
- { manager: 'pnpm', file: 'pnpm-lock.yaml' },
28
- { manager: 'yarn', file: 'yarn.lock' },
29
- { manager: 'bun', file: 'bun.lock' },
30
- { manager: 'npm', file: 'package-lock.json' }
31
- ];
32
-
33
- function getCliArguments(argv = process.argv) {
34
- const rawArgs = argv.slice(2);
35
- if (rawArgs.length === 0) {
36
- return { releaseType: 'patch', extraArgs: [] };
37
- }
38
-
39
- if (rawArgs[0].startsWith('-')) {
40
- return { releaseType: 'patch', extraArgs: rawArgs };
41
- }
42
-
43
- const [firstArg, ...rest] = rawArgs;
44
- return { releaseType: firstArg, extraArgs: rest };
45
- }
46
-
47
- function getNpmRunArgument(env = process.env) {
48
- try {
49
- const npmArgs = JSON.parse(env.npm_config_argv || '{}');
50
- const original = npmArgs.original || [];
51
- const releaseIndex = original.lastIndexOf('release');
52
- if (releaseIndex !== -1) {
53
- return original[releaseIndex + 1];
54
- }
55
- } catch (error) {
56
- // Ignore JSON parsing issues and fall back to defaults
57
- }
58
- return undefined;
59
- }
60
-
61
- function buildStandardVersionArgs({ releaseType, extraArgs }) {
62
- const args = [];
63
-
64
- // Handle --first-release flag
65
- const isFirstRelease = Array.isArray(extraArgs) && extraArgs.includes('--first-release');
66
- if (isFirstRelease) {
67
- args.push('--release-as', '0.0.1');
68
- // Remove --first-release from extraArgs to avoid passing it to standard-version
69
- extraArgs = extraArgs.filter(arg => arg !== '--first-release');
70
- } else if (releaseType) {
71
- const normalized = releaseType.trim();
72
- const isValid = VALID_RELEASE_TYPES.has(normalized) || SEMVER_REGEX.test(normalized);
73
- if (!isValid) {
74
- const allowed = Array.from(VALID_RELEASE_TYPES).join(', ');
75
- throw new Error(`Unknown release type "${normalized}". Use one of ${allowed} or a valid semver version.`);
76
- }
77
- args.push('--release-as', normalized);
78
- }
79
-
80
- if (Array.isArray(extraArgs) && extraArgs.length > 0) {
81
- args.push(...extraArgs);
82
- }
83
-
84
- return args;
85
- }
86
-
87
- function loadPackageJson(cwd = process.cwd()) {
88
- try {
89
- const packageJsonPath = path.join(cwd, 'package.json');
90
- const contents = fs.readFileSync(packageJsonPath, 'utf8');
91
- return JSON.parse(contents);
92
- } catch (error) {
93
- return null;
94
- }
95
- }
96
-
97
- function detectPackageManager({ env = process.env, cwd = process.cwd() } = {}) {
98
- const userAgent = env.npm_config_user_agent || '';
99
- if (userAgent.startsWith('pnpm/')) return 'pnpm';
100
- if (userAgent.startsWith('yarn/')) return 'yarn';
101
- if (userAgent.startsWith('bun/')) return 'bun';
102
-
103
- for (const { manager, file } of LOCKFILE_PREFERENCES) {
104
- if (fs.existsSync(path.join(cwd, file))) {
105
- return manager;
106
- }
107
- }
108
-
109
- return 'npm';
110
- }
111
-
112
- function buildTestCommand(packageManager) {
113
- switch (packageManager) {
114
- case 'pnpm':
115
- return { command: 'pnpm', args: ['test'] };
116
- case 'yarn':
117
- return { command: 'yarn', args: ['test'] };
118
- case 'bun':
119
- return { command: 'bun', args: ['test'] };
120
- default:
121
- return { command: 'npm', args: ['test'] };
122
- }
123
- }
124
-
125
- function runProjectTests({ spawn = spawnSync, env = process.env, cwd = process.cwd() } = {}) {
126
- const packageJson = loadPackageJson(cwd);
127
- const scripts = packageJson && packageJson.scripts ? packageJson.scripts : {};
128
-
129
- if (!scripts.test) {
130
- console.log('ℹ️ Skipping tests: no "test" script detected in package.json');
131
- return { status: 0 };
132
- }
133
-
134
- const packageManager = detectPackageManager({ env, cwd });
135
- const { command, args } = buildTestCommand(packageManager);
136
-
137
- console.log(`🧪 Running tests with ${packageManager} ${args.join(' ')}`.trim());
138
- return spawn(command, args, {
139
- stdio: 'inherit',
140
- env
141
- });
142
- }
143
-
144
- function runRelease({
145
- argv = process.argv,
146
- env = process.env,
147
- spawn = spawnSync,
148
- cwd = process.cwd(),
149
- dependencies = {}
150
- } = {}) {
151
- const { releaseType: cliReleaseType, extraArgs } = getCliArguments(argv);
152
- const inferredReleaseType = cliReleaseType || getNpmRunArgument(env);
153
- const standardVersionArgs = buildStandardVersionArgs({
154
- releaseType: inferredReleaseType,
155
- extraArgs
156
- });
157
-
158
- const checkWorkingTreeClean = dependencies.isWorkingTreeClean || (() => isWorkingTreeClean({ cwd }));
159
- const workingTreeClean = checkWorkingTreeClean();
160
- if (!workingTreeClean) {
161
- throw new Error('Working tree has uncommitted changes. Commit or stash before running release.');
162
- }
163
-
164
- const testResult = runProjectTests({ spawn, env });
165
- if (testResult && typeof testResult.status === 'number' && testResult.status !== 0) {
166
- return testResult;
167
- }
168
-
169
- const standardVersionBin = require.resolve('standard-version/bin/cli.js');
170
- const childEnv = appendPreloadToNodeOptions(env, FS_FOK_PRELOAD_FLAG);
171
- const releaseResult = spawn(process.execPath, [standardVersionBin, ...standardVersionArgs], {
172
- stdio: 'inherit',
173
- env: childEnv,
174
- cwd
175
- });
176
-
177
- if (releaseResult && typeof releaseResult.status === 'number' && releaseResult.status !== 0) {
178
- return releaseResult;
179
- }
180
-
181
- const releaseNotesPath = dependencies.releaseNotesPath || path.join('docs', 'release-notes', 'RELEASE_NOTES.md');
182
- const updateNotes = dependencies.updateReleaseNotes || ((options = {}) => updateReleaseNotes({ rootDir: cwd, ...options }));
183
-
184
- let notesUpdated = false;
185
- try {
186
- notesUpdated = updateNotes();
187
- } catch (error) {
188
- console.warn(`⚠️ Skipping release notes update: ${error.message}`);
189
- }
190
-
191
- if (notesUpdated) {
192
- const gitAddResult = spawnSync('git', ['add', releaseNotesPath], { stdio: 'inherit', cwd });
193
- if (!gitAddResult || typeof gitAddResult.status !== 'number' || gitAddResult.status !== 0) {
194
- console.warn('⚠️ Release notes updated but failed to stage. Please add manually: git add ' + releaseNotesPath);
195
- }
196
- }
197
-
198
- return releaseResult;
199
- }
200
- if (require.main === module) {
201
- try {
202
- const result = runRelease();
203
- if (result.status !== 0) {
204
- process.exit(result.status ?? 1);
205
- }
206
- } catch (error) {
207
- console.error(`❌ ${error.message}`);
208
- process.exit(1);
209
- }
210
- }
211
-
212
- module.exports = {
213
- VALID_RELEASE_TYPES,
214
- SEMVER_REGEX,
215
- getCliArguments,
216
- getNpmRunArgument,
217
- buildStandardVersionArgs,
218
- loadPackageJson,
219
- detectPackageManager,
220
- runProjectTests,
221
- runRelease,
222
- isWorkingTreeClean
223
- };
224
-
225
- function buildPreloadFlag(filePath) {
226
- const resolved = path.resolve(filePath);
227
- const escaped = resolved.includes(' ')
228
- ? `"${resolved.replace(/"/g, '\"')}"`
229
- : resolved;
230
- return `--require ${escaped}`;
231
- }
232
-
233
- function appendPreloadToNodeOptions(env, preloadFlag) {
234
- const nextEnv = { ...env };
235
- const existing = typeof nextEnv.NODE_OPTIONS === 'string' ? nextEnv.NODE_OPTIONS.trim() : '';
236
- nextEnv.NODE_OPTIONS = existing ? `${existing} ${preloadFlag}` : preloadFlag;
237
- return nextEnv;
238
- }
239
-
240
- function isWorkingTreeClean({ cwd = process.cwd() } = {}) {
241
- const result = spawnSync('git', ['status', '--porcelain'], {
242
- cwd,
243
- encoding: 'utf8'
244
- });
245
-
246
- if (result.error) {
247
- throw result.error;
248
- }
249
-
250
- if (typeof result.status === 'number' && result.status !== 0) {
251
- const stderr = typeof result.stderr === 'string' ? result.stderr.trim() : '';
252
- const message = stderr || 'Failed to determine working tree status.';
253
- throw new Error(message);
254
- }
255
-
256
- const output = typeof result.stdout === 'string' ? result.stdout : '';
257
- return output.trim().length === 0;
258
- }
1
+ #!/usr/bin/env node
2
+
3
+ const { spawnSync } = require('child_process');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+
7
+ const { updateReleaseNotes } = require('./update-release-notes.cjs');
8
+
9
+ const fsFokPreloadPath = path.join(__dirname, 'preload', 'fs-f-ok.cjs');
10
+ const FS_FOK_PRELOAD_FLAG = buildPreloadFlag(fsFokPreloadPath);
11
+
12
+ require('./preload/fs-f-ok.cjs');
13
+
14
+ const VALID_RELEASE_TYPES = new Set([
15
+ 'major',
16
+ 'minor',
17
+ 'patch',
18
+ 'premajor',
19
+ 'preminor',
20
+ 'prepatch',
21
+ 'prerelease'
22
+ ]);
23
+
24
+ const SEMVER_REGEX = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
25
+
26
+ const LOCKFILE_PREFERENCES = [
27
+ { manager: 'pnpm', file: 'pnpm-lock.yaml' },
28
+ { manager: 'yarn', file: 'yarn.lock' },
29
+ { manager: 'bun', file: 'bun.lock' },
30
+ { manager: 'npm', file: 'package-lock.json' }
31
+ ];
32
+
33
+ function getCliArguments(argv = process.argv) {
34
+ const rawArgs = argv.slice(2);
35
+ if (rawArgs.length === 0) {
36
+ return { releaseType: 'patch', extraArgs: [] };
37
+ }
38
+
39
+ if (rawArgs[0].startsWith('-')) {
40
+ return { releaseType: 'patch', extraArgs: rawArgs };
41
+ }
42
+
43
+ const [firstArg, ...rest] = rawArgs;
44
+ return { releaseType: firstArg, extraArgs: rest };
45
+ }
46
+
47
+ function getNpmRunArgument(env = process.env) {
48
+ try {
49
+ const npmArgs = JSON.parse(env.npm_config_argv || '{}');
50
+ const original = npmArgs.original || [];
51
+ const releaseIndex = original.lastIndexOf('release');
52
+ if (releaseIndex !== -1) {
53
+ return original[releaseIndex + 1];
54
+ }
55
+ } catch (error) {
56
+ // Ignore JSON parsing issues and fall back to defaults
57
+ }
58
+ return undefined;
59
+ }
60
+
61
+ function buildStandardVersionArgs({ releaseType, extraArgs }) {
62
+ const args = [];
63
+
64
+ // Handle --first-release flag
65
+ const isFirstRelease = Array.isArray(extraArgs) && extraArgs.includes('--first-release');
66
+ if (isFirstRelease) {
67
+ args.push('--release-as', '0.0.1');
68
+ // Remove --first-release from extraArgs to avoid passing it to standard-version
69
+ extraArgs = extraArgs.filter(arg => arg !== '--first-release');
70
+ } else if (releaseType) {
71
+ const normalized = releaseType.trim();
72
+ const isValid = VALID_RELEASE_TYPES.has(normalized) || SEMVER_REGEX.test(normalized);
73
+ if (!isValid) {
74
+ const allowed = Array.from(VALID_RELEASE_TYPES).join(', ');
75
+ throw new Error(`Unknown release type "${normalized}". Use one of ${allowed} or a valid semver version.`);
76
+ }
77
+ args.push('--release-as', normalized);
78
+ }
79
+
80
+ if (Array.isArray(extraArgs) && extraArgs.length > 0) {
81
+ args.push(...extraArgs);
82
+ }
83
+
84
+ return args;
85
+ }
86
+
87
+ function loadPackageJson(cwd = process.cwd()) {
88
+ try {
89
+ const packageJsonPath = path.join(cwd, 'package.json');
90
+ const contents = fs.readFileSync(packageJsonPath, 'utf8');
91
+ return JSON.parse(contents);
92
+ } catch (error) {
93
+ return null;
94
+ }
95
+ }
96
+
97
+ function detectPackageManager({ env = process.env, cwd = process.cwd() } = {}) {
98
+ const userAgent = env.npm_config_user_agent || '';
99
+ if (userAgent.startsWith('pnpm/')) return 'pnpm';
100
+ if (userAgent.startsWith('yarn/')) return 'yarn';
101
+ if (userAgent.startsWith('bun/')) return 'bun';
102
+
103
+ for (const { manager, file } of LOCKFILE_PREFERENCES) {
104
+ if (fs.existsSync(path.join(cwd, file))) {
105
+ return manager;
106
+ }
107
+ }
108
+
109
+ return 'npm';
110
+ }
111
+
112
+ function buildTestCommand(packageManager) {
113
+ switch (packageManager) {
114
+ case 'pnpm':
115
+ return { command: 'pnpm', args: ['test'] };
116
+ case 'yarn':
117
+ return { command: 'yarn', args: ['test'] };
118
+ case 'bun':
119
+ return { command: 'bun', args: ['test'] };
120
+ default:
121
+ return { command: 'npm', args: ['test'] };
122
+ }
123
+ }
124
+
125
+ function runProjectTests({ spawn = spawnSync, env = process.env, cwd = process.cwd() } = {}) {
126
+ const packageJson = loadPackageJson(cwd);
127
+ const scripts = packageJson && packageJson.scripts ? packageJson.scripts : {};
128
+
129
+ if (!scripts.test) {
130
+ console.log('ℹ️ Skipping tests: no "test" script detected in package.json');
131
+ return { status: 0 };
132
+ }
133
+
134
+ const packageManager = detectPackageManager({ env, cwd });
135
+ const { command, args } = buildTestCommand(packageManager);
136
+
137
+ console.log(`🧪 Running tests with ${packageManager} ${args.join(' ')}`.trim());
138
+ return spawn(command, args, {
139
+ stdio: 'inherit',
140
+ env
141
+ });
142
+ }
143
+
144
+ function runRelease({
145
+ argv = process.argv,
146
+ env = process.env,
147
+ spawn = spawnSync,
148
+ cwd = process.cwd(),
149
+ dependencies = {}
150
+ } = {}) {
151
+ const { releaseType: cliReleaseType, extraArgs } = getCliArguments(argv);
152
+ const inferredReleaseType = cliReleaseType || getNpmRunArgument(env);
153
+ const standardVersionArgs = buildStandardVersionArgs({
154
+ releaseType: inferredReleaseType,
155
+ extraArgs
156
+ });
157
+
158
+ const checkWorkingTreeClean = dependencies.isWorkingTreeClean || (() => isWorkingTreeClean({ cwd }));
159
+ const workingTreeClean = checkWorkingTreeClean();
160
+ if (!workingTreeClean) {
161
+ throw new Error('Working tree has uncommitted changes. Commit or stash before running release.');
162
+ }
163
+
164
+ const testResult = runProjectTests({ spawn, env });
165
+ if (testResult && typeof testResult.status === 'number' && testResult.status !== 0) {
166
+ return testResult;
167
+ }
168
+
169
+ const standardVersionBin = require.resolve('standard-version/bin/cli.js');
170
+ const childEnv = appendPreloadToNodeOptions(env, FS_FOK_PRELOAD_FLAG);
171
+ const releaseResult = spawn(process.execPath, [standardVersionBin, ...standardVersionArgs], {
172
+ stdio: 'inherit',
173
+ env: childEnv,
174
+ cwd
175
+ });
176
+
177
+ if (releaseResult && typeof releaseResult.status === 'number' && releaseResult.status !== 0) {
178
+ return releaseResult;
179
+ }
180
+
181
+ const releaseNotesPath = dependencies.releaseNotesPath || path.join('docs', 'release-notes', 'RELEASE_NOTES.md');
182
+ const updateNotes = dependencies.updateReleaseNotes || ((options = {}) => updateReleaseNotes({ rootDir: cwd, ...options }));
183
+
184
+ let notesUpdated = false;
185
+ try {
186
+ notesUpdated = updateNotes();
187
+ } catch (error) {
188
+ console.warn(`⚠️ Skipping release notes update: ${error.message}`);
189
+ }
190
+
191
+ if (notesUpdated) {
192
+ const gitAddResult = spawnSync('git', ['add', releaseNotesPath], { stdio: 'inherit', cwd });
193
+ if (!gitAddResult || typeof gitAddResult.status !== 'number' || gitAddResult.status !== 0) {
194
+ console.warn('⚠️ Release notes updated but failed to stage. Please add manually: git add ' + releaseNotesPath);
195
+ }
196
+ }
197
+
198
+ return releaseResult;
199
+ }
200
+ if (require.main === module) {
201
+ try {
202
+ const result = runRelease();
203
+ if (result.status !== 0) {
204
+ process.exit(result.status ?? 1);
205
+ }
206
+ } catch (error) {
207
+ console.error(`❌ ${error.message}`);
208
+ process.exit(1);
209
+ }
210
+ }
211
+
212
+ module.exports = {
213
+ VALID_RELEASE_TYPES,
214
+ SEMVER_REGEX,
215
+ getCliArguments,
216
+ getNpmRunArgument,
217
+ buildStandardVersionArgs,
218
+ loadPackageJson,
219
+ detectPackageManager,
220
+ runProjectTests,
221
+ runRelease,
222
+ isWorkingTreeClean
223
+ };
224
+
225
+ function buildPreloadFlag(filePath) {
226
+ const resolved = path.resolve(filePath);
227
+ const escaped = resolved.includes(' ')
228
+ ? `"${resolved.replace(/"/g, '\"')}"`
229
+ : resolved;
230
+ return `--require ${escaped}`;
231
+ }
232
+
233
+ function appendPreloadToNodeOptions(env, preloadFlag) {
234
+ const nextEnv = { ...env };
235
+ const existing = typeof nextEnv.NODE_OPTIONS === 'string' ? nextEnv.NODE_OPTIONS.trim() : '';
236
+ nextEnv.NODE_OPTIONS = existing ? `${existing} ${preloadFlag}` : preloadFlag;
237
+ return nextEnv;
238
+ }
239
+
240
+ function isWorkingTreeClean({ cwd = process.cwd() } = {}) {
241
+ const result = spawnSync('git', ['status', '--porcelain'], {
242
+ cwd,
243
+ encoding: 'utf8'
244
+ });
245
+
246
+ if (result.error) {
247
+ throw result.error;
248
+ }
249
+
250
+ if (typeof result.status === 'number' && result.status !== 0) {
251
+ const stderr = typeof result.stderr === 'string' ? result.stderr.trim() : '';
252
+ const message = stderr || 'Failed to determine working tree status.';
253
+ throw new Error(message);
254
+ }
255
+
256
+ const output = typeof result.stdout === 'string' ? result.stdout : '';
257
+ return output.trim().length === 0;
258
+ }