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

fix(compiler) restore old copy behaviour, with new ignore option #6020

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}