Skip to content

Commit e22c3fd

Browse files
authored
Merge branch 'master' into add-addon-blueprints
2 parents fe0d4a6 + d9e11c6 commit e22c3fd

File tree

9 files changed

+382
-4
lines changed

9 files changed

+382
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
88

99
### Added
1010

11+
* Blueprint (and tests) to generate in-repo addons configured for TypeScript
1112
* @ts-ignore component template import.
1213
* -addon blueprints for all the things to generate .ts files in `app/` in an addon.
1314

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/*'];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-env node */
2+
'use strict';
3+
4+
module.exports = {
5+
name: '<%= dasherizedModuleName %>',
6+
7+
isDevelopingAddon() {
8+
return true;
9+
}
10+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "<%= dasherizedModuleName %>",
3+
"keywords": [
4+
"ember-addon"
5+
]
6+
}

blueprints/in-repo-addon/index.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const path = require('path');
5+
const stringUtil = require('ember-cli-string-utils');
6+
const Blueprint = require('ember-cli/lib/models/blueprint'); // eslint-disable-line node/no-unpublished-require
7+
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');
9+
10+
module.exports = {
11+
description: 'The blueprint for addon in repo ember-cli addons.',
12+
13+
beforeInstall(options) {
14+
let libBlueprint = Blueprint.lookup('lib', {
15+
ui: this.ui,
16+
analytics: this.analytics,
17+
project: this.project,
18+
});
19+
20+
return libBlueprint.install(options);
21+
},
22+
23+
afterInstall(options) {
24+
this._generatePackageJson(options, true);
25+
this._updateTsconfigJson(options, true);
26+
},
27+
28+
afterUninstall(options) {
29+
this._generatePackageJson(options, false);
30+
this._updateTsconfigJson(options, false);
31+
},
32+
33+
_generatePackageJson(options, isInstall) {
34+
let packagePath = path.join(this.project.root, 'package.json');
35+
let contents = this._readJsonSync(packagePath);
36+
let name = stringUtil.dasherize(options.entity.name);
37+
let newPath = ['lib', name].join('/');
38+
let paths;
39+
40+
contents['ember-addon'] = contents['ember-addon'] || {};
41+
paths = contents['ember-addon']['paths'] = contents['ember-addon']['paths'] || [];
42+
43+
if (isInstall) {
44+
if (paths.indexOf(newPath) === -1) {
45+
paths.push(newPath);
46+
contents['ember-addon']['paths'] = paths.sort();
47+
}
48+
} else {
49+
let newPathIndex = paths.indexOf(newPath);
50+
if (newPathIndex > -1) {
51+
paths.splice(newPathIndex, 1);
52+
if (paths.length === 0) {
53+
delete contents['ember-addon']['paths'];
54+
}
55+
}
56+
}
57+
58+
this._writeFileSync(packagePath, stringifyAndNormalize(contents));
59+
},
60+
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+
87+
_readJsonSync(path) {
88+
return fs.readJsonSync(path);
89+
},
90+
91+
_writeFileSync(path, content) {
92+
fs.writeFileSync(path, content);
93+
},
94+
};
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+
}

0 commit comments

Comments
 (0)