| [9af201e] | 1 | /*
|
|---|
| 2 | Copyright 2021 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import glob from 'glob';
|
|---|
| 10 | import upath from 'upath';
|
|---|
| 11 |
|
|---|
| 12 | import {errors} from './errors';
|
|---|
| 13 | import {getFileSize} from './get-file-size';
|
|---|
| 14 | import {getFileHash} from './get-file-hash';
|
|---|
| 15 |
|
|---|
| 16 | import {GlobPartial} from '../types';
|
|---|
| 17 |
|
|---|
| 18 | interface FileDetails {
|
|---|
| 19 | file: string;
|
|---|
| 20 | hash: string;
|
|---|
| 21 | size: number;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | export function getFileDetails({
|
|---|
| 25 | globDirectory,
|
|---|
| 26 | globFollow,
|
|---|
| 27 | globIgnores,
|
|---|
| 28 | globPattern,
|
|---|
| 29 | globStrict,
|
|---|
| 30 | }: Omit<GlobPartial, 'globDirectory' | 'globPatterns' | 'templatedURLs'> & {
|
|---|
| 31 | // This will only be called when globDirectory is not undefined.
|
|---|
| 32 | globDirectory: string;
|
|---|
| 33 | globPattern: string;
|
|---|
| 34 | }): {
|
|---|
| 35 | globbedFileDetails: Array<FileDetails>;
|
|---|
| 36 | warning: string;
|
|---|
| 37 | } {
|
|---|
| 38 | let globbedFiles: Array<string>;
|
|---|
| 39 | let warning = '';
|
|---|
| 40 |
|
|---|
| 41 | try {
|
|---|
| 42 | globbedFiles = glob.sync(globPattern, {
|
|---|
| 43 | cwd: globDirectory,
|
|---|
| 44 | follow: globFollow,
|
|---|
| 45 | ignore: globIgnores,
|
|---|
| 46 | strict: globStrict,
|
|---|
| 47 | });
|
|---|
| 48 | } catch (err) {
|
|---|
| 49 | throw new Error(
|
|---|
| 50 | errors['unable-to-glob-files'] +
|
|---|
| 51 | ` '${err instanceof Error && err.message ? err.message : ''}'`,
|
|---|
| 52 | );
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (globbedFiles.length === 0) {
|
|---|
| 56 | warning =
|
|---|
| 57 | errors['useless-glob-pattern'] +
|
|---|
| 58 | ' ' +
|
|---|
| 59 | JSON.stringify({globDirectory, globPattern, globIgnores}, null, 2);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | const globbedFileDetails: Array<FileDetails> = [];
|
|---|
| 63 | for (const file of globbedFiles) {
|
|---|
| 64 | const fullPath = upath.join(globDirectory, file);
|
|---|
| 65 | const fileSize = getFileSize(fullPath);
|
|---|
| 66 | if (fileSize !== null) {
|
|---|
| 67 | const fileHash = getFileHash(fullPath);
|
|---|
| 68 | globbedFileDetails.push({
|
|---|
| 69 | file: `${upath.relative(globDirectory, fullPath)}`,
|
|---|
| 70 | hash: fileHash,
|
|---|
| 71 | size: fileSize,
|
|---|
| 72 | });
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return {globbedFileDetails, warning};
|
|---|
| 77 | }
|
|---|