From cad11e9ccd29126af38cb13731e252ba82424880 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 16 May 2019 11:15:48 -0400 Subject: [PATCH 1/4] Tweak eslint config to support ES2018. --- .eslintrc.js | 8 ++------ .prettierrc | 5 +++++ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 .prettierrc diff --git a/.eslintrc.js b/.eslintrc.js index 7e26d73..48faa91 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,7 +2,7 @@ module.exports = { root: true, parserOptions: { sourceType: 'script', - ecmaVersion: 2015 + ecmaVersion: 2018 }, plugins: [ 'node', @@ -17,11 +17,7 @@ module.exports = { node: true }, rules: { - 'prettier/prettier': ['error', { - singleQuote: true, - trailingComma: 'es5', - printWidth: 100, - }], + 'prettier/prettier': 'error', }, overrides: [ // test files diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1754bbf --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "trailingComma": 'es5', + "printWidth": 100 +} From 57fcb86577416bb1319131476e417b4d1143212f Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 16 May 2019 11:16:31 -0400 Subject: [PATCH 2/4] Drop Node 6 support. --- .travis.yml | 1 - package.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a783a3..fe1e80e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "6" - "8" - "10" - "12" diff --git a/package.json b/package.json index 707ff94..cac36a3 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "tmp": "^0.1.0" }, "engines": { - "node": "6.* || 8.* || >= 10.*" + "node": "8.* || >= 10.*" } } From 79c4111dae4fd237043da49077909f86bd92ef33 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 16 May 2019 11:16:52 -0400 Subject: [PATCH 3/4] Refactor to async/await usage. --- bin/ember-source-channel-url | 15 ++-- src/index.js | 8 +- tests/helpers/server.js | 2 +- tests/index-test.js | 145 +++++++++++++++++------------------ 4 files changed, 84 insertions(+), 86 deletions(-) diff --git a/bin/ember-source-channel-url b/bin/ember-source-channel-url index f3a14d8..b1b5a1a 100755 --- a/bin/ember-source-channel-url +++ b/bin/ember-source-channel-url @@ -37,11 +37,12 @@ EXAMPLE: `); } -if (['release', 'beta', 'canary'].indexOf(channel) === -1) { - printUsage(); - process.exitCode = 1; -} else { - getChannelURL(channel).then(url => { +async function main() { + if (['release', 'beta', 'canary'].indexOf(channel) === -1) { + printUsage(); + process.exitCode = 1; + } else { + let url = await getChannelURL(channel); if (process.stdout.isTTY) { console.log( `The URL for the latest tarball from ember-source's ${channel} channel is:\n\n\t${url}\n` @@ -85,5 +86,7 @@ if (['release', 'beta', 'canary'].indexOf(channel) === -1) { process.exitCode = 3; } } - }); + } } + +main(); diff --git a/src/index.js b/src/index.js index 5ce2260..aeb0da8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,11 @@ 'use strict'; const got = require('got'); -module.exports = function(channelType) { +module.exports = async function(channelType) { let HOST = process.env.EMBER_SOURCE_CHANNEL_URL_HOST || 'https://s3.amazonaws.com'; let PATH = 'builds.emberjs.com'; - return got(`${HOST}/${PATH}/${channelType}.json`, { json: true }).then( - result => `${HOST}/${PATH}${result.body.assetPath}` - ); + const result = await got(`${HOST}/${PATH}/${channelType}.json`, { json: true }); + + return `${HOST}/${PATH}${result.body.assetPath}`; }; diff --git a/tests/helpers/server.js b/tests/helpers/server.js index a5f8380..bb6fccc 100644 --- a/tests/helpers/server.js +++ b/tests/helpers/server.js @@ -4,7 +4,7 @@ const http = require('http'); const RSVP = require('rsvp'); const getPort = require('get-port'); -const host = module.exports.host = 'localhost'; +const host = (module.exports.host = 'localhost'); module.exports.createServer = function() { return getPort().then(port => { diff --git a/tests/index-test.js b/tests/index-test.js index 3b5c8f1..714ac86 100644 --- a/tests/index-test.js +++ b/tests/index-test.js @@ -18,25 +18,24 @@ QUnit.module('ember-source-channel-url', function(hooks) { return crypto.randomBytes(Math.ceil(length / 2)).toString('hex'); } - hooks.beforeEach(function() { + hooks.beforeEach(async function() { let dir = tmp.dirSync(); process.chdir(dir.name); - return createServer().then(server => { - process.env.EMBER_SOURCE_CHANNEL_URL_HOST = `http://localhost:${server.port}`; + let server = await createServer(); + process.env.EMBER_SOURCE_CHANNEL_URL_HOST = `http://localhost:${server.port}`; - this.server = server; - this.fakeSHA = randomString(20); - let assetPath = (this.assetPath = `/canary/shas/${this.fakeSHA}.tgz`); + this.server = server; + this.fakeSHA = randomString(20); + let assetPath = (this.assetPath = `/canary/shas/${this.fakeSHA}.tgz`); - this.expectedURL = `http://${server.host}:${server.port}/builds.emberjs.com${this.assetPath}`; + this.expectedURL = `http://${server.host}:${server.port}/builds.emberjs.com${this.assetPath}`; - server.on('/builds.emberjs.com/canary.json', (req, res) => { - res.end(JSON.stringify({ assetPath })); - }); - - return server.listen(server.port); + server.on('/builds.emberjs.com/canary.json', (req, res) => { + res.end(JSON.stringify({ assetPath })); }); + + return server.listen(server.port); }); hooks.afterEach(function() { @@ -45,53 +44,50 @@ QUnit.module('ember-source-channel-url', function(hooks) { return this.server.close(); }); - QUnit.test('works', function(assert) { + QUnit.test('works', async function(assert) { let expected = `http://${this.server.host}:${this.server.port}/builds.emberjs.com${ this.assetPath }`; - return getChannelURL('canary').then(actual => { - assert.equal(actual, expected); - }); + let actual = await getChannelURL('canary'); + assert.equal(actual, expected); }); QUnit.module('binary', function() { - QUnit.test('works', function(assert) { - return execa(EXECUTABLE_PATH, ['canary']).then(results => { - assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - }); + QUnit.test('works', async function(assert) { + let results = await execa(EXECUTABLE_PATH, ['canary']); + assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); }); - QUnit.test('when the terminal is not a TTY return only the URL', function(assert) { + QUnit.test('when the terminal is not a TTY return only the URL', async function(assert) { let file = tmp.fileSync(); - return execa(EXECUTABLE_PATH, ['canary'], { stdout: file.fd }).then(() => { - assert.equal( - fs.readFileSync(file.name, { encoding: 'utf8' }), - this.expectedURL, - 'stdout is the URL' - ); - }); + await execa(EXECUTABLE_PATH, ['canary'], { stdout: file.fd }); + assert.equal( + fs.readFileSync(file.name, { encoding: 'utf8' }), + this.expectedURL, + 'stdout is the URL' + ); }); - QUnit.test('updates local package.json when -w is passed (dependencies)', function(assert) { + QUnit.test('updates local package.json when -w is passed (dependencies)', async function( + assert + ) { fs.writeFileSync( 'package.json', JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }), { encoding: 'utf8' } ); - return execa(EXECUTABLE_PATH, ['canary', '-w']).then(results => { - assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - - assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), { - dependencies: { - 'ember-source': this.expectedURL, - }, - }); + let results = await execa(EXECUTABLE_PATH, ['canary', '-w']); + assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); + assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), { + dependencies: { + 'ember-source': this.expectedURL, + }, }); }); - QUnit.test('updates local package.json when --write is passed (dependencies)', function( + QUnit.test('updates local package.json when --write is passed (dependencies)', async function( assert ) { fs.writeFileSync( @@ -100,77 +96,76 @@ QUnit.module('ember-source-channel-url', function(hooks) { { encoding: 'utf8' } ); - return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => { - assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - - assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), { - dependencies: { - 'ember-source': this.expectedURL, - }, - }); + let results = await execa(EXECUTABLE_PATH, ['canary', '--write']); + assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); + assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), { + dependencies: { + 'ember-source': this.expectedURL, + }, }); }); - QUnit.test('updates local package.json when --write is passed (devDependencies)', function( - assert - ) { - fs.writeFileSync( - 'package.json', - JSON.stringify({ devDependencies: { 'ember-source': '^3.10.0' } }), - { encoding: 'utf8' } - ); + QUnit.test( + 'updates local package.json when --write is passed (devDependencies)', + async function(assert) { + fs.writeFileSync( + 'package.json', + JSON.stringify({ devDependencies: { 'ember-source': '^3.10.0' } }), + { encoding: 'utf8' } + ); - return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => { + let results = await execa(EXECUTABLE_PATH, ['canary', '--write']); assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), { devDependencies: { 'ember-source': this.expectedURL, }, }); - }); - }); + } + ); - QUnit.test('preserves line ending when updating package.json', function(assert) { + QUnit.test('preserves line ending when updating package.json', async function(assert) { fs.writeFileSync( 'package.json', JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }, null, 2) + '\n', { encoding: 'utf8' } ); - return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => { - assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - - let expected = - JSON.stringify({ dependencies: { 'ember-source': this.expectedURL } }, null, 2) + '\n'; - assert.deepEqual(fs.readFileSync('package.json', { encoding: 'utf8' }), expected); - }); + let results = await execa(EXECUTABLE_PATH, ['canary', '--write']); + assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); + let expected = + JSON.stringify({ dependencies: { 'ember-source': this.expectedURL } }, null, 2) + '\n'; + assert.deepEqual(fs.readFileSync('package.json', { encoding: 'utf8' }), expected); }); - QUnit.test('fails when package.json is missing', function(assert) { - return execa(EXECUTABLE_PATH, ['canary', '--write']).catch(results => { + QUnit.test('fails when package.json is missing', async function(assert) { + try { + await execa(EXECUTABLE_PATH, ['canary', '--write']); + } catch (results) { assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - assert.ok( results.stdout.includes('no package.json is available to update'), 'warning is printed indicating -w failed' ); - }); + } }); - QUnit.test('fails when ember-source is not a dep', function(assert) { - fs.writeFileSync('package.json', JSON.stringify({}), { encoding: 'utf8' }); + QUnit.test('fails when ember-source is not a dep', async function(assert) { + fs.writeFileSync('package.json', JSON.stringify({}), { + encoding: 'utf8', + }); - return execa(EXECUTABLE_PATH, ['canary', '--write']).catch(results => { + try { + await execa(EXECUTABLE_PATH, ['canary', '--write']); + } catch (results) { assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout'); - assert.ok( results.stdout.includes( 'ember-source is not included in dependencies or devDependencies' ), 'warning is printed indicating -w failed' ); - }); + } }); }); }); From 3ed2b296bc3bd82bea840ed03441efb848edc4ce Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 16 May 2019 11:17:22 -0400 Subject: [PATCH 4/4] Add `yarn lint:js` script. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index cac36a3..699d46d 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "bin": "./bin/ember-source-channel-url", "repository": "git@github.com:rwjblue/ember-source-channel-url.git", "scripts": { + "lint:js": "eslint .", "test": "qunit tests/**/*-test.js" }, "dependencies": {