8000 Feature/add-chalk by camipozas · Pull Request #3 · camipozas/cordyceps · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature/add-chalk #3

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

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 19 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"plugins": ["@typescript-eslint", "prettier", "import"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"eslint-config-prettier"
],
"parserOptions": {
"project": "./tsconfig.json"
"project": "./tsconfig.json",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/no-floating-promises": "off",
"semi": ["error", "always"],
"indent": ["error", 2],
"quotes": ["error", "single"],
"no-unused-vars": "warn",
"no-console": "warn",
"no-undef": "warn",
"no-use-before-define": "warn"
"no-console": "off",
"no-undef": "off",
"no-use-before-define": "warn",
"import/order": [
"error",
{
"groups": [
["builtin", "external"],
"internal",
"parent",
"sibling",
"index"
],
"newlines-between": "always-and-inside-groups"
}
]
}
}
39 changes: 36 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
{
"singleQuote": true,
"semi": true,
"tabWidth": 2,
"trailingComma": "es5"
"trailingComma": "all",
"overrides": [
{
"files": "*.ts",
"options": {
"parser": "typescript",
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "consistent",
"arrowParens": "always",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"endOfLine": "lf",
"importOrder": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"importDeclarationSorter": {
"order": [
"single",
"multiple",
"namespace",
"type",
"default"
]
}
}
}
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"@types/node": "^18.14.2",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"chalk": "^4.1.2",
"eslint": "^8.36.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.7",
"ts-node-dev": "^2.0.0",
Expand Down
17 changes: 14 additions & 3 deletions src/clone-repositories.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import simpleGit, { SimpleGit } from 'simple-git';
import { env } from './env/env';
import chalk from 'chalk';
import path from 'path';

import fs from 'fs';

import { env } from './env/env';

const log = console.log;

/**
* It clones a list of repositories into a local folder
* @param {string[]} repoNames - An array of strings that represent the names of the repositories to
* clone.
*/
export const cloneRepositories = async (repoNames: string[]) => {
const git: SimpleGit = simpleGit();
log(chalk.blue.bold('🚀 Cloning repositories 🚀'));

for (const repoName of repoNames) {
const repoUrl = `https://github.com/${env.GITHUB_ORG}/${repoName}`;
const localPath = path.join(env.HOME, env.FOLDER, repoName);

await git.clone(repoUrl, localPath);
console.log(`Cloned ${repoName} into ${localPath}`);
if (fs.existsSync(localPath)) {
log(chalk.yellow(`❌ ${repoName} already exists in ${localPath}`));
} else {
await git.clone(repoUrl, localPath);
log(chalk.green(`✅ Cloned ${repoName} into ${localPath}`));
}
}
};
12 changes: 11 additions & 1 deletion src/get-repositories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { request } from '@octokit/request';

import chalk from 'chalk';

import { env } from './env/env';

const log = console.log;

/**
* It makes a request to the GitHub API to get a list of all the repositories in the organization, and
* then filters out the ones that don't start with "your-filter-prefix-" and is not empty.
Expand Down Expand Up @@ -43,9 +48,14 @@ export const accessibleRepos = async (repoNames: string[]) => {
});
accessibleRepos.push(repoName);
} catch (error) {
console.log(`Error: ${repoName} is not accessible`);
log(chalk.red(`❌ ${repoName} is not accessible`));
}
}

log(chalk.green(`✅ ${accessibleRepos.length} accessible repos.`));
log(chalk.red(`❌ ${repoNames.length - accessibleRepos.length} inaccessible repos`));
log(chalk.blue.bold('🚀 List of repos 🚀'));
log(accessibleRepos.map((repoName) => chalk.green(`✅ ${repoName}`)).join('\n'));

return accessibleRepos;
};
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import chalk from 'chalk';

import { getAllRepositories, accessibleRepos } from './get-repositories';
import { cloneRepositories } from './clone-repositories';
import { repoStatus } from './repo-status';

const main = async () => {
const log = console.log;
log(chalk.blue.bold('🚀 Create your own GitHub organization backup script 🚀'));
const allRepos = await getAllRepositories();
const accessible = await accessibleRepos(allRepos);
await cloneRepositories(accessible);
console.log(accessible);
await repoStatus(accessible);
};

main();
30 changes: 30 additions & 0 deletions src/repo-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chalk from 'chalk';
import path from 'path';
import simpleGit, { SimpleGit } from 'simple-git';

import { env } from './env/env';

const log = console.log;

/**
* It checks the status of the local repository and pulls the latest changes if there are any
* @param {string[]} repoNames - An array of repository names.
*/
export const repoStatus = async (repoNames: string[]) => {
const git: SimpleGit = simpleGit();
log(chalk.blue.bold('🚀 Checking repository status 🚀'));

for (const repoName of repoNames) {
const localPath = path.join(env.HOME, env.FOLDER, repoName);
const status = await git.status();

try {
if (status.behind > 0) {
await git.pull();
log(chalk.green(`🐛 Pulled latest changes for ${repoName}`));
}
} catch (error) {
log(chalk.red(`❌ ${repoName} can't be pulled`));
}
}
};
Loading
0