-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-manager.js
More file actions
324 lines (269 loc) Β· 9.87 KB
/
Copy pathversion-manager.js
File metadata and controls
324 lines (269 loc) Β· 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { readFileSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Version management configuration
const VERSION_CONFIG = {
channels: {
core: {
package: 'core-package.json',
tag: 'latest-core',
prerelease: false,
increment: 'minor' // Core gets conservative updates
},
full: {
package: 'full-package.json',
tag: 'latest-full',
prerelease: false,
increment: 'minor'
},
module: {
package: 'module-package.json',
tag: 'latest-module',
prerelease: false,
increment: 'patch' // Module gets frequent small updates
},
experimental: {
package: 'experimental-package.json',
tag: 'experimental',
prerelease: true,
increment: 'patch',
preid: 'experimental'
}
},
semantic: {
core: 'Conservative versioning for stable core features',
full: 'Standard semantic versioning for complete library',
module: 'Frequent patches for ESM optimizations',
experimental: 'Rapid iteration with experimental pre-releases'
}
};
class VersionManager {
constructor() {
this.currentVersion = this.getCurrentVersion();
this.channel = process.argv[2]; // core, full, module, experimental
this.bumpType = process.argv[3]; // major, minor, patch
this.preid = process.argv[4]; // alpha, beta, rc, experimental
}
getCurrentVersion() {
try {
const mainPackage = JSON.parse(readFileSync('package.json', 'utf8'));
return mainPackage.version;
} catch (error) {
console.error('β Could not read current version from package.json');
return '0.1.0';
}
}
parseVersion(version) {
const parts = version.split('-');
const [major, minor, patch] = parts[0].split('.').map(Number);
const prerelease = parts[1] || null;
return { major, minor, patch, prerelease };
}
incrementVersion(currentVersion, bumpType, preid = null) {
const { major, minor, patch } = this.parseVersion(currentVersion);
let newVersion;
switch (bumpType) {
case 'major':
newVersion = { major: major + 1, minor: 0, patch: 0 };
break;
case 'minor':
newVersion = { major, minor: minor + 1, patch: 0 };
break;
case 'patch':
newVersion = { major, minor, patch: patch + 1 };
break;
default:
throw new Error(`Invalid bump type: ${bumpType}`);
}
let versionString = `${newVersion.major}.${newVersion.minor}.${newVersion.patch}`;
if (preid) {
const prereleaseNumber = this.getPrereleaseNumber(currentVersion, preid);
versionString += `-${preid}.${prereleaseNumber}`;
}
return versionString;
}
getPrereleaseNumber(currentVersion, preid) {
const current = this.parseVersion(currentVersion);
if (current.prerelease && current.prerelease.startsWith(preid)) {
const [, number] = current.prerelease.split('.');
return parseInt(number) + 1;
}
return 1;
}
updatePackageVersion(packagePath, newVersion) {
try {
const packageData = JSON.parse(readFileSync(packagePath, 'utf8'));
const oldVersion = packageData.version;
packageData.version = newVersion;
writeFileSync(packagePath, JSON.stringify(packageData, null, 2));
console.log(`π¦ ${packagePath}:`);
console.log(` ${oldVersion} β ${newVersion}`);
return { oldVersion, newVersion };
} catch (error) {
console.error(`β Failed to update ${packagePath}:`, error.message);
throw error;
}
}
createVersionTag(version, channel) {
const tag = `${channel}-${version}`;
console.log(`π·οΈ Creating git tag: ${tag}`);
try {
execSync(`git tag -a ${tag} -m "Release ${VERSION_CONFIG.channels[channel].package} v${version}"`);
console.log(`β
Git tag created: ${tag}`);
} catch (error) {
console.warn(`β οΈ Could not create git tag: ${error.message}`);
}
return tag;
}
publishPackage(packagePath, tag) {
console.log(`π Publishing ${packagePath} with tag: ${tag}`);
try {
// Copy package to temp location and publish
const tempPackage = 'temp-package.json';
const packageData = JSON.parse(readFileSync(packagePath, 'utf8'));
writeFileSync(tempPackage, JSON.stringify(packageData, null, 2));
// Set npm version temporarily
execSync(`npm version ${packageData.version} --no-git-tag-version`, { stdio: 'inherit' });
// Publish with specific tag
execSync(`npm publish --tag ${tag}`, { stdio: 'inherit' });
// Clean up temp file
execSync('rm temp-package.json');
console.log(`β
Published successfully with tag: ${tag}`);
} catch (error) {
console.error(`β Failed to publish: ${error.message}`);
throw error;
}
}
generateChangelog(channel, oldVersion, newVersion) {
const changes = [
`## [${newVersion}] - ${new Date().toISOString().split('T')[0]}`,
'',
`### Channel: ${channel}`,
`### Package: ${VERSION_CONFIG.channels[channel].package}`,
`### Tag: ${VERSION_CONFIG.channels[channel].tag}`,
'',
'### Changed',
`- Updated ${channel} package version from ${oldVersion} to ${newVersion}`,
'',
'### Technical Details',
`- Build target: ${VERSION_CONFIG.channels[channel].incremental}`,
`- SemVer compliance: ${VERSION_CONFIG.channels[channel].prerelease ? 'Pre-release' : 'Stable'}`,
''
];
return changes.join('\n');
}
updateChangelog(channel, oldVersion, newVersion) {
try {
const changelogPath = 'CHANGELOG.md';
let changelog = '';
try {
changelog = readFileSync(changelogPath, 'utf8');
} catch (error) {
changelog = '# Changelog\n\n';
}
const newEntry = this.generateChangelog(channel, oldVersion, newVersion);
const updatedChangelog = newEntry + '\n' + changelog;
writeFileSync(changelogPath, updatedChangelog);
console.log(`π Changelog updated: ${changelogPath}`);
} catch (error) {
console.warn(`β οΈ Could not update changelog: ${error.message}`);
}
}
async versionChannel(channel, bumpType = 'patch', preid = null) {
if (!channel || !VERSION_CONFIG.channels[channel]) {
console.error('β Invalid channel. Available channels:', Object.keys(VERSION_CONFIG.channels));
process.exit(1);
}
console.log(`π§ Versioning ${channel} channel...`);
console.log(`π Current version: ${this.currentVersion}`);
try {
const packagePath = VERSION_CONFIG.channels[channel].package;
const packageData = JSON.parse(readFileSync(packagePath, 'utf8'));
const oldVersion = packageData.version;
let newVersion;
if (preid) {
newVersion = this.incrementVersion(oldVersion, bumpType, preid);
} else {
newVersion = this.incrementVersion(oldVersion, bumpType);
}
// Update package version
const { oldVersion: updatedOldVersion } = this.updatePackageVersion(packagePath, newVersion);
// Create git tag
const tag = this.createVersionTag(newVersion, channel);
// Update changelog
this.updateChangelog(channel, oldVersion, newVersion);
// Publish if requested
if (process.argv.includes('--publish')) {
this.publishPackage(packagePath, VERSION_CONFIG.channels[channel].tag);
}
console.log(`\nπ ${channel} channel versioned successfully!`);
console.log(`π¦ Package: ${VERSION_CONFIG.channels[channel].package}`);
console.log(`π Tag: ${VERSION_CONFIG.channels[channel].tag}`);
console.log(`π·οΈ Version: ${oldVersion} β ${newVersion}`);
return {
channel,
oldVersion,
newVersion,
tag,
package: packagePath
};
} catch (error) {
console.error(`β Versioning failed:`, error.message);
process.exit(1);
}
}
showChannelInfo() {
console.log('π Channel Configuration:');
console.log(JSON.stringify(VERSION_CONFIG, null, 2));
}
showCurrentVersions() {
console.log('π Current Channel Versions:');
Object.entries(VERSION_CONFIG.channels).forEach(([channel, config]) => {
try {
const packageData = JSON.parse(readFileSync(config.package, 'utf8'));
console.log(` ${channel.padEnd(12)}: ${packageData.version} (${config.tag})`);
} catch (error) {
console.log(` ${channel.padEnd(12)}: Not found`);
}
});
}
}
// CLI interface
const manager = new VersionManager();
if (process.argv.includes('--info')) {
manager.showChannelInfo();
} else if (process.argv.includes('--versions')) {
manager.showCurrentVersions();
} else if (manager.channel && manager.bumpType) {
manager.versionChannel(manager.channel, manager.bumpType, manager.preid);
} else {
console.log(`
π 9th.js Version Manager
Usage:
npm run version:channel <channel> <bump-type> [--publish]
npm run version:channels [--publish]
Channels:
core - Minimal core library (conservative updates)
full - Complete library (standard updates)
module - ESM-only distribution (frequent patches)
experimental - Bleeding-edge features (pre-releases)
Bump Types:
major - Breaking changes
minor - New features (backward compatible)
patch - Bug fixes
Pre-release IDs:
alpha, beta, rc, experimental
Examples:
npm run version:channel core patch
npm run version:channel experimental patch alpha --publish
npm run version:channels --publish
npm run version:channel full minor
Options:
--publish - Automatically publish after versioning
--info - Show channel configuration
--versions - Show current versions
`);
}