Skip to content

Commit

Permalink
chore: detection of file extension improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniaky committed Sep 14, 2022
1 parent 3c7244e commit f1a1cd6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/util/constants.ts
Expand Up @@ -8,15 +8,31 @@ export const configPath = `${filesystem.homedir()}/.discloud`;
export const locales = ["en-US", "pt-BR"];

export const blocked_files = {
go: [".git", ".vscode"],
js: [".git", ".vscode", "node_modules", "package-lock.json", "yarn.lock"],
py: [".git", ".vscode"],
rb: [".git", ".vscode", "Gemfile.lock"],
rs: [".git", ".vscode", "Cargo.lock", "target"],
ts: [".git", ".vscode", "node_modules", "package-lock.json", "yarn.lock"],
common: [".git", ".vscode", "discloud"],
go: [],
js: ["node_modules", "package-lock.json", "yarn.lock"],
py: [],
rb: ["Gemfile.lock"],
rs: ["Cargo.lock", "target"],
};

export const file_ext = {
cjs: "js",
cts: "ts",
go: "go",
js: "js",
jsx: "js",
mjs: "js",
mts: "ts",
py: "py",
rb: "rb",
rs: "rs",
ts: "ts",
tsx: "ts",
} as const;

export const required_files = {
common: ["discloud.config"],
go: ["go.mod", "go.sum"],
js: ["package.json"],
py: ["requirements.txt"],
Expand Down
17 changes: 11 additions & 6 deletions src/util/index.ts
Expand Up @@ -3,7 +3,7 @@ import archiver from "archiver";
import { GlobSync } from "glob";
import { filesystem, http, print } from "gluegun";
import type { ResolveArgsOptions } from "../@types";
import { blocked_files, configPath, required_files } from "./constants";
import { blocked_files, configPath, file_ext, required_files } from "./constants";
import FsJson from "./FsJson";

export const config = new class Config extends FsJson {
Expand Down Expand Up @@ -41,11 +41,15 @@ export function configUpdate(save: Record<string, string>, path = ".") {
filesystem.write(`${path}/discloud.config`, objToString(data, "="));
}

export function getGitIgnore(path: string) {
export function getDiscloudIgnore(path: string) {
return [...new Set(Object.values(blocked_files).flat())]
.map(a => [`${a.replace(/^\/|\/$/, "")}/**`, `${path}/${a.replace(/^\/|\/$/, "")}/**`]).flat();
}

export function getFileExt(ext: string) {
return file_ext[<keyof typeof file_ext>ext] ?? ext;
}

function getKeys(array: Record<string, any>[]) {
const keys = <string[]>[];
for (let i = 0; i < array.length; i++) {
Expand All @@ -55,12 +59,12 @@ function getKeys(array: Record<string, any>[]) {
return [...new Set(keys)];
}

export function getMissingValues(obj: Record<any, any>, match: string[]) {
return match.filter(key => !obj[key]);
export function getMissingValues(obj: Record<any, any>, values: string[]) {
return values.filter(key => !obj[key]);
}

export function getNotIngnoredFiles(path: string) {
const ignore = getGitIgnore(path);
const ignore = getDiscloudIgnore(path);

path = (filesystem.isDirectory(path) || [".", "./"].includes(path) || !/\W+/.test(path)) ?
`${path.replace(/(\\|\/)$/, "")}/**` :
Expand Down Expand Up @@ -176,7 +180,8 @@ export function verifyRequiredFiles(
ext: keyof typeof required_files,
files: string | string[] = [],
) {
const requiredFiles = Object.values(required_files[ext]).concat("discloud.config", files);
const fileExt = getFileExt(ext);
const requiredFiles = Object.values(required_files[fileExt]).concat(required_files.common, files);

for (let i = 0; i < requiredFiles.length; i++) {
const file = requiredFiles[i];
Expand Down

0 comments on commit f1a1cd6

Please sign in to comment.