Skip to content

Commit

Permalink
chore(config): remove all usage of ember-get-config
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Feb 15, 2022
1 parent 34ca53e commit a5c7c0c
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 92 deletions.
57 changes: 57 additions & 0 deletions addon/-private/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getOwner } from '@ember/application';
import { cached } from 'tracked-toolbox';

/**
* Function to get the currently configured rootURL from the containers.
*
* @function getRootURL
* @private
* @param {*} target Instance of an ember class that has an owner
* @returns {String} The currently configured rootURL
*/
export function getRootURL(target) {
return getOwner(target).resolveRegistration('config:environment').rootURL;
}

/**
* Function to get the current configuration of `ember-cli-addon-docs` from the
* container.
*
* @function getAddonDocsConfig
* @private
* @param {*} target Instance of an ember class that has an owner
* @returns {Object} The `ember-cli-addon-docs` configuration object
*/
export function getAddonDocsConfig(target) {
return getOwner(target).resolveRegistration('config:environment')[
'ember-cli-addon-docs'
];
}

/**
* Decorator to use the `ember-cli-addon-docs` configuration object on a class.
*
* Usage:
*
* ```js
* class MyComponent extends Component {
* @addonDocsConfig config;
*
* get projectName() {
* // will return the value of `projectName` configured in the
* // `ember-cli-addon-docs` section of the host configuration
* return this.config.projectName:
* }
* }
* ```
*
* @function addonDocsConfig
* @private
*/
export function addonDocsConfig(target, property, descriptor) {
return cached(target, property, {
get() {
return getAddonDocsConfig(this);
},
});
}
6 changes: 2 additions & 4 deletions addon/adapters/-addon-docs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { getOwner } from '@ember/application';
import Adapter from '@ember-data/adapter';
import fetch from 'fetch';
import { getRootURL } from 'ember-cli-addon-docs/-private/config';

