-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfigManager.js
116 lines (99 loc) · 2.71 KB
/
configManager.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
import { join, relative } from "path";
/* eslint-disable import/no-extraneous-dependencies */
/* covered by nuxt */
import _ from "lodash";
import CmsConfigFile from "./utils/cms.config.file";
const NUXT_CONFIG_KEY = "netlifyCms";
const NUXT_DIST_DIR = "dist";
const CMS_CONFIG_KEY = "cmsConfig";
const CMS_CONFIG_DIR = "netlify-cms";
const CMS_CONFIG_FILENAME = "config.yml";
// Defaults
const DEFAULTS = {
adminPath: "admin",
adminTitle: "Content Manager",
cmsConfig: {
media_folder: "static/uploads"
}
};
class ConfigManager {
constructor(nuxtOptions, moduleOptions) {
this._relativeSrcDir = relative(nuxtOptions.rootDir, nuxtOptions.srcDir);
this._userOptions = {
...nuxtOptions[NUXT_CONFIG_KEY],
...moduleOptions
};
const options = _.omit(
{
...DEFAULTS,
...this._userOptions
},
CMS_CONFIG_KEY
);
options.adminPath = options.adminPath.replace(/\/?$/, "/");
options.buildDir = join(
nuxtOptions.buildDir,
NUXT_DIST_DIR,
options.adminPath
);
options.moduleConfigDir = join(nuxtOptions.srcDir, CMS_CONFIG_DIR);
this._config = Object.freeze(options);
this._cmsConfigFile = new CmsConfigFile(
join(this._config.moduleConfigDir, CMS_CONFIG_FILENAME)
);
}
get config() {
return this._config;
}
get cmsConfig() {
const config = this.constructor.setConfigPaths(
{
...DEFAULTS[CMS_CONFIG_KEY],
...this._cmsConfigFile.config,
...this._userOptions[CMS_CONFIG_KEY]
},
this._relativeSrcDir
);
return config;
}
get cmsConfigFileName() {
return this._cmsConfigFile.fileName;
}
readCmsConfigFile() {
return this._cmsConfigFile.readFile();
}
static setConfigPaths(configObject, enforcedPath) {
const newConfig = {
collections: []
};
newConfig.media_folder = join(enforcedPath, configObject.media_folder);
if (configObject.collections) {
configObject.collections.forEach(function(collection) {
if (collection.folder) {
newConfig.collections.push({
...collection,
folder: join(enforcedPath, collection.folder)
});
} else if (collection.files) {
const collectionFiles = [];
collection.files.forEach(function(fileEntry) {
fileEntry.file &&
collectionFiles.push({
...fileEntry,
file: join(enforcedPath, fileEntry.file)
});
});
newConfig.collections.push({
...collection,
files: collectionFiles
});
}
});
}
return {
...configObject,
...newConfig
};
}
}
export default ConfigManager;