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 8a05dbe
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 126 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
13 changes: 4 additions & 9 deletions addon/components/docs-header/search-box/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
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 { addonDocsConfig } 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;

this.projectName = projectName;
this.fetchProject.perform();
}

@addonDocsConfig config;

// TODO: The searchbox doesn't work without the project being fetched.
// We should move this logic (and everywhere else in the code that's fetching
// the project) within a new addonDocs service that wires all that up together.
// I think it's fine if our Docs-* components assume there is a single global
// project.
@task
*fetchProject() {
yield this.store.findRecord('project', this.projectName);
yield this.store.findRecord('project', this.config.projectName);
}

@action
Expand Down
14 changes: 4 additions & 10 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 { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

@keyResponder
export default class DocsHeaderSearchResults extends Component {
Expand All @@ -15,23 +15,17 @@ export default class DocsHeaderSearchResults extends Component {
@tracked selectedIndex = null;
@tracked rawSearchResults = [];

@addonDocsConfig config;

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

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

this.projectName = projectName;

// Start downloading the search index immediately
this.docsSearch.loadSearchIndex();
}

get project() {
return this.store.peekRecord('project', this.projectName);
return this.store.peekRecord('project', this.config.projectName);
}

get trimmedQuery() {
Expand Down
8 changes: 4 additions & 4 deletions addon/components/docs-header/version-selector/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
>
{{#if
(or
(eq version.key this.latestVersionName)
(eq version.key this.primaryBranch)
(eq version.key this.config.latestVersionName)
(eq version.key this.config.primaryBranch)
)
}}
{{svg-jar
Expand All @@ -51,8 +51,8 @@
<span class="docs-text-xxs docs-font-mono docs-pl-1">
{{#if
(or
(eq version.key this.latestVersionName)
(eq version.key this.primaryBranch)
(eq version.key this.config.latestVersionName)
(eq version.key this.config.primaryBranch)
)
}}
{{#if version.tag}}
Expand Down
19 changes: 4 additions & 15 deletions addon/components/docs-header/version-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,20 @@ 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 { addonDocsConfig } 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'
];
this.latestVersionName = config.latestVersionName;
this.primaryBranch = config.primaryBranch;
}
@addonDocsConfig config;

@reads('projectVersion.currentVersion')
currentVersion;

get sortedVersions() {
let latestVersionName = this.latestVersionName;
let primaryBranch = this.primaryBranch;
let versions = A(this.projectVersion.versions);
let latest = versions.findBy('key', latestVersionName);
let primary = versions.findBy('key', primaryBranch);
let latest = versions.findBy('key', this.config.latestVersionName);
let primary = versions.findBy('key', this.config.primaryBranch);
let otherTags = versions
.reject((v) => [latest, primary].includes(v))
.sort((tagA, tagB) => {
Expand Down
27 changes: 8 additions & 19 deletions addon/components/docs-hero/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import {
addonPrefix,
unprefixedAddonName,
} from 'ember-cli-addon-docs/utils/computed';
import { getOwner } from '@ember/application';
import { classify } from '@ember/string';
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';

/**
A component that renders a hero banner. Useful for your docs site's homepage.
Expand All @@ -22,20 +21,7 @@ import { classify } from '@ember/string';
@public
*/
export default class DocsHeroComponent extends Component {
@tracked projectDescription;
@tracked projectName;

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

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

/**
The prefix to show, typically of: 'Ember', 'EmberCLI', or 'EmberData'
Expand All @@ -44,7 +30,7 @@ export default class DocsHeroComponent extends Component {
@type String
*/
get prefix() {
return this.args.prefix ?? addonPrefix(this.projectName);
return this.args.prefix ?? addonPrefix(this.config.projectName);
}

/**
Expand All @@ -54,7 +40,10 @@ export default class DocsHeroComponent extends Component {
@type String
*/
get heading() {
return this.args.heading ?? classify(unprefixedAddonName(this.projectName));
return (
this.args.heading ??
classify(unprefixedAddonName(this.config.projectName))
);
}

/**
Expand All @@ -64,6 +53,6 @@ export default class DocsHeroComponent extends Component {
@type String
*/
get byline() {
return this.args.byline ?? this.projectDescription;
return this.args.byline ?? this.config.projectDescription;
}
}
Loading

0 comments on commit 8a05dbe

Please sign in to comment.