Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for deleted files to prevent errors during processing #163

Merged
merged 10 commits into from
Dec 7, 2024
75 changes: 68 additions & 7 deletions src/dev/create-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,39 @@ const getProjectPath = (rawArgs: string[]) => {
return { projectPath };
};

const getChangedFilesFromFirstCommit = async (projectPath: string): Promise<string[]> => {
const getDiffFilesFromFirstCommit = async ({
projectPath,
isDeleted = false,
}: {
projectPath: string;
isDeleted?: boolean;
}): Promise<string[]> => {
if (isDeleted) {
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
// all files that have ever existed in the repo's history
const { stdout: allFiles } = await execa(
"git",
["log", "--all", "--diff-filter=ACDMRT", "--name-only", "--format="],
{ cwd: projectPath },
);

const { stdout: currentFiles } = await execa("git", ["ls-files"], {
cwd: projectPath,
});

const allFilesSet = new Set(allFiles.split("\n").filter(Boolean));
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
const currentFilesSet = new Set(currentFiles.split("\n").filter(Boolean));

return Array.from(allFilesSet).filter(file => !currentFilesSet.has(file));
}

const { stdout: firstCommit } = await execa("git", ["rev-list", "--max-parents=0", "HEAD"], {
cwd: projectPath,
});
const { stdout } = await execa("git", ["diff", "--name-only", `${firstCommit.trim()}..HEAD`], {

const { stdout } = await execa("git", ["diff", "--diff-filter=d", "--name-only", `${firstCommit.trim()}..HEAD`], {
cwd: projectPath,
});

return stdout.split("\n").filter(Boolean);
};

Expand Down Expand Up @@ -77,13 +103,47 @@ const findTemplateFiles = async (dir: string, templates: Set<string>) => {
}
};

const copyFiles = async (files: string[], projectName: string, projectPath: string, templates: Set<string>) => {
for (const file of files) {
const copyFiles = async (
changedFiles: string[],
deletedFiles: string[],
projectName: string,
projectPath: string,
templates: Set<string>,
) => {
for (const file of deletedFiles) {
const destPath = path.join(EXTERNAL_EXTENSIONS_DIR, projectName, TARGET_EXTENSION_DIR, file);
if (fs.existsSync(destPath)) {
await fs.promises.unlink(destPath);
prettyLog.success(`Removed deleted file: ${file}`, 2);
console.log("\n");

// remove empty directories
const dirPath = path.dirname(destPath);
try {
const remainingFiles = await fs.promises.readdir(dirPath);
if (remainingFiles.length === 0) {
await fs.promises.rmdir(dirPath);
prettyLog.success(`Removed empty directory: ${path.relative(EXTERNAL_EXTENSIONS_DIR, dirPath)}`, 2);
console.log("\n");
}
} catch {
// directory might already be deleted, ignore error
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

for (const file of changedFiles) {
const pathSegmentsOfFile = file.split(path.sep);
const sourcePath = path.resolve(projectPath, file);
const destPath = path.join(EXTERNAL_EXTENSIONS_DIR, projectName, TARGET_EXTENSION_DIR, file);
const sourceFileName = path.basename(sourcePath);

if (!fs.existsSync(sourcePath)) {
prettyLog.warning(`Source file not found, skipping: ${file}`, 2);
console.log("\n");
continue;
}

if (templates.has(file)) {
prettyLog.warning(`Skipping file: ${file}`, 2);
prettyLog.info(`Please instead create/update: ${destPath}.args.mjs`, 3);
Expand Down Expand Up @@ -150,15 +210,16 @@ const main = async (rawArgs: string[]) => {
prettyLog.info(`Extension name: ${projectName}\n`);

prettyLog.info("Getting list of changed files...", 1);
const changedFiles = await getChangedFilesFromFirstCommit(projectPath);
const changedFiles = await getDiffFilesFromFirstCommit({ projectPath });
const deletedFiles = await getDiffFilesFromFirstCommit({ projectPath, isDeleted: true });

if (changedFiles.length === 0) {
if (changedFiles.length === 0 && deletedFiles.length === 0) {
prettyLog.warning("No changed files to copy.", 1);
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
console.log("\n");
} else {
prettyLog.info(`Found ${changedFiles.length} changed files, processing them...`, 1);
console.log("\n");
await copyFiles(changedFiles, projectName, projectPath, templates);
await copyFiles(changedFiles, deletedFiles, projectName, projectPath, templates);
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
}

prettyLog.info(`Files processed successfully, updated ${EXTERNAL_EXTENSIONS_DIR}/${projectName} directory.`);
Expand Down
Loading