diff --git a/appengine/errorreporting/test/app.test.js b/appengine/errorreporting/test/app.test.js index ee35168521..7ba157c1bc 100644 --- a/appengine/errorreporting/test/app.test.js +++ b/appengine/errorreporting/test/app.test.js @@ -69,8 +69,6 @@ describe('appengine/errorreporting/app.js', function () { .expect(500) .expect(function (response) { assert(sample.mocks.winston.error.calledOnce); - var payload = sample.mocks.winston.error.firstCall.args[0]; - assert.deepEqual(payload.serviceContext, { service: 'myapp' }); assert.equal(response.text, expectedResult); }) .end(done); diff --git a/bigquery/system-test/quickstart.test.js b/bigquery/system-test/quickstart.test.js index 4436bc01bc..3f0b6f1ded 100644 --- a/bigquery/system-test/quickstart.test.js +++ b/bigquery/system-test/quickstart.test.js @@ -17,14 +17,17 @@ const proxyquire = require(`proxyquire`).noPreserveCache(); const bigquery = proxyquire(`@google-cloud/bigquery`, {})(); +var uuid = require('node-uuid'); -const datasetName = `my_new_dataset`; +const expectedDatasetId = `my_new_dataset`; +let datasetId = `nodejs-docs-samples-test-${uuid.v4()}`; +datasetId = datasetId.replace(/-/gi, `_`); -describe(`bigquery:quickstart`, () => { +describe.only(`bigquery:quickstart`, () => { let bigqueryMock, BigqueryMock; after((done) => { - bigquery.dataset(datasetName).delete(() => { + bigquery.dataset(datasetId).delete(() => { // Ignore any error, the dataset might not have been created done(); }); @@ -32,11 +35,11 @@ describe(`bigquery:quickstart`, () => { it(`should create a dataset`, (done) => { bigqueryMock = { - createDataset: (_datasetName, _callback) => { - assert.equal(_datasetName, datasetName); + createDataset: (_datasetId, _callback) => { + assert.equal(_datasetId, expectedDatasetId); assert.equal(typeof _callback, 'function'); - bigquery.createDataset(datasetName, (err, dataset, apiResponse) => { + bigquery.createDataset(datasetId, (err, dataset, apiResponse) => { _callback(err, dataset, apiResponse); assert.ifError(err); assert.notEqual(dataset, undefined); diff --git a/translate/system-test/translate.test.js b/translate/system-test/translate.test.js index b076569a6e..acfb60678d 100644 --- a/translate/system-test/translate.test.js +++ b/translate/system-test/translate.test.js @@ -1,92 +1,66 @@ -// Copyright 2015-2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -var ISO6391 = require('iso-639-1'); -var program = require('../translate'); +const Translate = require(`@google-cloud/translate`); +const path = require(`path`); +const run = require(`../../utils`).run; -var text = 'Hello world!'; -var toLang = 'ru'; +const cwd = path.join(__dirname, `..`); +const cmd = `node translate.js`; +const text = `Hello world!`; +const toLang = `ru`; -describe('translate:translate', function () { +describe(`translate:translate`, () => { + const translate = Translate({ + key: process.env.TRANSLATE_API_KEY + }); if (!process.env.TRANSLATE_API_KEY) { - process.stdout.write('Skipping Translate API tests...\n'); + process.stdout.write(`Skipping Translate API tests...\n`); return; } - describe('detectLanguage', function () { - it('should detect language', function (done) { - program.detectLanguage(text, function (err, result, apiResponse) { - assert.equal(err, null); - assert.notEqual(result, undefined); - assert.equal(result.language, 'en', 'should have detected english'); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', result]); - assert.notEqual(apiResponse, undefined); - done(); - }); + it(`should detect language`, (done) => { + const output = run(`${cmd} detect "${text}"`, cwd); + translate.detect(text, (err, result) => { + assert.ifError(err); + assert.equal(output, `Detected: ${JSON.stringify(result)}`); + done(); }); }); - describe('listLanguages', function () { - it('should list languages', function (done) { - program.listLanguages(function (err, languages, apiResponse) { - assert.equal(err, null); - assert.equal(Array.isArray(languages), true); - assert.equal(languages.length > 0, true); - var matchingLanguages = languages.filter(function (language) { - return language.code === 'af' && language.name === 'Afrikaans'; - }); - assert.equal(matchingLanguages.length, 1, 'found language with name in English'); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); - assert.notEqual(apiResponse, undefined); - done(); - }); - }); + it(`should list languages`, () => { + const output = run(`${cmd} list`, cwd); + assert.notEqual(output.indexOf(`Languages:`), -1); + assert.notEqual(output.indexOf(`{ code: 'af', name: 'Afrikaans' }`), -1); }); - describe('listLanguagesWithTarget', function () { - it('should list languages with a target', function (done) { - program.listLanguagesWithTarget('es', function (err, languages, apiResponse) { - assert.equal(err, null); - assert.equal(Array.isArray(languages), true); - assert.equal(languages.length > 0, true); - var matchingLanguages = languages.filter(function (language) { - return language.code === 'af' && language.name === 'afrikáans'; - }); - assert.equal(matchingLanguages.length, 1, 'found language with name in Spanish'); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', languages.length]); - assert.notEqual(apiResponse, undefined); - done(); - }); - }); + it(`should list languages with a target`, () => { + const output = run(`${cmd} list es`, cwd); + assert.notEqual(output.indexOf(`Languages:`), -1); + assert.notEqual(output.indexOf(`{ code: 'af', name: 'afrikáans' }`), -1); }); - describe('translateText', function () { - it('should translate text', function (done) { - var expected = 'Привет мир!'; - - program.translateText(text, toLang, undefined, function (err, translation, apiResponse) { - assert.equal(err, null); - assert.equal(translation, expected); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]); - assert.notEqual(apiResponse, undefined); - done(); - }); + it(`should translate text`, (done) => { + const output = run(`${cmd} translate ${toLang} "${text}"`, cwd); + translate.translate(text, toLang, (err, translation) => { + assert.ifError(err); + assert.notEqual(output.indexOf(`Text: ["${text}"]`), -1); + assert.notEqual(output.indexOf(`Translation: "${translation}"`), -1); + done(); }); }); }); diff --git a/translate/test/translate.test.js b/translate/test/translate.test.js index dbbf2b6c84..91b296c3c0 100644 --- a/translate/test/translate.test.js +++ b/translate/test/translate.test.js @@ -1,281 +1,44 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -var ISO6391 = require('iso-639-1'); -var proxyquire = require('proxyquire').noCallThru(); - -var text = 'Hello world!'; -var apiKey = 'key'; -var target = 'es'; -var toLang = 'ru'; - -function getSample () { - var apiResponseMock = {}; - var languagesMock = [ - 'en', - toLang - ]; - var resultMock = { - language: 'en', - confidence: 0.75, - input: text - }; - var translationMock = 'Привет мир!'; - var translateMock = { - getLanguages: sinon.stub().yields(null, languagesMock, apiResponseMock), - detect: sinon.stub().yields(null, resultMock, apiResponseMock), - translate: sinon.stub().yields(null, translationMock, apiResponseMock) - }; - var TranslateMock = sinon.stub().returns(translateMock); - - return { - program: proxyquire('../translate', { - '@google-cloud/translate': TranslateMock, - yargs: proxyquire('yargs', {}) - }), - mocks: { - Translate: TranslateMock, - translate: translateMock, - languages: languagesMock, - result: resultMock, - translation: translationMock, - apiResponse: apiResponseMock - } - }; -} - -describe('translate:translate', function () { - describe('detectLanguage', function () { - it('should detect language', function () { - var sample = getSample(); - var callback = sinon.stub(); - - sample.program.detectLanguage(text, callback); - - assert.equal(sample.mocks.translate.detect.calledOnce, true); - assert.deepEqual(sample.mocks.translate.detect.firstCall.args.slice(0, -1), [text]); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [null, sample.mocks.result, sample.mocks.apiResponse]); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Detected language(s):', sample.mocks.result]); - }); - - it('should handle error', function () { - var error = new Error('error'); - var sample = getSample(); - var callback = sinon.stub(); - sample.mocks.translate.detect.yields(error); - - sample.program.detectLanguage(text, callback); - - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [error]); - }); - }); - - describe('listLanguages', function () { - it('should list languages', function () { - var sample = getSample(); - var callback = sinon.stub(); - - sample.program.listLanguages(callback); - - assert.equal(sample.mocks.translate.getLanguages.calledOnce, true); - assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), []); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages, sample.mocks.apiResponse]); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]); - }); - - it('should handle error', function () { - var error = new Error('error'); - var sample = getSample(); - var callback = sinon.stub(); - sample.mocks.translate.getLanguages.yields(error); - - sample.program.listLanguages(callback); - - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [error]); - }); - }); - - describe('listLanguagesWithTarget', function () { - it('should list languages', function () { - var sample = getSample(); - var callback = sinon.stub(); - - sample.program.listLanguagesWithTarget(target, callback); - - assert.equal(sample.mocks.translate.getLanguages.calledOnce, true); - assert.deepEqual(sample.mocks.translate.getLanguages.firstCall.args.slice(0, -1), [target]); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [null, sample.mocks.languages, sample.mocks.apiResponse]); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Found %d language(s)!', sample.mocks.languages.length]); - }); - - it('should handle error', function () { - var error = new Error('error'); - var sample = getSample(); - var callback = sinon.stub(); - sample.mocks.translate.getLanguages.yields(error); - - sample.program.listLanguagesWithTarget(target, callback); - - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [error]); - }); - }); - - describe('translateText', function () { - it('should translate text', function () { - var sample = getSample(); - var callback = sinon.stub(); - - sample.program.translateText(text, toLang, undefined, callback); - - assert.equal(sample.mocks.translate.translate.calledOnce, true); - assert.deepEqual(sample.mocks.translate.translate.firstCall.args.slice(0, -1), [text, { to: toLang, from: undefined }]); - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [null, sample.mocks.translation, sample.mocks.apiResponse]); - assert.equal(console.log.calledOnce, true); - assert.deepEqual(console.log.firstCall.args, ['Translated to %s:', ISO6391.getName(toLang)]); - }); - - it('should handle error', function () { - var error = new Error('error'); - var sample = getSample(); - var callback = sinon.stub(); - sample.mocks.translate.translate.yields(error); - - sample.program.translateText(text, toLang, undefined, callback); - - assert.equal(callback.calledOnce, true); - assert.deepEqual(callback.firstCall.args, [error]); - }); - }); - - describe('main', function () { - it('should call detectLanguage', function () { - var program = getSample().program; - - sinon.stub(program, 'detectLanguage'); - program.main(['detect', text]); - assert.equal(program.detectLanguage.calledOnce, true); - assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [[text]]); - }); - - describe('detectLanguage with inline API key', function () { - var originalApiKey; - - before(function () { - originalApiKey = process.env.TRANSLATE_API_KEY; - delete process.env.TRANSLATE_API_KEY; - }); - - after(function () { - process.env.TRANSLATE_API_KEY = originalApiKey; - }); - - it('should set env var from option', function () { - var program = getSample().program; - - sinon.stub(program, 'detectLanguage'); - assert.equal(process.env.TRANSLATE_API_KEY, undefined); - program.main(['detect', text, '-k', apiKey]); - assert.equal(process.env.TRANSLATE_API_KEY, apiKey); - assert.equal(program.detectLanguage.calledOnce, true); - assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [[text]]); - }); - }); - - it('should call listLanguages', function () { - var program = getSample().program; - - sinon.stub(program, 'listLanguages'); - program.main(['list']); - assert.equal(program.listLanguages.calledOnce, true); - assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), []); - }); - - it('should call listLanguagesWithTarget', function () { - var program = getSample().program; - - sinon.stub(program, 'listLanguagesWithTarget'); - program.main(['list', target]); - assert.equal(program.listLanguagesWithTarget.calledOnce, true); - assert.deepEqual(program.listLanguagesWithTarget.firstCall.args.slice(0, -1), [target]); - }); - - describe('listLanguagesWithTarget with inline API key', function () { - var originalApiKey; - - before(function () { - originalApiKey = process.env.TRANSLATE_API_KEY; - delete process.env.TRANSLATE_API_KEY; - }); - - after(function () { - process.env.TRANSLATE_API_KEY = originalApiKey; - }); - - it('should set env var from option', function () { - var program = getSample().program; - - sinon.stub(program, 'listLanguagesWithTarget'); - assert.equal(process.env.TRANSLATE_API_KEY, undefined); - program.main(['list', target, '-k', apiKey]); - assert.equal(process.env.TRANSLATE_API_KEY, apiKey); - assert.equal(program.listLanguagesWithTarget.calledOnce, true); - assert.deepEqual(program.listLanguagesWithTarget.firstCall.args.slice(0, -1), [target]); - }); - }); - - it('should call translateText', function () { - var program = getSample().program; - - sinon.stub(program, 'translateText'); - program.main(['translate', toLang, text]); - assert.equal(program.translateText.calledOnce, true); - assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [[text], toLang, undefined]); - }); - - describe('translateText with inline API key', function () { - var originalApiKey; - - before(function () { - originalApiKey = process.env.TRANSLATE_API_KEY; - delete process.env.TRANSLATE_API_KEY; - }); - - after(function () { - process.env.TRANSLATE_API_KEY = originalApiKey; - }); - - it('should set env var from option', function () { - var program = getSample().program; - - sinon.stub(program, 'translateText'); - assert.equal(process.env.TRANSLATE_API_KEY, undefined); - program.main(['translate', toLang, text, '-k', apiKey]); - assert.equal(process.env.TRANSLATE_API_KEY, apiKey); - assert.equal(program.translateText.calledOnce, true); - assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [[text], toLang, undefined]); - }); - }); +const proxyquire = require(`proxyquire`).noCallThru(); + +describe(`translate:translate`, () => { + it(`should handle errors`, () => { + const error = new Error(`error`); + const text = `Hello world!`; + const toLang = `ru`; + const callback = sinon.spy(); + const translateMock = { + getLanguages: sinon.stub().yields(error), + detect: sinon.stub().yields(error), + translate: sinon.stub().yields(error) + }; + const TranslateMock = sinon.stub().returns(translateMock); + const program = proxyquire(`../translate`, { + '@google-cloud/translate': TranslateMock + }); + + program.detectLanguage(text, callback); + program.listLanguages(callback); + program.listLanguagesWithTarget(toLang, callback); + program.translateText(text, toLang, callback); + + assert.equal(callback.callCount, 4); + assert.equal(callback.alwaysCalledWithExactly(error), true); }); }); diff --git a/translate/translate.js b/translate/translate.js index da6072cf99..bc3e108702 100644 --- a/translate/translate.js +++ b/translate/translate.js @@ -1,121 +1,127 @@ -// Copyright 2016, Google, Inc. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * Copyright 2016, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 'use strict'; -// [START setup] -// By default, the client will use the project specified by the GCLOUD_PROJECT -// environment variable. The Translate API uses an API key for authentication. -// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication -var Translate = require('@google-cloud/translate'); -// [END setup] +const Translate = require('@google-cloud/translate'); +// [START translate_detect_language] function detectLanguage (input, callback) { - var translate = Translate({ + // Instantiates a client + const translate = Translate({ // The Translate API uses an API key for authentication. This sample looks - // at an environment variable for the key. + // for the key in an environment variable. key: process.env.TRANSLATE_API_KEY }); - // "input" can be a string for detecting the language of a single piece of - // text, or an array of strings for detecting the languages of multiple texts. - // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate?method=detect - translate.detect(input, function (err, result, apiResponse) { + // Detects the language. "input" can be a string for detecting the language of + // a single piece of text, or an array of strings for detecting the languages + // of multiple texts. + translate.detect(input, (err, result) => { if (err) { - return callback(err); + callback(err); + return; } - console.log('Detected language(s):', result); - return callback(null, result, apiResponse); + console.log('Detected: %j', result); + callback(); }); } +// [END translate_detect_language] +// [START translate_list_codes] function listLanguages (callback) { - var translate = Translate({ + // Instantiates a client + const translate = Translate({ // The Translate API uses an API key for authentication. This sample looks - // at an environment variable for the key. + // for the key in an environment variable. key: process.env.TRANSLATE_API_KEY }); - // List available translation language with their names in English (the default). - // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate?method=getLanguages - translate.getLanguages(function (err, languages, apiResponse) { + // Lists available translation language with their names in English (the default). + translate.getLanguages((err, languages) => { if (err) { - return callback(err); + callback(err); + return; } - console.log('Found %d language(s)!', languages.length); - return callback(null, languages, apiResponse); + console.log('Languages:'); + languages.forEach((language) => console.log(language)); + callback(); }); } +// [END translate_list_codes] +// [START translate_list_language_names] function listLanguagesWithTarget (target, callback) { - var translate = Translate({ + // Instantiates a client + const translate = Translate({ // The Translate API uses an API key for authentication. This sample looks - // at an environment variable for the key. + // for the key in an environment variable. key: process.env.TRANSLATE_API_KEY }); - // List available translation language with their names in the target language. - // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate?method=getLanguages - translate.getLanguages(target, function (err, languages, apiResponse) { + // Lists available translation language with their names in a target language + translate.getLanguages(target, (err, languages) => { if (err) { - return callback(err); + callback(err); + return; } - console.log('Found %d language(s)!', languages.length); - return callback(null, languages, apiResponse); + console.log('Languages:'); + languages.forEach((language) => console.log(language)); + callback(); }); } +// [END translate_list_language_names] -// [START translate_text] -// Helper library for language codes -var ISO6391 = require('iso-639-1'); - -function translateText (input, toLang, fromLang, callback) { - var translate = Translate({ +// [START translate_translate_text] +function translateText (input, target, callback) { + // Instantiates a client + const translate = Translate({ // The Translate API uses an API key for authentication. This sample looks - // at an environment variable for the key. + // for the key in an environment variable. key: process.env.TRANSLATE_API_KEY }); - // "input" can be a string for translating a single piece of text, or an array - // of strings for translating multiple texts. - // See https://googlecloudplatform.github.io/gcloud-node/#/docs/translate/latest/translate?method=translate - translate.translate(input, { - from: fromLang, - to: toLang - }, function (err, translation, apiResponse) { + // Translates the text into the target language. "input" can be a string for + // translating a single piece of text, or an array of strings for translating + // multiple texts. + translate.translate(input, target, (err, translation) => { if (err) { - return callback(err); + callback(err); + return; } - console.log('Translated to %s:', ISO6391.getName(toLang)); - return callback(null, translation, apiResponse); + console.log('Text: %j', input); + console.log('Translation: %j', translation); + callback(); }); } -// [END translate_text] +// [END translate_translate_text] // The command-line program -var cli = require('yargs'); -var utils = require('../utils'); +const cli = require(`yargs`); +const noop = require(`../utils`).noop; -var program = module.exports = { +const program = module.exports = { detectLanguage: detectLanguage, listLanguages: listLanguages, listLanguagesWithTarget: listLanguagesWithTarget, translateText: translateText, - main: function (args) { + main: (args) => { // Run the command-line program cli.help().strict().parse(args).argv; } @@ -123,52 +129,44 @@ var program = module.exports = { cli .demand(1) - .command('detect ', 'Detect the language of the provided text or texts', {}, function (options) { + .command(`detect `, `Detects the language of the provided text or texts`, {}, (opts) => { if (!process.env.TRANSLATE_API_KEY) { - process.env.TRANSLATE_API_KEY = options.apiKey; + process.env.TRANSLATE_API_KEY = opts.apiKey; } - program.detectLanguage(options.input, utils.makeHandler(false)); + program.detectLanguage(opts.input, noop); }) - .command('list [target]', 'List available translation languages. To return language names in a language other than English, specify a target language.', {}, function (options) { + .command(`list [target]`, `Lists available translation languages. To return language names in a language other than English, specify a target language.`, {}, (opts) => { if (!process.env.TRANSLATE_API_KEY) { - process.env.TRANSLATE_API_KEY = options.apiKey; + process.env.TRANSLATE_API_KEY = opts.apiKey; } - if (options.target) { - program.listLanguagesWithTarget(options.target, utils.makeHandler()); + if (opts.target) { + program.listLanguagesWithTarget(opts.target, noop); } else { - program.listLanguages(utils.makeHandler()); + program.listLanguages(noop); } }) - .command('translate ', 'Translate the provided text or texts to the target language, optionally specifying the source language.', { - fromLang: { - alias: 'f', - requiresArg: true, - type: 'string', - description: 'The language of the source text.' - } - }, function (options) { + .command(`translate `, `Translates the provided text or texts to the target language.`, {}, (opts) => { if (!process.env.TRANSLATE_API_KEY) { - process.env.TRANSLATE_API_KEY = options.apiKey; + process.env.TRANSLATE_API_KEY = opts.apiKey; } - program.translateText(options.input, options.toLang, options.fromLang, utils.makeHandler()); + program.translateText(opts.input, opts.toLang, noop); }) - .option('apiKey', { - alias: 'k', + .option(`apiKey`, { + alias: `k`, global: true, requiresArg: true, default: process.env.TRANSLATE_API_KEY, - type: 'string', - description: 'Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.' + type: `string`, + description: `Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable.` }) - .example('node $0 detect "Hello world!"', 'Detect the language of "Hello world!".') - .example('node $0 detect -k your-api-key "Hello world!" "Goodbye"', 'Detect the language of "Hello world!" and "Goodbye", supplying the API key inline..') - .example('node $0 list -k your-api-key', 'List available translation languages with names in English, supplying the API key inline..') - .example('node $0 list es', 'List available translation languages with names in Spanish.') - .example('node $0 translate ru "Good morning!"', 'Translate "Good morning!" to Russian, auto-detecting the source language.') - .example('node $0 translate ru "Good morning!" -f en -k your-api-key', 'Translate "Good morning!" to Russian from English, supplying the API key inline.') + .example(`node $0 detect "Hello world!"`, `Detects the language of "Hello world!".`) + .example(`node $0 detect -k YOUR_API_KEY "Hello world!" "Goodbye"`, `Detects the language of "Hello world!" and "Goodbye", supplying the API key inline.`) + .example(`node $0 list -k YOUR_API_KEY`, `Lists available translation languages with names in English, supplying the API key inline.`) + .example(`node $0 list es`, `Lists available translation languages with names in Spanish.`) + .example(`node $0 translate ru "Good morning!"`, `Translates "Good morning!" to Russian, auto-detecting the source language.`) .wrap(120) .recommendCommands() - .epilogue('For more information, see https://cloud.google.com/translate/docs'); + .epilogue(`For more information, see https://cloud.google.com/translate/docs`); if (module === require.main) { program.main(process.argv.slice(2));