export default class AddonDocsAdapter extends Adapter {
defaultSerializer = '-addon-docs';

get namespace() {
const rootURL =
getOwner(this).resolveRegistration('config:environment').rootURL;
return `${rootURL.replace(/\/$/, '')}/docs`;
return `${getRootURL(this).replace(/\/$/, '')}/docs`;
}

shouldBackgroundReloadAll() {
Expand Down
2 changes: 1 addition & 1 deletion addon/components/api/x-class/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{! wrapping in a div seems to work around https://github.com/ember-learn/ember-cli-addon-docs/issues/7 }}
<div data-test-class-description>{{{@class.description}}}</div>

{{#if (or (and @class.exportType this.showImportPaths) this.hasToggles)}}
{{#if (or (and @class.exportType this.config.showImportPaths) this.hasToggles)}}
<Api::XMetaPanel as |panel|>
{{#if @class.exportType}}
<panel.header>
Expand Down
6 changes: 2 additions & 4 deletions addon/components/api/x-class/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { tracked } from '@glimmer/tracking';
import { or } from '@ember/object/computed';
import { capitalize } from '@ember/string';
import { memberFilter } from '../../../utils/computed';
import config from 'ember-get-config';

const { showImportPaths } = config['ember-cli-addon-docs'];
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

export default class XClass extends Component {
showImportPaths = showImportPaths;
@addonDocsConfig config;

@tracked showInherited = false;
@tracked showProtected = false;
Expand Down
2 changes: 1 addition & 1 deletion addon/components/api/x-section/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{{{@item.description}}}
</p>

{{#if (or (and @item.exportType this.showImportPaths) this.shouldDisplayParams)}}
{{#if (or (and @item.exportType this.config.showImportPaths) this.shouldDisplayParams)}}
<Api::XMetaPanel as |panel|>
{{#if @item.exportType}}
<panel.header>
Expand Down
6 changes: 2 additions & 4 deletions addon/components/api/x-section/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Component from '@glimmer/component';
import config from 'ember-get-config';

const { showImportPaths } = config['ember-cli-addon-docs'];
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

/**
@class Api/XSection
@hide
*/
export default class XSection extends Component {
showImportPaths = showImportPaths;
@addonDocsConfig config;

/**
* Params shouldn't be displayed when there are no descriptions and no subparams,
Expand Down
6 changes: 3 additions & 3 deletions addon/components/docs-header/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<DocsHeader::Link @onClick={{toggle "isShowingVersionSelector" this}}>
<span data-test-id="current-version" data-version-selector class="docs-flex docs-items-center">

{{#if (eq this.currentVersion.key this.latestVersionName)}}
{{#if (eq this.currentVersion.key this.config.latestVersionName)}}
{{#if this.currentVersion.tag}}
{{this.currentVersion.tag}}
{{else}}
Expand All @@ -40,8 +40,8 @@
</span>
</DocsHeader::Link>

{{#if this.projectHref}}
<DocsHeader::Link @href={{this.projectHref}}>
{{#if this.config.projectHref}}
<DocsHeader::Link @href={{this.config.projectHref}}>
<span class="docs-flex">
{{svg-jar "github" width=24 height=24}}
</span>
Expand Down
19 changes: 10 additions & 9 deletions addon/components/docs-header/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import config from 'ember-get-config';
import { classify } from '@ember/string';
import { addonPrefix } from 'ember-cli-addon-docs/utils/computed';
import { inject as service } from '@ember/service';
import { reads } from '@ember/object/computed';
import { action } from '@ember/object';
import { localCopy } from 'tracked-toolbox';

const { projectName, projectHref, latestVersionName } =
config['ember-cli-addon-docs'];
import { trackedReset } from 'tracked-toolbox';
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

/**
Render a header showing a link to your documentation, your project logo, a
Expand All @@ -34,8 +31,7 @@ const { projectName, projectHref, latestVersionName } =
export default class DocsHeader extends Component {
@service projectVersion;

projectHref = projectHref;
latestVersionName = latestVersionName;
@addonDocsConfig config;

@tracked query;

Expand All @@ -57,7 +53,12 @@ export default class DocsHeader extends Component {
@argument prefix
@type String?
*/
@localCopy('args.prefix', addonPrefix(projectName))
@trackedReset({
memo: 'args.prefix',
update(component, key, last) {
return this.args.prefix ?? addonPrefix(component.config.projectName);
},
})
prefix;

/**
Expand All @@ -76,7 +77,7 @@ export default class DocsHeader extends Component {
if (this.args.name) {
return this.args.name;
} else {
let name = projectName;
let name = this.config.projectName;
name = name.replace('ember-data-', '');
name = name.replace('ember-cli-', '');
name = name.replace('ember-', '');
Expand Down
8 changes: 2 additions & 6 deletions addon/components/docs-header/search-box/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import Component from '@glimmer/component';
import { task } from 'ember-concurrency';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { formElementHasFocus } from 'ember-cli-addon-docs/keyboard-config';
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

export default class DocsHeaderSearchBox extends Component {
@service store;

constructor() {
super(...arguments);

const config =
getOwner(this).resolveRegistration('config:environment')[
'ember-cli-addon-docs'
];
const { projectName } = config;
const { projectName } = getAddonDocsConfig(this);

this.projectName = projectName;
this.fetchProject.perform();
Expand Down
8 changes: 2 additions & 6 deletions addon/components/docs-header/search-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { keyResponder, onKey } from 'ember-keyboard';
import { restartableTask } from 'ember-concurrency';
import { getOwner } from '@ember/application';
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

@keyResponder
export default class DocsHeaderSearchResults extends Component {
Expand All @@ -18,11 +18,7 @@ export default class DocsHeaderSearchResults extends Component {
constructor() {
super(...arguments);

const config =
getOwner(this).resolveRegistration('config:environment')[
'ember-cli-addon-docs'
];
const { projectName } = config;
const { projectName } = getAddonDocsConfig(this);

this.projectName = projectName;

Expand Down
7 changes: 2 additions & 5 deletions addon/components/docs-header/version-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import { inject as service } from '@ember/service';
import { reads } from '@ember/object/computed';
import { action } from '@ember/object';
import { A } from '@ember/array';
import { getOwner } from '@ember/application';
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

export default class VersionSelector extends Component {
@service projectVersion;

constructor() {
super(...arguments);

const config =
getOwner(this).resolveRegistration('config:environment')[
'ember-cli-addon-docs'
];
const config = getAddonDocsConfig(this);
this.latestVersionName = config.latestVersionName;
this.primaryBranch = config.primaryBranch;
}
Expand Down
8 changes: 2 additions & 6 deletions addon/components/docs-hero/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
addonPrefix,
unprefixedAddonName,
} from 'ember-cli-addon-docs/utils/computed';
import { getOwner } from '@ember/application';
import { classify } from '@ember/string';
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

/**
A component that renders a hero banner. Useful for your docs site's homepage.
Expand All @@ -28,11 +28,7 @@ export default class DocsHeroComponent extends Component {
constructor() {
super(...arguments);

const config =
getOwner(this).resolveRegistration('config:environment')[
'ember-cli-addon-docs'
];
const { projectDescription, projectName } = config;
const { projectDescription, projectName } = getAddonDocsConfig(this);
this.projectDescription = projectDescription;
this.projectName = projectName;
}
Expand Down
8 changes: 5 additions & 3 deletions addon/components/docs-viewer/x-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Component from '@glimmer/component';
import { bind } from '@ember/runloop';
import appFiles from 'ember-cli-addon-docs/app-files';
import addonFiles from 'ember-cli-addon-docs/addon-files';
import config from 'ember-get-config';
import { getOwner } from '@ember/application';
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

const tagToSize = { H2: 'xxs', H3: 'xxs' };
const tagToIndent = { H2: '0', H3: '4' };
Expand All @@ -17,6 +17,8 @@ export default class XMain extends Component {

@service docsRoutes;

@addonDocsConfig config;

@action
setupElement(element) {
let target = element.querySelector('[data-current-page-index-target]');
Expand Down Expand Up @@ -64,7 +66,7 @@ export default class XMain extends Component {
let match = this._locateFile(path);
if (match) {
let { projectHref, addonPathInRepo, docsAppPathInRepo, primaryBranch } =
config['ember-cli-addon-docs'];
this.config;
let parts = [projectHref, 'edit', primaryBranch];
if (match.inTree === 'addon') {
parts.push(addonPathInRepo);
Expand All @@ -81,7 +83,7 @@ export default class XMain extends Component {
_locateFile(path) {
path = path.replace(/\./g, '/');
if (path === 'docs/api/item') {
let { projectName } = config['ember-cli-addon-docs'];
let { projectName } = this.config;
let model = getOwner(this)
.lookup('route:application')
.modelFor('docs.api.item');
Expand Down
16 changes: 10 additions & 6 deletions addon/components/docs-viewer/x-nav/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { inject as service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { localCopy } from 'tracked-toolbox';
import config from 'ember-get-config';
import { classify } from '@ember/string';
import { addonLogo } from 'ember-cli-addon-docs/utils/computed';

const projectName = config['ember-cli-addon-docs'].projectName;
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

export default class XNav extends Component {
get projectName() {
return getAddonDocsConfig(this).projectName;
}

@localCopy('args.root', 'docs')
root;

Expand All @@ -17,19 +19,21 @@ export default class XNav extends Component {

@tracked isShowingMenu;

addonLogo = addonLogo(projectName);
get addonLogo() {
return addonLogo(this.projectName);
}

get addonTitle() {
let logo = this.addonLogo;

return classify(projectName.replace(`${logo}-`, ''));
return classify(this.projectName.replace(`${logo}-`, ''));
}

get project() {
if (this.args.project) {
return this.args.project;
}

return this.store.peekRecord('project', projectName);
return this.store.peekRecord('project', this.projectName);
}
}
9 changes: 5 additions & 4 deletions addon/routes/docs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import config from 'ember-get-config';

const projectName = config['ember-cli-addon-docs'].projectName;
import { getAddonDocsConfig } from 'ember-cli-addon-docs/-private/config';

export default class DocsRoute extends Route {
@service store;

model() {
return this.store.findRecord('project', projectName);
return this.store.findRecord(
'project',
getAddonDocsConfig(this).projectName
);
}
}
Loading

0 comments on commit a5c7c0c

Please sign in to comment.