Skip to content

Commit

Permalink
Adds --dryrun to run through eleventy without writing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 10, 2018
1 parent 3a67143 commit 9a9cf10
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ DEBUG=Eleventy* eleventy

This will tell you exactly what directories Eleventy is using for data, includes, input, and output. It’ll tell you what search globs it uses to find your templates and what templates it finds. If you’re having trouble, enable this.

_(New in Eleventy `v0.2.16`)_ Works great with `--dryrun` if you want to run Eleventy but not actually write any files.

Read more at the [`debug` package documentation](https://www.npmjs.com/package/debug).

### Example: Default options
Expand Down
4 changes: 3 additions & 1 deletion cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ EleventyNodeVersionCheck().then(function() {
(argv.pathprefix ? " --pathprefix=" + argv.pathprefix : "") +
(argv.quiet ? " --quiet" : "") +
(argv.version ? " --version" : "") +
(argv.watch ? " --watch" : "")
(argv.watch ? " --watch" : "") +
(argv.dryrun ? " --dryrun" : "")
);

let elev = new Eleventy(argv.input, argv.output);
elev.setConfigPath(argv.config);
elev.setPathPrefix(argv.pathprefix);
elev.setDryRun(argv.dryrun);
elev.setFormats(argv.formats);

if (process.env.DEBUG) {
Expand Down
8 changes: 8 additions & 0 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Eleventy(input, output) {
this.data = null;
this.isVerbose = true;
this.isDebug = false;
this.isDryRun = false;

this.start = new Date();
this.formatsOverride = null;
Expand All @@ -38,6 +39,10 @@ Eleventy.prototype._getDir = function(inputPath) {
return inputPath;
};

Eleventy.prototype.setDryRun = function(isDryRun) {
this.isDryRun = !!isDryRun;
};

Eleventy.prototype.setPathPrefix = function(pathPrefix) {
if (pathPrefix || pathPrefix === "") {
config.setPathPrefix(pathPrefix);
Expand Down Expand Up @@ -115,6 +120,7 @@ Output: ${this.outputDir}
Template Formats: ${formats.join(",")}`);

this.writer.setVerboseOutput(this.isVerbose);
this.writer.setDryRun(this.isDryRun);

return this.data.cacheData();
};
Expand Down Expand Up @@ -165,6 +171,8 @@ Eleventy.prototype.getHelp = function() {
out.push(
" Override the eleventy config file path (default: `.eleventy.js`)"
);
out.push(" --dryrun");
out.push(" Don’t write any files.");
out.push(" --help");
return out.join("\n");
};
Expand Down
18 changes: 13 additions & 5 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Template {
this.parsed.name === "index";

this.isVerbose = true;
this.isDryRun = false;
this.writeCount = 0;
this.initialLayout = undefined;
this.wrapWithLayouts = true;
Expand All @@ -72,6 +73,10 @@ class Template {
this.isVerbose = isVerbose;
}

setDryRun(isDryRun) {
this.isDryRun = !!isDryRun;
}

setWrapWithLayouts(wrap) {
this.wrapWithLayouts = wrap;
}
Expand Down Expand Up @@ -257,6 +262,7 @@ class Template {
data.page.date
);
}

data.page.date = await this.getMappedDate(data);

return data;
Expand Down Expand Up @@ -469,12 +475,15 @@ class Template {
// debug(`Template.write: ${outputPath}
// ${finalContent}`);
this.writeCount++;
await pify(fs.outputFile)(outputPath, finalContent);
if (!this.isDryRun) {
await pify(fs.outputFile)(outputPath, finalContent);
}

let writeDesc = this.isDryRun ? "Pretending to write" : "Writing";
if (this.isVerbose) {
console.log(`Writing ${outputPath} from ${this.inputPath}.`);
console.log(`${writeDesc} ${outputPath} from ${this.inputPath}.`);
} else {
debug("Writing %o from %o.", outputPath, this.inputPath);
debug(`${writeDesc} %o from %o.`, outputPath, this.inputPath);
}
} else {
debug(
Expand All @@ -492,6 +501,7 @@ class Template {
this.templateData
);
tmpl.setIsVerbose(this.isVerbose);
tmpl.setDryRun(this.isDryRun);
return tmpl;
}

Expand Down Expand Up @@ -562,8 +572,6 @@ class Template {
// CREATED
return createdDate;
}

return date;
}

async isEqual(compareTo) {
Expand Down
9 changes: 8 additions & 1 deletion src/TemplatePassthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ class TemplatePassthrough {
constructor(inputPath, outputDir) {
this.path = inputPath;
this.outputDir = outputDir;
this.isDryRun = false;
}

getOutputPath() {
return TemplatePath.normalize(this.outputDir, this.path);
}

setDryRun(isDryRun) {
this.isDryRun = !!isDryRun;
}

async write() {
// debug(
// `${this.path} has no TemplateEngine engine and will copy to ${
// this.outputDir
// }`
// );

return fs.copy(this.path, this.getOutputPath());
if (!this.isDryRun) {
return fs.copy(this.path, this.getOutputPath());
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/TemplateWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function TemplateWriter(inputPath, outputDir, extensions, templateData) {
this.outputDir = outputDir;
this.templateData = templateData;
this.isVerbose = true;
this.isDryRun = false;
this.writeCount = 0;
this.copyCount = 0;

Expand Down Expand Up @@ -174,6 +175,7 @@ TemplateWriter.prototype._getTemplate = function(path) {
);

tmpl.setIsVerbose(this.isVerbose);
tmpl.setDryRun(this.isDryRun);

/*
* Sample filter: arg str, return pretty HTML string
Expand Down Expand Up @@ -205,6 +207,7 @@ TemplateWriter.prototype._getTemplate = function(path) {

TemplateWriter.prototype._copyPassthroughPath = async function(path) {
let pass = new TemplatePassthrough(path, this.outputDir);
pass.setDryRun(this.isDryRun);
try {
await pass.write();
debugDev("Copied %o", path);
Expand Down Expand Up @@ -294,6 +297,10 @@ TemplateWriter.prototype.setVerboseOutput = function(isVerbose) {
this.isVerbose = isVerbose;
};

TemplateWriter.prototype.setDryRun = function(isDryRun) {
this.isDryRun = !!isDryRun;
};

TemplateWriter.prototype.getCopyCount = function() {
return this.copyCount;
};
Expand Down
6 changes: 6 additions & 0 deletions test/TemplatePassthroughTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ test("Constructor", t => {
t.truthy(pass);
t.is(pass.getOutputPath(), "_site/avatar.png");
});

test("Constructor Dry Run", t => {
let pass = new TemplatePassthrough("avatar.png", "_site");
pass.setDryRun(true);
t.is(pass.isDryRun, true);
});

0 comments on commit 9a9cf10

Please sign in to comment.