UNPKG

4.9 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const _ = require("lodash");
12const minimatch = require("minimatch");
13const spigot = require("stream-spigot");
14const File_1 = require("../File");
15const AbstractProject_1 = require("../support/AbstractProject");
16const projectUtils_1 = require("../support/projectUtils");
17const InMemoryFile_1 = require("./InMemoryFile");
18/**
19 * In memory Project implementation. Primarily intended
20 * for testing. BE WARNED: Does not correctly handle permissions and binary files!
21 */
22class InMemoryProject extends AbstractProject_1.AbstractProject {
23 constructor(xid) {
24 super(xid);
25 /**
26 * Directories added. May contain no files. Must
27 * be included when copying to a file system.
28 * @type {Array}
29 */
30 this.addedDirectoryPaths = [];
31 this.memFiles = [];
32 }
33 /**
34 * Create a new InMemoryProject
35 * @param id: RepoRef
36 * @param files files to include in the project
37 * @return {InMemoryProject}
38 */
39 static from(id, ...files) {
40 const inp = new InMemoryProject(id);
41 files.forEach(f => inp.recordAddFile(f.path, File_1.isFile(f) ? f.getContentSync() : f.content));
42 return inp;
43 }
44 /**
45 * Create a new InMemoryProject without an id
46 */
47 static of(...files) {
48 return InMemoryProject.from(undefined, ...files);
49 }
50 /**
51 * Make an independent copy of the given project, with the same files
52 * @param {Project} p
53 * @return {InMemoryProject}
54 */
55 static cache(p) {
56 const to = new InMemoryProject(p.id);
57 return projectUtils_1.copyFiles(p, to);
58 }
59 get fileCount() {
60 return this.memFiles.length;
61 }
62 get filesSync() {
63 return this.memFiles;
64 }
65 findFile(path) {
66 const file = this.memFiles.find(f => f.path === path);
67 return file ? Promise.resolve(file) : Promise.reject(`File not found at ${path}`);
68 }
69 getFile(path) {
70 return __awaiter(this, void 0, void 0, function* () {
71 return this.memFiles.find(f => f.path === path);
72 });
73 }
74 findFileSync(path) {
75 return this.memFiles.find(f => f.path === path);
76 }
77 recordAddFile(path, content) {
78 this.memFiles.push(new InMemoryFile_1.InMemoryFile(path, content));
79 return this;
80 }
81 addFileSync(path, content) {
82 this.recordAddFile(path, content);
83 }
84 addFile(path, content) {
85 this.addFileSync(path, content);
86 return Promise.resolve(this);
87 }
88 addDirectory(path) {
89 this.addedDirectoryPaths.push(path);
90 return Promise.resolve(this);
91 }
92 deleteDirectorySync(path) {
93 this.memFiles.forEach(f => {
94 if (f.path.startsWith(`${path}/`)) {
95 this.deleteFileSync(f.path);
96 }
97 });
98 }
99 deleteDirectory(path) {
100 this.deleteDirectorySync(path);
101 return Promise.resolve(this);
102 }
103 deleteFileSync(path) {
104 this.memFiles = this.memFiles.filter(f => f.path !== path);
105 return this;
106 }
107 deleteFile(path) {
108 this.deleteFileSync(path);
109 return Promise.resolve(this);
110 }
111 makeExecutableSync(path) {
112 throw new Error("unimplemented: makeExecutableSync");
113 }
114 directoryExistsSync(path) {
115 return this.memFiles.some(f => f.path.startsWith(`${path}/`));
116 }
117 fileExistsSync(path) {
118 return this.memFiles.some(f => f.path === path);
119 }
120 streamFilesRaw(globPatterns, opts) {
121 const positiveMatches = _.flatten(this.memFiles.filter(f => globPatterns.some(gp => !gp.startsWith("!") && minimatch.match([f.path], gp).includes(f.path))));
122 const matchingFiles = _.reject(positiveMatches, f => globPatterns.some(gp => gp.startsWith("!") && minimatch.match([f.path], gp.substring(1)).includes(f.path)));
123 return spigot.array({ objectMode: true }, matchingFiles);
124 }
125 makeExecutable(path) {
126 throw new Error("makeExecutable not implemented.");
127 }
128}
129exports.InMemoryProject = InMemoryProject;
130function isInMemoryProject(p) {
131 const maybe = p;
132 return maybe.addedDirectoryPaths !== undefined;
133}
134exports.isInMemoryProject = isInMemoryProject;
135//# sourceMappingURL=InMemoryProject.js.map
\No newline at end of file