Skip to content

Commit bdff6c0

Browse files
committed
Update tsconfig.json on in-repo-addon generate/destroy
1 parent 3c9728a commit bdff6c0

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

blueprints/ember-cli-typescript/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const updatePathsForAddon = require('../../lib/utilities/update-paths-for-addon');
56

67
const APP_DECLARATIONS = `
78
import Ember from 'ember';
@@ -62,10 +63,7 @@ module.exports = {
6263
}
6364

6465
for (let addon of inRepoAddons) {
65-
let addonName = path.basename(addon);
66-
paths[addonName] = [`${addon}/addon`];
67-
paths[`${addonName}/*`] = [`${addon}/addon/*`];
68-
paths[`${appName}/*`].push(`${addon}/app/*`);
66+
updatePathsForAddon(paths, path.basename(addon), appName);
6967
}
7068

7169
paths['*'] = ['types/*'];

blueprints/in-repo-addon/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const stringUtil = require('ember-cli-string-utils');
66
const Blueprint = require('ember-cli/lib/models/blueprint'); // eslint-disable-line node/no-unpublished-require
77
const stringifyAndNormalize = require('ember-cli/lib/utilities/stringify-and-normalize'); // eslint-disable-line node/no-unpublished-require
8+
const updatePathsForAddon = require('../../lib/utilities/update-paths-for-addon');
89

910
module.exports = {
1011
description: 'The blueprint for addon in repo ember-cli addons.',
@@ -21,10 +22,12 @@ module.exports = {
2122

2223
afterInstall(options) {
2324
this._generatePackageJson(options, true);
25+
this._updateTsconfigJson(options, true);
2426
},
2527

2628
afterUninstall(options) {
2729
this._generatePackageJson(options, false);
30+
this._updateTsconfigJson(options, false);
2831
},
2932

3033
_generatePackageJson(options, isInstall) {
@@ -55,6 +58,32 @@ module.exports = {
5558
this._writeFileSync(packagePath, stringifyAndNormalize(contents));
5659
},
5760

61+
_updateTsconfigJson(options, isInstall) {
62+
const tsconfigPath = path.join(this.project.root, 'tsconfig.json');
63+
const addonName = stringUtil.dasherize(options.entity.name);
64+
const appName = this.project.isEmberCLIAddon() ? 'dummy' : this.project.name();
65+
const addonPath = ['lib', addonName].join('/');
66+
let contents = this._readJsonSync(tsconfigPath);
67+
contents['compilerOptions'] = contents['compilerOptions'] || {};
68+
contents['include'] = contents['include'] || [];
69+
let paths = contents['compilerOptions']['paths'];
70+
71+
if (isInstall) {
72+
updatePathsForAddon(paths, addonName, appName);
73+
if (contents['include'].indexOf(addonPath) === -1) {
74+
contents['include'].push(addonPath);
75+
}
76+
} else {
77+
updatePathsForAddon(paths, addonName, appName, { removePaths: true });
78+
let addonPathIndex = contents['include'].indexOf(addonPath);
79+
if (addonPathIndex > -1) {
80+
contents['include'].splice(addonPathIndex, 1);
81+
}
82+
}
83+
84+
this._writeFileSync(tsconfigPath, stringifyAndNormalize(contents));
85+
},
86+
5887
_readJsonSync(path) {
5988
return fs.readJsonSync(path);
6089
},
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
module.exports = function(paths, addonName, appName, options) {
4+
options = options || {};
5+
const addonNameStar = [addonName, '*'].join('/');
6+
const addonPath = [options.isLinked ? 'node_modules' : 'lib', addonName].join('/');
7+
const addonAddonPath = [addonPath, 'addon'].join('/');
8+
const addonAppPath = [addonPath, 'app'].join('/');
9+
const appNameStar = [appName, '*'].join('/');
10+
let appStarPaths;
11+
paths = paths || {};
12+
appStarPaths = paths[appNameStar] = paths[appNameStar] || [];
13+
14+
if (options.removePaths) {
15+
if (paths.hasOwnProperty(addonName)) {
16+
delete paths[addonName];
17+
}
18+
if (paths.hasOwnProperty(addonNameStar)) {
19+
delete paths[addonNameStar]
20+
}
21+
let addonAppPathIndex = appStarPaths.indexOf([addonAppPath, '*'].join('/'));
22+
if (addonAppPathIndex > -1) {
23+
appStarPaths.splice(addonAppPathIndex, 1);
24+
paths[appNameStar] = appStarPaths;
25+
}
26+
} else {
27+
if (!paths.hasOwnProperty(addonName)) {
28+
paths[addonName] = [ addonAddonPath ];
29+
}
30+
if (!paths.hasOwnProperty(addonNameStar)) {
31+
paths[addonNameStar] = [ [addonAddonPath, '*'].join('/') ];
32+
}
33+
if (appStarPaths.indexOf(addonAppPath) === -1) {
34+
appStarPaths.push([addonAppPath, '*'].join('/'));
35+
paths[appNameStar] = appStarPaths;
36+
}
37+
}
38+
}

node-tests/blueprints/in-repo-addon-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ describe('Acceptance: ember generate and destroy in-repo-addon', function() {
1717

1818
it('in-repo-addon fooBar', function() {
1919
let args = ['in-repo-addon', 'fooBar'];
20+
let name, nameStar;
2021

2122
return emberNew()
23+
.then(function() {
24+
name = fs.readJsonSync('package.json')['name'];
25+
nameStar = [name, '*'].join('/');
26+
})
27+
.then(function() {
28+
return emberGenerate(['ember-cli-typescript']);
29+
})
2230
.then(function() {
2331
expect(fs.readJsonSync('package.json')['ember-addon']).to.be.undefined;
2432
})
@@ -41,6 +49,12 @@ describe('Acceptance: ember generate and destroy in-repo-addon', function() {
4149
"lib/foo-bar",
4250
],
4351
});
52+
53+
const tsconfigJson = fs.readJsonSync('tsconfig.json');
54+
expect(tsconfigJson['compilerOptions']['paths']['foo-bar']).to.have.all.members(['lib/foo-bar/addon']);
55+
expect(tsconfigJson['compilerOptions']['paths']['foo-bar/*']).to.have.all.members(['lib/foo-bar/addon/*']);
56+
expect(tsconfigJson['compilerOptions']['paths'][nameStar]).to.include.members(['lib/foo-bar/app/*']);
57+
expect(tsconfigJson['include']).to.include.members(['lib/foo-bar']);
4458
})
4559
.then(function() {
4660
return emberDestroy(args);
@@ -50,6 +64,12 @@ describe('Acceptance: ember generate and destroy in-repo-addon', function() {
5064
expect(file('lib/foo-bar/index.js')).to.not.exist;
5165

5266
expect(fs.readJsonSync('package.json')['ember-addon']['paths']).to.be.undefined;
67+
68+
const tsconfigJson = fs.readJsonSync('tsconfig.json');
69+
expect(tsconfigJson['compilerOptions']['paths']['foo-bar']).to.be.undefined;
70+
expect(tsconfigJson['compilerOptions']['paths']['foo-bar/*']).to.be.undefined;
71+
expect(tsconfigJson['compilerOptions']['paths'][nameStar]).to.not.include.members(['lib/foo-bar/app/*']);
72+
expect(tsconfigJson['include']).to.not.include.members(['lib/foo-bar']);
5373
});
5474
});
5575
});
@@ -64,6 +84,8 @@ describe('Unit: in-repo-addon blueprint', function() {
6484
blueprint = require('../../blueprints/in-repo-addon');
6585
blueprint.project = {
6686
root: 'test-project-root',
87+
isEmberCLIAddon: function() { return false; },
88+
name: function() { return 'foo-bar'; },
6789
};
6890

6991
options = {

0 commit comments

Comments
 (0)