Skip to content

Commit 731830a

Browse files
authored
Merge pull request #196 from ef4/auto-migrate
Site reorg including top-level latest
2 parents b4bfe91 + 2d78b41 commit 731830a

File tree

18 files changed

+405
-83
lines changed

18 files changed

+405
-83
lines changed

addon/components/docs-header/component.js

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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.key latestVersionName))}}
2929
{{#if currentVersion.tag}}
3030
{{currentVersion.tag}}
3131
{{else}}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ 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) ) {
17-
return a.name > b.name;
22+
if ([latestVersionName, primaryBranch].includes(a.key) || [latestVersionName, primaryBranch].includes(b.key) ) {
23+
return a.key > b.key;
1824
} else {
19-
return a.name < b.name;
25+
return a.key < b.key;
2026
}
2127
}),
2228

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
<li data-test-id='version'>
1010
<a {{action 'changeVersion' version}} href='#' class='text-black no-underline flex items-center px-4 py-3 hover:bg-grey-lighter'>
1111
<span class='w-6 flex'>
12-
{{#if (eq version.name currentVersion.name)}}
12+
{{#if (eq version.key currentVersion.key)}}
1313
{{svg-jar 'check' height=16 width=16}}
1414
{{/if}}
1515
</span>
1616
<span class='font-medium'>
17-
{{if (eq version.name 'latest') 'Latest' version.name}}
17+
{{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.key latestVersionName) (eq version.key 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.key latestVersionName) (eq version.key primaryBranch))}}
2929
{{#if version.tag}}
3030
{{version.tag}}
3131
{{else}}

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ 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+
import { assign } from '@ember/polyfills';
7+
8+
const { latestVersionName } = config['ember-cli-addon-docs'];
59

610
export default Service.extend({
711
docsFetch: service(),
812

913
_loadAvailableVersions: task(function*() {
1014
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') };
15+
let json;
16+
if(response.ok){
17+
json = yield response.json();
18+
}else{
19+
json = { [latestVersionName]: assign({}, this.get('currentVersion')) };
20+
}
21+
1222

1323
this.set('versions', Object.keys(json).map(key => {
1424
let version = json[key];
1525
version.truncatedSha = version.sha.substr(0,5);
26+
version.key = key;
1627

1728
return version;
1829
}));
@@ -38,7 +49,8 @@ export default Service.extend({
3849
// In development, this token won't have been replaced replaced
3950
if (currentVersion === 'ADDON_DOCS_DEPLOY_VERSION') {
4051
currentVersion = {
41-
name: 'latest',
52+
key: latestVersionName,
53+
name: latestVersionName,
4254
tag: config.projectTag,
4355
path: '',
4456
sha: 'abcde'

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 17 additions & 4 deletions
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
searchTokenSeparator: "\\s+"
5259
}
@@ -108,10 +115,7 @@ module.exports = {
108115

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

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

lib/config.js

Lines changed: 4 additions & 0 deletions
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/migration.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const fs = require('fs-extra');
2+
const path = require('path');
3+
const { JSDOM } = require('jsdom');
4+
const { LATEST_VERSION_NAME } = require('../..');
5+
const OLD_LATEST_NAME = 'latest';
6+
7+
module.exports = function maybeMigrateSiteFormat(context, plugin) {
8+
return shouldMigrate(context).then(weShould => {
9+
if (weShould) {
10+
return migrateSiteFormat(context, plugin);
11+
}
12+
})
13+
}
14+
15+
function shouldMigrate(context) {
16+
// we should migrate if there a `versions.json` file but no `versions` folder
17+
return directoryExists(path.join(context.gitDeploy.worktreePath, "versions"))
18+
.then(hasVersions => {
19+
if (hasVersions){
20+
return false;
21+
}
22+
return fs.stat(path.join(context.gitDeploy.worktreePath, "versions.json"))
23+
.then(() => true, () => false);
24+
});
25+
}
26+
27+
function migrateSiteFormat(context, plugin) {
28+
let stagingDirectory = context.addonDocs.stagingDirectory;
29+
let versionedApps = discoverLegacyVersionedApps(stagingDirectory);
30+
31+
moveVersionedApps(stagingDirectory, versionedApps);
32+
moveLatestToRoot(stagingDirectory);
33+
rewriteIndexHTMLs(stagingDirectory);
34+
rewriteVersionsJSON(stagingDirectory, plugin);
35+
}
36+
37+
function moveVersionedApps(stagingDirectory, versionedApps){
38+
fs.mkdirSync(path.join(stagingDirectory, "versions"));
39+
versionedApps.forEach(name => {
40+
fs.renameSync(path.join(stagingDirectory, name), path.join(stagingDirectory, 'versions', name));
41+
});
42+
}
43+
44+
function moveLatestToRoot(stagingDirectory){
45+
let latestContents;
46+
try {
47+
latestContents = fs.readdirSync(path.join(stagingDirectory, 'versions', OLD_LATEST_NAME));
48+
} catch (err){
49+
if (err.code !== 'ENOENT') {
50+
throw err;
51+
}
52+
latestContents = [];
53+
}
54+
latestContents.forEach(name => {
55+
fs.renameSync(path.join(stagingDirectory, 'versions', OLD_LATEST_NAME, name), path.join(stagingDirectory, name));
56+
});
57+
fs.rmdirSync(path.join(stagingDirectory, 'versions', OLD_LATEST_NAME));
58+
}
59+
60+
function rewriteIndexHTMLs(stagingDirectory){
61+
fs.readdirSync(path.join(stagingDirectory, 'versions')).forEach(appName => {
62+
rewriteIndexHTML(path.join(stagingDirectory, 'versions', appName, 'index.html'), appName, 'versions/' + appName);
63+
})
64+
rewriteIndexHTML(path.join(stagingDirectory, 'index.html'), OLD_LATEST_NAME, '');
65+
}
66+
67+
function rewriteIndexHTML(filename, oldPath, newPath){
68+
let indexPath = `${filename}`;
69+
let contents = fs.readFileSync(indexPath, 'utf-8');
70+
let updated = contents.replace(new RegExp(`/ember-cli-addon-docs/${oldPath}/assets`, 'g'), path.join('/ember-cli-addon-docs', newPath, 'assets'));
71+
let doc = new JSDOM(contents).window.document;
72+
let oldMeta = [...doc.querySelectorAll('meta')].find(m => /config\/environment$/.test(m.name)).content;
73+
let config = JSON.parse(decodeURIComponent(oldMeta));
74+
let newRootURL = config.rootURL.replace(`/${oldPath}/`, `/${newPath}` + (newPath ? '/' : ''));
75+
config.rootURL = newRootURL;
76+
if (config['ember-cli-addon-docs'] && config['ember-cli-addon-docs'].deployVersion) {
77+
config['ember-cli-addon-docs'].deployVersion.path = newPath;
78+
}
79+
let updatedMeta = encodeURIComponent(JSON.stringify(config));
80+
updated = updated.replace(oldMeta, updatedMeta);
81+
fs.writeFileSync(indexPath, updated);
82+
}
83+
84+
function rewriteVersionsJSON(stagingDirectory){
85+
let versionsFile = `${stagingDirectory}/versions.json`;
86+
let versions = require(versionsFile);
87+
88+
Object.keys(versions).forEach(key => {
89+
let entry = versions[key];
90+
let newPath = path.join('versions', entry.path);
91+
if (entry.path === OLD_LATEST_NAME) {
92+
newPath = '';
93+
entry.name = 'Latest';
94+
}
95+
entry.oldPath = entry.path;
96+
entry.path = newPath;
97+
});
98+
if (versions[OLD_LATEST_NAME]) {
99+
versions[LATEST_VERSION_NAME] = versions[OLD_LATEST_NAME];
100+
delete versions[OLD_LATEST_NAME];
101+
}
102+
fs.writeJSONSync(versionsFile, versions, { spaces: 2 });
103+
}
104+
105+
106+
107+
function discoverLegacyVersionedApps(stagingDirectory){
108+
return fs.readdirSync(stagingDirectory).filter(name => {
109+
// a versioned app is a directory that contains index.html
110+
let dir = path.join(stagingDirectory, name);
111+
if (!fs.statSync(dir).isDirectory()) {
112+
return false;
113+
}
114+
try {
115+
fs.statSync(path.join(dir, "index.html"));
116+
return true;
117+
} catch(err) {
118+
if (err.code !== 'ENOENT') {
119+
throw err;
120+
}
121+
return false;
122+
}
123+
});
124+
}
125+
126+
function directoryExists(dir){
127+
return fs.stat(dir)
128+
.then(stats => {
129+
return stats.isDirectory();
130+
}, err => {
131+
return false;
132+
});
133+
}

0 commit comments

Comments
 (0)