From c5fe17a7e9e59f97b46f57b839a0a061ec151b87 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 18 Dec 2019 11:29:50 -0500 Subject: [PATCH] Remove co.wrap usage in favor of using async/await directly. Used codemod to do the conversion: ``` npx jscodeshift --transform https://raw.githubusercontent.com/albinekb/co-to-async/master/codemod.js path/to/files/*.js works ``` --- .eslintrc.js | 2 +- commands/index.js | 7 +- features/application-template-wrapper.js | 11 +- features/template-only-glimmer-components.js | 17 ++- package.json | 1 - tests/commands-test.js | 103 +++++++++---------- yarn.lock | 4 - 7 files changed, 68 insertions(+), 77 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ebea71a..7e4a6f4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,7 @@ module.exports = { ], parserOptions: { sourceType: 'script', - ecmaVersion: 2015 + ecmaVersion: 2017 }, env: { browser: false, diff --git a/commands/index.js b/commands/index.js index d2bde02..ae75cdc 100644 --- a/commands/index.js +++ b/commands/index.js @@ -4,7 +4,6 @@ const VersionChecker = require('ember-cli-version-checker'); const chalk = require('chalk'); -const co = require('co'); const fs = require('fs'); const mkdirp = require('mkdirp'); const path = require('path'); @@ -47,7 +46,7 @@ const SHARED = { return configPath; }, - _setFeature: co.wrap(function *(name, value) { + _setFeature: async function (name, value) { let feature = FEATURES[name]; if (feature === undefined) { @@ -63,7 +62,7 @@ const SHARED = { } if (typeof feature.callback === 'function') { - yield feature.callback(this.project, value); + await feature.callback(this.project, value); } let config = {}; @@ -81,7 +80,7 @@ const SHARED = { let state = value ? 'Enabled' : 'Disabled'; console.log(chalk.green(`${state} ${chalk.bold(name)}. Be sure to commit ${chalk.underline('config/optional-features.json')} to source control!`)); - }) + } }; const USAGE = Object.assign({ diff --git a/features/application-template-wrapper.js b/features/application-template-wrapper.js index 3e47a7b..bd4b0af 100644 --- a/features/application-template-wrapper.js +++ b/features/application-template-wrapper.js @@ -2,7 +2,6 @@ 'use strict'; const chalk = require('chalk'); -const co = require('co'); const fs = require('fs'); const inquirer = require('inquirer'); const p = require('util.promisify'); @@ -14,7 +13,7 @@ module.exports = { url: 'https://github.com/emberjs/rfcs/pull/280', default: true, since: '3.1.0', - callback: co.wrap(function *(project, value) { + callback: async function (project, value) { if (value !== false) { return; } @@ -36,7 +35,7 @@ module.exports = { let absolutePath = path.join(root, templatePath); try { - originalContent = yield p(fs.readFile)(absolutePath, { encoding: 'UTF-8' }); + originalContent = await p(fs.readFile)(absolutePath, { encoding: 'UTF-8' }); } catch(err) { if (err.code === 'ENOENT') { return; @@ -63,7 +62,7 @@ module.exports = { To be very conservative, I could add the \`
\` wrapper to your application.hbs. (You can always remove it later.) `); - let response = yield inquirer.prompt({ + let response = await inquirer.prompt({ type: 'confirm', name: 'shouldRewrite', message: 'Would you like me to do that for you?', @@ -102,9 +101,9 @@ module.exports = { console.log(` ${chalk.yellow('overwrite')} ${templatePath}`); - yield p(fs.writeFile)(templatePath, content.join('\n'), { encoding: 'UTF-8' }); + await p(fs.writeFile)(templatePath, content.join('\n'), { encoding: 'UTF-8' }); console.log(); } - }) + } }; diff --git a/features/template-only-glimmer-components.js b/features/template-only-glimmer-components.js index 7631a8b..ba78186 100644 --- a/features/template-only-glimmer-components.js +++ b/features/template-only-glimmer-components.js @@ -2,7 +2,6 @@ 'use strict'; const chalk = require('chalk'); -const co = require('co'); const fs = require('fs'); const inquirer = require('inquirer'); const glob = require('glob'); @@ -26,7 +25,7 @@ module.exports = { url: 'https://github.com/emberjs/rfcs/pull/278', default: false, since: '3.1.0', - callback: co.wrap(function *(project, value) { + callback: async function (project, value) { if (value !== true) { return; } @@ -54,7 +53,7 @@ module.exports = { // Handle "Classic" layout let templatesRoot = path.join(root, 'app/templates/components'); - let templateCandidates = yield p(glob)('**/*.hbs', { cwd: templatesRoot }); + let templateCandidates = await p(glob)('**/*.hbs', { cwd: templatesRoot }); templateCandidates.forEach(template => { let templatePath = path.join('app/templates/components', template); @@ -72,7 +71,7 @@ module.exports = { // Handle "Pods" layout without prefix let componentsRoot = path.join(root, 'app/components'); - templateCandidates = yield p(glob)('**/template.hbs', { cwd: componentsRoot }); + templateCandidates = await p(glob)('**/template.hbs', { cwd: componentsRoot }); templateCandidates.forEach(template => { let templatePath = path.join('app/components', template); @@ -90,7 +89,7 @@ module.exports = { // Handle "Pods" layout *with* prefix componentsRoot = path.join(root, `app/${podsFolder}/components`); - templateCandidates = yield p(glob)('**/template.hbs', { cwd: componentsRoot }); + templateCandidates = await p(glob)('**/template.hbs', { cwd: componentsRoot }); templateCandidates.forEach(template => { let templatePath = path.join(`app/${podsFolder}/components`, template); @@ -138,7 +137,7 @@ module.exports = { `); } - let response = yield inquirer.prompt({ + let response = await inquirer.prompt({ type: 'confirm', name: 'shouldGenerate', message: 'Would you like me to generate these component files for you?', @@ -152,11 +151,11 @@ module.exports = { let componentPath = components[i]; console.log(` ${chalk.green('create')} ${componentPath}`); let absolutePath = path.join(project.root, componentPath); - yield p(mkdirp)(path.dirname(absolutePath)); - yield p(fs.writeFile)(absolutePath, ComponentFile, { encoding: 'UTF-8' }); + await p(mkdirp)(path.dirname(absolutePath)); + await p(fs.writeFile)(absolutePath, ComponentFile, { encoding: 'UTF-8' }); } console.log(); } - }) + } }; diff --git a/package.json b/package.json index 67df855..be37767 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ }, "dependencies": { "chalk": "^3.0.0", - "co": "^4.6.0", "ember-cli-version-checker": "^3.1.3", "glob": "^7.1.6", "inquirer": "^7.0.1", diff --git a/tests/commands-test.js b/tests/commands-test.js index 9bad7ff..975ffb1 100644 --- a/tests/commands-test.js +++ b/tests/commands-test.js @@ -3,7 +3,6 @@ const CWD = process.cwd(); const fs = require('fs'); -const co = require('co'); const createTempDir = require('broccoli-test-helper').createTempDir; const execa = require('execa'); const mkdirp = require('mkdirp'); @@ -26,8 +25,8 @@ function run(/*command, ...args, options */) { QUnit.module('commands', hooks => { let project; - hooks.beforeEach(co.wrap(function *() { - project = yield createTempDir(); + hooks.beforeEach(async function () { + project = await createTempDir(); project.write({ 'package.json': strip` @@ -58,21 +57,21 @@ QUnit.module('commands', hooks => { "version": "9.9.9" } `, { encoding: 'UTF-8' }); - })); + }); - hooks.afterEach(co.wrap(function *() { + hooks.afterEach(async function () { process.chdir(CWD); - yield project.dispose(); + await project.dispose(); fs.unlinkSync(p(CWD, 'node_modules', '@ember', 'optional-features')); fs.unlinkSync(p(CWD, 'node_modules', 'ember-source', 'package.json')); - })); + }); function USAGE(command) { - QUnit.test(`it prints the USAGE message`, co.wrap(function *(assert) { - let result = yield run(command); + QUnit.test(`it prints the USAGE message`, async function (assert) { + let result = await run(command); assert.ok(result.stdout.indexOf('Usage:') >= 0, 'it should print the USAGE message'); - })); + }); } QUnit.module('feature', () => { @@ -82,8 +81,8 @@ QUnit.module('commands', hooks => { QUnit.module('feature:list', () => { USAGE('feature:list'); - QUnit.test(`it lists all the available features`, co.wrap(function *(assert) { - let result = yield run('feature:list'); + QUnit.test(`it lists all the available features`, async function (assert) { + let result = await run('feature:list'); assert.ok(result.stdout.indexOf('Available features:') >= 0, 'it list the available features'); @@ -94,7 +93,7 @@ QUnit.module('commands', hooks => { assert.ok(result.stdout.indexOf(feature.description) >= 0, `it should include the description for ${key}`); assert.ok(result.stdout.indexOf(feature.url) >= 0, `it should include the URL for ${key}`); }); - })); + }); }); [ @@ -107,7 +106,7 @@ QUnit.module('commands', hooks => { } ].forEach(testCase => { QUnit.module(testCase.command, () => { - QUnit.test('it honors customized config path', co.wrap(function *(assert) { + QUnit.test('it honors customized config path', async function (assert) { project.write({ 'package.json': strip` { @@ -126,7 +125,7 @@ QUnit.module('commands', hooks => { ` }); - yield run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); + await run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); assert.deepEqual(project.read('foo/bar'), { 'optional-features.json': strip` @@ -135,10 +134,10 @@ QUnit.module('commands', hooks => { } ` }, 'it should have created the config file with the appropiate flags'); - })); + }); - QUnit.test('it creates the config file if one does not already exists', co.wrap(function *(assert) { - yield run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); + QUnit.test('it creates the config file if one does not already exists', async function (assert) { + await run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); assert.deepEqual(project.read('config'), { 'optional-features.json': strip` @@ -147,16 +146,16 @@ QUnit.module('commands', hooks => { } ` }, 'it should have created the config file with the appropiate flags'); - })); + }); - QUnit.test('it errors on invalid features', co.wrap(function *(assert) { - let result = yield run(testCase.command, 'foo-bar'); + QUnit.test('it errors on invalid features', async function (assert) { + let result = await run(testCase.command, 'foo-bar'); assert.ok(result.stdout.indexOf('Error:') >= 0, 'it should print an error'); assert.ok(result.stdout.indexOf('foo-bar is not a valid feature') >= 0, 'it should print an error'); - })); + }); - QUnit.test('it errors on invalid ember version', co.wrap(function *(assert) { + QUnit.test('it errors on invalid ember version', async function (assert) { project.write({ 'node_modules': { 'ember-source': { @@ -171,13 +170,13 @@ QUnit.module('commands', hooks => { } }); - let result = yield run(testCase.command, 'application-template-wrapper'); + let result = await run(testCase.command, 'application-template-wrapper'); assert.ok(result.stdout.indexOf('Error:') >= 0, 'it should print an error'); assert.ok(result.stdout.indexOf('application-template-wrapper is only available in Ember 3.1.0 or above') >= 0, 'it should print an error'); - })); + }); - QUnit.test('it rewrites the config file if one already exists', co.wrap(function *(assert) { + QUnit.test('it rewrites the config file if one already exists', async function (assert) { project.write({ config: { 'optional-features.json': strip(` @@ -188,7 +187,7 @@ QUnit.module('commands', hooks => { } }); - yield run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); + await run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); assert.deepEqual(project.read('config'), { 'optional-features.json': strip` @@ -198,9 +197,9 @@ QUnit.module('commands', hooks => { } ` }, 'it should have rewritten the config file with the appropiate flags'); - })); + }); - QUnit.test('it rewrites the config file with a custom config path', co.wrap(function *(assert) { + QUnit.test('it rewrites the config file with a custom config path', async function (assert) { project.write({ 'package.json': strip` { @@ -227,7 +226,7 @@ QUnit.module('commands', hooks => { `) }, 'foo/bar'); - yield run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); + await run(testCase.command, 'application-template-wrapper', { input: 'no\n' }); assert.deepEqual(project.read('foo/bar'), { 'optional-features.json': strip` @@ -237,12 +236,12 @@ QUnit.module('commands', hooks => { } ` }, 'it should have rewritten the config file with the appropiate flags'); - })); + }); }); }); QUnit.module('feature:disable application-template-wrapper', () => { - QUnit.test('it rewrites application.hbs when asked to', co.wrap(function *(assert) { + QUnit.test('it rewrites application.hbs when asked to', async function (assert) { project.write({ app: { templates: { @@ -261,7 +260,7 @@ QUnit.module('commands', hooks => { } }); - yield run('feature:disable', 'application-template-wrapper', { input: 'yes\n' }); + await run('feature:disable', 'application-template-wrapper', { input: 'yes\n' }); assert.deepEqual(project.read('app/templates'), { 'application.hbs': strip` @@ -278,9 +277,9 @@ QUnit.module('commands', hooks => {
` }, 'it should have rewritten the template with the wrapper'); - })); + }); - QUnit.test('it does not rewrite application.hbs when asked not to', co.wrap(function *(assert) { + QUnit.test('it does not rewrite application.hbs when asked not to', async function (assert) { project.write({ app: { templates: { @@ -299,7 +298,7 @@ QUnit.module('commands', hooks => { } }); - yield run('feature:disable', 'application-template-wrapper', { input: 'no\n' }); + await run('feature:disable', 'application-template-wrapper', { input: 'no\n' }); assert.deepEqual(project.read('app/templates'), { 'application.hbs': strip` @@ -314,7 +313,7 @@ QUnit.module('commands', hooks => { ` }, 'it should not have rewritten the template'); - })); + }); }); QUnit.module('feature:enable template-only-glimmer-components', () => { @@ -488,15 +487,15 @@ QUnit.module('commands', hooks => { }, }; - QUnit.test('it generates component files when asked to', co.wrap(function *(assert) { + QUnit.test('it generates component files when asked to', async function (assert) { project.write({ app: CLASSIC_BEFORE }); - yield run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); + await run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); assert.deepEqual(project.read('app'), CLASSIC_AFTER, 'it should have generated the component JS files'); - })); + }); - QUnit.test('it works for pods', co.wrap(function *(assert) { + QUnit.test('it works for pods', async function (assert) { project.write({ app: PODS_BEFORE, config: { @@ -509,12 +508,12 @@ QUnit.module('commands', hooks => { } }); - yield run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); + await run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); assert.deepEqual(project.read('app'), PODS_AFTER, 'it should have generated the component JS files'); - })); + }); - QUnit.test('it works for mixed layout apps', co.wrap(function *(assert) { + QUnit.test('it works for mixed layout apps', async function (assert) { project.write({ app: MIXED_BEFORE, config: { @@ -527,20 +526,20 @@ QUnit.module('commands', hooks => { } }); - yield run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); + await run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); assert.deepEqual(project.read('app'), MIXED_AFTER, 'it should have generated the component JS files'); - })); + }); - QUnit.test('it does not generates component files when asked not to', co.wrap(function *(assert) { + QUnit.test('it does not generates component files when asked not to', async function (assert) { project.write({ app: CLASSIC_BEFORE }); - yield run('feature:enable', 'template-only-glimmer-components', { input: 'no\n' }); + await run('feature:enable', 'template-only-glimmer-components', { input: 'no\n' }); assert.deepEqual(project.read('app'), CLASSIC_BEFORE, 'it should have generated the component JS files'); - })); + }); - QUnit.test('it fails for missing `modulePrefix` when `podModulePrefix` is set', co.wrap(function *(assert) { + QUnit.test('it fails for missing `modulePrefix` when `podModulePrefix` is set', async function (assert) { project.write({ app: PODS_BEFORE, config: { @@ -552,9 +551,9 @@ QUnit.module('commands', hooks => { } }); - let result = yield run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); + let result = await run('feature:enable', 'template-only-glimmer-components', { input: 'yes\n' }); assert.ok(result.stdout.includes('`podModulePrefix` could not be processed correctly')); - })); + }); }); }); diff --git a/yarn.lock b/yarn.lock index 03186d4..33688eb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1409,10 +1409,6 @@ clone@^2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f" -co@^4.6.0: - version "4.6.0" - resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" - code-point-at@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"