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

Report unused repos #74

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Repo {
this.repo = repo;
this.ref = ref;
this.filePaths = [];
this.usedRepos = new Set();
}
}
// File is the class that contains a filename and lines of the file
Expand Down Expand Up @@ -194,9 +195,9 @@ class Sync {
// Download repo as zip file.
// Extract to sync_repos directory.
// Get repository details and file paths.
const repositories = await this.getRepos();
const { repositories, usedRepos } = await this.getRepos();
// Search each origin file and scrape the snippets
const snippets = await this.extractSnippets(repositories);
const snippets = await this.extractSnippets(repositories, usedRepos);
// Get the infos (name, path) of all the files in the target directories
let targetFiles = await this.getTargetFilesInfos();
// Add the lines of each file
Expand All @@ -206,10 +207,13 @@ class Sync {
// Overwrite the files to the target directories
await this.writeFiles(splicedFiles);
// Delete the sync_repos directory

await this.cleanUp();
this.progress.updateOperation("done");
this.progress.stop();
this.logger.info("snipsync operation complete");
this.reportUnusedRepos(usedRepos);
console.log("done");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? The logger statement above seems to cover this. I wonder if you move the previous line above the logger, that's enough. If you disagree, I think I'd use this.logger instead of console.log.

return;
}
// clear is the method that will remove snippets from target merge files
Expand All @@ -225,7 +229,10 @@ class Sync {
}
// getRepos is the method that downloads all of the Github repos
async getRepos() {
const repositories = [];
const reposData = {
repositories: [],
usedRepos: new Set(),
};
this.progress.updateOperation("retrieving source files");
this.progress.updateTotal(this.origins.length);
await Promise.all(
Expand All @@ -235,13 +242,14 @@ class Sync {
const filePaths = glob.sync(pattern).map((f) => ({
name: basename(f), directory: dirname(f),
}));
repositories.push({
reposData.repositories.push({
rtype: 'local',
owner: origin.files.owner,
repo: origin.files.repo,
ref: origin.files.ref,
filePaths: filePaths,
});
reposData.usedRepos.add(`${origin.files.owner}/${origin.files.repo}`);
return;
}
if (!("owner" in origin && "repo" in origin)) {
Expand All @@ -254,11 +262,12 @@ class Sync {
const buffer = arrayBuffToBuff(byteArray);
await writeAsync(fileName, buffer);
repository.filePaths = await this.unzip(fileName);
repositories.push(repository);
reposData.repositories.push(repository);
reposData.usedRepos.add(`${owner}/${repo}`);
this.progress.increment();
})
);
return repositories;
return reposData;
}
// unzip unzips the Github repo archive
async unzip(filename) {
Expand All @@ -277,8 +286,21 @@ class Sync {
});
return result.data;
}
// extractSnippets returns an array of code snippets that are found in the repositories
async extractSnippets(repositories) {
reportUnusedRepos(usedRepos) {
const configRepos = new Set(
this.origins.map((origin) => `${origin.owner}/${origin.repo}`)
);
const unusedRepos = new Set(
[...configRepos].filter((repo) => !usedRepos.has(repo))
);
if (unusedRepos.size > 0) {
this.logger.warn("Unused repositories:");
for (const repo of unusedRepos) {
this.logger.warn(`- ${repo}`);
}
}
}
async extractSnippets(repositories, usedRepos) {
const snippets = [];
this.progress.updateOperation("extracting snippets");
await Promise.all(
Expand Down Expand Up @@ -309,13 +331,20 @@ class Sync {
fileSnips.push(snip);
}
});
if (fileSnips.length > 0) {
usedRepos.add(`${owner}/${repo}`);
}
snippets.push(...fileSnips);
this.progress.increment();
}
})
);
return snippets;
}




// getTargetFilesInfos identifies the paths to the target write files
async getTargetFilesInfos() {
this.progress.updateOperation("gathering information of target files");
Expand Down
Loading