Skip to content

Commit d526f01

Browse files
committed
refactor(core): extract config and cms config file management
Add `lodash.omit` and `needlepoint` dependencies Add `babel-plugin-transform-decorators-legacy` devDependencies
1 parent 312c14b commit d526f01

File tree

8 files changed

+176
-121
lines changed

8 files changed

+176
-121
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
],
2525
"dependencies": {
2626
"js-yaml": "^3.9.1",
27+
"lodash.omit": "^4.5.0",
28+
"needlepoint": "^1.0.5",
2729
"style-loader": "^0.18.2"
2830
},
2931
"peerDependencies": {
@@ -32,6 +34,7 @@
3234
"devDependencies": {
3335
"babel-cli": "^6.26.0",
3436
"babel-eslint": "^7.2.3",
37+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
3538
"babel-plugin-transform-object-rest-spread": "^6.26.0",
3639
"babel-preset-env": "^1.6.0",
3740
"codecov": "^2.3.0",
@@ -65,7 +68,8 @@
6568
{
6669
"useBuiltIns": true
6770
}
68-
]
71+
],
72+
"transform-decorators-legacy"
6973
],
7074
"env": {
7175
"test": {

src/configManager.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { join, relative } from "path";
2+
3+
import omit from "lodash.omit";
4+
5+
import CmsConfigFile from "./utils/cms.config.file";
6+
7+
const NUXT_CONFIG_KEY = "netlifyCms";
8+
const CMS_CONFIG_KEY = "cmsConfig";
9+
const CMS_CONFIG_FILENAME = "netlify-cms.yml";
10+
11+
// Defaults
12+
const DEFAULTS = {
13+
adminPath: "admin",
14+
adminTitle: "Content Manager",
15+
extensionsDir: "netlify-cms",
16+
cmsConfig: {
17+
media_folder: "static/uploads"
18+
}
19+
};
20+
21+
class ConfigManager {
22+
constructor(nuxtOptions, moduleOptions) {
23+
this._cmsConfigFile = new CmsConfigFile(
24+
join(nuxtOptions.rootDir, CMS_CONFIG_FILENAME)
25+
);
26+
27+
this._relativeSrcDir = relative(nuxtOptions.rootDir, nuxtOptions.srcDir);
28+
29+
this._userOptions = {
30+
...nuxtOptions[NUXT_CONFIG_KEY],
31+
...moduleOptions
32+
};
33+
34+
const options = omit(
35+
{
36+
...DEFAULTS,
37+
...this._userOptions
38+
},
39+
CMS_CONFIG_KEY
40+
);
41+
options.adminPath = options.adminPath.replace(/\/?$/, "/");
42+
options.extensionsDir = join(nuxtOptions.srcDir, options.extensionsDir);
43+
options.buildDir = join(nuxtOptions.buildDir, "dist", options.adminPath);
44+
this._config = options;
45+
}
46+
47+
get config() {
48+
return this._config;
49+
}
50+
51+
get cmsConfig() {
52+
const config = this.constructor.setConfigPaths(
53+
{
54+
...DEFAULTS[CMS_CONFIG_KEY],
55+
...this._cmsConfigFile.config,
56+
...this._userOptions[CMS_CONFIG_KEY]
57+
},
58+
this._relativeSrcDir
59+
);
60+
return config;
61+
}
62+
63+
static setConfigPaths(configObject, enforcedPath) {
64+
const newConfig = {
65+
collections: []
66+
};
67+
68+
if (configObject.media_folder) {
69+
newConfig.media_folder = join(enforcedPath, configObject.media_folder);
70+
}
71+
72+
if (configObject.collections) {
73+
configObject.collections.forEach(function(collection) {
74+
collection.folder &&
75+
newConfig.collections.push({
76+
...collection,
77+
folder: join(enforcedPath, collection.folder)
78+
});
79+
});
80+
}
81+
82+
return {
83+
...configObject,
84+
...newConfig
85+
};
86+
}
87+
}
88+
89+
export default ConfigManager;

src/module.js

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { join, resolve, relative } from "path";
2-
31
/* eslint-disable import/no-extraneous-dependencies */
42
/* covered by nuxt */
53
import _ from "lodash";
@@ -13,97 +11,39 @@ import Debug from "debug";
1311

1412
import pkg from "../package.json";
1513

16-
import { loadYAMLFile, toYAML } from "./utils/yaml.js";
17-
import { setConfigPaths } from "./utils/netlify.config.js";
18-
import webpackNetlifyConfig from "./webpack.config.js";
14+
import ConfigManager from "./configManager";
15+
import getWebpackNetlifyConfig from "./webpack.config";
16+
import { toYAML } from "./utils/yaml";
1917

2018
const debug = Debug("nuxt:netlify-cms");
2119

2220
const WEBPACK_CLIENT_COMPILER_NAME = "client";
2321
const WEBPACK_NETLIFY_COMPILER_NAME = "netlify-cms";
24-
25-
// Defaults
26-
const DEFAULTS = {
27-
adminPath: "admin",
28-
adminTitle: "Content Manager",
29-
extensionsDir: "netlify-cms",
30-
cmsConfig: {
31-
media_folder: "static/uploads"
32-
}
33-
};
22+
const NETLIFY_CONFIG_FILE_NAME = "config.yml";
3423

3524
export default function NetlifyCmsModule(moduleOptions) {
36-
const NETLIFY_CONFIG_FILE_NAME = join(
37-
this.options.rootDir,
38-
"netlify-cms.yml"
39-
);
40-
41-
const getNetlifyCmsConfigFromFile = function() {
42-
return loadYAMLFile(NETLIFY_CONFIG_FILE_NAME) || {};
43-
};
44-
45-
const transformNetlifyCmsConfigPaths = config => {
46-
return setConfigPaths(
47-
config,
48-
relative(this.options.rootDir, this.options.srcDir)
49-
);
50-
};
51-
52-
const getWebpackNetlifyConfig = function(
53-
builder,
54-
adminPath,
55-
adminTitle,
56-
extensionsDir
57-
) {
58-
return webpackNetlifyConfig.call(
59-
builder,
60-
WEBPACK_NETLIFY_COMPILER_NAME,
61-
adminPath,
62-
adminTitle,
63-
extensionsDir
64-
);
65-
};
66-
67-
const getDistDir = adminPath => {
68-
return resolve(this.options.buildDir, "dist", adminPath);
69-
};
70-
71-
const getOptions = () => {
72-
const netlifyCmsConfig = getNetlifyCmsConfigFromFile();
73-
const options = {
74-
...DEFAULTS,
75-
cmsConfig: {
76-
...netlifyCmsConfig
77-
},
78-
...this.options.netlifyCms,
79-
...moduleOptions
80-
};
81-
options.cmsConfig = transformNetlifyCmsConfigPaths(options.cmsConfig);
82-
return options;
83-
};
25+
const configManager = new ConfigManager(this.options, moduleOptions);
26+
const config = configManager.config;
8427

85-
const options = getOptions();
86-
const ADMIN_PATH = options.adminPath.replace(/\/?$/, "/");
87-
const ADMIN_TITLE = options.adminTitle;
88-
const EXTENSIONS_DIR = options.extensionsDir;
89-
const DIST_DIR = getDistDir(ADMIN_PATH);
28+
const ADMIN_PATH = config.adminPath;
29+
const EXTENSIONS_DIR = config.extensionsDir;
30+
const BUILD_DIR = config.buildDir;
9031

9132
// This will be called once when builder started
9233
this.nuxt.plugin("build", async builder => {
9334
// This will be run just before webpack compiler starts
9435
builder.plugin("compile", ({ builder, compiler }) => {
9536
const webpackConfig = getWebpackNetlifyConfig(
96-
builder,
97-
ADMIN_PATH,
98-
ADMIN_TITLE,
99-
EXTENSIONS_DIR
37+
WEBPACK_NETLIFY_COMPILER_NAME,
38+
this.options,
39+
config
10040
);
10141

10242
webpackConfig.plugins.push({
10343
apply(compiler) {
10444
compiler.plugin("emit", (compilation, cb) => {
105-
const netlifyConfigYAML = toYAML(getOptions().cmsConfig);
106-
compilation.assets["config.yml"] = {
45+
const netlifyConfigYAML = toYAML(configManager.cmsConfig);
46+
compilation.assets[NETLIFY_CONFIG_FILE_NAME] = {
10747
source: () => netlifyConfigYAML,
10848
size: () => netlifyConfigYAML.length
10949
};
@@ -178,7 +118,10 @@ export default function NetlifyCmsModule(moduleOptions) {
178118
});
179119

180120
// Start watching config file
181-
const patterns = [Utils.r(NETLIFY_CONFIG_FILE_NAME)];
121+
const patterns = [
122+
Utils.r(NETLIFY_CONFIG_FILE_NAME),
123+
Utils.r(EXTENSIONS_DIR)
124+
];
182125

183126
const options = {
184127
...this.options.watchers.chokidar,
@@ -207,7 +150,7 @@ export default function NetlifyCmsModule(moduleOptions) {
207150
// Statically serve netlify CMS (i.e. .nuxt/dist/admin/) files in production
208151
this.addServerMiddleware({
209152
path: ADMIN_PATH,
210-
handler: serveStatic(DIST_DIR, {
153+
handler: serveStatic(BUILD_DIR, {
211154
maxAge: "1y" // 1 year in production
212155
})
213156
});

src/utils/cms.config.file.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { singleton } from "needlepoint";
2+
3+
import { loadYAMLFile } from "./yaml";
4+
5+
@singleton
6+
class CmsConfig {
7+
constructor(fileName) {
8+
this._fileName = fileName;
9+
this.readFile();
10+
}
11+
12+
readFile() {
13+
this._config = loadYAMLFile(this._fileName) || {};
14+
}
15+
16+
get config() {
17+
return this._config;
18+
}
19+
}
20+
21+
export default CmsConfig;

src/utils/netlify.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/webpack.config.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from "fs";
2-
import { join, resolve } from "path";
2+
import { resolve } from "path";
33

44
import { Utils } from "nuxt";
55
/* eslint-disable import/no-extraneous-dependencies */
@@ -10,19 +10,29 @@ import ExtractTextPlugin from "extract-text-webpack-plugin";
1010

1111
export default function webpackNetlifyCmsConfig(
1212
name,
13-
urlPath,
14-
pageTitle,
15-
extensionsDir
13+
nuxtOptions,
14+
moduleConfig
1615
) {
17-
const EXTENSIONS_DIR = join(this.options.srcDir, extensionsDir);
16+
const ENTRY = resolve(__dirname, "../lib/entry.js");
17+
const BUILD_DIR = moduleConfig.buildDir;
18+
const CHUNK_FILENAME = nuxtOptions.build.filenames.chunk;
19+
const PUBLIC_PATH = Utils.urlJoin(
20+
nuxtOptions.router.base,
21+
moduleConfig.adminPath
22+
);
23+
const EXTENSIONS_DIR = moduleConfig.extensionsDir;
24+
const PAGE_TITLE = moduleConfig.adminTitle;
25+
const PAGE_TEMPLATE = resolve(__dirname, "../lib/template", "index.html");
26+
const REQUIRE_EXTENSIONS = existsSync(EXTENSIONS_DIR) ? true : false;
27+
1828
const config = {
1929
name,
20-
entry: resolve(__dirname, "../lib/entry.js"),
30+
entry: ENTRY,
2131
output: {
22-
path: resolve(this.options.buildDir, "dist", urlPath),
32+
path: BUILD_DIR,
2333
filename: "bundle.[chunkhash].js",
24-
chunkFilename: this.options.build.filenames.chunk,
25-
publicPath: Utils.urlJoin(this.options.router.base, urlPath)
34+
chunkFilename: CHUNK_FILENAME,
35+
publicPath: PUBLIC_PATH
2636
},
2737
module: {
2838
loaders: [
@@ -44,22 +54,22 @@ export default function webpackNetlifyCmsConfig(
4454
},
4555
plugins: [
4656
new HTMLPlugin({
47-
title: pageTitle,
57+
title: PAGE_TITLE,
4858
filename: "index.html",
49-
template: resolve(__dirname, "../lib/template", "index.html"),
59+
template: PAGE_TEMPLATE,
5060
inject: true,
5161
chunksSortMode: "dependency"
5262
}),
5363
new webpack.DefinePlugin({
54-
REQUIRE_EXTENSIONS: existsSync(EXTENSIONS_DIR) ? true : false
64+
REQUIRE_EXTENSIONS
5565
})
5666
]
5767
};
5868

5969
// --------------------------------------
6070
// Production specific config
6171
// --------------------------------------
62-
if (!this.options.dev) {
72+
if (!nuxtOptions.dev) {
6373
// CSS extraction
6474
config.plugins.push(
6575
new ExtractTextPlugin({

test/__snapshots__/config.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`config netlify-cms.yml 1`] = `
4-
"backend:
4+
"media_folder: test/fixture/static/uploads
5+
backend:
56
name: github
67
repo: me/test
78
branch: master
8-
media_folder: test/fixture/static/uploads
99
collections:
1010
- name: pages
1111
label: Page

0 commit comments

Comments
 (0)