Skip to content

chore: Update implementation and move to github actions. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions .circleci/config.yml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Verify Hello App
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 9 * * *'
push:
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md' # Do not need to run CI for markdown changes.
pull_request:
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md'

jobs:
build-and-run:
runs-on: ubuntu-latest

permissions:
id-token: write # Needed if using OIDC to get release secrets.

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4

- run: npm i

- uses: launchdarkly/gh-actions/actions/[email protected]
with:
use_server_key: true
role_arn: ${{ vars.AWS_ROLE_ARN }}
command: npm start
92 changes: 59 additions & 33 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,71 @@
import * as LaunchDarkly from '@launchdarkly/node-server-sdk';

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

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

// Set up the context properties. This use should appear on your LaunchDarkly
// contexts dashboard soon after you run the demo.
function showBanner() {
console.log(
` ██
██
████████
███████
██ LAUNCHDARKLY █
███████
████████
██
██
`,
);
}

function printValueAndBanner(flagValue: boolean) {
console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`);

if (flagValue) showBanner();
}

if (!sdkKey) {
console.log('*** Please edit index.js to set sdkKey to your LaunchDarkly SDK key first.');
process.exit(1);
}
Comment on lines +30 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have this, should we not default the SDK variable above to "your-sdk-key"?



const ldClient = LaunchDarkly.init(sdkKey);

// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
// soon after you run the demo.
const context = {
"kind": "user",
"name": "Sandy",
"key": "example-context-key"
kind: 'user',
key: 'example-user-key',
name: 'Sandy',
};

function showMessage(s: string) {
console.log("*** " + s);
console.log("");
}
async function main() {
try {
await ldClient.waitForInitialization({timeout: 10});

const client = LaunchDarkly.init(sdkKey);

client.once('ready', function () {
showMessage("SDK successfully initialized!");
client.variation(featureFlagKey, context, false, function (err, showFeature) {
client.track("event-called", context);
if (showFeature) {
// application code to show the feature
showMessage("Feature flag '" + featureFlagKey + "' is true for this context");
} else {
// the code to run if the feature is off
showMessage("Feature flag '" + featureFlagKey + "' is false for this context");
}
console.log('*** SDK successfully initialized!');

// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
// the context properties and flag usage statistics will not appear on your dashboard. In a
// normal long-running application, the SDK would continue running and events would be
// delivered automatically in the background.
client.flush(function () {
client.close();
const eventKey = `update:${featureFlagKey}`;
ldClient.on(eventKey, async () => {
const flagValue = await ldClient.variation(featureFlagKey, context, false);
printValueAndBanner(flagValue);
});
});
});

const flagValue = await ldClient.variation(featureFlagKey, context, 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();
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"author": "LaunchDarkly <[email protected]>",
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "20.4.2",
"ts-node": "10.9.1",
"typescript": "5.1.6"
"@types/node": "22.10.1",
"ts-node": "10.9.2",
"typescript": "5.7.2"
},
"dependencies": {
"@launchdarkly/node-server-sdk": ">= 8.0.0"
"@launchdarkly/node-server-sdk": ">= 9.0.0"
}
}
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"strict": true
"module": "NodeNext",
"strict": true,
"target": "ES2020",
"moduleResolution": "NodeNext",
}
}
}
Loading