Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 20, 2020
2 parents f20a7f9 + 5cc1270 commit b454117
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 143 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Prettier for File Structures

[![npm version](https://badge.fury.io/js/destiny.svg)](https://badge.fury.io/js/destiny)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/benawad/destiny/issues)
[![](https://github.com/benawad/destiny/workflows/ci/badge.svg)](https://github.com/benawad/destiny/actions?query=workflow%3Aci)

---

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@
"rollup-plugin-add-shebang": "^0.3.0",
"rollup-plugin-babel": "^4.3.0",
"rollup-plugin-terser": "^5.2.0",
"semantic-release": "^17.0.4",
"tree-node-cli": "^1.2.5",
"typescript": "^3.7.5",
"semantic-release": "^17.0.4"
"typescript": "^3.7.5"
},
"dependencies": {
"@babel/runtime": "^7.8.4",
"chalk": "^3.0.0",
"core-js": "^3.6.4",
"cosmiconfig": "^6.0.0",
"fs-extra": "^8.1.0",
"glob": "^7.1.6",
"resolve": "^1.15.1",
Expand Down
135 changes: 48 additions & 87 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import chalk from "chalk";
import glob from "glob";
import { cosmiconfigSync } from "cosmiconfig";
import { existsSync, lstatSync, readdirSync } from "fs-extra";
import path from "path";

import getFilePaths from "./index/getFilePaths";
import logger from "./shared/logger";
import { formatFileStructure } from "./index/formatFileStructure";
import { version } from "../package.json";
import logger from "./shared/logger";

const { argv } = process;
const defaults = {
options: {
help: false,
version: false,
detectRoots: false,
},
paths: [],

export type Options = {
detectRoots: boolean;
help: boolean;
version: boolean;
};

type ParsedArgs = {
options: { help: boolean; version: boolean; detectRoots: boolean };
paths: string[];
const defaultOptions: Options = {
detectRoots: false,
help: false,
version: false,
};

const printVersion = () => console.log("v" + version);
Expand All @@ -44,89 +43,51 @@ const printHelp = (exitCode: number) => {
return process.exit(exitCode);
};

const parseArgs = (args: any[]): ParsedArgs =>
args.reduce((acc, arg) => {
switch (arg) {
case "-h":
case "--help":
acc.options.help = true;
break;
case "-V":
case "--version":
acc.options.version = true;
break;
case "-dr":
case "--detect-roots":
acc.options.detectRoots = true;
break;
default:
acc.paths.push(arg);
}

return acc;
}, defaults);

const getFilePaths = (paths: string[], detectRoots: boolean) => {
const files: string[][] = [];

while (paths.length > 0) {
const filePath = paths.pop();

if (!filePath) continue;
if (glob.hasMagic(filePath)) {
const globFiles = glob.sync(filePath);

if (globFiles.length === 0) {
logger.error("Could not find any files for: " + filePath, 1);
}
files.push(
globFiles.filter(x => {
const isFile = lstatSync(x).isFile();

if (!isFile) {
logger.warn(`Skipping non file: ${x}`);
}
return isFile;
})
);
} else if (!existsSync(filePath)) {
logger.error(`Unable to resolve the path: ${filePath}`);
} else {
const stats = lstatSync(filePath);

if (stats.isDirectory()) {
if (detectRoots) {
paths.push(
...readdirSync(path.resolve(filePath)).map(x =>
path.join(filePath, x)
)
);
detectRoots = false;
console.log(paths);
} else {
paths.push(path.join(filePath, "/**/*.*"));
}
} else if (stats.isFile()) {
files.push([filePath]);
} else {
logger.warn(`Skipping: ${filePath}`);
const parseArgs = (
args: any[]
): { options: Partial<Options>; paths: string[] } =>
args.reduce(
(acc, arg) => {
switch (arg) {
case "-h":
case "--help":
acc.options.help = true;
break;
case "-V":
case "--version":
acc.options.version = true;
break;
case "-dr":
case "--detect-roots":
acc.options.detectRoots = true;
break;
default:
acc.paths.push(arg);
}
}
}

return files;
};
return acc;
},
{ options: {}, paths: [] }
);

export const run = async (args: any[]) => {
export const run = async (args: string[]) => {
const config: Partial<Options> =
cosmiconfigSync("destiny").search()?.config ?? {};
const { options, paths } = parseArgs(args);

if (options.help) return printHelp(0);
if (options.version) return printVersion();
const mergedOptions: Options = {
...defaultOptions,
...config,
...options,
};

if (mergedOptions.help) return printHelp(0);
if (mergedOptions.version) return printVersion();
if (paths.length === 0) return printHelp(1);

logger.info("Resolving files.");

const filesToRestructure = getFilePaths(paths, options.detectRoots);
const filesToRestructure = getFilePaths(paths, mergedOptions);
const filesToEdit = filesToRestructure.flat();

if (filesToRestructure.length === 0) {
Expand Down
10 changes: 4 additions & 6 deletions src/index/formatFileStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ export const formatFileStructure = async (
removeEmptyFolders(parentFolder);
}

if (unusedFiles.length) {
if (unusedFiles.length > 0) {
logger.warn(
"Unused files:" +
`Found ${unusedFiles.length} unused files:` +
"\n" +
[...unusedFiles, ...unusedFiles]
.map(file => " ".repeat(8) + file)
.join("\n")
unusedFiles.map(file => " ".repeat(8) + file).join("\n")
);
}

logger.info("Successfully restructured!");
logger.info("Restructure was successful!");
};
30 changes: 16 additions & 14 deletions src/index/formatFileStructure/removeEmptyFolders.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import fs from "fs";
import path from "path";

export const removeEmptyFolders = (p: string) => {
const files = fs.readdirSync(p);
if (!files) {
fs.rmdirSync(p);
}
for (const file of files) {
const fullPath = path.join(p, file);
if (fs.lstatSync(fullPath).isDirectory()) {
removeEmptyFolders(fullPath);
if (!fs.readdirSync(fullPath).length) {
fs.rmdirSync(fullPath);
}
}
/** Recursively removes all empty folders. */
export function removeEmptyFolders(directory: string): void {
const files = fs.readdirSync(directory);
if (!files) return fs.rmdirSync(directory);

for (const filePath of files) {
const fullPath = path.resolve(directory, filePath);

const isDirectory = fs.lstatSync(fullPath).isDirectory();
if (!isDirectory) continue;

removeEmptyFolders(fullPath);

const isEmpty = fs.readdirSync(fullPath).length === 0;
if (isEmpty) fs.rmdirSync(fullPath);
}
};
}
61 changes: 61 additions & 0 deletions src/index/getFilePaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import glob from "glob";
import path from "path";
import { existsSync, lstatSync, readdirSync } from "fs-extra";

import logger from "../shared/logger";
import { Options } from "../index";

const isDirectory = (filePath: string) => lstatSync(filePath).isDirectory();
const isFile = (filePath: string) => lstatSync(filePath).isFile();

const globSearch = (pattern: string) => {
const matches = glob.sync(pattern);
const files = matches.filter(match => isFile(match));

if (files.length === 0) {
logger.error("Could not find any files for: " + pattern, 1);
}

return files;
};

/** Recursively get all file paths. */
export const getFilePaths = (paths: string[], options: Options) => {
let { detectRoots } = options;
const files: string[][] = [];

while (paths.length > 0) {
const filePath = paths.pop();

if (filePath == null || filePath.length === 0) continue;

const isGlobPattern = glob.hasMagic(filePath);
if (isGlobPattern) {
files.push(globSearch(filePath));
continue;
}

if (existsSync(filePath)) {
if (isFile(filePath)) {
files.push([filePath]);
} else if (isDirectory(filePath)) {
if (detectRoots) {
const childDirectories = readdirSync(path.resolve(filePath))
.map(x => path.join(filePath, x))
.filter(x => isDirectory(x));

paths.push(...childDirectories);
detectRoots = false;
} else {
paths.push(path.join(filePath, "/**/*.*"));
}
}
} else {
logger.error(`Unable to resolve the path: ${filePath}`);
}
}

return files;
};

export default getFilePaths;
36 changes: 2 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2650,7 +2650,7 @@ debug@^3.1.0, debug@^3.2.6:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3949,7 +3949,7 @@ import-local@^3.0.2:
pkg-dir "^4.2.0"
resolve-cwd "^3.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5253,11 +5253,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5266,33 +5261,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -5333,11 +5306,6 @@ lodash.isstring@^4.0.1:
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit b454117

Please sign in to comment.