UNPKG

2.24 kBJavaScriptView Raw
1'use strict';
2
3var _stringify = require('babel-runtime/core-js/json/stringify');
4
5var _stringify2 = _interopRequireDefault(_stringify);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9/*
10 Copyright 2017 Google Inc.
11
12 Licensed under the Apache License, Version 2.0 (the "License");
13 you may not use this file except in compliance with the License.
14 You may obtain a copy of the License at
15
16 https://www.apache.org/licenses/LICENSE-2.0
17
18 Unless required by applicable law or agreed to in writing, software
19 distributed under the License is distributed on an "AS IS" BASIS,
20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 See the License for the specific language governing permissions and
22 limitations under the License.
23*/
24
25var glob = require('glob');
26var path = require('path');
27
28var errors = require('./errors');
29var getFileSize = require('./get-file-size');
30var getFileHash = require('./get-file-hash');
31
32module.exports = function (globOptions) {
33 var globDirectory = globOptions.globDirectory,
34 globFollow = globOptions.globFollow,
35 globIgnores = globOptions.globIgnores,
36 globPattern = globOptions.globPattern,
37 globStrict = globOptions.globStrict;
38
39 var globbedFiles = void 0;
40 try {
41 globbedFiles = glob.sync(globPattern, {
42 cwd: globDirectory,
43 follow: globFollow,
44 ignore: globIgnores,
45 strict: globStrict
46 });
47 } catch (err) {
48 throw new Error(errors['unable-to-glob-files'] + ` '${err.message}'`);
49 }
50
51 if (globbedFiles.length === 0) {
52 throw new Error(errors['useless-glob-pattern'] + ' ' + (0, _stringify2.default)({ globDirectory, globPattern, globIgnores }, null, 2));
53 }
54
55 var fileDetails = globbedFiles.map(function (file) {
56 var fullPath = path.join(globDirectory, file);
57 var fileSize = getFileSize(fullPath);
58 if (fileSize === null) {
59 return null;
60 }
61
62 var fileHash = getFileHash(fullPath);
63 return {
64 file: `${path.relative(globDirectory, fullPath)}`,
65 hash: fileHash,
66 size: fileSize
67 };
68 });
69
70 // If !== null, means it's a valid file.
71 return fileDetails.filter(function (details) {
72 return details !== null;
73 });
74};
\No newline at end of file