Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

### Added

* Blueprint (and tests) to generate in-repo addons configured for TypeScript
* @ts-ignore component template import.

### Changed
Expand Down
6 changes: 2 additions & 4 deletions blueprints/ember-cli-typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs');
const path = require('path');
const updatePathsForAddon = require('../../lib/utilities/update-paths-for-addon');

const APP_DECLARATIONS = `
import Ember from 'ember';
Expand Down Expand Up @@ -62,10 +63,7 @@ module.exports = {
}

for (let addon of inRepoAddons) {
let addonName = path.basename(addon);
paths[addonName] = [`${addon}/addon`];
paths[`${addonName}/*`] = [`${addon}/addon/*`];
paths[`${appName}/*`].push(`${addon}/app/*`);
updatePathsForAddon(paths, path.basename(addon), appName);
}

paths['*'] = ['types/*'];
Expand Down
10 changes: 10 additions & 0 deletions blueprints/in-repo-addon/files/lib/__name__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-env node */
'use strict';

module.exports = {
name: '<%= dasherizedModuleName %>',

isDevelopingAddon() {
return true;
}
};
6 changes: 6 additions & 0 deletions blueprints/in-repo-addon/files/lib/__name__/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "<%= dasherizedModuleName %>",
"keywords": [
"ember-addon"
]
}
94 changes: 94 additions & 0 deletions blueprints/in-repo-addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';

const fs = require('fs-extra');
const path = require('path');
const stringUtil = require('ember-cli-string-utils');
const Blueprint = require('ember-cli/lib/models/blueprint'); // eslint-disable-line node/no-unpublished-require
const stringifyAndNormalize = require('ember-cli/lib/utilities/stringify-and-normalize'); // eslint-disable-line node/no-unpublished-require
const updatePathsForAddon = require('../../lib/utilities/update-paths-for-addon');

module.exports = {
description: 'The blueprint for addon in repo ember-cli addons.',

beforeInstall(options) {
let libBlueprint = Blueprint.lookup('lib', {
ui: this.ui,
analytics: this.analytics,
project: this.project,
});

return libBlueprint.install(options);
},

afterInstall(options) {
this._generatePackageJson(options, true);
this._updateTsconfigJson(options, true);
},

afterUninstall(options) {
this._generatePackageJson(options, false);
this._updateTsconfigJson(options, false);
},

_generatePackageJson(options, isInstall) {
let packagePath = path.join(this.project.root, 'package.json');
let contents = this._readJsonSync(packagePath);
let name = stringUtil.dasherize(options.entity.name);
let newPath = ['lib', name].join('/');
let paths;

contents['ember-addon'] = contents['ember-addon'] || {};
paths = contents['ember-addon']['paths'] = contents['ember-addon']['paths'] || [];

if (isInstall) {
if (paths.indexOf(newPath) === -1) {
paths.push(newPath);
contents['ember-addon']['paths'] = paths.sort();
}
} else {
let newPathIndex = paths.indexOf(newPath);
if (newPathIndex > -1) {
paths.splice(newPathIndex, 1);
if (paths.length === 0) {
delete contents['ember-addon']['paths'];
}
}
}

this._writeFileSync(packagePath, stringifyAndNormalize(contents));
},

_updateTsconfigJson(options, isInstall) {
const tsconfigPath = path.join(this.project.root, 'tsconfig.json');
const addonName = stringUtil.dasherize(options.entity.name);
const appName = this.project.isEmberCLIAddon() ? 'dummy' : this.project.name();
const addonPath = ['lib', addonName].join('/');
let contents = this._readJsonSync(tsconfigPath);
contents['compilerOptions'] = contents['compilerOptions'] || {};
contents['include'] = contents['include'] || [];
let paths = contents['compilerOptions']['paths'];

if (isInstall) {
updatePathsForAddon(paths, addonName, appName);
if (contents['include'].indexOf(addonPath) === -1) {
contents['include'].push(addonPath);
}
} else {
updatePathsForAddon(paths, addonName, appName, { removePaths: true });
let addonPathIndex = contents['include'].indexOf(addonPath);
if (addonPathIndex > -1) {
contents['include'].splice(addonPathIndex, 1);
}
}

this._writeFileSync(tsconfigPath, stringifyAndNormalize(contents));
},

_readJsonSync(path) {
return fs.readJsonSync(path);
},

_writeFileSync(path, content) {
fs.writeFileSync(path, content);
},
};
38 changes: 38 additions & 0 deletions lib/utilities/update-paths-for-addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

module.exports = function(paths, addonName, appName, options) {
options = options || {};
const addonNameStar = [addonName, '*'].join('/');
const addonPath = [options.isLinked ? 'node_modules' : 'lib', addonName].join('/');
const addonAddonPath = [addonPath, 'addon'].join('/');
const addonAppPath = [addonPath, 'app'].join('/');
const appNameStar = [appName, '*'].join('/');
let appStarPaths;
paths = paths || {};
appStarPaths = paths[appNameStar] = paths[appNameStar] || [];

if (options.removePaths) {
if (paths.hasOwnProperty(addonName)) {
delete paths[addonName];
}
if (paths.hasOwnProperty(addonNameStar)) {
delete paths[addonNameStar]
}
let addonAppPathIndex = appStarPaths.indexOf([addonAppPath, '*'].join('/'));
if (addonAppPathIndex > -1) {
appStarPaths.splice(addonAppPathIndex, 1);
paths[appNameStar] = appStarPaths;
}
} else {
if (!paths.hasOwnProperty(addonName)) {
paths[addonName] = [ addonAddonPath ];
}
if (!paths.hasOwnProperty(addonNameStar)) {
paths[addonNameStar] = [ [addonAddonPath, '*'].join('/') ];
}
if (appStarPaths.indexOf(addonAppPath) === -1) {
appStarPaths.push([addonAppPath, '*'].join('/'));
paths[appNameStar] = appStarPaths;
}
}
}
Loading