From e2975503a4f59f193f60d219ec64d9be5360d7c8 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Mon, 18 Nov 2024 03:42:22 +0100 Subject: [PATCH] Automatically untrack files implemented by ui modules --- src/bin/postinstall/postinstall.ts | 24 +++++++++++++++++++++++- src/bin/tools/isTrackedByGit.ts | 29 +++++++++++++++++++++++++++++ src/bin/tools/untrackFromGit.ts | 24 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/bin/tools/isTrackedByGit.ts create mode 100644 src/bin/tools/untrackFromGit.ts diff --git a/src/bin/postinstall/postinstall.ts b/src/bin/postinstall/postinstall.ts index 5c640e21..ae03f46e 100644 --- a/src/bin/postinstall/postinstall.ts +++ b/src/bin/postinstall/postinstall.ts @@ -9,6 +9,8 @@ import { dirname as pathDirname } from "path"; import { join as pathJoin } from "path"; import { existsAsync } from "../tools/fs.existsAsync"; import * as fsPr from "fs/promises"; +import { getIsTrackedByGit } from "../tools/isTrackedByGit"; +import { untrackFromGit } from "../tools/untrackFromGit"; export async function command(params: { buildContext: BuildContext }) { const { buildContext } = params; @@ -45,8 +47,10 @@ export async function command(params: { buildContext: BuildContext }) { fileRelativePath ); + const doesFileExist = await existsAsync(destFilePath); + skip_condition: { - if (!(await existsAsync(destFilePath))) { + if (!doesFileExist) { break skip_condition; } @@ -61,6 +65,24 @@ export async function command(params: { buildContext: BuildContext }) { return; } + git_untrack: { + if (!destFilePath) { + break git_untrack; + } + + const isTracked = await getIsTrackedByGit({ + filePath: destFilePath + }); + + if (!isTracked) { + break git_untrack; + } + + await untrackFromGit({ + filePath: destFilePath + }); + } + { const dirName = pathDirname(destFilePath); diff --git a/src/bin/tools/isTrackedByGit.ts b/src/bin/tools/isTrackedByGit.ts new file mode 100644 index 00000000..ae4961d7 --- /dev/null +++ b/src/bin/tools/isTrackedByGit.ts @@ -0,0 +1,29 @@ +import * as child_process from "child_process"; +import { dirname as pathDirname, basename as pathBasename } from "path"; +import { Deferred } from "evt/tools/Deferred"; + +export function getIsTrackedByGit(params: { filePath: string }): Promise { + const { filePath } = params; + + const dIsTracked = new Deferred(); + + child_process.exec( + `git ls-files --error-unmatch ${pathBasename(filePath)}`, + { cwd: pathDirname(filePath) }, + error => { + if (error === null) { + dIsTracked.resolve(true); + return; + } + + if (error.code === 1) { + dIsTracked.resolve(false); + return; + } + + dIsTracked.reject(error); + } + ); + + return dIsTracked.pr; +} diff --git a/src/bin/tools/untrackFromGit.ts b/src/bin/tools/untrackFromGit.ts new file mode 100644 index 00000000..3c602764 --- /dev/null +++ b/src/bin/tools/untrackFromGit.ts @@ -0,0 +1,24 @@ +import * as child_process from "child_process"; +import { dirname as pathDirname, basename as pathBasename } from "path"; +import { Deferred } from "evt/tools/Deferred"; + +export async function untrackFromGit(params: { filePath: string }): Promise { + const { filePath } = params; + + const dDone = new Deferred(); + + child_process.exec( + `git rm --cached ${pathBasename(filePath)}`, + { cwd: pathDirname(filePath) }, + error => { + if (error !== null) { + dDone.reject(error); + return; + } + + dDone.resolve(); + } + ); + + await dDone.pr; +}