Skip to content

Commit c65a760

Browse files
authored
chore: Update implementation and move to github actions. (#7)
1 parent 495c725 commit c65a760

File tree

5 files changed

+101
-80
lines changed

5 files changed

+101
-80
lines changed

.circleci/config.yml

Lines changed: 0 additions & 40 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Verify Hello App
2+
on:
3+
schedule:
4+
# * is a special character in YAML so you have to quote this string
5+
- cron: '0 9 * * *'
6+
push:
7+
branches: [ main, 'feat/**' ]
8+
paths-ignore:
9+
- '**.md' # Do not need to run CI for markdown changes.
10+
pull_request:
11+
branches: [ main, 'feat/**' ]
12+
paths-ignore:
13+
- '**.md'
14+
15+
jobs:
16+
build-and-run:
17+
runs-on: ubuntu-latest
18+
19+
permissions:
20+
id-token: write # Needed if using OIDC to get release secrets.
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- uses: actions/setup-node@v4
26+
27+
- run: npm i
28+
29+
- uses: launchdarkly/gh-actions/actions/[email protected]
30+
with:
31+
use_server_key: true
32+
role_arn: ${{ vars.AWS_ROLE_ARN }}
33+
command: npm start

index.ts

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
11
import * as LaunchDarkly from '@launchdarkly/node-server-sdk';
22

33
// Set sdkKey to your LaunchDarkly SDK key.
4-
const sdkKey = "";
4+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY ?? 'your-sdk-key';
55

66
// Set featureFlagKey to the feature flag key you want to evaluate.
7-
const featureFlagKey = "my-boolean-flag";
7+
const featureFlagKey = process.env.LAUNCHDARKLY_FLAG_KEY ?? 'sample-feature';
88

9-
// Set up the context properties. This use should appear on your LaunchDarkly
10-
// contexts dashboard soon after you run the demo.
9+
function showBanner() {
10+
console.log(
11+
` ██
12+
██
13+
████████
14+
███████
15+
██ LAUNCHDARKLY █
16+
███████
17+
████████
18+
██
19+
██
20+
`,
21+
);
22+
}
23+
24+
function printValueAndBanner(flagValue: boolean) {
25+
console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`);
26+
27+
if (flagValue) showBanner();
28+
}
29+
30+
if (!sdkKey) {
31+
console.log('*** Please edit index.js to set sdkKey to your LaunchDarkly SDK key first.');
32+
process.exit(1);
33+
}
34+
35+
36+
const ldClient = LaunchDarkly.init(sdkKey);
37+
38+
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
39+
// soon after you run the demo.
1140
const context = {
12-
"kind": "user",
13-
"name": "Sandy",
14-
"key": "example-context-key"
41+
kind: 'user',
42+
key: 'example-user-key',
43+
name: 'Sandy',
1544
};
1645

17-
function showMessage(s: string) {
18-
console.log("*** " + s);
19-
console.log("");
20-
}
46+
async function main() {
47+
try {
48+
await ldClient.waitForInitialization({timeout: 10});
2149

22-
const client = LaunchDarkly.init(sdkKey);
23-
24-
client.once('ready', function () {
25-
showMessage("SDK successfully initialized!");
26-
client.variation(featureFlagKey, context, false, function (err, showFeature) {
27-
client.track("event-called", context);
28-
if (showFeature) {
29-
// application code to show the feature
30-
showMessage("Feature flag '" + featureFlagKey + "' is true for this context");
31-
} else {
32-
// the code to run if the feature is off
33-
showMessage("Feature flag '" + featureFlagKey + "' is false for this context");
34-
}
50+
console.log('*** SDK successfully initialized!');
3551

36-
// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
37-
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
38-
// the context properties and flag usage statistics will not appear on your dashboard. In a
39-
// normal long-running application, the SDK would continue running and events would be
40-
// delivered automatically in the background.
41-
client.flush(function () {
42-
client.close();
52+
const eventKey = `update:${featureFlagKey}`;
53+
ldClient.on(eventKey, async () => {
54+
const flagValue = await ldClient.variation(featureFlagKey, context, false);
55+
printValueAndBanner(flagValue);
4356
});
44-
});
45-
});
57+
58+
const flagValue = await ldClient.variation(featureFlagKey, context, false);
59+
printValueAndBanner(flagValue);
60+
61+
if (typeof process.env.CI !== "undefined") {
62+
process.exit(0);
63+
}
64+
} catch (error) {
65+
console.log(`*** SDK failed to initialize: ${error}`);
66+
process.exit(1);
67+
}
68+
69+
}
70+
71+
main();

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
"author": "LaunchDarkly <[email protected]>",
1010
"license": "Apache-2.0",
1111
"devDependencies": {
12-
"@types/node": "20.4.2",
13-
"ts-node": "10.9.1",
14-
"typescript": "5.1.6"
12+
"@types/node": "22.10.1",
13+
"ts-node": "10.9.2",
14+
"typescript": "5.7.2"
1515
},
1616
"dependencies": {
17-
"@launchdarkly/node-server-sdk": ">= 8.0.0"
17+
"@launchdarkly/node-server-sdk": ">= 9.0.0"
1818
}
1919
}

tsconfig.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
4-
"strict": true
3+
"module": "NodeNext",
4+
"strict": true,
5+
"target": "ES2020",
6+
"moduleResolution": "NodeNext",
57
}
6-
}
8+
}

0 commit comments

Comments
 (0)