forked from brianvoe/slim-select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.js
More file actions
191 lines (158 loc) · 5.47 KB
/
release.js
File metadata and controls
191 lines (158 loc) · 5.47 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
#!/usr/bin/env node
import { execSync } from 'child_process'
import { readFileSync, writeFileSync } from 'fs'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import readline from 'readline'
import process from 'process'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const rootDir = __dirname
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m'
}
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`)
}
function exec(command, options = {}) {
try {
log(`Running: ${command}`, 'cyan')
execSync(command, {
stdio: 'inherit',
cwd: rootDir,
...options
})
return true
} catch (error) {
log(`Command failed: ${command}`, 'red')
log(`Exit code: ${error.status}`, 'red')
return false
}
}
function readPackageJson() {
const packagePath = join(rootDir, 'package.json')
return JSON.parse(readFileSync(packagePath, 'utf8'))
}
function writePackageJson(packageJson) {
const packagePath = join(rootDir, 'package.json')
writeFileSync(packagePath, JSON.stringify(packageJson, null, 2) + '\n')
}
function askQuestion(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close()
resolve(answer)
})
})
}
function validateVersion(currentVersion, newVersion) {
// Check if version format is valid (x.y.z)
const versionRegex = /^\d+\.\d+\.\d+$/
if (!versionRegex.test(newVersion)) {
log('❌ Invalid version format. Please use format: x.y.z (e.g., 1.0.1)', 'red')
return null
}
const [major1, minor1, patch1] = currentVersion.split('.').map(Number)
const [major2, minor2, patch2] = newVersion.split('.').map(Number)
// Check if new version is greater than current
if (major2 > major1) return 'major'
if (major2 === major1 && minor2 > minor1) return 'minor'
if (major2 === major1 && minor2 === minor1 && patch2 > patch1) return 'patch'
log('❌ New version must be greater than current version', 'red')
log(` Current: ${currentVersion}`, 'yellow')
log(` New: ${newVersion}`, 'yellow')
return null
}
async function main() {
log('🚀 Starting release process...', 'bright')
// Step 1: Run tests
log('\n📋 Step 1: Running tests...', 'yellow')
if (!exec('npm test')) {
log('❌ Tests failed. Aborting release.', 'red')
process.exit(1)
}
log('✅ Tests passed!', 'green')
// Step 2: Build the project
log('\n🔨 Step 2: Building project...', 'yellow')
// Then build
if (!exec('npm run build')) {
log('❌ Build failed. Aborting release.', 'red')
process.exit(1)
}
log('✅ Build successful!', 'green')
// Step 3: Confirm documentation update
log('\n📚 Step 3: Documentation update confirmation...', 'yellow')
log(' Please ensure you have updated the documentation before proceeding.', 'cyan')
let docConfirmed = false
do {
const docAnswer = await askQuestion('Have you updated the documentation? (y/n): ')
docConfirmed = docAnswer.toLowerCase() === 'y' || docAnswer.toLowerCase() === 'yes'
if (!docConfirmed) {
log('❌ Please update the documentation before continuing with the release.', 'red')
log(' You can run: npm run build:docs', 'cyan')
}
} while (!docConfirmed)
log('✅ Documentation update confirmed!', 'green')
// Step 4: Get current version and ask for new version
const packageJson = readPackageJson()
const currentVersion = packageJson.version
log(`\n📦 Current version: ${currentVersion}`, 'blue')
log(' Format: x.y.z (e.g., 1.0.1, 1.1.0, 2.0.0)', 'cyan')
let newVersion
let versionType
do {
newVersion = await askQuestion(`Enter new version (current: ${currentVersion}): `)
versionType = validateVersion(currentVersion, newVersion)
} while (!versionType)
log(`\n🔄 Step 4: Updating version from ${currentVersion} to ${newVersion} (${versionType} release)...`, 'yellow')
// Update package.json
packageJson.version = newVersion
writePackageJson(packageJson)
log('✅ Package.json updated!', 'green')
// Update package-lock.json to match the new version
log('Updating package-lock.json...', 'cyan')
if (!exec('npm install --package-lock-only')) {
log('❌ Failed to update package-lock.json. Aborting release.', 'red')
process.exit(1)
}
log('✅ Package-lock.json updated!', 'green')
// Step 5: NPM login
log('\n🔐 Step 5: NPM login...', 'yellow')
if (!exec('npm login')) {
log('❌ NPM login failed. Aborting release.', 'red')
process.exit(1)
}
log('✅ NPM login successful!', 'green')
// Step 6: NPM publish
log('\n📤 Step 6: Publishing to NPM...', 'yellow')
if (!exec('npm publish')) {
log('❌ NPM publish failed. Aborting release.', 'red')
process.exit(1)
}
log('✅ Package published to NPM!', 'green')
log('\n🎉 Release completed successfully!', 'bright')
log(`📦 Version ${newVersion} is now published on NPM`, 'green')
log(`🔗 Package: https://www.npmjs.com/package/${packageJson.name}`, 'blue')
}
// Handle errors
process.on('unhandledRejection', (error) => {
log(`❌ Unhandled error: ${error}`, 'red')
process.exit(1)
})
// Run the release script
main().catch((error) => {
log(`❌ Release failed: ${error}`, 'red')
process.exit(1)
})