|
| 1 | +import path from 'path'; |
| 2 | + |
| 3 | +import fs from 'fs-extra'; |
| 4 | + |
| 5 | +import { log } from '../../utils/logging'; |
| 6 | +import { readBaseTemplateFile } from '../../utils/template'; |
| 7 | + |
| 8 | +import { getDestinationManifest } from './analysis/package'; |
| 9 | +import { createDestinationFileReader } from './analysis/project'; |
| 10 | +import { mergeWithIgnoreFile } from './processing/ignoreFile'; |
| 11 | + |
| 12 | +export const refreshIgnoreFiles = async () => { |
| 13 | + const manifest = await getDestinationManifest(); |
| 14 | + |
| 15 | + const destinationRoot = path.dirname(manifest.path); |
| 16 | + |
| 17 | + const readDestinationFile = createDestinationFileReader(destinationRoot); |
| 18 | + |
| 19 | + const refreshIgnoreFile = async (filename: string) => { |
| 20 | + const [inputFile, templateFile] = await Promise.all([ |
| 21 | + readDestinationFile(filename), |
| 22 | + readBaseTemplateFile(`_${filename}`), |
| 23 | + ]); |
| 24 | + |
| 25 | + const data = inputFile |
| 26 | + ? mergeWithIgnoreFile(templateFile)(inputFile) |
| 27 | + : templateFile; |
| 28 | + |
| 29 | + const filepath = path.join(destinationRoot, filename); |
| 30 | + |
| 31 | + await fs.promises.writeFile(filepath, data); |
| 32 | + }; |
| 33 | + |
| 34 | + await Promise.all([ |
| 35 | + refreshIgnoreFile('.eslintignore'), |
| 36 | + refreshIgnoreFile('.gitignore'), |
| 37 | + refreshIgnoreFile('.prettierignore'), |
| 38 | + ]); |
| 39 | +}; |
| 40 | + |
| 41 | +export const tryRefreshIgnoreFiles = async () => { |
| 42 | + try { |
| 43 | + await refreshIgnoreFiles(); |
| 44 | + } catch (err) { |
| 45 | + log.warn('Failed to refresh ignore files.'); |
| 46 | + log.warn(err); |
| 47 | + } |
| 48 | +}; |
0 commit comments