Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config run async #1429

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ api-docs/
_site/

package-lock.json
yarn.lock
23 changes: 14 additions & 9 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const pkg = require("./package.json");
const chalk = require("chalk"); // node 8+
require("please-upgrade-node")(pkg, {
message: function(requiredVersion) {
message: function (requiredVersion) {
return chalk.red(
`Eleventy requires Node ${requiredVersion}. You’ll need to upgrade to use it!`
);
}
},
});

if (process.env.DEBUG) {
Expand All @@ -19,8 +19,8 @@ try {
const argv = require("minimist")(process.argv.slice(2), {
boolean: ["quiet"],
default: {
quiet: null
}
quiet: null,
},
});
const Eleventy = require("./src/Eleventy");
const EleventyCommandCheck = require("./src/EleventyCommandCheck");
Expand All @@ -31,10 +31,10 @@ try {
`Unhandled rejection in promise (${promise})`
);
});
process.on("uncaughtException", error => {
process.on("uncaughtException", (error) => {
EleventyErrorHandler.fatal(error, "Uncaught exception");
});
process.on("rejectionHandled", promise => {
process.on("rejectionHandled", (promise) => {
EleventyErrorHandler.warn(
promise,
"A promise rejection was handled asynchronously"
Expand All @@ -46,12 +46,15 @@ try {
cmdCheck.hasUnknownArguments();

let elev = new Eleventy(argv.input, argv.output);
elev.setConfigPathOverride(argv.config);

elev.setPathPrefix(argv.pathprefix);
elev.setDryRun(argv.dryrun);
elev.setIncrementalBuild(argv.incremental);
elev.setPassthroughAll(argv.passthroughall);
elev.setFormats(argv.formats);
if (argv.config) {
await elev.setConfigPathOverride(argv.config);
}

// --quiet and --quiet=true resolves to true
if (argv.quiet === true || argv.quiet === false) {
Expand All @@ -62,13 +65,15 @@ try {
// with old node versions in `please-upgrade-node` above.
elev
.init()
.then(function() {
.then(async function () {
// Async config is going to require everything inside init()
MadeByMike marked this conversation as resolved.
Show resolved Hide resolved

if (argv.version) {
console.log(elev.getVersion());
} else if (argv.help) {
console.log(elev.getHelp());
} else if (argv.serve) {
elev.watch().then(function() {
elev.watch().then(function () {
elev.serve(argv.port);
});
} else if (argv.watch) {
Expand Down
57 changes: 33 additions & 24 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@ const debug = require("debug")("Eleventy");
*/
class Eleventy {
constructor(input, output) {
/** @member {Object} - tbd. */
this.config = config.getConfig();

/**
* @member {String} - The path to Eleventy's config file.
* @default null
*/
this.configPath = null;

/**
* @member {Boolean} - Is Eleventy running in verbose mode?
* @default true
*/
this.isVerbose = process.env.DEBUG ? false : !this.config.quietMode;

/**
* @member {Boolean} - Was verbose mode overridden manually?
* @default false
Expand All @@ -64,6 +55,7 @@ class Eleventy {
*/
this.isDryRun = false;

//TODO: does this belong here still? Move into init?
if (performance) {
debug("Eleventy warm up time (in ms) %o", performance.now());
}
Expand All @@ -77,9 +69,6 @@ class Eleventy {
*/
this.formatsOverride = null;

/** @member {Object} - tbd. */
this.eleventyServe = new EleventyServe();

/** @member {String} - Holds the path to the input directory. */
this.rawInput = input;

Expand All @@ -91,8 +80,9 @@ class Eleventy {

/** @member {Object} - tbd. */
this.watchTargets = new EleventyWatchTargets();
this.watchTargets.addAndMakeGlob(this.config.additionalWatchTargets);
this.watchTargets.watchJavaScriptDependencies = this.config.watchJavaScriptDependencies;

/** @member {Object} - tbd. */
this.eleventyServe = new EleventyServe();
}

getNewTimestamp() {
Expand Down Expand Up @@ -183,11 +173,11 @@ class Eleventy {
* @method
* @param {String} configPath - The new config path.
*/
setConfigPathOverride(configPath) {
async setConfigPathOverride(configPath) {
if (configPath) {
this.configPath = configPath;

config.setProjectConfigPath(configPath);
await config.setProjectConfigPath(configPath);
this.config = config.getConfig();
}
}
Expand Down Expand Up @@ -289,30 +279,49 @@ class Eleventy {
* @returns {} - tbd.
*/
async init() {
if (!this.config) {
await config.init();
}
/** @member {Object} - tbd. */
this.config = config.getConfig();
/**
* @member {Boolean} - Is Eleventy running in verbose mode?
* @default true
*/
this.isVerbose = process.env.DEBUG ? false : !this.config.quietMode;

this.watchTargets.addAndMakeGlob(this.config.additionalWatchTargets);
this.watchTargets.watchJavaScriptDependencies = this.config.watchJavaScriptDependencies;

/** @member {Object} - tbd. */
this.eleventyServe.config = config;

this.config.inputDir = this.inputDir;

let formats = this.formatsOverride || this.config.templateFormats;
this.extensionMap = new EleventyExtensionMap(formats);
this.extensionMap = new EleventyExtensionMap(formats, config);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d super rather just use the setter style here rather than changing the constructor signature. this.extensionMap.config =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can attempt this, but in some cases config is already used in the constructor and I'm not sure I know how to handle that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could really use an example I think.


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

this.templateData = new TemplateData(this.inputDir);
this.templateData = new TemplateData(this.inputDir, config);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the comment above but it looks like this one will be a little more annoying because there isn’t a setter in place already

this.templateData.extensionMap = this.extensionMap;
this.eleventyFiles.setTemplateData(this.templateData);
this.eleventyFiles.init();

this.writer = new TemplateWriter(
this.input,
this.outputDir,
formats,
this.templateData,
this.isPassthroughAll
this.isPassthroughAll,
config
);

this.writer.extensionMap = this.extensionMap;
Expand Down Expand Up @@ -425,8 +434,8 @@ Arguments:
*
* @method
*/
resetConfig() {
config.reset();
async resetConfig() {
await config.reset();

this.config = config.getConfig();
this.eleventyServe.config = this.config;
Expand Down Expand Up @@ -465,7 +474,7 @@ Arguments:

// reset and reload global configuration :O
if (this.watchManager.hasQueuedFile(config.getLocalProjectConfigFile())) {
this.resetConfig();
await this.resetConfig();
}

await this.restart();
Expand Down
9 changes: 5 additions & 4 deletions src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const TemplateEngineManager = require("./TemplateEngineManager");
const TemplatePath = require("./TemplatePath");

class EleventyExtensionMap {
constructor(formatKeys) {
constructor(formatKeys, templateConfig) {
this.formatKeys = formatKeys;

this.config = templateConfig.getConfig();
this.templateConfig = templateConfig;
this.setFormats(formatKeys);
}

Expand All @@ -23,15 +24,15 @@ class EleventyExtensionMap {
}

get config() {
return this.configOverride || require("./Config").getConfig();
return this.configOverride || this.config;
Comment on lines -26 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason I am confused about using a getter\setter is that config is not the same thing as templateConfig need multiple getters\setters?

}
set config(cfg) {
this.configOverride = cfg;
}

get engineManager() {
if (!this._engineManager) {
this._engineManager = new TemplateEngineManager();
this._engineManager = new TemplateEngineManager(this.templateConfig);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getter/Setter here too?

this._engineManager.config = this.config;
}

Expand Down
16 changes: 9 additions & 7 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ const TemplateData = require("./TemplateData");
const TemplateGlob = require("./TemplateGlob");
const TemplatePath = require("./TemplatePath");
const TemplatePassthroughManager = require("./TemplatePassthroughManager");

const config = require("./Config");
const debug = require("debug")("Eleventy:EleventyFiles");
// const debugDev = require("debug")("Dev:Eleventy:EleventyFiles");
const aggregateBench = require("./BenchmarkManager").get("Aggregate");

class EleventyFiles {
constructor(input, outputDir, formats, passthroughAll) {
this.config = config.getConfig();
constructor(input, outputDir, formats, passthroughAll, templateConfig) {
this.config = templateConfig.getConfig();
this.templateConfig = templateConfig;
this.input = input;
this.inputDir = TemplatePath.getDir(this.input);
this.outputDir = outputDir;
Expand Down Expand Up @@ -100,7 +99,10 @@ class EleventyFiles {
get extensionMap() {
// for tests
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap(this.formats);
this._extensionMap = new EleventyExtensionMap(
this.formats,
this.templateConfig
);
this._extensionMap.config = this.config;
}
return this._extensionMap;
Expand All @@ -111,7 +113,7 @@ class EleventyFiles {
}

initPassthroughManager() {
let mgr = new TemplatePassthroughManager();
let mgr = new TemplatePassthroughManager(this.templateConfig);
mgr.setInputDir(this.inputDir);
mgr.setOutputDir(this.outputDir);
mgr.extensionMap = this.extensionMap;
Expand All @@ -134,7 +136,7 @@ class EleventyFiles {
// TODO make this a getter
getTemplateData() {
if (!this.templateData) {
this.templateData = new TemplateData(this.inputDir);
this.templateData = new TemplateData(this.inputDir, this.templateConfig);
}
return this.templateData;
}
Expand Down
12 changes: 6 additions & 6 deletions src/EleventyServe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const fs = require("fs-extra");

const TemplatePath = require("./TemplatePath");
const config = require("./Config");
const debug = require("debug")("EleventyServe");

class EleventyServe {
constructor() {}

get config() {
return this.configOverride || config.getConfig();
return this.configOverride || this.config;
}

set config(config) {
this.configOverride = config;
}
Expand Down Expand Up @@ -41,7 +41,7 @@ class EleventyServe {

// TODO customize this in Configuration API?
let serverConfig = {
baseDir: this.outputDir
baseDir: this.outputDir,
};

let redirectDirName = this.getRedirectDirOverride();
Expand All @@ -68,7 +68,7 @@ class EleventyServe {
watch: false,
open: false,
notify: false,
index: "index.html"
index: "index.html",
},
this.config.browserSyncConfig
);
Expand All @@ -78,7 +78,7 @@ class EleventyServe {
if (dirName && dirName !== "/") {
let savedPathFilename = this.getRedirectFilename(dirName);

setTimeout(function() {
setTimeout(function () {
if (!fs.existsSync(savedPathFilename)) {
debug(`Cleanup redirect: Could not find ${savedPathFilename}`);
return;
Expand All @@ -94,7 +94,7 @@ class EleventyServe {
return;
}

fs.unlink(savedPathFilename, err => {
fs.unlink(savedPathFilename, (err) => {
if (!err) {
debug(`Cleanup redirect: Deleted ${savedPathFilename}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Engines/Custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const TemplateEngine = require("./TemplateEngine");
const getJavaScriptData = require("../Util/GetJavaScriptData");

class CustomEngine extends TemplateEngine {
constructor(name, includesDir) {
super(name, includesDir);
constructor(name, includesDir, eleventyConfig) {
super(name, includesDir, eleventyConfig);

this.entry = this.getExtensionMapEntry();
this.needsInit =
Expand Down
8 changes: 4 additions & 4 deletions src/Engines/Ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const ejsLib = require("ejs");
const TemplateEngine = require("./TemplateEngine");

class Ejs extends TemplateEngine {
constructor(name, includesDir) {
super(name, includesDir);
constructor(name, includesDir, templateConfig) {
super(name, includesDir, templateConfig);

this.ejsOptions = {};

Expand Down Expand Up @@ -31,7 +31,7 @@ class Ejs extends TemplateEngine {
{
root: "./" + includesDir,
compileDebug: true,
filename: "./" + includesDir
filename: "./" + includesDir,
},
this.ejsOptions || {}
);
Expand All @@ -46,7 +46,7 @@ class Ejs extends TemplateEngine {

let fn = this.ejsLib.compile(str, options);

return function(data) {
return function (data) {
return fn(data);
};
}
Expand Down
Loading