Skip to content

Commit

Permalink
Init the octorg project
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoDev1611 committed Jan 9, 2022
0 parents commit 5a51932
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

[*.ts]
indent_size = 2
indent_style = space

[*.json]
indent_size = 2
indent_style = space

[Makefile]
indent_size = 4
indent_style = tab

[*.md]
indent_size = 2
indent_style = space
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"deno.suggest.imports.hosts": {
"https://deno.land": true,
"https://cdn.nest.land": true,
"https://crux.land": true
},
"deno.importMap": "./import_map.json"
}
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fmt:
deno fmt && dprint fmt

install-win:
powershell -Command 'iwr https://deno.land/install.ps1 -useb | iex'
powershell -Command 'iwr https://dprint.dev/install.ps1 -useb | iex'

install-sh:
curl -fsSL https://dprint.dev/install.sh | sh
curl -fsSL https://deno.land/install.sh | s
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Octorg

Octocat help you to organize the repositories

## Installation

```
deno install -A -f --no-check -n octorg --import-map=https://deno.land/x/octorg/import_map.json https://deno.land/x/octorg/cli.ts
```

---

Made with :heart: in Ecuador
47 changes: 47 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Denomander from "https://deno.land/x/[email protected]/mod.ts";
import { CloneRepo } from "git/clone.ts";
import { GoTooctorg } from "git/goToDir.ts";

const app = new Denomander({
app_name: "octorg",
app_version: "1.0",
app_description: "The best way to organize your github repos!",
});

app
.command("get [repo]", "Clone the repo into the octorg path!")
.option("-g --github", "Download with the github preset!")
.option("-n --nofast", "Not clone with the depth flag")
.action(({ repo }: any) => {
let github;
if (typeof app.github == "boolean") {
if (app.github == true) {
github = true;
} else {
github = false;
}
}
let nofast;
if (typeof app.nofast == "boolean") {
if (app.nofast == true) {
nofast = false;
}
}
if (typeof repo == "string") {
nofast = (typeof app.nofast == "undefined") ? true : nofast;
CloneRepo(repo, github, nofast);
}
});
app
.command("cd", "Go to the path")
.option("-r --repo", "Add the repo url for go more fast!")
.action(() => {
if (typeof app.repo == "string") {
if (app.repo != " ") {
GoTooctorg(app.repo);
}
}
GoTooctorg();
});

app.parse(Deno.args);
4 changes: 4 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as fs from "https://deno.land/[email protected]/fs/mod.ts";
export * as dirs from "https://deno.land/x/[email protected]/mod.ts";
export * as path from "https://deno.land/[email protected]/path/mod.ts";
export * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
10 changes: 10 additions & 0 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"incremental": true,
"markdown": {
},
"includes": ["**/*.{md}"],
"excludes": [],
"plugins": [
"https://plugins.dprint.dev/markdown-0.12.0.wasm"
]
}
33 changes: 33 additions & 0 deletions git/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { exec } from "https://deno.land/x/[email protected]/mod.ts";
import * as files from "utils/files.ts";
import * as log from "utils/logs.ts";

function endsWith(str: string, suffix: string) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

export async function CloneRepo(
repo: string,
github?: boolean,
fast?: boolean,
) {
if (repo.substring(0, 8) === "https://") {
repo = `https://${repo}`;
}
if (github === true && repo.substring(0, 8) != "https://") {
repo = `https://github.com/${repo}`;
}
let cmd = "";
if (fast === true) {
cmd = `git clone --depth=1 ${repo} ${
files.GetoctorgPath(repo.replace("https://", ""))
}`;
} else {
cmd = `git clone ${repo} ${
files.GetoctorgPath(repo.replace("https://", ""))
}`;
}
log.info(`Repo to clone!: ${repo}`);
log.info(`Cmd to execute!: ${cmd}`);
await exec(cmd);
}
13 changes: 13 additions & 0 deletions git/goToDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as files from "utils/files.ts";
export function GoTooctorg(repo?: string) {
let cmd;
if (typeof repo == "string") {
cmd = repo.replace("https://", "");
console.log(files.GetoctorgPath(cmd));

Deno.exit(2);
}
cmd = files.GetoctorgPath(" ");
console.log(cmd);
Deno.exit(1);
}
9 changes: 9 additions & 0 deletions import_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"imports": {
"src/": "./src/",
"@/": "./",
"runner/": "./runner/",
"utils/": "./utils/",
"git/": "./git/"
}
}
69 changes: 69 additions & 0 deletions utils/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { dirs, fs, path } from "@/deps.ts";
import * as log from "utils/logs.ts";

