MCPcopy Create free account
hub / github.com/Kong/insomnia / writeFile

Method writeFile

packages/insomnia/src/sync/git/ne-db-client.ts:117–161  ·  view source on GitHub ↗

* Writes file data to the NeDB database as if it were a regular file * Converts YAML data back to database records during Git operations * * @param filePath - The file path to write to * @param data - The file contents as Buffer or string

(filePath: string, data: Buffer | string)

Source from the content-addressed store, hash-verified

115 * @param data - The file contents as Buffer or string
116 */
117 async writeFile(filePath: string, data: Buffer | string) {
118 filePath = path.normalize(filePath);
119 const { root, id, type } = parseGitPath(filePath);
120
121 // Only process files within the .insomnia directory
122 if (root !== GIT_INSOMNIA_DIR_NAME) {
123 console.log(`[git] Ignoring external file ${filePath}`);
124 return;
125 }
126
127 const dataStr = data.toString();
128
129 // Skip the file if there is a conflict marker (Git merge conflict)
130 if (dataStr.split('\n').includes('=======')) {
131 return;
132 }
133
134 // Parse the YAML data back to a database document
135 const doc: BaseModel = YAML.parse(dataStr);
136
137 // Validate that the document ID matches the file path
138 if (id !== doc._id) {
139 throw new Error(`Doc _id does not match file path [${doc._id} != ${id || 'null'}]`);
140 }
141
142 // Validate that the document type matches the file path
143 if (type !== doc.type) {
144 throw new Error(`Doc type does not match file path [${doc.type} != ${type || 'null'}]`);
145 }
146
147 // Special handling for workspaces: ensure they stay in the correct project
148 if (models.workspace.isWorkspace(doc)) {
149 console.log('[git] setting workspace parent to be that of the active project', {
150 original: doc.parentId,
151 new: this._projectId,
152 });
153 // Whenever we write a workspace into nedb we should set the parentId to be that of the current project
154 // This is because the parentId (or a project) is not synced into git, so it will be cleared whenever git writes the workspace into the db, thereby removing it from the project on the client
155 // In order to reproduce this bug, comment out the following line, then clone a repository into a local project, then open the workspace, you'll notice it will have moved into the default project
156 doc.parentId = this._projectId;
157 }
158
159 // Update the document in the database
160 await db.update(doc);
161 }
162
163 async unlink(filePath: string) {
164 filePath = path.normalize(filePath);

Callers

nothing calls this directly

Calls 5

parseGitPathFunction · 0.85
updateMethod · 0.65
logMethod · 0.45
toStringMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected