Skip to content

Commit

Permalink
Refactor to async/await usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed May 16, 2019
1 parent 57fcb86 commit 79c4111
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 86 deletions.
15 changes: 9 additions & 6 deletions bin/ember-source-channel-url
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -85,5 +86,7 @@ if (['release', 'beta', 'canary'].indexOf(channel) === -1) {
process.exitCode = 3;
}
}
});
}
}

main();
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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}`;
};
2 changes: 1 addition & 1 deletion tests/helpers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
145 changes: 70 additions & 75 deletions tests/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(
Expand All @@ -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'
);
});
}
});
});
});

0 comments on commit 79c4111

Please sign in to comment.