@oneuptime/common 7.0.4790 → 7.0.4791
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Universal Service Worker Generator for OneUptime Services
|
|
5
|
+
*
|
|
6
|
+
* This script can be used by any OneUptime service to generate
|
|
7
|
+
* a service worker from a template with dynamic versioning.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* node generate-service-worker.js [template-path] [output-path]
|
|
11
|
+
*
|
|
12
|
+
* Example:
|
|
13
|
+
* node generate-service-worker.js sw.js.template public/sw.js
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const crypto = require('crypto');
|
|
19
|
+
|
|
20
|
+
// Default values
|
|
21
|
+
const DEFAULT_APP_VERSION = '1.0.0';
|
|
22
|
+
const DEFAULT_GIT_SHA = 'local';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get app version from environment or package.json
|
|
26
|
+
*/
|
|
27
|
+
function getAppVersion(packageJsonPath) {
|
|
28
|
+
// First try environment variable (Docker build)
|
|
29
|
+
if (process.env.APP_VERSION) {
|
|
30
|
+
return process.env.APP_VERSION;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Fallback to package.json version
|
|
34
|
+
try {
|
|
35
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
36
|
+
return packageJson.version || DEFAULT_APP_VERSION;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.warn('Could not read package.json, using default version');
|
|
39
|
+
return DEFAULT_APP_VERSION;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get git SHA from environment
|
|
45
|
+
*/
|
|
46
|
+
function getGitSha() {
|
|
47
|
+
// Try environment variable first (Docker build)
|
|
48
|
+
if (process.env.GIT_SHA) {
|
|
49
|
+
return process.env.GIT_SHA.substring(0, 8); // Short SHA
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Try to get from git command if available
|
|
53
|
+
try {
|
|
54
|
+
const { execSync } = require('child_process');
|
|
55
|
+
const gitSha = execSync('git rev-parse --short HEAD', { encoding: 'utf8' }).trim();
|
|
56
|
+
return gitSha;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
// Fallback to timestamp-based hash for local development
|
|
59
|
+
const timestamp = Date.now().toString();
|
|
60
|
+
const hash = crypto.createHash('md5').update(timestamp).digest('hex');
|
|
61
|
+
return hash.substring(0, 8);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Generate service worker from template
|
|
67
|
+
*/
|
|
68
|
+
function generateServiceWorker(templatePath, outputPath, serviceName = 'OneUptime') {
|
|
69
|
+
// Check if template exists
|
|
70
|
+
if (!fs.existsSync(templatePath)) {
|
|
71
|
+
console.error('❌ Service worker template not found:', templatePath);
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Read template
|
|
76
|
+
const template = fs.readFileSync(templatePath, 'utf8');
|
|
77
|
+
|
|
78
|
+
// Get version information
|
|
79
|
+
const packageJsonPath = path.join(path.dirname(templatePath), 'package.json');
|
|
80
|
+
const appVersion = getAppVersion(packageJsonPath);
|
|
81
|
+
const gitSha = getGitSha();
|
|
82
|
+
const buildTimestamp = new Date().toISOString();
|
|
83
|
+
|
|
84
|
+
console.log(`🔧 Generating service worker for ${serviceName}...`);
|
|
85
|
+
console.log(` App Version: ${appVersion}`);
|
|
86
|
+
console.log(` Git SHA: ${gitSha}`);
|
|
87
|
+
console.log(` Build Time: ${buildTimestamp}`);
|
|
88
|
+
|
|
89
|
+
// Replace placeholders
|
|
90
|
+
const generatedContent = template
|
|
91
|
+
.replace(/\{\{APP_VERSION\}\}/g, appVersion)
|
|
92
|
+
.replace(/\{\{GIT_SHA\}\}/g, gitSha)
|
|
93
|
+
.replace(/\{\{BUILD_TIMESTAMP\}\}/g, buildTimestamp)
|
|
94
|
+
.replace(/\{\{SERVICE_NAME\}\}/g, serviceName);
|
|
95
|
+
|
|
96
|
+
// Add generation comment at the top
|
|
97
|
+
const header = `/*
|
|
98
|
+
* Generated Service Worker for ${serviceName}
|
|
99
|
+
*
|
|
100
|
+
* Generated at: ${buildTimestamp}
|
|
101
|
+
* App Version: ${appVersion}
|
|
102
|
+
* Git SHA: ${gitSha}
|
|
103
|
+
*
|
|
104
|
+
* DO NOT EDIT THIS FILE DIRECTLY
|
|
105
|
+
* Edit the template file instead and run the generator script
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
const finalContent = header + generatedContent;
|
|
111
|
+
|
|
112
|
+
// Ensure output directory exists
|
|
113
|
+
const outputDir = path.dirname(outputPath);
|
|
114
|
+
if (!fs.existsSync(outputDir)) {
|
|
115
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Write generated service worker
|
|
119
|
+
fs.writeFileSync(outputPath, finalContent, 'utf8');
|
|
120
|
+
|
|
121
|
+
console.log('✅ Service worker generated successfully:', outputPath);
|
|
122
|
+
console.log(` Cache version: oneuptime-v${appVersion}-${gitSha}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Command line interface
|
|
126
|
+
if (require.main === module) {
|
|
127
|
+
const args = process.argv.slice(2);
|
|
128
|
+
const templatePath = args[0] || 'sw.js.template';
|
|
129
|
+
const outputPath = args[1] || 'public/sw.js';
|
|
130
|
+
const serviceName = args[2] || path.basename(process.cwd());
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
// Resolve paths relative to current working directory
|
|
134
|
+
const resolvedTemplatePath = path.resolve(templatePath);
|
|
135
|
+
const resolvedOutputPath = path.resolve(outputPath);
|
|
136
|
+
|
|
137
|
+
generateServiceWorker(resolvedTemplatePath, resolvedOutputPath, serviceName);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error('❌ Failed to generate service worker:', error.message);
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = { generateServiceWorker, getAppVersion, getGitSha };
|