Skip to content

Commit

Permalink
Merge branch 'includefiles' into feature/include
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/cli/index.ts
#	src/core/packager.ts
  • Loading branch information
yamadashy committed Aug 3, 2024
2 parents 5017984 + e78585c commit cbcf63e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
3 changes: 2 additions & 1 deletion repopack.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"useGitignore": true,
"useDefaultPatterns": true,
"customPatterns": []
}
},
"includeFiles": []
}
6 changes: 6 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface CliOptions extends OptionValues {
topFilesLen?: number;
outputShowLineNumbers?: boolean;
style?: RepopackOutputStyle;
include?: string[];
}

async function executeAction(directory: string, rootDir: string, options: CliOptions) {
Expand All @@ -33,6 +34,7 @@ async function executeAction(directory: string, rootDir: string, options: CliOpt
console.log(pc.dim(`\n📦 Repopack v${version}\n`));

logger.setVerbose(options.verbose || false);
logger.trace('Loaded CLI options:', options);

const fileConfig: RepopackConfigFile = await loadFileConfig(rootDir, options.config ?? null);
logger.trace('Loaded file config:', fileConfig);
Expand All @@ -53,6 +55,9 @@ async function executeAction(directory: string, rootDir: string, options: CliOpt
if (options.style) {
cliConfig.output = { ...cliConfig.output, style: options.style.toLowerCase() as RepopackOutputStyle };
}
if (options.include) {
cliConfig.include = options.include;
}
logger.trace('CLI config:', cliConfig);

const config: RepopackConfigMerged = mergeConfigs(fileConfig, cliConfig);
Expand Down Expand Up @@ -106,6 +111,7 @@ export async function run() {
.arguments('[directory]')
.option('-v, --version', 'show version information')
.option('-o, --output <file>', 'specify the output file name')
.option('--include <files...>', 'space-separated list of files to include')
.option('-i, --ignore <patterns>', 'additional ignore patterns (comma-separated)')
.option('-c, --config <path>', 'path to a custom config file')
.option('--top-files-len <number>', 'specify the number of top files to display', parseInt)
Expand Down
7 changes: 6 additions & 1 deletion src/config/configLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@ export function mergeConfigs(fileConfig: RepopackConfigFile, cliConfig: Repopack
...(cliConfig.ignore?.customPatterns || []),
],
},
include: [
...(defaultConfig.include || []),
...(fileConfig.include || []),
...(cliConfig.include || []),
],
};
}
}
3 changes: 2 additions & 1 deletion src/config/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export const defaultConfig: RepopackConfigDefault = {
useDefaultPatterns: true,
customPatterns: [],
},
};
include: [],
};
8 changes: 7 additions & 1 deletion src/core/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { generateOutput as defaultGenerateOutput } from './outputGenerator.js';
import { checkFileWithSecretLint, createSecretLintConfig } from '../utils/secretLintUtils.js';
import { logger } from '../utils/logger.js';
import { filterIncludedFiles } from '../utils/includeUtils.js';


export interface Dependencies {
getAllIgnorePatterns: typeof defaultGetAllIgnorePatterns;
Expand Down Expand Up @@ -42,9 +44,13 @@ export async function pack(

// Get all file paths in the directory
const allFilePaths = await getFilePaths(rootDir, '', ignoreFilter, config);
const filteredPaths = filterIncludedFiles(rootDir, allFilePaths, config.include);

// Use filteredPaths if not empty, otherwise use filePaths
const pathsToProcess = filteredPaths.length > 0 ? filteredPaths : allFilePaths;

// Perform security check
const suspiciousFilesResults = await performSecurityCheck(allFilePaths, rootDir);
const suspiciousFilesResults = await performSecurityCheck(pathsToProcess, rootDir);

// Filter out suspicious files
const safeFilePaths = allFilePaths.filter(
Expand Down
6 changes: 5 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface RepopackConfigBase {
useDefaultPatterns?: boolean;
customPatterns?: string[];
};
include?: string[];
}

export type RepopackConfigDefault = RepopackConfigBase & {
Expand All @@ -32,6 +33,7 @@ export type RepopackConfigDefault = RepopackConfigBase & {
useDefaultPatterns: boolean;
customPatterns?: string[];
};
include: string[];
};

export type RepopackConfigFile = RepopackConfigBase & {
Expand All @@ -49,6 +51,7 @@ export type RepopackConfigFile = RepopackConfigBase & {
useDefaultPatterns?: boolean;
customPatterns?: string[];
};
include?: string[];
};

export type RepopackConfigCli = RepopackConfigBase & {
Expand All @@ -66,6 +69,7 @@ export type RepopackConfigCli = RepopackConfigBase & {
useDefaultPatterns?: boolean;
customPatterns?: string[];
};
include?: string[];
};

export type RepopackConfigMerged = RepopackConfigDefault & RepopackConfigFile & RepopackConfigCli;
export type RepopackConfigMerged = RepopackConfigDefault & RepopackConfigFile & RepopackConfigCli;
29 changes: 29 additions & 0 deletions src/utils/includeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path';
import { logger } from '../utils/logger.js';

export function filterIncludedFiles(rootDir: string, filePaths: string[], include: string[]): string[] {
if (include.length === 0) {
return filePaths;
}

// Normalize paths to handle case insensitivity and different representations
const normalizePath = (p: string) => path.normalize(path.resolve(rootDir, p)).toLowerCase();

// Create a Set of normalized absolute paths for the included files
const includeSet = new Set(include.map(normalizePath));

// Map file paths to their normalized absolute paths and filter those that are in the include set
const filteredAbsolutePaths = filePaths
.map(filePath => path.resolve(rootDir, filePath))
.filter(absolutePath => includeSet.has(normalizePath(absolutePath)));

// Log any files in includeSet that are not found in filePaths
const absoluteFilePathsSet = new Set(filePaths.map(filePath => normalizePath(path.resolve(rootDir, filePath))));
const missingFiles = [...includeSet].filter(includedFile => !absoluteFilePathsSet.has(includedFile));
if (missingFiles.length > 0) {
logger.trace(`Included files not found: ${missingFiles.join(', ')}`);
}

// Convert filtered absolute paths back to relative paths
return filteredAbsolutePaths.map(filePath => path.relative(rootDir, filePath));
}

0 comments on commit cbcf63e

Please sign in to comment.