Automatically untrack files implemented by ui modules

This commit is contained in:
Joseph Garrone 2024-11-18 03:42:22 +01:00
parent 1231c92198
commit e2975503a4
3 changed files with 76 additions and 1 deletions

View File

@ -9,6 +9,8 @@ import { dirname as pathDirname } from "path";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import { existsAsync } from "../tools/fs.existsAsync"; import { existsAsync } from "../tools/fs.existsAsync";
import * as fsPr from "fs/promises"; import * as fsPr from "fs/promises";
import { getIsTrackedByGit } from "../tools/isTrackedByGit";
import { untrackFromGit } from "../tools/untrackFromGit";
export async function command(params: { buildContext: BuildContext }) { export async function command(params: { buildContext: BuildContext }) {
const { buildContext } = params; const { buildContext } = params;
@ -45,8 +47,10 @@ export async function command(params: { buildContext: BuildContext }) {
fileRelativePath fileRelativePath
); );
const doesFileExist = await existsAsync(destFilePath);
skip_condition: { skip_condition: {
if (!(await existsAsync(destFilePath))) { if (!doesFileExist) {
break skip_condition; break skip_condition;
} }
@ -61,6 +65,24 @@ export async function command(params: { buildContext: BuildContext }) {
return; 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); const dirName = pathDirname(destFilePath);

View File

@ -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<boolean> {
const { filePath } = params;
const dIsTracked = new Deferred<boolean>();
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;
}

View File

@ -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<void> {
const { filePath } = params;
const dDone = new Deferred<void>();
child_process.exec(
`git rm --cached ${pathBasename(filePath)}`,
{ cwd: pathDirname(filePath) },
error => {
if (error !== null) {
dDone.reject(error);
return;
}
dDone.resolve();
}
);
await dDone.pr;
}