Skip to content

Commit

Permalink
fix(compiler) restore old copy behaviour, with new ignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Payne committed Oct 28, 2024
1 parent 86a36b4 commit d21408f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
49 changes: 32 additions & 17 deletions src/sys/node/node-copy-tasks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildError, catchError, flatOne, isGlob, normalizePath } from '@utils';
import { glob, type GlobOptions } from 'glob';
import { glob } from 'glob';
import path from 'path';

import type * as d from '../../declarations';
Expand All @@ -13,7 +13,7 @@ export async function nodeCopyTasks(copyTasks: Required<d.CopyTask>[], srcDir: s
};

try {
copyTasks = flatOne(await Promise.all(copyTasks.map((task) => processGlobTask(task, srcDir))));
copyTasks = flatOne(await Promise.all(copyTasks.map((task) => processGlobs(task, srcDir))));

const allCopyTasks: d.CopyTask[] = [];

Expand Down Expand Up @@ -44,21 +44,31 @@ export async function nodeCopyTasks(copyTasks: Required<d.CopyTask>[], srcDir: s
return results;
}

async function processGlobs(copyTask: Required<d.CopyTask>, srcDir: string): Promise<Required<d.CopyTask>[]> {
return isGlob(copyTask.src)
? await processGlobTask(copyTask, srcDir)
: [
{
src: getSrcAbsPath(srcDir, copyTask.src),
dest: copyTask.keepDirStructure ? path.join(copyTask.dest, copyTask.src) : copyTask.dest,
warn: copyTask.warn,
ignore: copyTask.ignore,
keepDirStructure: copyTask.keepDirStructure,
},
];
}

function getSrcAbsPath(srcDir: string, src: string) {
if (path.isAbsolute(src)) {
return src;
}
return path.join(srcDir, src);
}

async function processGlobTask(copyTask: Required<d.CopyTask>, srcDir: string): Promise<Required<d.CopyTask>[]> {
/**
* To properly match all files within a certain directory we have to ensure to attach a `/**` to
* the end of the pattern. However we only want to do this if the `src` entry is not a glob pattern
* already or a file with an extension.
*/
const pattern =
isGlob(copyTask.src) || path.extname(copyTask.src).length > 0
? copyTask.src
: './' + path.relative(srcDir, path.join(path.resolve(srcDir, copyTask.src), '**')).replaceAll(path.sep, '/');

const files = await asyncGlob(pattern, {
const files = await asyncGlob(copyTask.src, {
cwd: srcDir,
nodir: true,
absolute: false,
ignore: copyTask.ignore,
});
return files.map((globRelPath) => createGlobCopyTask(copyTask, srcDir, globRelPath));
Expand Down Expand Up @@ -89,7 +99,7 @@ async function processCopyTask(results: d.CopyResults, allCopyTasks: d.CopyTask[
}

await processCopyTaskDirectory(results, allCopyTasks, copyTask);
} else {
} else if (!shouldIgnore(copyTask)) {
// this is a file we should copy
if (!results.filePaths.includes(copyTask.dest)) {
results.filePaths.push(copyTask.dest);
Expand Down Expand Up @@ -162,6 +172,11 @@ function addMkDir(mkDirs: string[], destDir: string) {

const ROOT_DIR = normalizePath(path.resolve('/'));

export async function asyncGlob(pattern: string, opts: GlobOptions): Promise<string[]> {
return glob(pattern, { ...opts, withFileTypes: false });
function shouldIgnore({ src, ignore = [] }: d.CopyTask) {
const filePath = src.trim().toLowerCase();
return ignore.some((ignoreFile) => filePath.endsWith(ignoreFile));
}

export function asyncGlob(pattern: string, opts: any) {
return glob(pattern, opts);
}
2 changes: 1 addition & 1 deletion test/copy-task/stencil.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const config: Config = {
type: 'dist-custom-elements',
copy: [
{
src: './utils',
src: './utils/**',
dest: './dist/utilsExtra',
},
],
Expand Down

0 comments on commit d21408f

Please sign in to comment.