From 69634b854580b162111ffdb995d9ca63c9048fc9 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Tue, 24 Oct 2017 16:17:05 -0700 Subject: [PATCH] Repo Migration (#6) --- translate/.eslintrc.yml | 3 + translate/package.json | 33 ++--- translate/quickstart.js | 11 +- translate/system-test/.eslintrc.yml | 5 + translate/system-test/quickstart.test.js | 13 +- translate/system-test/translate.test.js | 44 +++++-- translate/translate.js | 160 +++++++++++++---------- 7 files changed, 154 insertions(+), 115 deletions(-) create mode 100644 translate/.eslintrc.yml create mode 100644 translate/system-test/.eslintrc.yml diff --git a/translate/.eslintrc.yml b/translate/.eslintrc.yml new file mode 100644 index 00000000000..282535f55f6 --- /dev/null +++ b/translate/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + no-console: off diff --git a/translate/package.json b/translate/package.json index 3cd11dc889e..33e343ddf3c 100644 --- a/translate/package.json +++ b/translate/package.json @@ -4,40 +4,23 @@ "private": true, "license": "Apache-2.0", "author": "Google Inc.", - "repository": { - "type": "git", - "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" - }, + "repository": "googleapis/nodejs-translate", "engines": { "node": ">=4.3.2" }, "scripts": { - "lint": "samples lint", - "pretest": "npm run lint", - "test": "samples test run --cmd ava -- -T 20s --verbose system-test/*.test.js" + "ava": "ava -T 20s --verbose test/*.test.js ./system-test/*.test.js", + "cover": "nyc --reporter=lcov --cache ava -T 20s --verbose test/*.test.js ./system-test/*.test.js && nyc report", + "test": "repo-tools test run --cmd npm -- run cover" }, "dependencies": { "@google-cloud/translate": "1.0.0", - "yargs": "8.0.2" + "yargs": "10.0.3" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "1.4.17", - "ava": "0.21.0", + "@google-cloud/nodejs-repo-tools": "2.0.11", + "ava": "0.22.0", "proxyquire": "1.8.0", - "sinon": "3.2.0" - }, - "cloud-repo-tools": { - "requiresKeyFile": true, - "requiresProjectId": true, - "product": "translate", - "samples": [ - { - "id": "translate", - "name": "Translate", - "file": "translate.js", - "docs_link": "https://cloud.google.com/translate/docs", - "usage": "node translate.js --help" - } - ] + "sinon": "4.0.1" } } diff --git a/translate/quickstart.js b/translate/quickstart.js index 473201dc215..6fd4e0350b1 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -23,8 +23,8 @@ const Translate = require('@google-cloud/translate'); const projectId = 'YOUR_PROJECT_ID'; // Instantiates a client -const translateClient = Translate({ - projectId: projectId +const translate = new Translate({ + projectId: projectId, }); // The text to translate @@ -33,14 +33,15 @@ const text = 'Hello, world!'; const target = 'ru'; // Translates some text into Russian -translateClient.translate(text, target) - .then((results) => { +translate + .translate(text, target) + .then(results => { const translation = results[0]; console.log(`Text: ${text}`); console.log(`Translation: ${translation}`); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_quickstart] diff --git a/translate/system-test/.eslintrc.yml b/translate/system-test/.eslintrc.yml new file mode 100644 index 00000000000..c0289282a68 --- /dev/null +++ b/translate/system-test/.eslintrc.yml @@ -0,0 +1,5 @@ +--- +rules: + node/no-unpublished-require: off + node/no-unsupported-features: off + no-empty: off diff --git a/translate/system-test/quickstart.test.js b/translate/system-test/quickstart.test.js index 408d722e469..8516f23cb56 100644 --- a/translate/system-test/quickstart.test.js +++ b/translate/system-test/quickstart.test.js @@ -25,7 +25,7 @@ test.before(tools.checkCredentials); test.before(tools.stubConsole); test.after.always(tools.restoreConsole); -test.cb(`should translate a string`, (t) => { +test.cb(`should translate a string`, t => { const string = `Hello, world!`; const expectedTranslation = `Привет мир!`; const targetLanguage = `ru`; @@ -34,7 +34,8 @@ test.cb(`should translate a string`, (t) => { t.is(_string, string); t.is(_targetLanguage, targetLanguage); - return translate.translate(_string, _targetLanguage) + return translate + .translate(_string, _targetLanguage) .then(([translation]) => { t.is(translation, expectedTranslation); @@ -42,7 +43,9 @@ test.cb(`should translate a string`, (t) => { try { t.is(console.log.callCount, 2); t.deepEqual(console.log.getCall(0).args, [`Text: ${string}`]); - t.deepEqual(console.log.getCall(1).args, [`Translation: ${expectedTranslation}`]); + t.deepEqual(console.log.getCall(1).args, [ + `Translation: ${expectedTranslation}`, + ]); t.end(); } catch (err) { t.end(err); @@ -51,10 +54,10 @@ test.cb(`should translate a string`, (t) => { return [translation]; }); - } + }, }; proxyquire(`../quickstart`, { - '@google-cloud/translate': sinon.stub().returns(translateMock) + '@google-cloud/translate': sinon.stub().returns(translateMock), }); }); diff --git a/translate/system-test/translate.test.js b/translate/system-test/translate.test.js index 206dbf33255..420a3e594f5 100644 --- a/translate/system-test/translate.test.js +++ b/translate/system-test/translate.test.js @@ -29,55 +29,71 @@ const toLang = `ru`; test.before(tools.checkCredentials); -test(`should detect language of a single string`, async (t) => { +test(`should detect language of a single string`, async t => { const output = await tools.runAsync(`${cmd} detect "${text}"`, cwd); const [detection] = await translate.detect(text); const expected = `Detections:\n${text} => ${detection.language}`; t.is(output, expected); }); -test(`should detect language of multiple strings`, async (t) => { - const output = await tools.runAsync(`${cmd} detect "${text}" "${text2}"`, cwd); +test(`should detect language of multiple strings`, async t => { + const output = await tools.runAsync( + `${cmd} detect "${text}" "${text2}"`, + cwd + ); const [detections] = await translate.detect([text, text2]); - const expected = `Detections:\n${text} => ${detections[0].language}\n${text2} => ${detections[1].language}`; + const expected = `Detections:\n${text} => ${detections[0] + .language}\n${text2} => ${detections[1].language}`; t.is(output, expected); }); -test(`should list languages`, async (t) => { +test(`should list languages`, async t => { const output = await tools.runAsync(`${cmd} list`, cwd); t.true(output.includes(`Languages:`)); t.true(output.includes(`{ code: 'af', name: 'Afrikaans' }`)); }); -test(`should list languages with a target`, async (t) => { +test(`should list languages with a target`, async t => { const output = await tools.runAsync(`${cmd} list es`, cwd); t.true(output.includes(`Languages:`)); t.true(output.includes(`{ code: 'af', name: 'afrikáans' }`)); }); -test(`should translate a single string`, async (t) => { - const output = await tools.runAsync(`${cmd} translate ${toLang} "${text}"`, cwd); +test(`should translate a single string`, async t => { + const output = await tools.runAsync( + `${cmd} translate ${toLang} "${text}"`, + cwd + ); const [translation] = await translate.translate(text, toLang); const expected = `Translations:\n${text} => (${toLang}) ${translation}`; t.is(output, expected); }); -test(`should translate multiple strings`, async (t) => { - const output = await tools.runAsync(`${cmd} translate ${toLang} "${text}" "${text2}"`, cwd); +test(`should translate multiple strings`, async t => { + const output = await tools.runAsync( + `${cmd} translate ${toLang} "${text}" "${text2}"`, + cwd + ); const [translations] = await translate.translate([text, text2], toLang); const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`; t.is(output, expected); }); -test(`should translate a single string with a model`, async (t) => { - const output = await tools.runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}"`, cwd); +test(`should translate a single string with a model`, async t => { + const output = await tools.runAsync( + `${cmd} translate-with-model ${toLang} ${model} "${text}"`, + cwd + ); const [translation] = await translate.translate(text, toLang); const expected = `Translations:\n${text} => (${toLang}) ${translation}`; t.is(output, expected); }); -test(`should translate multiple strings with a model`, async (t) => { - const output = await tools.runAsync(`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, cwd); +test(`should translate multiple strings with a model`, async t => { + const output = await tools.runAsync( + `${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`, + cwd + ); const [translations] = await translate.translate([text, text2], toLang); const expected = `Translations:\n${text} => (${toLang}) ${translations[0]}\n${text2} => (${toLang}) ${translations[1]}`; t.is(output, expected); diff --git a/translate/translate.js b/translate/translate.js index c1145ce0d9e..ac1d78213f9 100644 --- a/translate/translate.js +++ b/translate/translate.js @@ -15,173 +15,184 @@ 'use strict'; -function detectLanguage (text) { +function detectLanguage(text) { // [START translate_detect_language] // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); - // Instantiates a client - const translate = Translate(); + // Creates a client + const translate = new Translate(); - // The text for which to detect language, e.g. "Hello, world!" - // const text = 'Hello, world!'; + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const text = 'The text for which to detect language, e.g. Hello, world!'; // Detects the language. "text" 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(text) - .then((results) => { + translate + .detect(text) + .then(results => { let detections = results[0]; detections = Array.isArray(detections) ? detections : [detections]; console.log('Detections:'); - detections.forEach((detection) => { + detections.forEach(detection => { console.log(`${detection.input} => ${detection.language}`); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_detect_language] } -function listLanguages () { +function listLanguages() { // [START translate_list_codes] // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); - // Instantiates a client - const translate = Translate(); + // Creates a client + const translate = new Translate(); // Lists available translation language with their names in English (the default). - translate.getLanguages() - .then((results) => { + translate + .getLanguages() + .then(results => { const languages = results[0]; console.log('Languages:'); - languages.forEach((language) => console.log(language)); + languages.forEach(language => console.log(language)); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_list_codes] } -function listLanguagesWithTarget (target) { +function listLanguagesWithTarget(target) { // [START translate_list_language_names] // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); - // Instantiates a client - const translate = Translate(); + // Creates a client + const translate = new Translate(); - // The target language for language names, e.g. "ru" - // const target = 'ru'; + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const target = 'The target language for language names, e.g. ru'; // Lists available translation language with their names in a target language - translate.getLanguages(target) - .then((results) => { + translate + .getLanguages(target) + .then(results => { const languages = results[0]; console.log('Languages:'); - languages.forEach((language) => console.log(language)); + languages.forEach(language => console.log(language)); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_list_language_names] } -function translateText (text, target) { +function translateText(text, target) { // [START translate_translate_text] // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); - // Instantiates a client - const translate = Translate(); + // Creates a client + const translate = new Translate(); - // The text to translate, e.g. "Hello, world!" - // const text = 'Hello, world!'; - - // The target language, e.g. "ru" - // const target = 'ru'; + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const text = 'The text to translate, e.g. Hello, world!'; + // const target = 'The target language, e.g. ru'; // Translates the text into the target language. "text" can be a string for // translating a single piece of text, or an array of strings for translating // multiple texts. - translate.translate(text, target) - .then((results) => { + translate + .translate(text, target) + .then(results => { let translations = results[0]; - translations = Array.isArray(translations) ? translations : [translations]; + translations = Array.isArray(translations) + ? translations + : [translations]; console.log('Translations:'); translations.forEach((translation, i) => { console.log(`${text[i]} => (${target}) ${translation}`); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_translate_text] } -function translateTextWithModel (text, target, model) { +function translateTextWithModel(text, target, model) { // [START translate_text_with_model] // Imports the Google Cloud client library const Translate = require('@google-cloud/translate'); - // Instantiates a client - const translate = Translate(); - - // The text to translate, e.g. "Hello, world!" - // const text = 'Hello, world!'; - - // The target language, e.g. "ru" - // const target = 'ru'; + // Creates a client + const translate = new Translate(); - // The model to use, e.g. "nmt" - // const model = 'nmt'; + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const text = 'The text to translate, e.g. Hello, world!'; + // const target = 'The target language, e.g. ru'; + // const model = 'The model to use, e.g. nmt'; const options = { // The target language, e.g. "ru" to: target, // Make sure your project is whitelisted. // Possible values are "base" and "nmt" - model: model + model: model, }; // Translates the text into the target language. "text" can be a string for // translating a single piece of text, or an array of strings for translating // multiple texts. - translate.translate(text, options) - .then((results) => { + translate + .translate(text, options) + .then(results => { let translations = results[0]; - translations = Array.isArray(translations) ? translations : [translations]; + translations = Array.isArray(translations) + ? translations + : [translations]; console.log('Translations:'); translations.forEach((translation, i) => { console.log(`${text[i]} => (${target}) ${translation}`); }); }) - .catch((err) => { + .catch(err => { console.error('ERROR:', err); }); // [END translate_text_with_model] } -require(`yargs`) // eslint-disable-line +require(`yargs`) .demand(1) .command( `detect `, `Detects the language of one or more strings.`, {}, - (opts) => detectLanguage(opts.text) + opts => detectLanguage(opts.text) ) .command( `list [target]`, `Lists available translation languages. To language names in a language other than English, specify a target language.`, {}, - (opts) => { + opts => { if (opts.target) { listLanguagesWithTarget(opts.target); } else { @@ -193,24 +204,41 @@ require(`yargs`) // eslint-disable-line `translate `, `Translates one or more strings into the target language.`, {}, - (opts) => translateText(opts.text, opts.toLang) + opts => translateText(opts.text, opts.toLang) ) .command( `translate-with-model `, `Translates one or more strings into the target language using the specified model.`, {}, - (opts) => translateTextWithModel(opts.text, opts.toLang, opts.model) + opts => translateTextWithModel(opts.text, opts.toLang, opts.model) ) .example(`node $0 detect "Hello world!"`, `Detects the language of a string.`) - .example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the languages of multiple strings.`) - .example(`node $0 list`, `Lists available translation languages with names in English.`) - .example(`node $0 list es`, `Lists available translation languages with names in Spanish.`) - .example(`node $0 translate ru "Good morning!"`, `Translates a string into Russian.`) - .example(`node $0 translate ru "Good morning!" "Good night!"`, `Translates multiple strings into Russian.`) - .example(`node $0 translate-with-model ru nmt "Good morning!" "Good night!"`, `Translates multiple strings into Russian using the Premium model.`) + .example( + `node $0 detect "Hello world!" "Goodbye"`, + `Detects the languages of multiple strings.` + ) + .example( + `node $0 list`, + `Lists available translation languages with names in English.` + ) + .example( + `node $0 list es`, + `Lists available translation languages with names in Spanish.` + ) + .example( + `node $0 translate ru "Good morning!"`, + `Translates a string into Russian.` + ) + .example( + `node $0 translate ru "Good morning!" "Good night!"`, + `Translates multiple strings into Russian.` + ) + .example( + `node $0 translate-with-model ru nmt "Good morning!" "Good night!"`, + `Translates multiple strings into Russian using the Premium model.` + ) .wrap(120) .recommendCommands() .epilogue(`For more information, see https://cloud.google.com/translate/docs`) .help() - .strict() - .argv; + .strict().argv;