forked from CenterForOpenScience/ember-osf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (120 loc) · 5.32 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-env node */
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
'use strict';
var path = require('path');
var config = require('config');
var Funnel = require('broccoli-funnel');
var mergeTrees = require('broccoli-merge-trees');
var compileSass = require('broccoli-sass-source-maps');
// Fetch a list of known backends. The user can always choose to override any of these URLs via ENV vars
var knownBackends = require('./config/backends');
// Closure to fetch a key from one of two places (environment vars or a specified object). Env vars take precedence.
function envOrSource(env, source) {
function getKey(keyName) {
return env[keyName] || source[keyName];
}
return getKey;
}
module.exports = {
name: 'ember-osf',
blueprintsPath: function() {
return path.join(__dirname, 'blueprints');
},
config: function(environment, ENV) {
let BACKEND = process.env.BACKEND || 'local';
// Settings required to configure the developer application, primarily for OAuth2
let configFileSettings = {};
// Backwards compatibility: old config/*.yml files were nested, with keys like "stage", "test", etc.
// New files are flat- you specify the values you want once. If there is no <backendname> key, assume
// this is a flat config file and assume the settings we want are at the top level.
configFileSettings = config[BACKEND] || config;
// For i18n
ENV.i18n = {
defaultLocale: 'en-US'
};
const eitherConfig = envOrSource(process.env, configFileSettings);
ENV.OSF = {
clientId: eitherConfig('CLIENT_ID'),
scope: eitherConfig('OAUTH_SCOPES'),
apiNamespace: 'v2', // URL suffix (after host)
backend: BACKEND,
redirectUri: eitherConfig('REDIRECT_URI')
};
// Fetch configuration information for the application
var backendUrlConfig = knownBackends[BACKEND] || {};
if (!Object.keys(knownBackends).includes(BACKEND)) {
console.warn('WARNING: You have specified an unknown backend environment. If you need to customize URL settings, specify BACKEND=env');
}
if (BACKEND === 'local') {
backendUrlConfig.accessToken = eitherConfig('PERSONAL_ACCESS_TOKEN');
backendUrlConfig.isLocal = true;
} else if (BACKEND === 'prod') {
console.warn("WARNING: you've specified production as a backend. Please do not use production for testing or development purposes");
} else if (BACKEND === 'env') {
// Optionally draw backend URL settings entirely from environment variables.
// This is all or nothing: If you want to specify a custom backend, you must provide ALL URLs.
let newConfig = {};
// Map internal config names to the corresponding env var names, eg {url: OSF_URL}. All keys must be present
Object.keys(backendUrlConfig).forEach(internalName => {
const envVarName = backendUrlConfig[internalName];
newConfig[internalName] = eitherConfig(envVarName);
});
backendUrlConfig = newConfig;
}
// Warn the user if some URL entries not present
Object.keys(backendUrlConfig).forEach(key => {
if (!backendUrlConfig[key]) console.error(`This backend must define a value for: ${key}`);
});
// Combine URLs + auth settings into final auth config
Object.assign(ENV.OSF, backendUrlConfig);
const defaultAuthorizationType = 'token';
ENV.authorizationType = defaultAuthorizationType;
ENV['ember-simple-auth'] = {
authorizer: `authorizer:osf-${defaultAuthorizationType}`,
authenticator: `authenticator:osf-${defaultAuthorizationType}`
};
},
afterInstall: function(options) {
if (options['ember-osf'].includeStyles) {
this.addAddonToProject('ember-font-awesome');
}
},
included: function(app) {
// Documentation of the `included` hook is mostly in the comment
// threads of `ember-cli` issues on github. For example:
// https://github.com/ember-cli/ember-cli/issues/3531#issuecomment-81133458
this._super.included.apply(this, arguments);
if (app.options['ember-osf'] && app.options['ember-osf'].includeStyles) {
app.options['ember-font-awesome'] = {
useScss: true
};
}
return app;
},
treeForAddon: function(tree) {
this.addonTree = tree;
return this._super.treeForAddon.apply(this, arguments);
},
treeForVendor: function(tree) {
var addonStyleTree = this._treeFor('addon-styles');
var addonPodStyles = new Funnel(path.resolve(this.root, 'addon'), {
include: [
'components/**/*css'
]
});
var addonCss = compileSass(
[addonStyleTree, addonPodStyles],
'addon.scss',
'assets/ember-osf.css',
{
annotation: 'EmberOsf Sass Tree'
});
return mergeTrees([tree, addonCss].filter(Boolean));
},
treeForPublic() {
var assetDir = path.join(path.resolve(this.root, ''), 'addon/assets');
return new Funnel(assetDir, {
destDir: 'assets/'
});
}
};