Charles Lyding 28f302855f refactor(@angular-devkit/build-angular): remove Node.js 10 copyfile workaround
The workaround code was gated on the presence of Node.js 10 but the CLI no longer supports Node.js 10 and will execute with an error if attempted. As a result, the workaround code would never be executed.
2021-08-05 06:48:55 +02:00

67 lines
1.7 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import glob from 'glob';
import * as path from 'path';
function globAsync(pattern: string, options: glob.IOptions) {
return new Promise<string[]>((resolve, reject) =>
glob(pattern, options, (e, m) => (e ? reject(e) : resolve(m))),
);
}
export async function copyAssets(
entries: {
glob: string;
ignore?: string[];
input: string;
output: string;
flatten?: boolean;
followSymlinks?: boolean;
}[],
basePaths: Iterable<string>,
root: string,
changed?: Set<string>,
) {
const defaultIgnore = ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'];
for (const entry of entries) {
const cwd = path.resolve(root, entry.input);
const files = await globAsync(entry.glob, {
cwd,
dot: true,
nodir: true,
ignore: entry.ignore ? defaultIgnore.concat(entry.ignore) : defaultIgnore,
follow: entry.followSymlinks,
});
const directoryExists = new Set<string>();
for (const file of files) {
const src = path.join(cwd, file);
if (changed && !changed.has(src)) {
continue;
}
const filePath = entry.flatten ? path.basename(file) : file;
for (const base of basePaths) {
const dest = path.join(base, entry.output, filePath);
const dir = path.dirname(dest);
if (!directoryExists.has(dir)) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
directoryExists.add(dir);
}
fs.copyFileSync(src, dest, fs.constants.COPYFILE_FICLONE);
}
}
}
}