Skip to content

Commit 0c657cb

Browse files
committed
updates
1 parent 4b39a74 commit 0c657cb

File tree

9 files changed

+149
-22
lines changed

9 files changed

+149
-22
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ This example demonstrates all factory functions from the Optimizely SDK, includi
88
- `datafile.json` - Test datafile with Optimizely configuration
99
- `datafile-server.js` - Simple HTTP server for testing polling datafile manager
1010

11-
## Setup
11+
## Running the Example
12+
13+
### From Project Root (Recommended)
1214

13-
Install dependencies:
15+
Run from the project root to automatically build the SDK, create a tarball, and run the example:
1416
```bash
15-
npm install
17+
npm run ts-example
1618
```
1719

18-
## Running the Example
20+
This will:
21+
1. Install SDK dependencies
22+
2. Pack the SDK as a tarball (triggers build via `prepare` script)
23+
3. Install the tarball in this example
24+
4. Build and run the TypeScript example
25+
5. Start the datafile server automatically
26+
6. Clean up after completion
27+
28+
### Manual Setup
1929

2030
### 1. Start the Datafile Server (for polling manager test)
2131

Lines changed: 96 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
"start": "node dist/index.js",
1010
"dev": "tsc && node dist/index.js"
1111
},
12-
"dependencies": {
13-
"@optimizely/optimizely-sdk": "file:../"
14-
},
1512
"devDependencies": {
1613
"@types/node": "^20.0.0",
1714
"typescript": "^4.7.4"
15+
},
16+
"dependencies": {
17+
"@optimizely/optimizely-sdk": "file:../../optimizely-optimizely-sdk-6.3.0.tgz"
1818
}
1919
}

scripts/run-ts-example.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
const { execSync, spawn } = require('child_process');
2020
const path = require('path');
21+
const fs = require('fs');
2122

2223
const rootDir = path.join(__dirname, '..');
23-
const exampleDir = path.join(rootDir, 'ts-example');
24+
const exampleDir = path.join(rootDir, 'examples', 'typescript');
2425

2526
let datafileServer = null;
2627

@@ -40,6 +41,23 @@ function run(command, cwd) {
4041
}
4142
}
4243

44+
function runQuiet(command, cwd) {
45+
try {
46+
const output = execSync(command, {
47+
cwd,
48+
encoding: 'utf8',
49+
shell: true
50+
}).trim();
51+
// npm pack outputs the tarball filename on the last line
52+
const lines = output.split('\n');
53+
return lines[lines.length - 1].trim();
54+
} catch (error) {
55+
console.error(`\nError executing: ${command}`);
56+
cleanup();
57+
process.exit(1);
58+
}
59+
}
60+
4361
function startDatafileServer() {
4462
console.log('\n=== Starting Datafile Server ===');
4563
console.log('Starting server at http://localhost:8910...\n');
@@ -84,20 +102,32 @@ async function cleanup() {
84102
async function main() {
85103
console.log('=== Building SDK and Running TypeScript Example ===\n');
86104

87-
console.log('Step 1: Building SDK...');
88-
run('npm run build', rootDir);
105+
console.log('Installing SDK dependencies...');
106+
run('npm install', rootDir);
89107

90-
console.log('\nStep 2: Installing ts-example dependencies...');
108+
console.log('\nPacking SDK tarball...');
109+
const packOutput = runQuiet('npm pack', rootDir);
110+
const tarballPath = path.join(rootDir, packOutput);
111+
console.log(`Created: ${packOutput}`);
112+
113+
console.log('\nInstalling ts-example devDependencies...');
91114
run('npm install', exampleDir);
92115

93-
console.log('\nStep 3: Building ts-example...');
116+
console.log('\nInstalling SDK tarball in ts-example...');
117+
run(`npm install ${tarballPath}`, exampleDir);
118+
119+
console.log('\nBuilding ts-example...');
94120
run('npm run build', exampleDir);
95121

96122
await startDatafileServer();
97123

98-
console.log('\nStep 4: Running ts-example...');
124+
console.log('\nRunning ts-example...');
99125
run('npm start', exampleDir);
100126

127+
console.log('\nCleaning up tarball...');
128+
fs.unlinkSync(tarballPath);
129+
console.log(`Removed: ${packOutput}`);
130+
101131
await cleanup();
102132
console.log('\n=== Example completed successfully! ===\n');
103133
}

0 commit comments

Comments
 (0)