-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
3,542 additions
and
638 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {Options} from "./options"; | ||
import {simpleGit} from "simple-git"; | ||
import {glob} from "glob"; | ||
import path from "path"; | ||
|
||
export async function filterFiles(options: Options, directory: string, globalExclude?: string) { | ||
|
||
const userExclude = options.exclude || ''; | ||
const combinedExclude = [globalExclude ?? '', userExclude].filter(Boolean).join(','); | ||
const excludePatterns = combinedExclude.split(','); | ||
|
||
let files: string[]; | ||
|
||
try { | ||
const git = simpleGit(directory); | ||
const isGitRepo = await git.checkIsRepo(); | ||
|
||
if (isGitRepo) { | ||
const gitFiles = await git.raw(['ls-files', directory]); | ||
files = gitFiles.split('\n').filter(Boolean); | ||
|
||
if (combinedExclude.length > 0) { | ||
files = files.filter(file => | ||
!excludePatterns.some(pattern => | ||
file.endsWith(pattern) || | ||
(glob.hasMagic(pattern) | ||
? glob.sync(pattern, {cwd: directory}).includes(file) | ||
: false))); | ||
} | ||
} else { | ||
const globPattern = path.join(directory, '**/*'); | ||
files = await glob(globPattern, {nodir: true, ignore: excludePatterns}); | ||
} | ||
} catch (error: any) { | ||
console.error('Error listing files:', error); | ||
throw new Error('Error listing files:', error); | ||
} | ||
|
||
return files; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Options { | ||
exclude?: string; | ||
verbose?: boolean; | ||
file?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import path from "path"; | ||
import os from "os"; | ||
import fs from "fs/promises"; | ||
|
||
export async function readGlobalConfig(): Promise<string> { | ||
const configPath = path.join(os.homedir(), '.copa'); | ||
try { | ||
const configContent = await fs.readFile(configPath, 'utf-8'); | ||
const ignoreLine = configContent.split('\n').find(line => line.startsWith('ignore:')); | ||
if (ignoreLine) { | ||
return ignoreLine.split(':')[1].trim(); | ||
} | ||
} catch (error) { | ||
// If the file doesn't exist or can't be read, return an empty string | ||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { | ||
console.warn('Warning: Unable to read global config file:', error); | ||
} | ||
} | ||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { filterFiles } from '../src/filterFiles'; | ||
import * as path from 'path'; | ||
|
||
const testDir = path.join(__dirname, 'test_files'); | ||
|
||
describe('CoPa Functionality', () => { | ||
describe('filterFiles', () => { | ||
test('includes all files when no exclusions', async () => { | ||
const files = await filterFiles({}, testDir); | ||
expect(files.length).toBe(9); // Assuming there are 9 files in the test_files directory | ||
expect(files).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/file_1\.js$/), | ||
expect.stringMatching(/file_1\.md$/), | ||
expect.stringMatching(/file_1\.yml$/), | ||
expect.stringMatching(/file_2\.js$/), | ||
expect.stringMatching(/file_2\.md$/), | ||
expect.stringMatching(/file_2\.yml$/), | ||
expect.stringMatching(/file_3\.js$/), | ||
expect.stringMatching(/file_3\.md$/), | ||
expect.stringMatching(/file_3\.yml$/), | ||
])); | ||
}); | ||
|
||
test('excludes files based on command line options', async () => { | ||
const files = await filterFiles({ exclude: 'js,md' }, testDir); | ||
expect(files.length).toBe(3); | ||
expect(files).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/\.yml$/), | ||
])); | ||
expect(files).not.toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/\.js$/), | ||
expect.stringMatching(/\.md$/), | ||
])); | ||
}); | ||
|
||
test('excludes files based on command line options', async () => { | ||
const files = await filterFiles({ exclude: 'yml' }, testDir); | ||
expect(files.length).toBe(6); | ||
expect(files).toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/\.js$/), | ||
expect.stringMatching(/\.md$/), | ||
])); | ||
expect(files).not.toEqual(expect.arrayContaining([ | ||
expect.stringMatching(/\.yml$/), | ||
])); | ||
}); | ||
|
||
test('handles wildcard patterns', async () => { | ||
const files = await filterFiles({ exclude: 'file_*.yml' }, testDir); | ||
console.log(files); | ||
const yamlFiles = files.filter(file => file.endsWith('.yml')); | ||
expect(yamlFiles.length).toBe(0); | ||
expect(files.length).toBe(6); // Should include all js and md files | ||
}); | ||
}); | ||
|
||
}); |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.