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

feat: add --ignore property/flag #98

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ OPTIONS
-h, --help Output usage information
-w, --write Restructure and edit folders and files
-S, --avoid-single-file Flag to indicate if single files in folders should be avoided
--ignore {path} .gitignore like file path expressions to ignore
danielpza marked this conversation as resolved.
Show resolved Hide resolved
```

Dry run which will output what the resulting file structure will look like:
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { argv } = process;
export type Config = {
help: boolean;
include: string[];
ignore: string[];
version: boolean;
write: boolean;
avoidSingleFile: boolean;
Expand All @@ -22,6 +23,7 @@ export type Config = {
const defaultConfig: Config = {
help: false,
include: [],
ignore: [],
version: false,
write: false,
avoidSingleFile: false,
Expand All @@ -44,6 +46,7 @@ const printHelp = (exitCode: number) => {
-h, --help Output usage information
-w, --write Restructure and edit folders and files
-S, --avoid-single-file Flag to indicate if single files in folders should be avoided
--ignore {path} .gitignore like file path expressions to ignore
danielpza marked this conversation as resolved.
Show resolved Hide resolved
`
);

Expand Down Expand Up @@ -75,6 +78,9 @@ const parseArgs = (args: string[]) => {
case "--avoid-single-file":
cliConfig.avoidSingleFile = true;
break;
case "--ignore":
cliConfig.ignore = [...(cliConfig.ignore ?? []), args.shift() ?? ""];
break;
danielpza marked this conversation as resolved.
Show resolved Hide resolved
default: {
if (fs.existsSync(arg) || glob.hasMagic(arg)) {
cliConfig.include = [...(cliConfig.include ?? []), arg];
Expand Down Expand Up @@ -105,7 +111,10 @@ export const run = async (args: string[]) => {
if (mergedConfig.version) return printVersion();
if (mergedConfig.include.length === 0) return printHelp(1);

const restructureMap = getRestructureMap(mergedConfig.include);
const restructureMap = getRestructureMap(
mergedConfig.include,
mergedConfig.ignore
);
const filesToEdit = Object.values(restructureMap).flat();

if (filesToEdit.length === 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/index/getFilePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import logger from "../shared/logger";
const isDirectory = (filePath: string) => fs.lstatSync(filePath).isDirectory();
const isFile = (filePath: string) => fs.lstatSync(filePath).isFile();

export const globSearch = (pattern: string) => {
const matches = glob.sync(pattern);
export const globSearch = (pattern: string, ignore: string[]) => {
const matches = glob.sync(pattern, { ignore });
const files = matches.filter(match => isFile(match));

if (files.length === 0) {
Expand All @@ -17,7 +17,7 @@ export const globSearch = (pattern: string) => {
return files;
};

export const getFilePaths = (rootPath: string) => {
export const getFilePaths = (rootPath: string, ignore: string[]) => {
const filePaths: string[] = [];
const paths = [rootPath];

Expand All @@ -27,7 +27,7 @@ export const getFilePaths = (rootPath: string) => {

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

Expand All @@ -50,11 +50,11 @@ export const getFilePaths = (rootPath: string) => {
};

/** Get a restructure map with rootPath keys and filePaths values. */
export const getRestructureMap = (rootPaths: string[]) =>
export const getRestructureMap = (rootPaths: string[], ignore: string[]) =>
rootPaths.reduce<{ [key: string]: string[] }>(
(acc, rootPath) => ({
...acc,
[rootPath]: getFilePaths(rootPath),
[rootPath]: getFilePaths(rootPath, ignore),
}),
{}
);
Expand Down