UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.isSymbolicLink = void 0;
7const path_1 = __importDefault(require("path"));
8const debug_1 = __importDefault(require("../debug"));
9const file_fs_ref_1 = __importDefault(require("../file-fs-ref"));
10const fs_extra_1 = require("fs-extra");
11const S_IFMT = 61440; /* 0170000 type of file */
12const S_IFLNK = 40960; /* 0120000 symbolic link */
13function isSymbolicLink(mode) {
14 return (mode & S_IFMT) === S_IFLNK;
15}
16exports.isSymbolicLink = isSymbolicLink;
17async function downloadFile(file, fsPath) {
18 const { mode } = file;
19 if (mode && isSymbolicLink(mode) && file.type === 'FileFsRef') {
20 const [target] = await Promise.all([
21 fs_extra_1.readlink(file.fsPath),
22 fs_extra_1.mkdirp(path_1.default.dirname(fsPath)),
23 ]);
24 await fs_extra_1.symlink(target, fsPath);
25 return file_fs_ref_1.default.fromFsPath({ mode, fsPath });
26 }
27 else {
28 const stream = file.toStream();
29 return file_fs_ref_1.default.fromStream({ mode, stream, fsPath });
30 }
31}
32async function removeFile(basePath, fileMatched) {
33 const file = path_1.default.join(basePath, fileMatched);
34 await fs_extra_1.remove(file);
35}
36async function download(files, basePath, meta) {
37 const { isDev = false, skipDownload = false, filesChanged = null, filesRemoved = null, } = meta || {};
38 if (isDev || skipDownload) {
39 // In `vercel dev`, the `download()` function is a no-op because
40 // the `basePath` matches the `cwd` of the dev server, so the
41 // source files are already available.
42 return files;
43 }
44 debug_1.default('Downloading deployment source files...');
45 const start = Date.now();
46 const files2 = {};
47 const filenames = Object.keys(files);
48 await Promise.all(filenames.map(async (name) => {
49 // If the file does not exist anymore, remove it.
50 if (Array.isArray(filesRemoved) && filesRemoved.includes(name)) {
51 await removeFile(basePath, name);
52 return;
53 }
54 // If a file didn't change, do not re-download it.
55 if (Array.isArray(filesChanged) && !filesChanged.includes(name)) {
56 return;
57 }
58 const file = files[name];
59 const fsPath = path_1.default.join(basePath, name);
60 files2[name] = await downloadFile(file, fsPath);
61 }));
62 const duration = Date.now() - start;
63 debug_1.default(`Downloaded ${filenames.length} source files: ${duration}ms`);
64 return files2;
65}
66exports.default = download;