-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (55 loc) · 1.84 KB
/
index.js
File metadata and controls
72 lines (55 loc) · 1.84 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
const LaunchDarkly = require('launchdarkly-node-client-sdk');
// Set clientSideId to your LaunchDarkly client-side ID.
const clientSideId = process.env.LAUNCHDARKLY_CLIENT_SIDE_ID ?? 'your-client-side-id';
// Set featureFlagKey to the feature flag key you want to evaluate.
const featureFlagKey = process.env.LAUNCHDARKLY_FLAG_KEY ?? 'sample-feature';
function showBanner() {
console.log(
` ██
██
████████
███████
██ LAUNCHDARKLY █
███████
████████
██
██
`,
);
}
function printValueAndBanner(flagValue) {
console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`);
if (flagValue) showBanner();
}
if (!clientSideId) {
console.log('*** Please set LAUNCHDARKLY_CLIENT_SIDE_ID to your client-side ID, or edit index.js to use your client-side ID.');
process.exit(1);
}
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
// soon after you run the demo.
const context = {
kind: 'user',
key: 'example-user-key',
name: 'Sandy',
};
const ldClient = LaunchDarkly.initialize(clientSideId, context);
async function main() {
try {
await ldClient.waitForInitialization(5);
console.log('*** SDK successfully initialized!');
const eventKey = `change:${featureFlagKey}`;
ldClient.on(eventKey, async () => {
const flagValue = await ldClient.variation(featureFlagKey, false);
printValueAndBanner(flagValue);
});
const flagValue = await ldClient.variation(featureFlagKey, false);
printValueAndBanner(flagValue);
if (typeof process.env.CI !== "undefined") {
process.exit(0);
}
} catch (error) {
console.log(`*** SDK failed to initialize: ${error}`);
process.exit(1);
}
}
main();