@browserbasehq/sdk-functions 0.0.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -62,6 +62,6 @@ To check status later, run:
|
|
|
62
62
|
`).forEach(i=>{i.trim()&&console.log(a.blue("[Runtime]"),i);});}),this.process.stderr?.on("data",r=>{r.toString().trim().split(`
|
|
63
63
|
`).forEach(i=>{i.trim()&&(i.includes("Watching for file changes")?console.log(a.green("\u2713 Runtime watching for file changes")):i.includes("Restarting")?console.log(a.yellow("\u21BB Runtime restarting due to file change...")):console.error(a.red("[Runtime Error]"),i));});}),this.process.on("exit",(r,s)=>{this.isShuttingDown||(r!==0?(console.error(a.red(`\u2717 Runtime process exited unexpectedly with code ${r}`)),s&&console.error(a.red(` Signal: ${s}`))):console.log(a.gray("Runtime process exited")),this.process=null);}),this.process.on("error",r=>{r.code==="ENOENT"?console.error(a.red("\u2717 Failed to start runtime: tsx not found"),a.yellow(`
|
|
64
64
|
Make sure tsx is installed: npm install -g tsx or pnpm add tsx`)):console.error(a.red("\u2717 Failed to start runtime process:"),r),this.process=null;}),await new Promise(r=>setTimeout(r,100)),!this.process||this.process.exitCode!==null)throw new Error("Failed to start runtime process");console.log(a.green("\u2713 Runtime process started"));}async stop(){if(this.process)return this.isShuttingDown=true,this.verbose&&console.log(a.gray("Stopping runtime process...")),new Promise(e=>{if(!this.process){e();return}let o=setTimeout(()=>{this.process&&(console.log(a.yellow("\u26A0\uFE0F Force killing runtime process")),this.process.kill("SIGKILL"));},5e3);this.process.on("exit",()=>{clearTimeout(o),this.process=null,console.log(a.green("\u2713 Runtime process stopped")),e();}),this.process.kill("SIGTERM");})}isRunning(){return this.process!==null&&this.process.exitCode===null}};async function he(n){let{entrypoint:e,port:o,host:t,verbose:r}=n;process.env.NODE_ENV==="production"&&console.warn(a.yellow("\u26A0\uFE0F Warning: Running dev server in production mode. This is not recommended."));let s=`${t}:${o}`;r&&console.log(a.gray(`Runtime API URL: ${s}`));let i=new O(r),u=new U;await u.initialize();let c=new q;c.loadManifests();let d=new L({bridge:i,browserManager:u,manifestStore:c}),h=new W({entrypoint:e,runtimeApiUrl:s,verbose:r}),w=null;try{w=await ye({port:o,host:t,bridge:i,browserManager:u,handlers:d}),console.log(a.green(`\u2713 Development server listening on http://${t}:${o}`)),console.log(a.cyan("Starting runtime process...")),await h.start();let D=1e4,_e=200,ze=Date.now(),le=!1;for(;Date.now()-ze<D;){if(i.isRuntimeConnected()){le=!0,console.log(a.green("\u2713 Runtime connected and ready")),c.loadManifests();break}await new Promise(X=>setTimeout(X,_e));}le||(console.log(a.yellow("\u26A0\uFE0F Runtime is taking longer than expected to connect...")),c.loadManifests());let pe=async()=>(console.log(a.cyan(`
|
|
65
|
-
\u{1F4E6} Shutting down...`)),await h.stop(),new Promise(X=>{w?.close(()=>{console.log(a.green("\u2713 Server closed")),X();});}));process.on("SIGINT",async()=>{await pe(),process.exit(0);}),process.on("SIGTERM",async()=>{await pe(),process.exit(0);});}catch(D){throw console.error(a.red("Failed to start:"),D),h.isRunning()&&await h.stop(),w&&w.close(),D}}var an=fileURLToPath(import.meta.url),ne=dirname(an);async function Ie(n){if(!yn(n.projectName))throw new Error(`Invalid project name "${n.projectName}". Project names must start with a letter and contain only letters, numbers, hyphens, and underscores.`);let e=resolve(process.cwd(),n.projectName);if(existsSync(e))throw new Error(`Directory "${n.projectName}" already exists. Please choose a different name or delete the existing directory.`);console.log(a.cyan(`\u{1F680} Creating new Browserbase Functions project: ${a.bold(n.projectName)}`)),mkdirSync(e,{recursive:true});try{cn(),existsSync(join(e,".git"))?console.log(a.yellow("\u2713 Git repository already exists")):(console.log(a.gray("Initializing git repository...")),execSync("git init",{cwd:e,stdio:"pipe"}),console.log(a.green("\u2713 Git repository initialized"))),gn(e),existsSync(join(e,"package.json"))?console.log(a.yellow("\u2713 package.json already exists")):(console.log(a.gray("Creating package.json...")),execSync("pnpm init",{cwd:e,stdio:"pipe"}),console.log(a.green("\u2713 package.json created")));let o=ln(n.packageManager);pn(e,o),console.log(a.gray("Installing dependencies...")),un(e,o),console.log(a.green("\u2713 Dependencies installed")),dn(e),existsSync(join(e,"tsconfig.json"))?console.log(a.yellow("\u2713 TypeScript configuration already exists")):(console.log(a.gray("Initializing TypeScript configuration...")),execSync(`${o==="pnpm"?"pnpm":"npx"} tsc --init`,{cwd:e,stdio:"pipe"}),fn(e),console.log(a.green("\u2713 TypeScript configuration created"))),mn(e),console.log(""),console.log(a.green.bold("\u2728 Project initialized successfully!")),console.log(""),console.log(a.cyan("Next steps:")),console.log(a.gray("1. Navigate to your project:")),console.log(a.white(` cd ${n.projectName}`)),console.log(a.gray("2. Add your Browserbase API key and project ID to .env")),console.log(a.gray("3. Run your function locally:")),console.log(a.white(` ${o==="pnpm"?"pnpm":"npx"} bb dev index.ts`)),console.log(a.gray("4. When ready, publish your function:")),console.log(a.white(` ${o==="pnpm"?"pnpm":"npx"} bb publish index.ts`)),console.log(""),console.log(a.gray("Learn more at https://browserbase.com/docs"));}catch(o){console.error(a.red("\u274C Initialization failed:"),o instanceof Error?o.message:o),process.exit(1);}}function cn(){let n=[{command:"node --version",name:"Node.js"},{command:"pnpm --version",name:"pnpm"},{command:"git --version",name:"git"}];for(let{command:e,name:o}of n)try{execSync(e,{stdio:"pipe"});}catch{throw new Error(`${o} is not installed. Please install ${o} and try again.`)}}function ln(n){if(n)return n;let e=process.env.npm_config_user_agent;return e&&e.includes("pnpm"),"pnpm"}function pn(n,e){let o=join(n,"package.json"),t=JSON.parse(readFileSync(o,"utf-8")),r;try{e==="pnpm"?(r=execSync("pnpm --version",{stdio:"pipe"}).toString().trim(),t.packageManager=`pnpm@${r}`):(r=execSync("npm --version",{stdio:"pipe"}).toString().trim(),t.packageManager=`npm@${r}`);}catch{t.packageManager=e==="pnpm"?"pnpm@9.0.0":"npm@10.0.0";}t.type="module",writeFileSync(o,JSON.stringify(t,null,2)),console.log(a.green(`\u2713 Package manager set to ${t.packageManager}`));}function un(n,e){let o=e==="pnpm"?"pnpm add":"npm install",t=e==="pnpm"?"pnpm add -D":"npm install --save-dev";console.log(a.gray(" Installing @browserbasehq/sdk-functions...")),execSync(`${o} @browserbasehq/sdk-functions`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing playwright-core...")),execSync(`${o} playwright-core`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing zod...")),execSync(`${o} zod`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing TypeScript and type definitions...")),execSync(`${t} typescript @types/node`,{cwd:n,stdio:"pipe"});}function dn(n){let e=join(n,".env");if(existsSync(e))console.log(a.yellow("\u2713 .env file already exists"));else {let o=join(ne,"templates",".env.template");copyFileSync(o,e),console.log(a.green("\u2713 .env file created"));}}function gn(n){let e=join(n,".gitignore");if(existsSync(e))console.log(a.yellow("\u2713 .gitignore file already exists"));else {let o=join(ne,"templates",".gitignore.template");copyFileSync(o,e),console.log(a.green("\u2713 .gitignore file created"));}}function mn(n){let e=join(n,"index.ts");if(existsSync(e))console.log(a.yellow("\u2713 index.ts already exists"));else {let o=join(ne,"templates","starter-function.ts.template");copyFileSync(o,e),console.log(a.green("\u2713 Starter function created (index.ts)"));}}function fn(n){let e=join(n,"tsconfig.json");try{let o=JSON.parse(readFileSync(e,"utf-8"));o.compilerOptions={...o.compilerOptions,target:"ES2022",module:"NodeNext",moduleResolution:"NodeNext",esModuleInterop:!0,forceConsistentCasingInFileNames:!0,strict:!0,skipLibCheck:!0,resolveJsonModule:!0},writeFileSync(e,JSON.stringify(o,null,2));}catch{console.log(a.yellow(" Using default TypeScript configuration"));}}function yn(n){return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(n)}var M=new Command;M.name("bb").description("Browserbase Functions CLI").version("0.0
|
|
65
|
+
\u{1F4E6} Shutting down...`)),await h.stop(),new Promise(X=>{w?.close(()=>{console.log(a.green("\u2713 Server closed")),X();});}));process.on("SIGINT",async()=>{await pe(),process.exit(0);}),process.on("SIGTERM",async()=>{await pe(),process.exit(0);});}catch(D){throw console.error(a.red("Failed to start:"),D),h.isRunning()&&await h.stop(),w&&w.close(),D}}var an=fileURLToPath(import.meta.url),ne=dirname(an);async function Ie(n){if(!yn(n.projectName))throw new Error(`Invalid project name "${n.projectName}". Project names must start with a letter and contain only letters, numbers, hyphens, and underscores.`);let e=resolve(process.cwd(),n.projectName);if(existsSync(e))throw new Error(`Directory "${n.projectName}" already exists. Please choose a different name or delete the existing directory.`);console.log(a.cyan(`\u{1F680} Creating new Browserbase Functions project: ${a.bold(n.projectName)}`)),mkdirSync(e,{recursive:true});try{cn(),existsSync(join(e,".git"))?console.log(a.yellow("\u2713 Git repository already exists")):(console.log(a.gray("Initializing git repository...")),execSync("git init",{cwd:e,stdio:"pipe"}),console.log(a.green("\u2713 Git repository initialized"))),gn(e),existsSync(join(e,"package.json"))?console.log(a.yellow("\u2713 package.json already exists")):(console.log(a.gray("Creating package.json...")),execSync("pnpm init",{cwd:e,stdio:"pipe"}),console.log(a.green("\u2713 package.json created")));let o=ln(n.packageManager);pn(e,o),console.log(a.gray("Installing dependencies...")),un(e,o),console.log(a.green("\u2713 Dependencies installed")),dn(e),existsSync(join(e,"tsconfig.json"))?console.log(a.yellow("\u2713 TypeScript configuration already exists")):(console.log(a.gray("Initializing TypeScript configuration...")),execSync(`${o==="pnpm"?"pnpm":"npx"} tsc --init`,{cwd:e,stdio:"pipe"}),fn(e),console.log(a.green("\u2713 TypeScript configuration created"))),mn(e),console.log(""),console.log(a.green.bold("\u2728 Project initialized successfully!")),console.log(""),console.log(a.cyan("Next steps:")),console.log(a.gray("1. Navigate to your project:")),console.log(a.white(` cd ${n.projectName}`)),console.log(a.gray("2. Add your Browserbase API key and project ID to .env")),console.log(a.gray("3. Run your function locally:")),console.log(a.white(` ${o==="pnpm"?"pnpm":"npx"} bb dev index.ts`)),console.log(a.gray("4. When ready, publish your function:")),console.log(a.white(` ${o==="pnpm"?"pnpm":"npx"} bb publish index.ts`)),console.log(""),console.log(a.gray("Learn more at https://browserbase.com/docs"));}catch(o){console.error(a.red("\u274C Initialization failed:"),o instanceof Error?o.message:o),process.exit(1);}}function cn(){let n=[{command:"node --version",name:"Node.js"},{command:"pnpm --version",name:"pnpm"},{command:"git --version",name:"git"}];for(let{command:e,name:o}of n)try{execSync(e,{stdio:"pipe"});}catch{throw new Error(`${o} is not installed. Please install ${o} and try again.`)}}function ln(n){if(n)return n;let e=process.env.npm_config_user_agent;return e&&e.includes("pnpm"),"pnpm"}function pn(n,e){let o=join(n,"package.json"),t=JSON.parse(readFileSync(o,"utf-8")),r;try{e==="pnpm"?(r=execSync("pnpm --version",{stdio:"pipe"}).toString().trim(),t.packageManager=`pnpm@${r}`):(r=execSync("npm --version",{stdio:"pipe"}).toString().trim(),t.packageManager=`npm@${r}`);}catch{t.packageManager=e==="pnpm"?"pnpm@9.0.0":"npm@10.0.0";}t.type="module",writeFileSync(o,JSON.stringify(t,null,2)),console.log(a.green(`\u2713 Package manager set to ${t.packageManager}`));}function un(n,e){let o=e==="pnpm"?"pnpm add":"npm install",t=e==="pnpm"?"pnpm add -D":"npm install --save-dev";console.log(a.gray(" Installing @browserbasehq/sdk-functions...")),execSync(`${o} @browserbasehq/sdk-functions`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing playwright-core...")),execSync(`${o} playwright-core`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing zod...")),execSync(`${o} zod`,{cwd:n,stdio:"pipe"}),console.log(a.gray(" Installing TypeScript and type definitions...")),execSync(`${t} typescript @types/node`,{cwd:n,stdio:"pipe"});}function dn(n){let e=join(n,".env");if(existsSync(e))console.log(a.yellow("\u2713 .env file already exists"));else {let o=join(ne,"templates",".env.template");copyFileSync(o,e),console.log(a.green("\u2713 .env file created"));}}function gn(n){let e=join(n,".gitignore");if(existsSync(e))console.log(a.yellow("\u2713 .gitignore file already exists"));else {let o=join(ne,"templates",".gitignore.template");copyFileSync(o,e),console.log(a.green("\u2713 .gitignore file created"));}}function mn(n){let e=join(n,"index.ts");if(existsSync(e))console.log(a.yellow("\u2713 index.ts already exists"));else {let o=join(ne,"templates","starter-function.ts.template");copyFileSync(o,e),console.log(a.green("\u2713 Starter function created (index.ts)"));}}function fn(n){let e=join(n,"tsconfig.json");try{let o=JSON.parse(readFileSync(e,"utf-8"));o.compilerOptions={...o.compilerOptions,target:"ES2022",module:"NodeNext",moduleResolution:"NodeNext",esModuleInterop:!0,forceConsistentCasingInFileNames:!0,strict:!0,skipLibCheck:!0,resolveJsonModule:!0},writeFileSync(e,JSON.stringify(o,null,2));}catch{console.log(a.yellow(" Using default TypeScript configuration"));}}function yn(n){return /^[a-zA-Z][a-zA-Z0-9_-]*$/.test(n)}var M=new Command;M.name("bb").description("Browserbase Functions CLI").version("1.0.0");var ce=["npm","pnpm"];M.command("init [project-name]").description("Initialize a new Browserbase Functions project (defaults to my-browserbase-function)").option("-p, --package-manager <manager>",`Package manager to use (${ce.join(" or ")})`,"pnpm").action(async(n,e)=>{try{let o=n||"my-browserbase-function",t=e.packageManager.toLowerCase();ce.includes(t)||(console.error(a.red(`Error: Invalid package manager "${e.packageManager}"`)),console.error(a.gray(`Valid options are: ${ce.join(", ")}`)),process.exit(1)),await Ie({projectName:o,packageManager:t});}catch(o){console.error(a.red("Initialization failed:"),o),process.exit(1);}});M.command("dev").description("Start a local development server for testing Browserbase Functions").argument("<entrypoint>","Path to the TypeScript/JavaScript file that imports all your functions").option("-p, --port <number>","Port to listen on","14113").option("-h, --host <string>","Host to bind to","127.0.0.1").action(async(n,e)=>{try{let o=await import('fs'),t=await import('path'),r=t.resolve(n);o.existsSync(r)||(console.error(a.red(`Error: Entrypoint file not found: ${r}`)),process.exit(1));let s=t.extname(r);[".ts",".tsx",".js",".jsx",".mjs",".cjs"].includes(s)||(console.error(a.red("Error: Invalid file extension. Expected .ts, .tsx, .js, .jsx, .mjs, or .cjs")),process.exit(1));let i=parseInt(e.port,10);(isNaN(i)||i<1||i>65535)&&(console.error(a.red("Error: Invalid port number. Must be between 1 and 65535.")),process.exit(1)),console.log(a.cyan("Starting Browserbase Functions development server...")),console.log(a.gray(`Entrypoint: ${r}`)),await he({entrypoint:r,port:i,host:e.host,verbose:e.verbose});}catch(o){console.error(a.red("Failed to start development server:"),o),process.exit(1);}});M.command("publish").description("Publish your Browserbase Function to the cloud").argument("<entrypoint>","Path to the TypeScript/JavaScript file that imports all your functions").option("-u, --api-url <url>","API endpoint URL").option("--dry-run","Show what would be published without uploading").action(async(n,e)=>{try{let{publishFunction:o}=await Promise.resolve().then(()=>(De(),je));await o({entrypoint:n,apiUrl:e.apiUrl,dryRun:e.dryRun});}catch(o){console.error(a.red("Publish failed:"),o),process.exit(1);}});M.command("invoke").description("Invoke a deployed Browserbase Function").argument("<functionId>","The function ID to invoke").option("-p, --params <json>","JSON parameters to pass to the function").option("-u, --api-url <url>","API endpoint URL").option("--no-wait","Don't wait for the invocation to complete").option("--check-status <invocationId>","Check the status of an existing invocation").action(async(n,e)=>{try{let{invoke:o}=await Promise.resolve().then(()=>(qe(),Ue));await o({functionId:n,params:e.params,apiUrl:e.apiUrl,noWait:e.noWait,checkStatus:e.checkStatus});}catch(o){console.error(a.red("Invoke failed:"),o),process.exit(1);}});M.parse();
|
|
66
66
|
//# sourceMappingURL=cli.js.map
|
|
67
67
|
//# sourceMappingURL=cli.js.map
|