-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync-version.js
More file actions
184 lines (160 loc) · 5.29 KB
/
Copy pathsync-version.js
File metadata and controls
184 lines (160 loc) · 5.29 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
#!/usr/bin/env node
/**
* Sync version from package.json to src/index.ts and package-lock.json.
* package.json is the single source of truth for releases.
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-.]+)?(?:\+[0-9A-Za-z-.]+)?$/;
/**
* Extract version from package.json
* @param {string} filePath - Path to package.json
* @returns {string | null} Version string or null if not found
*/
export function extractVersionFromFile(filePath) {
try {
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
return typeof json.version === 'string' ? json.version : null;
} catch (error) {
throw new Error(`Failed to read file: ${filePath}\n${error.message}`);
}
}
/**
* Validate release version against SemVer
* @param {string} version - Version to validate
*/
export function validateVersion(version) {
if (!SEMVER_PATTERN.test(version)) {
throw new Error(`Invalid SemVer version: ${version}`);
}
}
/**
* Update VERSION constant in src/index.ts
* @param {string} filePath - Path to source file
* @param {string} newVersion - New version to set
* @returns {boolean} True if file was updated, false if already at correct version
*/
export function updateSourceVersion(filePath, newVersion) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const pattern = /const VERSION = ["']([^"']+)["'];/;
const match = content.match(pattern);
if (!match) {
throw new Error('Could not find VERSION constant in source file');
}
if (match[1] === newVersion) {
return false;
}
const updatedContent = content.replace(pattern, `const VERSION = "${newVersion}";`);
fs.writeFileSync(filePath, updatedContent, 'utf8');
return true;
} catch (error) {
throw new Error(`Failed to update source version: ${filePath}\n${error.message}`);
}
}
/**
* Update a JSON file's version field
* @param {string} filePath - Path to JSON file
* @param {string} newVersion - New version to set
* @param {string} versionKey - Key to update (default: 'version')
* @returns {boolean} True if file was updated, false if already at correct version
*/
export function updateJsonFile(filePath, newVersion, versionKey = 'version') {
try {
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
if (json[versionKey] === newVersion) {
return false;
}
json[versionKey] = newVersion;
fs.writeFileSync(filePath, JSON.stringify(json, null, 2) + '\n', 'utf8');
return true;
} catch (error) {
throw new Error(`Failed to update JSON file: ${filePath}\n${error.message}`);
}
}
/**
* Update package-lock.json version (both root and packages[""])
* @param {string} filePath - Path to package-lock.json
* @param {string} newVersion - New version to set
* @returns {boolean} True if file was updated, false if already at correct version
*/
export function updatePackageLockVersion(filePath, newVersion) {
try {
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
let updated = false;
// Update root version
if (json.version !== newVersion) {
json.version = newVersion;
updated = true;
}
// Update packages[""] version
if (json.packages?.['']?.version !== newVersion) {
if (!json.packages) json.packages = {};
if (!json.packages['']) json.packages[''] = {};
json.packages[''].version = newVersion;
updated = true;
}
if (updated) {
fs.writeFileSync(filePath, JSON.stringify(json, null, 2) + '\n', 'utf8');
}
return updated;
} catch (error) {
throw new Error(`Failed to update package-lock.json: ${filePath}\n${error.message}`);
}
}
/**
* Main sync function
*/
function main() {
const indexPath = path.join(__dirname, 'src', 'index.ts');
const packagePath = path.join(__dirname, 'package.json');
const lockPath = path.join(__dirname, 'package-lock.json');
// Extract version from package.json
const version = extractVersionFromFile(packagePath);
if (!version) {
console.error('❌ Could not find version in package.json');
process.exit(1);
}
try {
validateVersion(version);
} catch (error) {
console.error(`❌ ${error.message}`);
process.exit(1);
}
// Update src/index.ts
try {
const sourceUpdated = updateSourceVersion(indexPath, version);
if (sourceUpdated) {
console.log(`✅ Updated src/index.ts to v${version}`);
} else {
console.log(`✅ src/index.ts already at v${version}`);
}
} catch (error) {
console.error(`❌ ${error.message}`);
process.exit(1);
}
// Update package-lock.json
try {
const lockUpdated = updatePackageLockVersion(lockPath, version);
if (lockUpdated) {
console.log(`✅ Updated package-lock.json to v${version}`);
} else {
console.log(`✅ package-lock.json already at v${version}`);
}
} catch (error) {
console.error(`❌ ${error.message}`);
process.exit(1);
}
// Exit with standard codes: 0 = success, 1 = error
process.exit(0);
}
// Run main if executed directly
if (import.meta.url.startsWith('file:')) {
const modulePath = fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
main();
}
}