refreshTypesFile saves the embedded TS declarations as a file on the disk.
()
| 485 | |
| 486 | // refreshTypesFile saves the embedded TS declarations as a file on the disk. |
| 487 | func (p *plugin) refreshTypesFile() error { |
| 488 | fullPath := p.fullTypesPath() |
| 489 | |
| 490 | // ensure that the types directory exists |
| 491 | dir := filepath.Dir(fullPath) |
| 492 | if err := os.MkdirAll(dir, os.ModePerm); err != nil { |
| 493 | return err |
| 494 | } |
| 495 | |
| 496 | // retrieve the types data to write |
| 497 | data, err := generated.Types.ReadFile(typesFileName) |
| 498 | if err != nil { |
| 499 | return err |
| 500 | } |
| 501 | |
| 502 | // read the first timestamp line of the old file (if exists) and compare it to the embedded one |
| 503 | // (note: ignore errors to allow always overwriting the file if it is invalid) |
| 504 | existingFile, err := os.Open(fullPath) |
| 505 | if err == nil { |
| 506 | timestamp := make([]byte, 13) |
| 507 | io.ReadFull(existingFile, timestamp) |
| 508 | existingFile.Close() |
| 509 | |
| 510 | if len(data) >= len(timestamp) && bytes.Equal(data[:13], timestamp) { |
| 511 | return nil // nothing new to save |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return os.WriteFile(fullPath, data, 0644) |
| 516 | } |
| 517 | |
| 518 | // prependToEmptyFile prepends the specified text to an empty file. |
| 519 | // |
no test coverage detected