1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.createMinimalFS = void 0;
|
7 | const path_1 = require("path");
|
8 | const deindent_1 = __importDefault(require("deindent"));
|
9 | function createMinimalFS({ files, trimWS }) {
|
10 | const creationDate = new Date();
|
11 | const filePaths = new Map(Object.entries(files).map(([filePath, { content, mtime = creationDate }]) => [
|
12 | filePath,
|
13 | { content, mtime },
|
14 | ]));
|
15 | const directoryPaths = new Set();
|
16 | for (const filePath of filePaths.keys()) {
|
17 | for (const directoryPath of getParentPaths(path_1.dirname(filePath))) {
|
18 | directoryPaths.add(directoryPath);
|
19 | }
|
20 | }
|
21 | const fs = {
|
22 | readFileSync(path) {
|
23 | if (!files[path]) {
|
24 | throw new Error('Cannot find file: ' + path);
|
25 | }
|
26 | if (trimWS) {
|
27 | return deindent_1.default(files[path].content).trim();
|
28 | }
|
29 | return files[path].content;
|
30 | },
|
31 | statSync(path) {
|
32 | const isDirectory = directoryPaths.has(path);
|
33 | const fileEntry = filePaths.get(path);
|
34 | if (!fileEntry && !isDirectory) {
|
35 | throw new Error(`ENOENT: no such file or directory, stat ${path}`);
|
36 | }
|
37 | return {
|
38 | isDirectory() {
|
39 | return isDirectory;
|
40 | },
|
41 | isFile() {
|
42 | return !!fileEntry;
|
43 | },
|
44 | mtime: fileEntry ? fileEntry.mtime : new Date(),
|
45 | };
|
46 | },
|
47 | readlinkSync() {
|
48 | throw new Error(`not implemented`);
|
49 | },
|
50 | };
|
51 | const requireModule = function require(id) {
|
52 | const _module = {
|
53 | id,
|
54 | exports: {},
|
55 | };
|
56 | try {
|
57 | if (!id.match(/\.js$/)) {
|
58 | id += '.js';
|
59 | }
|
60 |
|
61 | const fn = new Function('module', 'exports', 'require', files[id].content);
|
62 | fn(_module, _module.exports, requireModule);
|
63 | }
|
64 | catch (e) {
|
65 | throw new Error('Cannot require file: ' + id);
|
66 | }
|
67 | return _module.exports;
|
68 | };
|
69 | function resolvePath(_ctx, path) {
|
70 | return path;
|
71 | }
|
72 | return {
|
73 | fs,
|
74 | requireModule,
|
75 | resolvePath,
|
76 | };
|
77 | }
|
78 | exports.createMinimalFS = createMinimalFS;
|
79 | function getParentPaths(initialDirectoryPath) {
|
80 | const parentPaths = [];
|
81 | let currentPath = initialDirectoryPath;
|
82 | let lastPath;
|
83 | while (currentPath !== lastPath) {
|
84 | parentPaths.push(currentPath);
|
85 | lastPath = currentPath;
|
86 | currentPath = path_1.dirname(currentPath);
|
87 | }
|
88 | return parentPaths;
|
89 | }
|
90 |
|
\ | No newline at end of file |