diff --git a/lib/babel-options-util.js b/lib/babel-options-util.js index f48a8956..3683354f 100644 --- a/lib/babel-options-util.js +++ b/lib/babel-options-util.js @@ -1,4 +1,5 @@ const VersionChecker = require("ember-cli-version-checker"); +const resolvePackagePath = require("resolve-package-path"); const clone = require("clone"); const semver = require("semver"); @@ -406,16 +407,23 @@ function _shouldIncludeDecoratorPlugins(config) { function _shouldHandleTypeScript(config, parent) { let emberCLIBabelConfig = config["ember-cli-babel"] || {}; + if (typeof emberCLIBabelConfig.enableTypeScriptTransform === "boolean") { return emberCLIBabelConfig.enableTypeScriptTransform; } - let typeScriptAddon = - parent.addons && - parent.addons.find((a) => a.name === "ember-cli-typescript"); - return ( - typeof typeScriptAddon !== "undefined" && - semver.gte(typeScriptAddon.pkg.version, "4.0.0-alpha.1") - ); + + let tsDependency = + parent.pkg && + parent.pkg.dependencies && + parent.pkg.dependencies["ember-cli-typescript"]; + + if (typeof tsDependency !== "undefined") { + let tsPkgPath = resolvePackagePath("ember-cli-typescript", parent.root); + let tsPkg = tsPkgPath ? require(tsPkgPath) : null; + return tsPkg ? semver.gte(tsPkg.version, "4.0.0-alpha.1") : false; + } + + return false; } function _getAddonProvidedConfig(addonOptions) { diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index d3d5a1bd..c2db9bce 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -800,14 +800,51 @@ describe('ember-cli-babel', function() { }); describe('TypeScript transpilation', function() { - beforeEach(function() { - this.addon.parent.addons.push({ - name: 'ember-cli-typescript', - pkg: { - version: '4.0.0-alpha.1' - } + let input; + let output; + let subject; + let project; + let unlink; + + beforeEach(co.wrap(function*() { + let fixturifyProject = new FixturifyProject('whatever', '0.0.1'); + fixturifyProject.addDependency('ember-cli-typescript', '4.0.0-alpha.1', addon => { + return prepareAddon(addon); }); - }); + fixturifyProject.addDependency('ember-cli-babel', 'babel/ember-cli-babel#master'); + let pkg = JSON.parse(fixturifyProject.toJSON('package.json')); + fixturifyProject.writeSync(); + + let linkPath = path.join(fixturifyProject.root, 'whatever/node_modules/ember-cli-babel'); + let addonPath = path.resolve(__dirname, '../'); + rimraf.sync(linkPath); + fs.symlinkSync(addonPath, linkPath, 'junction'); + unlink = () => { + fs.unlinkSync(linkPath); + }; + + let cli = new MockCLI(); + let root = path.join(fixturifyProject.root, 'whatever'); + project = new EmberProject(root, pkg, cli.ui, cli); + project.initializeAddons(); + this.addon = project.addons.find(a => { return a.name === 'ember-cli-babel'; }); + input = yield createTempDir(); + })); + + afterEach(co.wrap(function*() { + unlink(); + + if (input) { + yield input.dispose(); + } + + if (output) { + yield output.dispose(); + } + + // shut down workers after the tests are run so that mocha doesn't hang + yield terminateWorkerPool(); + })); it('should transpile .ts files', co.wrap(function*() { input.write({ 'foo.ts': `let foo: string = "hi";` }); @@ -967,40 +1004,66 @@ describe('ember-cli-babel', function() { }); describe('_shouldHandleTypeScript', function() { + let project; + let unlink; + + let setupTsAddon = function*(context, version = '4.0.0-alpha.1') { + let fixturifyProject = new FixturifyProject('whatever', '0.0.1'); + fixturifyProject.addDependency('ember-cli-typescript', version, addon => { + return prepareAddon(addon); + }); + fixturifyProject.addDependency('ember-cli-babel', 'babel/ember-cli-babel#master'); + let pkg = JSON.parse(fixturifyProject.toJSON('package.json')); + fixturifyProject.writeSync(); + + let linkPath = path.join(fixturifyProject.root, 'whatever/node_modules/ember-cli-babel'); + let addonPath = path.resolve(__dirname, '../'); + rimraf.sync(linkPath); + fs.symlinkSync(addonPath, linkPath, 'junction'); + unlink = () => { + fs.unlinkSync(linkPath); + }; + + let cli = new MockCLI(); + let root = path.join(fixturifyProject.root, 'whatever'); + project = new EmberProject(root, pkg, cli.ui, cli); + project.initializeAddons(); + context.addon = project.addons.find(a => { return a.name === 'ember-cli-babel'; }); + input = yield createTempDir(); + } + + afterEach(co.wrap(function*() { + if (unlink) { + unlink(); + unlink = undefined; + } + + // shut down workers after the tests are run so that mocha doesn't hang + yield terminateWorkerPool(); + })); + it('should return false by default', function() { expect(_shouldHandleTypeScript({}, this.addon.parent)).to.be.false; }); - it('should return true when ember-cli-typescript >= 4.0.0-alpha.1 is installed', function() { - this.addon.parent.addons.push({ - name: 'ember-cli-typescript', - pkg: { - version: '4.0.0-alpha.1', - }, - }); + it('should return true when ember-cli-typescript >= 4.0.0-alpha.1 is installed', function*() { + yield setupTsAddon(this); expect(_shouldHandleTypeScript({}, this.addon.parent)).to.be.true; }); - it('should return false when ember-cli-typescript < 4.0.0-alpha.1 is installed', function() { - this.addon.parent.addons.push({ - name: 'ember-cli-typescript', - pkg: { - version: '3.0.0', - }, - }); + it('should return false when ember-cli-typescript < 4.0.0-alpha.1 is installed', function*() { + yield setupTsAddon(this, '3.0.0'); expect(_shouldHandleTypeScript({}, this.addon.parent)).to.be.false; }); - it('should return true when the TypeScript transform is manually enabled', function() { + it('should return true when the TypeScript transform is manually enabled', function*() { + yield setupTsAddon(this, '3.0.0'); expect(_shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: true } }, this.addon.parent)).to.be.true; }); + it('should return false when the TypeScript transforms is manually disabled', function() { expect(_shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: false } }, this.addon.parent)).to.be.false; }); - it('should return false when the TypeScript transform is manually disabled, even when ember-cli-typescript >= 4.0.0-alpha.1 is installed', function() { - this.addon.parent.addons.push({ - name: 'ember-cli-typescript', - pkg: { - version: '4.0.0-alpha.1', - }, - }); + + it('should return false when the TypeScript transform is manually disabled, even when ember-cli-typescript >= 4.0.0-alpha.1 is installed', function*() { + yield setupTsAddon(this, '4.1.0'); expect(_shouldHandleTypeScript({ 'ember-cli-babel': { enableTypeScriptTransform: false } }, this.addon.parent)).to.be.false; }); });