Skip to content

Commit 8dee01b

Browse files
committed
add demoUrl to ember-addon config on install
1 parent ffb5e8c commit 8dee01b

File tree

18 files changed

+246
-3
lines changed

18 files changed

+246
-3
lines changed

blueprints/ember-cli-addon-docs/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const fs = require('fs-extra');
4+
const path = require('path');
5+
const updateDemoUrl = require('../../lib/utils/update-demo-url');
46

57
module.exports = {
68
name: 'ember-cli-addon-docs',
@@ -39,7 +41,7 @@ module.exports = {
3941
}
4042

4143
fs.writeFileSync(configPath, configContents, 'utf-8');
42-
44+
4345
if (fs.existsSync('.npmignore')) {
4446
this.insertIntoFile('.npmignore', '/config/addon-docs.js');
4547
}
@@ -50,10 +52,20 @@ module.exports = {
5052
return isPlugin || isPluginPack;
5153
});
5254

55+
const packageJsonPath = path.join(this.project.root, 'package.json');
56+
const updatedDemoUrl = updateDemoUrl(packageJsonPath);
57+
58+
if (!updatedDemoUrl) {
59+
this.ui.writeWarnLine(
60+
`Unable to update the "ember-addon.demoUrl" configuration in your package.json. To include this for ` +
61+
`including a link on Ember Observer, set it to https://{ORGANIZATION}.github.io/{REPO}`
62+
);
63+
}
64+
5365
if (!hasPlugins) {
5466
return this.addAddonsToProject({
5567
packages: ['ember-cli-addon-docs-yuidoc']
56-
})
68+
});
5769
}
5870
}
5971
};

lib/utils/update-demo-url.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const execa = require('execa');
5+
const hostedGitInfo = require('hosted-git-info');
6+
const parseGitConfig = require('parse-git-config');
7+
8+
function gitConfigUrl(configPath) {
9+
const options = configPath ? { path: configPath } : {};
10+
const config = parseGitConfig.sync(options);
11+
const originProp = Object.keys(config).find(key => /^remote/.test(key));
12+
13+
return originProp ? config[originProp].url : '';
14+
}
15+
16+
function repoFromPackage(packageJson) {
17+
const repo = packageJson.repository;
18+
return typeof repo === 'object' ? repo.url : repo;
19+
}
20+
21+
module.exports = function updateDemoUrl(packageJsonPath, gitConfigPath) {
22+
const packageJson = fs.readJsonSync(packageJsonPath);
23+
const addonConfig = packageJson['ember-addon'] || {};
24+
const hasDemo = addonConfig.demoUrl;
25+
26+
if (hasDemo) {
27+
return true;
28+
}
29+
30+
const repo = repoFromPackage(packageJson) || gitConfigUrl(gitConfigPath);
31+
const gitInfo = hostedGitInfo.fromUrl(repo);
32+
33+
if (gitInfo) {
34+
addonConfig.demoUrl = `https://${gitInfo.user}.github.io/${gitInfo.project}`;
35+
packageJson['ember-addon'] = addonConfig;
36+
37+
fs.writeJSONSync(packageJsonPath, packageJson, {
38+
spaces: 2
39+
});
40+
41+
return true;
42+
} else {
43+
return false;
44+
}
45+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"eslint-plugin-ember": "^5.0.3",
102102
"eslint-plugin-node": "^5.2.1",
103103
"loader.js": "^4.6.0",
104+
"parse-git-config": "^1.1.1",
104105
"qunit": "^2.5.0",
105106
"qunit-dom": "^0.5.0"
106107
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"repository": "https://github.com/ember-learn/ember-cli-addon-docs"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"repository": "https://github.com/ember-learn/ember-cli-addon-docs",
3+
"ember-addon": {
4+
"demoUrl": "https://ember-learn.github.io/ember-cli-addon-docs"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"repository": "https://github.com/ember-learn/ember-cli-addon-docs",
3+
"ember-addon": {
4+
"configPath": "tests/dummy/config"
5+
}
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"repository": "https://github.com/ember-learn/ember-cli-addon-docs",
3+
"ember-addon": {
4+
"configPath": "tests/dummy/config",
5+
"demoUrl": "https://ember-learn.github.io/ember-cli-addon-docs"
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[remote "origin"]
2+
url = git@github.com:ember-learn/ember-cli-addon-docs.git
3+
fetch = +refs/heads/*:refs/remotes/origin/*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ember-addon": {
3+
"configPath": "tests/dummy/config"
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ember-addon": {
3+
"configPath": "tests/dummy/config",
4+
"demoUrl": "https://ember-learn.github.io/ember-cli-addon-docs"
5+
}
6+
}

0 commit comments

Comments
 (0)