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