This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
forked from segmentio/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile
executable file
·91 lines (79 loc) · 2.56 KB
/
compile
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
#!/usr/bin/env node
const fs = require('fs')
const process = require('process')
const program = require('commander')
const build = require('./scripts/build')
const buildIntegrations = require('./scripts/buildIntegrations')
const addSettings = require('./scripts/settings')
const path = require('path')
const server = require('./server')
const { spawnSync } = require('child_process')
const generateFile = require('./scripts/generateFile')
const open = require('open')
const filePath = path.join(__dirname + '/src/index.html')
const buildName = `${Math.floor(new Date() / 1000)}`;
const fileName = path.join(__dirname, 'builds', `analytics-${buildName}.js`)
program
.option('-w, --writeKey <writeKey>', 'Segment writeKey to for viewing events in the debugger')
.option('-s, --settings <settings>', '(Optional) Relative path to custom integrations settings file')
.option('-c, --cdnDomain <cdnDomain>', 'Segment CDN domain', 'http://cdn.segment.com')
.option('-n, --noRebuild', 'Skips yarn rebuild')
.option('-p, --port <port>', 'Set a port to serve the local ajs file from', 3000)
.parse(process.argv)
// Get program parameters.
const {
cdnDomain,
port,
settings,
noRebuild,
writeKey,
} = program
if (!writeKey) {
console.error('Error: --writeKey required')
process.exit(1)
}
const opts = {
shell: true,
cwd: __dirname
}
if (!noRebuild) {
// check if any files have changed since most recent `git add`
const diff = spawnSync('git diff --name-only', opts)
if (diff.stdout && (diff.stdout).toString().length) {
opts.stdio = 'inherit'
// if not, run `yarn install --no-lockfile` to pull in any updates made in the /integrations directory
spawnSync('yarn install --no-lockfile', opts)
}
}
let customSettings
if (settings) {
const settingsPath = path.join(__dirname, settings)
customSettings = JSON.parse(fs.readFileSync(settingsPath))
} else {
console.info('**No custom settings file specified. Retrieving settings from Segment\'s CDN.**')
}
buildIntegrations()
build(async (ajs, integrationVersions, coreVersion) => {
let compiled
try {
compiled = await addSettings({
ajs,
cdnDomain,
integrationVersions,
coreVersion,
writeKey,
customSettings
})
} catch(err) {
console.error('Error:', err)
return
}
if (!compiled) {
console.error('Looks like we couldn\'t compile A.js! Please read the error output above and try again.')
process.exit(1)
}
fs.writeFileSync(fileName, compiled)
generateFile(writeKey, port)
server(compiled, filePath, port)
open(`http://localhost:${port}`)
})