-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathproject-version.js
More file actions
85 lines (71 loc) · 2.07 KB
/
Copy pathproject-version.js
File metadata and controls
85 lines (71 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import Service from '@ember/service';
import { getOwner } from '@ember/application';
import { task } from 'ember-concurrency';
import { tracked } from '@glimmer/tracking';
import {
addonDocsConfig,
getRootURL,
} from 'ember-cli-addon-docs/-private/config';
export default class ProjectVersionService extends Service {
@tracked versions;
@addonDocsConfig config;
_loadAvailableVersions = task(async () => {
let fastboot = getOwner(this).lookup('service:fastboot');
if (fastboot?.isFastBoot) {
this.versions = [
{
...this.currentVersion,
truncatedSha: this.currentVersion.sha?.substr(0, 5) || '',
key: this.config.latestVersionName,
},
];
return;
}
let response = await fetch(`${this.root}versions.json`);
let json;
if (response.ok) {
json = await response.json();
} else {
json = {
[this.config.latestVersionName]: Object.assign({}, this.currentVersion),
};
}
this.versions = Object.keys(json).map((key) => {
let version = json[key];
version.truncatedSha = version.sha.substr(0, 5);
version.key = key;
return version;
});
});
redirectTo(version) {
if (typeof window === 'undefined') return;
window.location.href = `${this.root}${version.path}`;
}
loadAvailableVersions() {
return this._loadAvailableVersions.perform();
}
get root() {
return getRootURL(this).replace(`/${this.currentVersion.path}/`, '/');
}
get currentVersion() {
if (this._currentVersion) {
return this._currentVersion;
}
let currentVersion = this.config.deployVersion;
// In development, this token won't have been replaced replaced
if (currentVersion === 'ADDON_DOCS_DEPLOY_VERSION') {
currentVersion = {
key: this.config.latestVersionName,
name: this.config.latestVersionName,
tag: this.config.projectTag,
path: '',
sha: 'abcde',
};
}
return currentVersion;
}
// only used for tests
set currentVersion(val) {
this._currentVersion = val;
}
}