Skip to content

Commit 6cc4e85

Browse files
committed
Eliminate free-floating 'latest' and 'master' strings
1 parent a83d556 commit 6cc4e85

File tree

13 files changed

+112
-31
lines changed

13 files changed

+112
-31
lines changed

addon/components/docs-header/component.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { addonLogo } from 'ember-cli-addon-docs/utils/computed';
77
import { inject as service } from '@ember/service';
88
import { reads } from '@ember/object/computed';
99

10-
const { projectName, projectHref } = config['ember-cli-addon-docs'];
10+
const { projectName, projectHref, latestVersionName } = config['ember-cli-addon-docs'];
1111

1212
/**
1313
Render a header showing a link to your documentation, your project logo, a
@@ -36,6 +36,7 @@ export default Component.extend({
3636
projectVersion: service(),
3737

3838
projectHref,
39+
latestVersionName,
3940

4041
didInsertElement() {
4142
this._super(...arguments);

addon/components/docs-header/template.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
{{#docs-header/link on-click=(action (toggle 'isShowingVersionSelector' this)) data-test-id='current-version'}}
2626
<span data-version-selector class='flex items-center'>
2727

28-
{{#if (or (eq currentVersion.name 'latest'))}}
28+
{{#if (or (eq currentVersion.name latestVersionName))}}
2929
{{#if currentVersion.tag}}
3030
{{currentVersion.tag}}
3131
{{else}}

addon/components/docs-header/version-selector/component.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ import { inject as service } from '@ember/service';
33
import layout from './template';
44
import { sort } from '@ember/object/computed';
55
import { reads } from '@ember/object/computed';
6+
import config from 'dummy/config/environment';
7+
8+
const { latestVersionName, primaryBranch } = config['ember-cli-addon-docs'];
69

710
export default Component.extend({
811
layout,
912

13+
latestVersionName,
14+
primaryBranch,
15+
1016
projectVersion: service(),
1117
'on-close'() {},
1218

1319
currentVersion: reads('projectVersion.currentVersion'),
1420

1521
sortedVersions: sort('projectVersion.versions', function(a, b) {
16-
if (['latest', 'master'].includes(a.name) || ['latest', 'master'].includes(b.name) ) {
22+
if ([latestVersionName, primaryBranch].includes(a.name) || [latestVersionName, primaryBranch].includes(b.name) ) {
1723
return a.name > b.name;
1824
} else {
1925
return a.name < b.name;

addon/components/docs-header/version-selector/template.hbs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
{{/if}}
1515
</span>
1616
<span class='font-medium'>
17-
{{if (eq version.name 'latest') 'Latest' version.name}}
17+
{{if (eq version.name latestVersionName) 'Latest' version.name}}
1818
</span>
1919

2020
<span class="ml-auto pl-8 flex items-center opacity-50">
21-
{{#if (or (eq version.name 'latest') (eq version.name 'master'))}}
21+
{{#if (or (eq version.name latestVersionName) (eq version.name primaryBranch))}}
2222
{{svg-jar (if version.tag 'git-tag' 'git-sha') height=16 width=16}}
2323
{{else}}
2424
{{svg-jar 'git-sha' height=16 width=16}}
2525
{{/if}}
2626

2727
<span class='text-xs font-mono pl-1'>
28-
{{#if (or (eq version.name 'latest') (eq version.name 'master'))}}
28+
{{#if (or (eq version.name latestVersionName) (eq version.name primaryBranch))}}
2929
{{#if version.tag}}
3030
{{version.tag}}
3131
{{else}}

addon/components/docs-viewer/x-main/component.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getOwner } from '@ember/application';
99

1010
import layout from './template';
1111

12-
const projectHref = config['ember-cli-addon-docs'].projectHref;
12+
const { projectHref, primaryBranch } = config['ember-cli-addon-docs'];
1313

1414
const tagToSize = { H2: 'xs', H3: 'xs' };
1515
const tagToIndent = { H2: '0', H3: '4' };
@@ -76,14 +76,14 @@ export default Component.extend({
7676
let file = addonFiles.find(f => f.match(path));
7777

7878
if (file) {
79-
return `${projectHref}/edit/master/addon/${file}`;
79+
return `${projectHref}/edit/${primaryBranch}/addon/${file}`;
8080
}
8181
} else {
8282
let file = appFiles
8383
.filter(file => file.match(/template.(hbs|md)/))
8484
.find(file => file.match(path));
8585

86-
return `${projectHref}/edit/master/tests/dummy/app/${file}`;
86+
return `${projectHref}/edit/${primaryBranch}/tests/dummy/app/${file}`;
8787
}
8888
})
8989

addon/services/project-version.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import Service, { inject as service } from '@ember/service';
22
import { getOwner } from '@ember/application';
33
import { computed } from '@ember/object';
44
import { task } from 'ember-concurrency';
5+
import config from 'dummy/config/environment';
6+
7+
const { latestVersionName } = config['ember-cli-addon-docs'];
58

69
export default Service.extend({
710
docsFetch: service(),
811

912
_loadAvailableVersions: task(function*() {
1013
let response = yield this.get('docsFetch').fetch({ url: `${this.get('root')}versions.json` }).response();
11-
let json = yield response.ok ? response.json() : { latest: this.get('currentVersion') };
14+
let json = yield response.ok ? response.json() : { [latestVersionName]: this.get('currentVersion') };
1215

1316
this.set('versions', Object.keys(json).map(key => {
1417
let version = json[key];
@@ -38,7 +41,7 @@ export default Service.extend({
3841
// In development, this token won't have been replaced replaced
3942
if (currentVersion === 'ADDON_DOCS_DEPLOY_VERSION') {
4043
currentVersion = {
41-
name: 'latest',
44+
name: latestVersionName,
4245
tag: config.projectTag,
4346
path: '',
4447
sha: 'abcde'

blueprints/ember-cli-addon-docs/files/config/addon-docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
const AddonDocsConfig = require('ember-cli-addon-docs/lib/config');
55

66
module.exports = class extends AddonDocsConfig {
7-
// See https://ember-learn.github.io/ember-cli-addon-docs/latest/docs/deploying
7+
// See https://ember-learn.github.io/ember-cli-addon-docs/docs/deploying
88
// for details on configuration you can override here.
99
};

index.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app'); // eslint-disable-
99
const Plugin = require('broccoli-plugin');
1010
const walkSync = require('walk-sync');
1111

12+
const LATEST_VERSION_NAME = '-latest';
13+
1214
module.exports = {
1315
name: 'ember-cli-addon-docs',
1416

17+
LATEST_VERSION_NAME,
18+
1519
options: {
1620
nodeAssets: {
1721
'highlight.js': {
@@ -38,6 +42,7 @@ module.exports = {
3842
config(env, baseConfig) {
3943
let repo = this.parent.pkg.repository;
4044
let info = require('hosted-git-info').fromUrl(repo.url || repo);
45+
let userConfig = this._readUserConfig();
4146

4247
let config = {
4348
'ember-component-css': {
@@ -47,6 +52,8 @@ module.exports = {
4752
projectName: this.parent.pkg.name,
4853
projectTag: this.parent.pkg.version,
4954
projectHref: info && info.browse(),
55+
primaryBranch: userConfig.getPrimaryBranch(),
56+
latestVersionName: LATEST_VERSION_NAME,
5057
deployVersion: 'ADDON_DOCS_DEPLOY_VERSION'
5158
}
5259
};
@@ -107,10 +114,7 @@ module.exports = {
107114

108115
createDeployPlugin() {
109116
const AddonDocsDeployPlugin = require('./lib/deploy/plugin');
110-
const readConfig = require('./lib/utils/read-config');
111-
112-
let userConfig = readConfig(this.project);
113-
return new AddonDocsDeployPlugin(userConfig);
117+
return new AddonDocsDeployPlugin(this._readUserConfig());
114118
},
115119

116120
setupPreprocessorRegistry(type, registry) {
@@ -210,6 +214,15 @@ module.exports = {
210214
srcDir: 'styles',
211215
destDir: 'highlightjs-styles'
212216
});
217+
},
218+
219+
_readUserConfig() {
220+
if (!this._userConfig) {
221+
const readConfig = require('./lib/utils/read-config');
222+
this._userConfig = readConfig(this.project);
223+
}
224+
225+
return this._userConfig;
213226
}
214227
};
215228

lib/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module.exports = class AddonDocsConfig {
1010
this.repoInfo = gitRepoInfo();
1111
}
1212

13+
getPrimaryBranch() {
14+
return 'master';
15+
}
16+
1317
getRootURL() {
1418
let repository = this.project.pkg.repository || '';
1519
let info = hostedGitInfo.fromUrl(repository.url || repository);

lib/deploy/plugin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const hostedGitInfo = require('hosted-git-info');
88
const walkSync = require('walk-sync');
99

1010
const VERSION_PREFIX = 'v';
11+
const { LATEST_VERSION_NAME } = require('../..');
1112

1213
module.exports = class AddonDocsDeployPlugin {
1314
constructor(userConfig) {
@@ -83,7 +84,7 @@ module.exports = class AddonDocsDeployPlugin {
8384
versions[version.name] = version;
8485

8586
if (this.userConfig.shouldUpdateLatest()) {
86-
versions['latest'] = this._latestDeployVersion();
87+
versions[LATEST_VERSION_NAME] = this._latestDeployVersion();
8788
}
8889

8990
fs.writeJSONSync(versionsFile, versions, { spaces: 2 });
@@ -150,7 +151,7 @@ module.exports = class AddonDocsDeployPlugin {
150151

151152
_latestDeployVersion() {
152153
let path = '';
153-
let name = 'latest';
154+
let name = LATEST_VERSION_NAME;
154155
let sha = this.userConfig.repoInfo.sha;
155156
let tag = this.userConfig.repoInfo.tag;
156157
return { path, name, sha, tag };

tests/acceptance/version-selector-test.js

+59-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { module, test } from 'qunit';
22
import { setupApplicationTest } from 'ember-qunit';
33
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
44
import { visit, click } from '@ember/test-helpers';
5+
import config from 'dummy/config/environment';
56

67
module('Acceptance | Version selector test', function(hooks) {
78
setupApplicationTest(hooks);
@@ -11,8 +12,8 @@ module('Acceptance | Version selector test', function(hooks) {
1112
this.owner.lookup('service:project-version').set('currentVersion', {
1213
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
1314
"tag": 'v0.1.0',
14-
"path": "latest",
15-
"name": "latest"
15+
"path": "",
16+
"name": "-latest"
1617
});
1718

1819
await visit('/');
@@ -24,8 +25,8 @@ module('Acceptance | Version selector test', function(hooks) {
2425
this.owner.lookup('service:project-version').set('currentVersion', {
2526
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
2627
"tag": null,
27-
"path": "latest",
28-
"name": "latest"
28+
"path": "",
29+
"name": "-latest"
2930
});
3031

3132
await visit('/');
@@ -35,11 +36,11 @@ module('Acceptance | Version selector test', function(hooks) {
3536

3637
test(`the version selector renders correctly`, async function(assert) {
3738
server.get('/versions.json', {
38-
"latest": {
39+
"-latest": {
3940
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
4041
"tag": null,
41-
"path": "latest",
42-
"name": "latest"
42+
"path": "",
43+
"name": "-latest"
4344
},
4445
"master": {
4546
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
@@ -71,11 +72,11 @@ module('Acceptance | Version selector test', function(hooks) {
7172

7273
test(`the version selector renders a tag for latest if present`, async function(assert) {
7374
server.get('/versions.json', {
74-
"latest": {
75+
"-latest": {
7576
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
7677
"tag": 'v0.1.0',
77-
"path": "latest",
78-
"name": "latest"
78+
"path": "",
79+
"name": "-latest"
7980
},
8081
"master": {
8182
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
@@ -97,4 +98,52 @@ module('Acceptance | Version selector test', function(hooks) {
9798
assert.dom('[data-test-id="version"]:nth-child(1)').includesText('Latest', 'latest is rendered first');
9899
assert.dom('[data-test-id="version"]:nth-child(1)').includesText('v0.1.0', 'latest renders a tag if present');
99100
});
101+
102+
module('with a custom primary branch configured', function(hooks) {
103+
let oldPrimaryBranch;
104+
hooks.beforeEach(function() {
105+
oldPrimaryBranch = config.primaryBranch;
106+
config.primaryBranch = 'develop';
107+
});
108+
109+
hooks.afterEach(function() {
110+
config.primaryBranch = oldPrimaryBranch;
111+
});
112+
113+
test(`the version selector honors the primary branch`, async function(assert) {
114+
server.get('/versions.json', {
115+
"-latest": {
116+
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
117+
"tag": null,
118+
"path": "",
119+
"name": "-latest"
120+
},
121+
"master": {
122+
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
123+
"tag": null,
124+
"path": "master",
125+
"name": "master"
126+
},
127+
"develop": {
128+
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
129+
"tag": null,
130+
"path": "develop",
131+
"name": "develop"
132+
}
133+
});
134+
135+
await visit('/');
136+
await click('[data-test-id="current-version"]');
137+
138+
assert.dom('[data-test-id="version"]:nth-child(1)').includesText('Latest', 'latest is rendered first');
139+
assert.dom('[data-test-id="version"]:nth-child(1)').includesText('53b73', 'latest renders a sha when tag is null');
140+
assert.dom('[data-test-id="version"]:nth-child(1)').includesText('check', 'the current version has a check');
141+
142+
assert.dom('[data-test-id="version"]:nth-child(2)').includesText('develop', 'develop is rendered second');
143+
assert.dom('[data-test-id="version"]:nth-child(2)').includesText('53b73');
144+
145+
assert.dom('[data-test-id="version"]:nth-child(3)').includesText('master', 'other branches are rendered last');
146+
assert.dom('[data-test-id="version"]:nth-child(3)').includesText('53b73');
147+
});
148+
})
100149
});

tests/dummy/app/pods/docs/deploying/template.md

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ If instead, however, you want to [set up a CNAME for your project](https://help.
142142

143143
**Note**: if you change this configuration after you've already deployed copies of your docs site, you'll need to check out your `gh-pages` branch and find/replace your previous root URL in those copies in order for them to continue to function in their new location.
144144

145+
### `getPrimaryBranch()`
146+
147+
This method determines what Addon Docs considers to be your primary branch, which is where links such as "edit this page" will point. By default, this branch is `master`, but you can override this method to choose a different branch instead, e.g. `develop`.
148+
145149
## Removing a deployed version
146150

147151
Deploying a version of your documentation does two things: it copies the `dist` directory of your built docs app into a particular place on your `gh-pages` branch, and it adds or updates an entry in the `versions.json` manifest in the root of that branch. To remove a version, then, you just need to undo those two things.

tests/dummy/mirage/config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const projectTag = config['ember-cli-addon-docs'].projectTag;
44
export default function() {
55
this.get('/versions.json', () => {
66
return {
7-
"latest": {
7+
"-latest": {
88
"sha": "53b73465d31925f26fd1f77881aefcaccce2915a",
99
"tag": projectTag,
10-
"path": "latest",
11-
"name": "latest"
10+
"path": "",
11+
"name": "-latest"
1212
},
1313
"master": {
1414
"sha": "12345",

0 commit comments

Comments
 (0)