-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublish.js
More file actions
51 lines (42 loc) · 1.79 KB
/
Copy pathpublish.js
File metadata and controls
51 lines (42 loc) · 1.79 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
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
const tag = process.argv[2];
const __dirname = dirname(fileURLToPath(import.meta.url));
const packageJsonPath = resolve(__dirname, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const originalVersion = packageJson.version;
const bumpedVersion = originalVersion.split('.').map((part, index) => {
if (index === 2) {
return parseInt(part, 10) + 1;
}
return parseInt(part, 10);
}
).join('.');
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const timestamp = `${year}${month}${day}${hours}${minutes}.${seconds}`;
const newVersion = `${bumpedVersion}-${tag}.${timestamp}`;
packageJson.version = newVersion;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Updated version to ${newVersion}`);
try {
execSync("npm run build", { stdio: 'inherit' });
execSync(`npm publish --tag ${tag} --access public`, { stdio: 'inherit' });
}
catch (error) {
console.error('Error during publish:', error);
packageJson.version = originalVersion;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Reverted version back to ${originalVersion}`);
throw error;
}
packageJson.version = originalVersion;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Reverted version back to ${originalVersion}`);