-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
243 lines (212 loc) · 7.11 KB
/
deploy.js
File metadata and controls
243 lines (212 loc) · 7.11 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
#!/usr/bin/env node
/**
* PWA Template Deployment Script
*
* This script automatically updates version numbers and timestamps
* when deploying the PWA template to ensure proper cache management.
*
* Usage:
* node deploy.js [version]
*
* Examples:
* node deploy.js # Auto-increment patch version
* node deploy.js 4.1.0 # Set specific version
* node deploy.js --patch # Increment patch version
* node deploy.js --minor # Increment minor version
* node deploy.js --major # Increment major version
*/
const fs = require('fs');
const path = require('path');
// Configuration
const CONFIG = {
versionFile: './version.json',
serviceWorkerFile: './service-worker.js',
manifestFile: './manifest.json',
defaultVersion: '4.0.0'
};
// Parse command line arguments
const args = process.argv.slice(2);
let newVersion = null;
let incrementType = null;
if (args.length === 0) {
incrementType = 'patch';
} else if (args[0] === '--patch') {
incrementType = 'patch';
} else if (args[0] === '--minor') {
incrementType = 'minor';
} else if (args[0] === '--major') {
incrementType = 'major';
} else {
newVersion = args[0];
}
// Version management functions
function parseVersion(version) {
const parts = version.split('.').map(Number);
return {
major: parts[0] || 0,
minor: parts[1] || 0,
patch: parts[2] || 0
};
}
function formatVersion(versionObj) {
return `${versionObj.major}.${versionObj.minor}.${versionObj.patch}`;
}
function incrementVersion(currentVersion, type) {
const version = parseVersion(currentVersion);
switch (type) {
case 'major':
version.major++;
version.minor = 0;
version.patch = 0;
break;
case 'minor':
version.minor++;
version.patch = 0;
break;
case 'patch':
version.patch++;
break;
}
return formatVersion(version);
}
function getCurrentVersion() {
try {
if (fs.existsSync(CONFIG.versionFile)) {
const versionData = JSON.parse(fs.readFileSync(CONFIG.versionFile, 'utf8'));
return versionData.version;
}
} catch (error) {
console.warn('Could not read current version:', error.message);
}
return CONFIG.defaultVersion;
}
// Update version.json
function updateVersionFile(version) {
const now = new Date();
const buildTime = Date.now();
const versionData = {
version: version,
buildTime: buildTime,
lastUpdated: now.toISOString(),
changelog: [
`Version ${version} - Auto-generated deployment`,
"Enhanced auto-updater and cache manager",
"Improved browser caching strategy",
"Added version control and update notifications",
"Better offline support and cache management"
],
features: {
autoUpdate: true,
cacheManagement: true,
offlineSupport: true,
updateNotifications: true
},
cacheConfig: {
maxAge: 604800000,
maxEntries: 100,
checkInterval: 1800000
}
};
fs.writeFileSync(CONFIG.versionFile, JSON.stringify(versionData, null, 2));
console.log(`✅ Updated ${CONFIG.versionFile} to version ${version}`);
}
// Update service worker version
function updateServiceWorkerVersion(version) {
try {
let content = fs.readFileSync(CONFIG.serviceWorkerFile, 'utf8');
// Update version in cache config
content = content.replace(
/version: ['"]\d+\.\d+\.\d+['"]/,
`version: '${version}'`
);
// Update cache name
content = content.replace(
/name: ['"]pwa-template-v\d+['"]/,
`name: 'pwa-template-v${version.split('.')[0]}'`
);
// Update build time
content = content.replace(
/buildTime: \d+/,
`buildTime: ${Date.now()}`
);
fs.writeFileSync(CONFIG.serviceWorkerFile, content);
console.log(`✅ Updated ${CONFIG.serviceWorkerFile} to version ${version}`);
} catch (error) {
console.error(`❌ Error updating service worker:`, error.message);
}
}
// Update manifest version (if needed)
function updateManifestVersion(version) {
try {
const manifestPath = CONFIG.manifestFile;
if (fs.existsSync(manifestPath)) {
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
// Add version to manifest if it doesn't exist
if (!manifest.version) {
manifest.version = version;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log(`✅ Added version ${version} to ${manifestPath}`);
}
}
} catch (error) {
console.error(`❌ Error updating manifest:`, error.message);
}
}
// Create deployment info
function createDeploymentInfo(version) {
const deploymentInfo = {
version: version,
deployedAt: new Date().toISOString(),
timestamp: Date.now(),
environment: process.env.NODE_ENV || 'development',
gitCommit: process.env.GITHUB_SHA || 'unknown',
buildNumber: process.env.GITHUB_RUN_NUMBER || 'local'
};
fs.writeFileSync('./deployment-info.json', JSON.stringify(deploymentInfo, null, 2));
console.log(`✅ Created deployment-info.json`);
}
// Main deployment function
function deploy() {
console.log('🚀 Starting PWA Template Deployment...\n');
// Determine new version
const currentVersion = getCurrentVersion();
if (newVersion) {
// Use specified version
console.log(`📦 Setting version to ${newVersion}`);
} else if (incrementType) {
// Increment version
newVersion = incrementVersion(currentVersion, incrementType);
console.log(`📦 Incrementing ${incrementType} version: ${currentVersion} → ${newVersion}`);
} else {
newVersion = currentVersion;
console.log(`📦 Using current version: ${newVersion}`);
}
// Update files
try {
updateVersionFile(newVersion);
updateServiceWorkerVersion(newVersion);
updateManifestVersion(newVersion);
createDeploymentInfo(newVersion);
console.log('\n✅ Deployment completed successfully!');
console.log(`📋 Version: ${newVersion}`);
console.log(`🕒 Build Time: ${new Date().toLocaleString()}`);
console.log('\n📝 Next steps:');
console.log(' 1. Commit your changes');
console.log(' 2. Push to your repository');
console.log(' 3. Deploy to your hosting platform');
console.log(' 4. The PWA will automatically update for users');
} catch (error) {
console.error('\n❌ Deployment failed:', error.message);
process.exit(1);
}
}
// Run deployment
if (require.main === module) {
deploy();
}
module.exports = {
deploy,
updateVersionFile,
updateServiceWorkerVersion,
incrementVersion
};