* 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)
| 114 | * @param data - The file contents as Buffer or string |
| 115 | */ |
| 116 | async writeFile(filePath: string, data: Buffer | string) { |
| 117 | filePath = path.normalize(filePath); |
| 118 | const { root, id, type } = parseGitPath(filePath); |
| 119 | |
| 120 | // Only process files within the .insomnia directory |
| 121 | if (root !== GIT_INSOMNIA_DIR_NAME) { |
| 122 | console.log(`[git] Ignoring external file ${filePath}`); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | const dataStr = data.toString(); |
| 127 | |
| 128 | // Skip the file if there is a conflict marker (Git merge conflict) |
| 129 | if (dataStr.split('\n').includes('=======')) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // Parse the YAML data back to a database document |
| 134 | const doc: BaseModel = YAML.parse(dataStr); |
| 135 | |
| 136 | // Validate that the document ID matches the file path |
| 137 | if (id !== doc._id) { |
| 138 | throw new Error(`Doc _id does not match file path [${doc._id} != ${id || 'null'}]`); |
| 139 | } |
| 140 | |
| 141 | // Validate that the document type matches the file path |
| 142 | if (type !== doc.type) { |
| 143 | throw new Error(`Doc type does not match file path [${doc.type} != ${type || 'null'}]`); |
| 144 | } |
| 145 | |
| 146 | // Only types explicitly marked as syncable may be written from a Git checkout. |
| 147 | // A folder named after a non-syncable/global-singleton type (e.g. Settings) |
| 148 | // is ignored, even though its path and id/type fields are well-formed. |
| 149 | if (!models.canSync(doc)) { |
| 150 | console.log(`[git] Ignoring non-syncable document type ${doc.type} at ${filePath}`); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Special handling for workspaces: ensure they stay in the correct project |
| 155 | if (models.workspace.isWorkspace(doc)) { |
| 156 | console.log('[git] setting workspace parent to be that of the active project', { |
| 157 | original: doc.parentId, |
| 158 | new: this._projectId, |
| 159 | }); |
| 160 | // Whenever we write a workspace into nedb we should set the parentId to be that of the current project |
| 161 | // 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 |
| 162 | // 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 |
| 163 | doc.parentId = this._projectId; |
| 164 | } |
| 165 | |
| 166 | // Update the document in the database |
| 167 | await db.update(doc); |
| 168 | } |
| 169 | |
| 170 | async unlink(filePath: string) { |
| 171 | filePath = path.normalize(filePath); |
nothing calls this directly
no test coverage detected