-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
140 lines (127 loc) · 4.44 KB
/
Copy pathbuild.js
File metadata and controls
140 lines (127 loc) · 4.44 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
#!/usr/bin/env node
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
// Load environment variables from .env file
require('dotenv').config();
// Get environment variables with defaults
const MONITORING_ACCOUNT = process.env.MONITORING_ACCOUNT || '';
const MONITORING_PROFILE_ID = process.env.MONITORING_PROFILE_ID || 'guesty-onsite-monitoring';
// Parse DEBUG_ACCOUNT_IDS from JSON array string
const DEBUG_ACCOUNT_IDS_RAW = process.env.DEBUG_ACCOUNT_IDS || '[]';
const DEBUG_ACCOUNT_IDS = JSON.parse(DEBUG_ACCOUNT_IDS_RAW);
// Parse command line arguments
const args = process.argv.slice(2);
const integration = args[0]; // 'cloudbeds', 'mews', or 'guesty'
const watch = args.includes('--watch');
const deploy = args.includes('--deploy');
// Build configuration
const buildConfigs = {
cloudbeds: {
entryPoint: 'src/cloudbeds/klaviyo_hotel_tracking.js',
outfile: 'public/klaviyo_hotel_tracking_cloudbeds.js',
surgeDomain: 'klaviyo-hotel-cloudbeds.surge.sh'
},
mews: {
entryPoint: 'src/mews/klaviyo_hotel_tracking.js',
outfile: 'public/klaviyo_hotel_tracking_mews.js',
surgeDomain: 'klaviyo-hotel-mews.surge.sh'
},
guesty: {
entryPoint: 'src/guesty/klaviyo_hotel_tracking.js',
outfile: 'public/klaviyo_hotel_tracking_guesty.js',
surgeDomain: 'klaviyo-hotel-guesty.surge.sh'
},
olo: {
entryPoint: 'src/olo/klaviyo_restaurant_tracking.js',
outfile: 'public/klaviyo_restaurant_tracking_olo.js',
surgeDomain: 'klaviyo-hotel-olo.surge.sh'
}
};
// Serialize surge deploys. esbuild's watcher (unlike nodemon) never kills a
// running child process, and this lock means a save during an in-flight upload
// queues exactly one more deploy instead of interrupting it — interrupted
// uploads were what left the surge domain torn down (404).
let deploying = false;
let pendingDomain = null;
function deployToSurge(domain) {
if (!domain) return;
if (deploying) { pendingDomain = domain; return; }
deploying = true;
console.log(`Deploying public/ -> https://${domain} ...`);
exec(`surge public ${domain}`, (error, stdout, stderr) => {
deploying = false;
if (error) {
console.error(`surge deploy failed: ${(stderr || error.message).trim()}`);
} else {
console.log(`Deployed: https://${domain}`);
}
if (pendingDomain) {
const next = pendingDomain;
pendingDomain = null;
deployToSurge(next);
}
});
}
async function build(config) {
const plugins = [];
// With --watch --deploy, redeploy to surge after each successful rebuild.
if (watch && deploy) {
plugins.push({
name: 'surge-deploy',
setup(b) {
b.onEnd((result) => {
if (result.errors && result.errors.length) {
console.error('Build had errors — skipping deploy');
return;
}
deployToSurge(config.surgeDomain);
});
}
});
}
const options = {
entryPoints: [config.entryPoint],
bundle: true,
outfile: config.outfile,
format: 'iife',
define: {
'process.env.MONITORING_ACCOUNT': JSON.stringify(MONITORING_ACCOUNT),
'process.env.MONITORING_PROFILE_ID': JSON.stringify(MONITORING_PROFILE_ID),
'process.env.DEBUG_ACCOUNT_IDS': JSON.stringify(DEBUG_ACCOUNT_IDS) // Injected as array
},
plugins
};
try {
if (watch) {
const context = await esbuild.context(options);
await context.watch(); // runs an initial build (deploys via onEnd), then watches
console.log(`Watching ${config.entryPoint}...` + (deploy ? ` (auto-deploy to ${config.surgeDomain} on save)` : ''));
} else {
await esbuild.build(options);
console.log(`Built ${config.outfile}`);
}
} catch (error) {
console.error(`Build failed for ${config.entryPoint}:`, error);
process.exit(1);
}
}
// Main execution
async function main() {
if (!integration) {
console.error('Usage: node build.js <integration> [--watch] [--deploy]');
console.error(' integration: cloudbeds, mews, guesty, or olo');
console.error(' --watch rebuild on file changes');
console.error(' --deploy with --watch, redeploy to surge after each build');
process.exit(1);
}
const config = buildConfigs[integration];
if (!config) {
console.error(`Unknown integration: ${integration}`);
console.error('Valid options: cloudbeds, mews, guesty, olo');
process.exit(1);
}
await build(config);
}
main();