From 42637edc10a273d14c94e8712deaa3f6ddf7c171 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 21 Oct 2020 11:44:04 -0700 Subject: [PATCH] docs: modernize patterns in subset of samples (#660) --- .../google-cloud-node/samples/.eslintrc.yml | 1 + .../google-cloud-node/samples/package.json | 8 +- .../google-cloud-node/samples/quickstart.js | 73 +++++++++++-------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/packages/google-cloud-node/samples/.eslintrc.yml b/packages/google-cloud-node/samples/.eslintrc.yml index 282535f55f6a..98634adbeffa 100644 --- a/packages/google-cloud-node/samples/.eslintrc.yml +++ b/packages/google-cloud-node/samples/.eslintrc.yml @@ -1,3 +1,4 @@ --- rules: no-console: off + node/no-unsupported-features/node-builtins: off diff --git a/packages/google-cloud-node/samples/package.json b/packages/google-cloud-node/samples/package.json index a918915b1d55..2aedbad1fb0f 100644 --- a/packages/google-cloud-node/samples/package.json +++ b/packages/google-cloud-node/samples/package.json @@ -2,11 +2,15 @@ "name": "nodejs-docs-samples-speech", "private": true, "license": "Apache-2.0", - "author": "Google Inc.", + "author": "Google LLC", "repository": "googleapis/nodejs-speech", "engines": { - "node": ">=8" + "node": ">=10.17.0" }, + "files": [ + "*.js", + "resources/" + ], "scripts": { "test": "c8 mocha system-test --timeout 600000" }, diff --git a/packages/google-cloud-node/samples/quickstart.js b/packages/google-cloud-node/samples/quickstart.js index 85fcd4be5f8e..9bb1a940c470 100644 --- a/packages/google-cloud-node/samples/quickstart.js +++ b/packages/google-cloud-node/samples/quickstart.js @@ -14,42 +14,51 @@ 'use strict'; -// [START speech_quickstart] -async function main() { +function main() { + // [START speech_quickstart] // Imports the Google Cloud client library const speech = require('@google-cloud/speech'); - const fs = require('fs'); + const fs = require('fs').promises; // Creates a client const client = new speech.SpeechClient(); - // The name of the audio file to transcribe - const fileName = './resources/audio.raw'; - - // Reads a local audio file and converts it to base64 - const file = fs.readFileSync(fileName); - const audioBytes = file.toString('base64'); - - // The audio file's encoding, sample rate in hertz, and BCP-47 language code - const audio = { - content: audioBytes, - }; - const config = { - encoding: 'LINEAR16', - sampleRateHertz: 16000, - languageCode: 'en-US', - }; - const request = { - audio: audio, - config: config, - }; - - // Detects speech in the audio file - const [response] = await client.recognize(request); - const transcription = response.results - .map(result => result.alternatives[0].transcript) - .join('\n'); - console.log(`Transcription: ${transcription}`); + async function quickstart() { + // The name of the audio file to transcribe + const fileName = './resources/audio.raw'; + + // Reads a local audio file and converts it to base64 + const file = await fs.readFile(fileName); + const audioBytes = file.toString('base64'); + + // The audio file's encoding, sample rate in hertz, and BCP-47 language code + const audio = { + content: audioBytes, + }; + const config = { + encoding: 'LINEAR16', + sampleRateHertz: 16000, + languageCode: 'en-US', + }; + const request = { + audio: audio, + config: config, + }; + + // Detects speech in the audio file + const [response] = await client.recognize(request); + const transcription = response.results + .map(result => result.alternatives[0].transcript) + .join('\n'); + console.log(`Transcription: ${transcription}`); + } + quickstart(); + // [END speech_quickstart] } -main().catch(console.error); -// [END speech_quickstart] + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2));