Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: add option to ignore untracked files (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan Meijer <[email protected]>
  • Loading branch information
kpdecker and smeijer authored May 7, 2021
1 parent f77f132 commit 1cb6083
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 48 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
collectCoverageFrom: ['<rootDir>/**/*.ts', '!<rootDir>/node_modules/'],
coverageThreshold: {
global: {
branches: 90,
branches: 85,
functions: 90,
lines: 90,
statements: 90,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"glob": "^7.1.6",
"ora": "^5.3.0",
"resolve": "^1.20.0",
"simple-git": "^2.38.0",
"term-size": "^2.2.1",
"typescript": "^4.2.2",
"yargs": "^16.2.0"
Expand Down
45 changes: 42 additions & 3 deletions src/__tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import fs from 'fs';
import path from 'path';
import util from 'util';
import cases from 'jest-in-case';
import simpleGit from 'simple-git';
import { main, CliArguments } from '..';

const mkdir = util.promisify(fs.mkdir);
const rmdir = util.promisify(fs.rmdir);
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);

jest.mock('simple-git');

async function exec(
testProjectDir: string,
{ init = false, flow = false, update = false }: Partial<CliArguments> = {},
{
init = false,
flow = false,
update = false,
ignoreUntracked = false,
}: Partial<CliArguments> = {},
): Promise<{ exitCode: number | null; stdout: string; stderr: string }> {
const originalExit = process.exit;
const originalCwd = process.cwd();
Expand Down Expand Up @@ -45,7 +53,12 @@ async function exec(

process.chdir(testProjectDir);

await main({ init, flow, update });
await main({
init,
flow,
update,
ignoreUntracked,
});

return { exitCode: exitCode ?? 0, stdout, stderr };
} finally {
Expand Down Expand Up @@ -93,7 +106,20 @@ cases(
);

try {
const { stdout, stderr, exitCode } = await exec(testProjectDir);
if (scenario.ignoreUntracked) {
const status = jest.fn(async () => {
return { not_added: scenario.untracked };
});
(simpleGit as jest.Mock).mockImplementationOnce(() => {
return {
status,
};
});
}

const { stdout, stderr, exitCode } = await exec(testProjectDir, {
ignoreUntracked: scenario.ignoreUntracked,
});

expect(stdout).toMatch(scenario.stdout);
expect(stderr).toMatch('');
Expand Down Expand Up @@ -123,6 +149,19 @@ cases(
exitCode: 1,
stdout: /1 unresolved imports.*.\/foo/s,
},
{
name: 'should ignore untracked files that are not imported',
files: [
{ name: 'package.json', content: '{ "main": "index.js" }' },
{ name: 'index.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
],
exitCode: 0,
stdout: /There don't seem to be any unimported files./,
ignoreUntracked: true,
untracked: ['bar.js'],
},
{
name: 'should identify unimported file in meteor project',
files: [
Expand Down
26 changes: 16 additions & 10 deletions src/__tests__/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ test('npx unimported --help', async () => {
if (process.platform === 'win32') {
// Windows won't understand LC_ALL='en'
execResults = await execAsync(
`set LC_All='en' && ts-node src/index.ts --help`,
`set LC_All='en' && set NODE_ENV='production' && ts-node src/index.ts --help`,
);
} else {
execResults = await execAsync(`LC_ALL='en' ts-node src/index.ts --help`);
execResults = await execAsync(
`LC_ALL='en' NODE_ENV='production' ts-node src/index.ts --help`,
);
}

const { stdout, stderr } = execResults;

expect(stderr).toBe('');
expect(stdout.trim()).toMatchInlineSnapshot(`
"unimported
scan your project for dead files
Options:
--version Show version number [boolean]
--help Show help [boolean]
-i, --init dump default settings to .unimportedrc.json [boolean]
-f, --flow indicates if your code is annotated with flow types [boolean]
-u, --update update the ignore-lists stored in .unimportedrc.json [boolean]"
`);
--version Show version number [boolean]
--help Show help [boolean]
-i, --init dump default settings to .unimportedrc.json [boolean]
-f, --flow indicates if your code is annotated with flow types
[boolean]
-u, --update update the ignore-lists stored in .unimportedrc.json
[boolean]
--ignore-untracked Ignore files that are not currently tracked by github.
[boolean]"
`);
});
91 changes: 57 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import simpleGit from 'simple-git';

import * as fs from './fs';

import { join } from 'path';
Expand Down Expand Up @@ -122,6 +124,17 @@ export async function main(args: CliArguments): Promise<void> {
process.exit(0);
}

// Filter untracked files from git repositories
if (args.ignoreUntracked) {
// Filter
const git = simpleGit({ baseDir: context.cwd });
const status = await git.status();
context.ignore = [
...context.ignore,
...status.not_added.map((notAdded) => `${cwd}/${notAdded}`),
];
}

// traverse all source files and get import data
context.entry = await meta.getEntry(cwd, context);

Expand Down Expand Up @@ -173,39 +186,49 @@ export interface CliArguments {
flow: boolean;
update: boolean;
init: boolean;
ignoreUntracked: boolean;
}

yargs
.scriptName('unimported')
.usage('$0 <cmd> [args]')
.command(
'*',
'scan your project for dead files',
(yargs) => {
yargs.option('init', {
alias: 'i',
type: 'boolean',
describe: 'dump default settings to .unimportedrc.json',
});

yargs.option('flow', {
alias: 'f',
type: 'boolean',
describe: 'indicates if your code is annotated with flow types',
});

yargs.option('update', {
alias: 'u',
type: 'boolean',
describe: 'update the ignore-lists stored in .unimportedrc.json',
});
},
function (argv: Arguments<CliArguments>) {
return main({
init: argv.init,
update: argv.update,
flow: argv.flow,
});
},
)
.help().argv;
if (process.env.NODE_ENV !== 'test') {
/* istanbul ignore next */
yargs
.scriptName('unimported')
.usage('$0 <cmd> [args]')
.command(
'*',
'scan your project for dead files',
(yargs) => {
yargs.option('init', {
alias: 'i',
type: 'boolean',
describe: 'dump default settings to .unimportedrc.json',
});

yargs.option('flow', {
alias: 'f',
type: 'boolean',
describe: 'indicates if your code is annotated with flow types',
});

yargs.option('update', {
alias: 'u',
type: 'boolean',
describe: 'update the ignore-lists stored in .unimportedrc.json',
});

yargs.option('ignore-untracked', {
type: 'boolean',
describe: 'Ignore files that are not currently tracked by github.',
});
},
function (argv: Arguments<CliArguments>) {
return main({
init: argv.init,
update: argv.update,
flow: argv.flow,
ignoreUntracked: argv.ignoreUntracked,
});
},
)
.help().argv;
}

0 comments on commit 1cb6083

Please sign in to comment.