1
const path = require('node:path')
2
const fse = require('fs-extra')
3
const { sync: fastGlob, convertPathToPattern } = require('fast-glob')
4
const { createPatch } = require('diff')
5
const { highlight } = require('cli-highlight')
7
const rootFolder = path.resolve(__dirname, '..')
9
function resolve (_path) {
10
return path.resolve(rootFolder, _path)
13
function relative (_path) {
14
return path.relative(rootFolder, _path)
23
module.exports = function prepareDiff (locationPath) {
24
const absolutePath = resolve(locationPath)
27
if (!fse.existsSync(absolutePath)) {
31
let pattern = convertPathToPattern(absolutePath)
33
if (fse.lstatSync(absolutePath).isDirectory()) {
37
const originalsMap = new Map()
38
const originalFiles = fastGlob(pattern)
41
if (originalFiles.length === 0) {
46
originalFiles.forEach(filePath => {
47
originalsMap.set(filePath, fse.readFileSync(filePath, 'utf-8'))
51
process.on('exit', code => {
52
if (code !== 0) return
54
const currentFiles = fastGlob(pattern)
55
const currentMap = new Map()
57
let somethingChanged = false
59
currentFiles.forEach(filePath => {
60
const relativePath = relative(filePath)
61
currentMap.set(filePath, true)
63
if (originalsMap.has(filePath) === false) {
64
console.log(`\n 📜 New file: ${ relativePath }`)
65
somethingChanged = true
69
const currentContent = fse.readFileSync(filePath, 'utf-8')
70
const originalContent = originalsMap.get(filePath)
72
if (originalContent !== currentContent) {
73
const diffPatch = createPatch(filePath, originalContent, currentContent)
75
console.log(`\n 📜 Changes for ${ relativePath }\n`)
76
console.log(highlight(diffPatch, { language: 'diff' }))
77
somethingChanged = true
81
originalsMap.forEach((_, filePath) => {
82
if (currentMap.has(filePath) === false) {
83
console.log(`\n 📜 Removed file: ${ relative(filePath) }\n`)
84
somethingChanged = true
88
if (somethingChanged === false) {
89
console.log('\n 📜 No changes detected.\n')