-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.js
More file actions
173 lines (146 loc) Β· 4.48 KB
/
Copy pathpublish.js
File metadata and controls
173 lines (146 loc) Β· 4.48 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
#!/usr/bin/env node
/**
* NPM Package Publisher
*
* This script provides an interactive interface for publishing packages
* with proper semantic versioning and tag management.
*/
import { execSync } from 'child_process';
import fs from 'fs/promises';
import readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
const log = (message, color = colors.reset) => {
console.log(`${color}${message}${colors.reset}`);
};
const ask = (question) => {
return new Promise((resolve) => {
rl.question(question, resolve);
});
};
async function getCurrentVersion() {
try {
const packageJson = JSON.parse(await fs.readFile('package.json', 'utf8'));
return packageJson.version;
} catch (error) {
throw new Error('Could not read package.json');
}
}
async function verifyPackage() {
log('π Verifying package before publishing...', colors.cyan);
try {
// Run verification script
execSync('node verify-build.js', { stdio: 'inherit' });
log('β
Package verification passed', colors.green);
} catch (error) {
log('β Package verification failed', colors.red);
log('Please fix the issues and try again', colors.yellow);
process.exit(1);
}
}
async function publishPackage(versionType, tag = null) {
log(`π Publishing package as ${versionType}...`, colors.cyan);
try {
// Build the package
log('π¦ Building package...', colors.yellow);
execSync('npm run build', { stdio: 'inherit' });
// Run tests
log('π§ͺ Running tests...', colors.yellow);
execSync('npm test', { stdio: 'inherit' });
// Version and publish
log(`π Creating ${versionType} release...`, colors.yellow);
execSync(`npm version ${versionType}`, { stdio: 'inherit' });
// Get the new version
const newVersion = await getCurrentVersion();
// Publish to npm
log(`π€ Publishing version ${newVersion}...`, colors.yellow);
if (tag) {
execSync(`npm publish --tag ${tag}`, { stdio: 'inherit' });
} else {
execSync('npm publish', { stdio: 'inherit' });
}
log(`π Successfully published version ${newVersion}!`, colors.green);
if (tag) {
log(`π¦ Published to @${tag} tag`, colors.blue);
}
} catch (error) {
log('β Publishing failed', colors.red);
log(error.message, colors.red);
process.exit(1);
}
}
async function runSemanticRelease() {
log('π€ Running semantic-release...', colors.cyan);
try {
execSync('npx semantic-release', { stdio: 'inherit' });
log('π Semantic release completed!', colors.green);
} catch (error) {
log('β Semantic release failed', colors.red);
process.exit(1);
}
}
async function main() {
log('π NPM Package Publisher', colors.cyan);
log('=' .repeat(30), colors.cyan);
const currentVersion = await getCurrentVersion();
log(`Current version: ${currentVersion}`, colors.blue);
log('\nPublishing options:', colors.cyan);
log('1. Patch release (1.0.1)', colors.yellow);
log('2. Minor release (1.1.0)', colors.yellow);
log('3. Major release (2.0.0)', colors.yellow);
log('4. Alpha release', colors.yellow);
log('5. Beta release', colors.yellow);
log('6. Release candidate (RC)', colors.yellow);
log('7. Semantic release (auto-detect)', colors.yellow);
const answer = await ask('\nSelect an option (1-7): ');
switch (answer.trim()) {
case '1':
await verifyPackage();
await publishPackage('patch');
break;
case '2':
await verifyPackage();
await publishPackage('minor');
break;
case '3':
await verifyPackage();
await publishPackage('major');
break;
case '4':
await verifyPackage();
await publishPackage('prerelease --preid=alpha', 'alpha');
break;
case '5':
await verifyPackage();
await publishPackage('prerelease --preid=beta', 'beta');
break;
case '6':
await verifyPackage();
await publishPackage('prerelease --preid=rc', 'next');
break;
case '7':
await verifyPackage();
await runSemanticRelease();
break;
default:
log('Invalid option', colors.red);
process.exit(1);
}
rl.close();
}
if (require.main === module) {
main().catch(error => {
log(`\nπ₯ Publisher failed: ${error.message}`, colors.red);
process.exit(1);
});
}