( snapshotPath: string, update: Config.SnapshotUpdateState, )
| 131 | }; |
| 132 | |
| 133 | export const getSnapshotData = ( |
| 134 | snapshotPath: string, |
| 135 | update: Config.SnapshotUpdateState, |
| 136 | ): { |
| 137 | data: SnapshotData; |
| 138 | dirty: boolean; |
| 139 | } => { |
| 140 | const data = Object.create(null); |
| 141 | let snapshotContents = ''; |
| 142 | let dirty = false; |
| 143 | |
| 144 | if (fs.existsSync(snapshotPath)) { |
| 145 | try { |
| 146 | snapshotContents = fs.readFileSync(snapshotPath, 'utf8'); |
| 147 | // eslint-disable-next-line no-new-func |
| 148 | const populate = new Function('exports', snapshotContents); |
| 149 | populate(data); |
| 150 | } catch {} |
| 151 | } |
| 152 | |
| 153 | const validationResult = validateSnapshotHeader(snapshotContents); |
| 154 | const isInvalid = snapshotContents && validationResult; |
| 155 | |
| 156 | if (update === 'none' && isInvalid) { |
| 157 | throw validationResult; |
| 158 | } |
| 159 | |
| 160 | if ((update === 'all' || update === 'new') && isInvalid) { |
| 161 | dirty = true; |
| 162 | } |
| 163 | |
| 164 | return {data, dirty}; |
| 165 | }; |
| 166 | |
| 167 | export const escapeBacktickString = (str: string): string => |
| 168 | str.replaceAll(/`|\\|\${/g, '\\$&'); |
no test coverage detected