Skip to content

Commit

Permalink
Removes a bunch of static usage to get ready for #117
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 5, 2019
1 parent 01665c2 commit 831df5f
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 293 deletions.
6 changes: 6 additions & 0 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const TemplatePath = require("./TemplatePath");
const TemplateData = require("./TemplateData");
const TemplateWriter = require("./TemplateWriter");
const EleventyExtensionMap = require("./EleventyExtensionMap");
const EleventyErrorHandler = require("./EleventyErrorHandler");
const EleventyServe = require("./EleventyServe");
const EleventyWatchTargets = require("./EleventyWatchTargets");
Expand Down Expand Up @@ -140,15 +141,19 @@ class Eleventy {

async init() {
let formats = this.formatsOverride || this.config.templateFormats;
this.extensionMap = new EleventyExtensionMap(formats);

this.eleventyFiles = new EleventyFiles(
this.input,
this.outputDir,
formats,
this.isPassthroughAll
);
this.eleventyFiles.extensionMap = this.extensionMap;
this.eleventyFiles.init();

this.templateData = new TemplateData(this.inputDir);
this.templateData.extensionMap = this.extensionMap;
this.eleventyFiles.setTemplateData(this.templateData);

this.writer = new TemplateWriter(
Expand All @@ -159,6 +164,7 @@ class Eleventy {
this.isPassthroughAll
);

this.writer.extensionMap = this.extensionMap;
this.writer.setEleventyFiles(this.eleventyFiles);

// TODO maybe isVerbose -> console.log?
Expand Down
57 changes: 16 additions & 41 deletions src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const TemplatePath = require("./TemplatePath");
const config = require("./Config");

class EleventyExtensionMap {
constructor(formatKeys = []) {
constructor(formatKeys) {
this.setFormats(formatKeys);
}

setFormats(formatKeys = []) {
this.unfilteredFormatKeys = formatKeys.map(function(key) {
return key.trim().toLowerCase();
});
Expand Down Expand Up @@ -71,8 +75,8 @@ class EleventyExtensionMap {
}

hasExtension(key) {
for (var extension in EleventyExtensionMap.keyMap) {
if (EleventyExtensionMap.keyMap[extension] === key) {
for (var extension in this.keyMap) {
if (this.keyMap[extension] === key) {
return true;
}
}
Expand All @@ -89,17 +93,15 @@ class EleventyExtensionMap {
return extensions;
}

getKey(pathOrKey) {
return EleventyExtensionMap._getKey(pathOrKey, this.keyMap);
}
static getKey(pathOrKey) {
return EleventyExtensionMap._getKey(pathOrKey, EleventyExtensionMap.keyMap);
hasEngine(pathOrKey) {
return !!this.getKey(pathOrKey);
}
static _getKey(pathOrKey, map) {
pathOrKey = pathOrKey.toLowerCase();

for (var extension in map) {
let key = map[extension];
getKey(pathOrKey) {
pathOrKey = (pathOrKey || "").toLowerCase();

for (var extension in this.keyMap) {
let key = this.keyMap[extension];
if (pathOrKey === extension) {
return key;
} else if (pathOrKey.endsWith("." + extension)) {
Expand All @@ -109,16 +111,7 @@ class EleventyExtensionMap {
}

removeTemplateExtension(path) {
return EleventyExtensionMap._removeTemplateExtension(path, this.keyMap);
}
static removeTemplateExtension(path) {
return EleventyExtensionMap._removeTemplateExtension(
path,
EleventyExtensionMap.keyMap
);
}
static _removeTemplateExtension(path, map) {
for (var extension in map) {
for (var extension in this.keyMap) {
if (path === extension || path.endsWith("." + extension)) {
return path.substr(0, path.length - 1 - extension.length);
}
Expand All @@ -127,19 +120,7 @@ class EleventyExtensionMap {
}

get keyMap() {
return EleventyExtensionMap._getKeyMap(
this.config.templateExtensionAliases || {}
);
}
static get keyMap() {
return EleventyExtensionMap._getKeyMap(
config.getConfig().templateExtensionAliases || {}
);
}

// file extension => key
static _getKeyMap(aliases) {
let fileExtensionToKeyMap = {
return {
ejs: "ejs",
md: "md",
jstl: "jstl",
Expand All @@ -152,12 +133,6 @@ class EleventyExtensionMap {
liquid: "liquid",
"11ty.js": "11ty.js"
};

for (let extension in aliases) {
fileExtensionToKeyMap[extension] = aliases[extension];
}

return fileExtensionToKeyMap;
}
}

Expand Down
33 changes: 21 additions & 12 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class EleventyFiles {
this.passthroughAll = !!passthroughAll;

this.formats = formats;
this.extensionMap = new EleventyExtensionMap(formats);
}

initConfig() {
Expand All @@ -42,7 +41,7 @@ class EleventyFiles {

init() {
this.initFormatsGlobs();
this.setPassthroughManager();
this.initPassthroughManager();
this.setupGlobs();
}

Expand All @@ -56,15 +55,22 @@ class EleventyFiles {
this.config = config;
this.initConfig();
}
/* For testing */
_setExtensionMap(map) {
this.extensionMap = map;
}
/* Set command root for local project paths */
_setLocalPathRoot(dir) {
this.localPathRoot = dir;
}

set extensionMap(extensionMap) {
this._extensionMap = extensionMap;
}

get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap(this.formats);
}
return this._extensionMap;
}

setPassthroughAll(passthroughAll) {
this.passthroughAll = !!passthroughAll;
}
Expand All @@ -80,17 +86,20 @@ class EleventyFiles {
}
}

initPassthroughManager() {
let mgr = new TemplatePassthroughManager();
mgr.setInputDir(this.inputDir);
mgr.setOutputDir(this.outputDir);
mgr.extensionMap = this.extensionMap;
this.passthroughManager = mgr;
}

getPassthroughManager() {
return this.passthroughManager;
}

setPassthroughManager(mgr) {
if (!mgr) {
mgr = new TemplatePassthroughManager();
mgr.setInputDir(this.inputDir);
mgr.setOutputDir(this.outputDir);
}

mgr.extensionMap = this.extensionMap;
this.passthroughManager = mgr;
}

Expand Down
13 changes: 3 additions & 10 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const normalize = require("normalize-path");
const lodashIsObject = require("lodash/isObject");
const { DateTime } = require("luxon");

const EleventyExtensionMap = require("./EleventyExtensionMap");
const TemplateData = require("./TemplateData");
const TemplateContent = require("./TemplateContent");
const TemplatePath = require("./TemplatePath");
Expand Down Expand Up @@ -71,9 +70,7 @@ class Template extends TemplateContent {
}

get baseFile() {
return (this._extensionMap || EleventyExtensionMap).removeTemplateExtension(
this.parsed.base
);
return this.extensionMap.removeTemplateExtension(this.parsed.base);
}

get htmlIOException() {
Expand Down Expand Up @@ -450,9 +447,7 @@ class Template extends TemplateContent {
get templateContent() {
if (!this._templateContent) {
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${
this.inputPath
} page ${this.pageNumber})`
`Tried to use templateContent too early (${this.inputPath} page ${this.pageNumber})`
);
}
return this._templateContent;
Expand Down Expand Up @@ -609,9 +604,7 @@ class Template extends TemplateContent {
let date = DateTime.fromISO(data.date, { zone: "utc" });
if (!date.isValid) {
throw new Error(
`date front matter value (${data.date}) is invalid for ${
this.inputPath
}`
`date front matter value (${data.date}) is invalid for ${this.inputPath}`
);
}
debug(
Expand Down
12 changes: 10 additions & 2 deletions src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const normalize = require("normalize-path");
const matter = require("gray-matter");
const lodashSet = require("lodash/set");

const EleventyExtensionMap = require("./EleventyExtensionMap");
const TemplateData = require("./TemplateData");
const TemplateRender = require("./TemplateRender");
const EleventyBaseError = require("./EleventyBaseError");
Expand All @@ -26,7 +27,14 @@ class TemplateContent {
}

/* Used by tests */
_setExtensionMap(map) {
get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap();
}
return this._extensionMap;
}

set extensionMap(map) {
this._extensionMap = map;
}

Expand All @@ -51,7 +59,7 @@ class TemplateContent {
this._templateRender = new TemplateRender(
this.inputPath,
this.inputDir,
this._extensionMap
this.extensionMap
);
}

Expand Down
29 changes: 18 additions & 11 deletions src/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class TemplateData {
this.globalData = null;
}

// for tests
get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap();
}
return this._extensionMap;
}

set extensionMap(map) {
this._extensionMap = map;
}

/* Used by tests */
_setConfig(config) {
this.config = config;
Expand Down Expand Up @@ -161,11 +173,7 @@ class TemplateData {
// TODO maybe merge these two? (if both valid objects)
if (dataFileConflicts[folders]) {
debugWarn(
`Warning: the key for a global data file (${
files[j]
}) will overwrite data from an already existing global data file (${
dataFileConflicts[folders]
})`
`Warning: the key for a global data file (${files[j]}) will overwrite data from an already existing global data file (${dataFileConflicts[folders]})`
);
}
dataFileConflicts[folders] = files[j];
Expand Down Expand Up @@ -290,7 +298,7 @@ class TemplateData {
debugDev("parsed.dir: %o", parsed.dir);

if (parsed.dir) {
let fileNameNoExt = EleventyExtensionMap.removeTemplateExtension(
let fileNameNoExt = this.extensionMap.removeTemplateExtension(
parsed.base
);
let filePathNoExt = parsed.dir + "/" + fileNameNoExt;
Expand Down Expand Up @@ -338,12 +346,11 @@ class TemplateData {
}

static cleanupData(data) {
if ("tags" in data){
if (typeof data.tags === "string"){
if ("tags" in data) {
if (typeof data.tags === "string") {
data.tags = data.tags ? [data.tags] : [];
}
else if (data.tags === null){
data.tags = []
} else if (data.tags === null) {
data.tags = [];
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/TemplatePassthroughManager.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const config = require("./Config");
const EleventyExtensionMap = require("./EleventyExtensionMap");
const EleventyBaseError = require("./EleventyBaseError");
const TemplatePassthrough = require("./TemplatePassthrough");
const TemplateRender = require("./TemplateRender");
const TemplatePath = require("./TemplatePath");
const debug = require("debug")("Eleventy:TemplatePassthroughManager");

class TemplatePassthroughManagerCopyError extends EleventyBaseError {}

class TemplatePassthroughManager {
// TODO bunch of unused args here
constructor(inputDir, outputDir, isDryRun) {
this.config = config.getConfig();

Expand All @@ -23,6 +24,17 @@ class TemplatePassthroughManager {
this.config = configOverride || {};
}

set extensionMap(extensionMap) {
this._extensionMap = extensionMap;
}

get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap();
}
return this._extensionMap;
}

setOutputDir(outputDir) {
this.outputDir = outputDir;
}
Expand Down Expand Up @@ -65,7 +77,7 @@ class TemplatePassthroughManager {

let matches = [];
for (let path of paths) {
if (!TemplateRender.hasEngine(path)) {
if (!this.extensionMap.hasEngine(path)) {
matches.push(path);
}
}
Expand Down
Loading

0 comments on commit 831df5f

Please sign in to comment.