export const currentDir: string = Deno.cwd();

export const configDir: string = dirs.baseDirs.setup().configDir;
export const homeDir: string = dirs.baseDirs.setup().homeDir;

/**
* The function to generate and create if dont exists the logs folder
* @param file File to joint to the log path
* @returns {string} Path to the octorg log folder
*/
export function getLogPath(file: string): string {
const cacheDir: string = dirs.baseDirs.setup().cacheDir;
if (existsFile(path.join(cacheDir, "octorg")) != true) {
Deno.mkdir(path.join(cacheDir, "octorg"));
log.info("octorg folder created");
}
return path.join(cacheDir, "octorg", file);
}

/**
* Function to generate the folder and get the path
* @param file File to join to the config dir
* @returns {string} Path to the octorg config folder
*/
export function getConfigDir(file: string): string {
if (existsFile(path.join(configDir, "octorg")) != true) {
Deno.mkdir(path.join(configDir, "octorg"));
log.info("octorg config folder created");
}
return path.join(configDir, "octorg", file);
}

export function GetoctorgPath(folderName: string): string {
if (existsFile(path.join(homeDir, "octorg")) != true) {
Deno.mkdir(path.join(homeDir, "octorg"));
log.info("octorg Path base is created!");
}
return path.join(homeDir, "octorg", folderName);
}

/**
* Write a json to any path and log
* @param path Path to write the json
* @param data Data to write the json
*/
export function writeJson(path: string, data: Record<string, unknown>) {
try {
Deno.writeTextFileSync(path, JSON.stringify(data));
log.info("Writed the new config.json file!");
} catch (e) {
log.error(e);
}
}

/**
* Check if file or folder exists
* @param file File or folder to check if exists
* @returns {boolean} Result true if exists or false if not
*/
export function existsFile(file: string): boolean {
if (fs.existsSync(file)) {
return true;
} else {
return false;
}
}
79 changes: 79 additions & 0 deletions utils/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as log from "https://deno.land/[email protected]/log/mod.ts";
import { colors } from "@/deps.ts";
import { getLogPath } from "utils/files.ts";

export const Headers = {
octorgHeader: `octorg ->`,
logsSym: {
info: "ⓘ INFO:",
error: "✗ ERROR:",
warn: "⚠ WARNING:",
},
};

await log.setup({
handlers: {
file: new log.handlers.FileHandler("INFO", {
filename: getLogPath("octorg.log"),
formatter: "[ octorg ]: {levelName} -> {msg}",
}),
infoFormatter: new log.handlers.ConsoleHandler("INFO", {
formatter: (logRecord: any) => {
const InfoSym = colors.brightCyan(Headers.logsSym.info);
return `${InfoSym} ${logRecord.msg}`;
},
}),
errorFormatter: new log.handlers.ConsoleHandler("ERROR", {
formatter: (logRecord: any) => {
const ErrorSym = colors.brightRed(Headers.logsSym.error);
return `${ErrorSym} ${logRecord.msg}`;
},
}),
warnFormatter: new log.handlers.ConsoleHandler("WARNING", {
formatter: (logRecord: any) => {
const WarnSym = colors.brightYellow(Headers.logsSym.warn);
return `${WarnSym} ${logRecord.msg}`;
},
}),
},

loggers: {
infoFmt: {
level: "INFO",
handlers: ["file", "infoFormatter"],
},
errorFmt: {
level: "ERROR",
handlers: ["file", "errorFormatter"],
},
warnFmt: {
level: "WARNING",
handlers: ["file", "warnFormatter"],
},
},
});

/**
* Log to the info level and add to the octorg.log file
* @param message Message to log
*/
export function info(message: string) {
log.getLogger("infoFmt").info(`${Headers.octorgHeader} ${message} `);
}

/**
* Log to the warning level and add to the octorg.log file
* @param message Message to log
*/
export function warn(message: string) {
log.getLogger("warnFmt").warning(`${Headers.octorgHeader} ${message}`);
}

/**
* Log to the error level, add to the octorg.log and exit to 2 code
* @param message Message to log
*/
export function error(message: string) {
log.getLogger("errorFmt").error(`${Headers.octorgHeader} ${message}`);
Deno.exit(2);
}

0 comments on commit 5a51932

Please sign in to comment.