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

addPassthroughCopy: expose copyOptions, allow to follow symlinks #1686

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/TemplatePassthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TemplatePassthrough {
this.outputPath = path.outputPath;
this.outputDir = outputDir;

this.copyOptions = path.copyOptions; // custom options for recursive-copy

this.isDryRun = false;
}

Expand Down Expand Up @@ -124,12 +126,19 @@ class TemplatePassthrough {
});
}

const copyOptions = {
overwrite: true,
dot: true,
junk: false,
// default options for recursive-copy
// see https://www.npmjs.com/package/recursive-copy#arguments
const copyOptionsDefault = {
overwrite: true, // overwrite output. fails when input is directory (mkdir) and output is file
dot: true, // copy dotfiles
junk: false, // copy cache files like Thumbs.db
results: false,
expand: false, // follow symlinks
debug: false,
};

const copyOptions = Object.assign(copyOptionsDefault, this.copyOptions);

let promises = [];

debug("Copying %o", this.inputPath);
Expand Down
47 changes: 25 additions & 22 deletions src/TemplatePassthroughManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,22 @@ class TemplatePassthroughManager {
this.incrementalFile = path;
}

_normalizePaths(path, outputPath) {
_normalizePaths(path, outputPath, copyOptions = {}) {
return {
inputPath: TemplatePath.addLeadingDotSlash(path),
outputPath: outputPath
? TemplatePath.stripLeadingDotSlash(outputPath)
: true,
copyOptions,
};
}

getConfigPaths() {
let paths = [];
let target = this.config.passthroughCopies || {};
debug("`addPassthroughCopy` config API paths: %o", target);
for (let path in target) {
paths.push(this._normalizePaths(path, target[path]));
const paths = [];
const pathsRaw = this.config.passthroughCopies || {};
debug("`addPassthroughCopy` config API paths: %o", pathsRaw);
for (const [inputPath, { outputPath, copyOptions }] of Object.entries(pathsRaw)) {
paths.push(this._normalizePaths(inputPath, outputPath, copyOptions));
}
debug("`addPassthroughCopy` config API normalized paths: %o", paths);
return paths;
Expand Down Expand Up @@ -175,23 +176,27 @@ class TemplatePassthroughManager {
return [this._normalizePaths(this.incrementalFile)];
}

let normalizedPaths = [];

let pathsFromConfigurationFile = this.getConfigPaths();
for (let path of pathsFromConfigurationFile) {
debug("TemplatePassthrough copying from config: %o", path);
normalizedPaths.push(path);
const normalizedPaths = this.getConfigPaths();
if (debug.enabled) {
for (const path of normalizedPaths) {
debug("TemplatePassthrough copying from config: %o", path);
}
}

if (paths && paths.length) {
let passthroughPaths = this.getNonTemplatePaths(paths);
for (let path of passthroughPaths) {
let normalizedPath = this._normalizePaths(path);
debug(
`TemplatePassthrough copying from non-matching file extension: ${normalizedPath.inputPath}`
);
const passthroughPaths = this.getNonTemplatePaths(paths);
for (const path of passthroughPaths) {
const normalizedPath = this._normalizePaths(path);
normalizedPaths.push(normalizedPath);
}
if (debug.enabled) {
for (const path of passthroughPaths) {
const normalizedPath = this._normalizePaths(path);
debug(
`TemplatePassthrough copying from non-matching file extension: ${normalizedPath.inputPath}`
);
}
}
}

return normalizedPaths;
Expand All @@ -203,10 +208,8 @@ class TemplatePassthroughManager {
async copyAll(paths) {
debug("TemplatePassthrough copy started.");
let normalizedPaths = this.getAllNormalizedPaths(paths);
let passthroughs = [];
for (let path of normalizedPaths) {
passthroughs.push(this.getTemplatePassthroughForPath(path));
}

const passthroughs = normalizedPaths.map(path => this.getTemplatePassthroughForPath(path));

return Promise.all(
passthroughs.map((pass) => this.copyPassthrough(pass))
Expand Down
12 changes: 9 additions & 3 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,20 @@ class UserConfig {
*
* @param {string|object} fileOrDir The path to the file or directory that should
* be copied. OR an object where the key is the input glob and the property is the output directory
* @param {object} copyOptions options for recursive-copy.
* see https://www.npmjs.com/package/recursive-copy#arguments
* default options are defined in TemplatePassthrough copyOptionsDefault
* @returns {any} a reference to the `EleventyConfig` object.
* @memberof EleventyConfig
*/
addPassthroughCopy(fileOrDir) {
addPassthroughCopy(fileOrDir, copyOptions = {}) {
if (typeof fileOrDir === "string") {
this.passthroughCopies[fileOrDir] = true;
const inputPath = fileOrDir;
this.passthroughCopies[inputPath] = { outputPath: true, copyOptions };
} else {
Object.assign(this.passthroughCopies, fileOrDir);
for (const [inputPath, outputPath] of Object.entries(fileOrDir)) {
this.passthroughCopies[inputPath] = { outputPath, copyOptions };
}
}

return this;
Expand Down
44 changes: 23 additions & 21 deletions test/TemplatePassthroughManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const EleventyExtensionMap = require("../src/EleventyExtensionMap");
test("Get paths from Config", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
img: true,
img: { outputPath: true },
};
let mgr = new TemplatePassthroughManager(eleventyConfig);

t.deepEqual(mgr.getConfigPaths(), [{ inputPath: "./img", outputPath: true }]);
t.deepEqual(mgr.getConfigPaths(), [
{ inputPath: "./img", outputPath: true, copyOptions: {} }
]);
});

test("isPassthroughCopyFile", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
img: true,
fonts: true,
img: { outputPath: true },
fonts: { outputPath: true },
};
let mgr = new TemplatePassthroughManager(eleventyConfig);

Expand All @@ -38,9 +40,9 @@ test("isPassthroughCopyFile", async (t) => {
test("Get glob paths from config", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
"test/stubs/img": true,
"test/stubs/img/**": "./",
"test/stubs/img/*.js": "./",
"test/stubs/img": { outputPath: true },
"test/stubs/img/**": { outputPath: "./" },
"test/stubs/img/*.js": { outputPath: "./" },
};
let mgr = new TemplatePassthroughManager(eleventyConfig);

Expand Down Expand Up @@ -90,11 +92,11 @@ test("Get file paths (one image path)", async (t) => {
test("Naughty paths outside of project dir", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
"../static": true,
"../*": "./",
"./test/stubs/template-passthrough2/static/*.css": "./",
"./test/stubs/template-passthrough2/static/*.js": "../../",
"./test/stubs/template-passthrough2/img.jpg": "../../",
"../static": { outputPath: true },
"../*": { outputPath: "./" },
"./test/stubs/template-passthrough2/static/*.css": { outputPath: "./" },
"./test/stubs/template-passthrough2/static/*.js": { outputPath: "../../" },
"./test/stubs/template-passthrough2/img.jpg": { outputPath: "../../" },
};

let mgr = new TemplatePassthroughManager(eleventyConfig);
Expand Down Expand Up @@ -128,36 +130,36 @@ test("Naughty paths outside of project dir", async (t) => {
test("getAllNormalizedPaths", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
img: true,
img: { outputPath: true },
};

let mgr = new TemplatePassthroughManager(eleventyConfig);
t.deepEqual(mgr.getAllNormalizedPaths(), [
{ inputPath: "./img", outputPath: true },
{ inputPath: "./img", outputPath: true, copyOptions: {} },
]);
});

test("getAllNormalizedPaths with globs", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
img: true,
"img/**": "./",
"img/*.js": "./",
img: { outputPath: true },
"img/**": { outputPath: "./" },
"img/*.js": { outputPath: "./" },
};

let mgr = new TemplatePassthroughManager(eleventyConfig);
t.deepEqual(mgr.getAllNormalizedPaths(), [
{ inputPath: "./img", outputPath: true },
{ inputPath: "./img/**", outputPath: "" },
{ inputPath: "./img/*.js", outputPath: "" },
{ inputPath: "./img", outputPath: true, copyOptions: {} },
{ inputPath: "./img/**", outputPath: "", copyOptions: {} },
{ inputPath: "./img/*.js", outputPath: "", copyOptions: {} },
]);
});

test("Look for uniqueness on template passthrough paths #1677", async (t) => {
let formats = [];
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
"./test/stubs/template-passthrough-duplicates/**/*.png": "./",
"./test/stubs/template-passthrough-duplicates/**/*.png": { outputPath: "./", copyOptions: {} },
};

let files = new EleventyFiles(
Expand Down
8 changes: 4 additions & 4 deletions test/TemplateWriterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@ test.skip("JavaScript with alias", async (t) => {
test("Passthrough file output", async (t) => {
let eleventyConfig = new TemplateConfig();
eleventyConfig.userConfig.passthroughCopies = {
"./test/stubs/template-passthrough/static": true,
"./test/stubs/template-passthrough/static/": "./",
"./test/stubs/template-passthrough/static/**/*": "./all/",
"./test/stubs/template-passthrough/static/**/*.js": "./js/",
"./test/stubs/template-passthrough/static": { outputPath: true, copyOptions: {} },
"./test/stubs/template-passthrough/static/": { outputPath: "./", copyOptions: {} },
"./test/stubs/template-passthrough/static/**/*": { outputPath: "./all/", copyOptions: {} },
"./test/stubs/template-passthrough/static/**/*.js": { outputPath: "./js/", copyOptions: {} },
};
let tw = new TemplateWriter(
"./test/stubs/template-passthrough/",
Expand Down
14 changes: 7 additions & 7 deletions test/UserConfigTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test("Set manual Pass-through File Copy (single call)", (t) => {
let userCfg = new UserConfig();
userCfg.addPassthroughCopy("img");

t.is(userCfg.passthroughCopies["img"], true);
t.deepEqual(userCfg.passthroughCopies["img"], { outputPath: true, copyOptions: {} });
});

test("Set manual Pass-through File Copy (chained calls)", (t) => {
Expand All @@ -106,10 +106,10 @@ test("Set manual Pass-through File Copy (chained calls)", (t) => {
.addPassthroughCopy({ "./src/static": "static" })
.addPassthroughCopy({ "./src/empty": "./" });

t.is(userCfg.passthroughCopies["css"], true);
t.is(userCfg.passthroughCopies["js"], true);
t.is(userCfg.passthroughCopies["./src/static"], "static");
t.is(userCfg.passthroughCopies["./src/empty"], "./");
t.deepEqual(userCfg.passthroughCopies["css"], { outputPath: true, copyOptions: {} });
t.deepEqual(userCfg.passthroughCopies["js"], { outputPath: true, copyOptions: {} });
t.deepEqual(userCfg.passthroughCopies["./src/static"], { outputPath: "static", copyOptions: {} });
t.deepEqual(userCfg.passthroughCopies["./src/empty"], { outputPath: "./", copyOptions: {} });
});

test("Set manual Pass-through File Copy (glob patterns)", (t) => {
Expand All @@ -124,8 +124,8 @@ test("Set manual Pass-through File Copy (glob patterns)", (t) => {
t.is(userCfg.passthroughCopies["js/**"], undefined);

// exists
t.is(userCfg.passthroughCopies["./src/static/**/*"], "renamed");
t.is(userCfg.passthroughCopies["./src/markdown/*.md"], "");
t.deepEqual(userCfg.passthroughCopies["./src/static/**/*"], { outputPath: "renamed", copyOptions: {} });
t.deepEqual(userCfg.passthroughCopies["./src/markdown/*.md"], { outputPath: "", copyOptions: {} });
});

test("Set Template Formats (string)", (t) => {
Expand Down