Skip to content

Commit

Permalink
fix: add --dev-filter to work with --dev-debug to filter logging
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Dec 13, 2022
1 parent 3df64f1 commit eb2cd39
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 55 deletions.
42 changes: 16 additions & 26 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,25 @@ export interface ProcessLike {
}

export function preprocessCliFlags(process: ProcessLike): void {
process.argv = process.argv.filter((arg, index) => {
switch (arg) {
case '--dev-debug': {
let debug = '*';
const lookahead = process.argv[index + 1];
if (lookahead && !lookahead.startsWith('-') && !lookahead.includes('=')) {
// --dev-debug var=arg -> DEBUG = *
// --dev-debug -a alias -> DEBUG = *
// --dev-debug --alias alias -> DEBUG = *
// sf config get --dev-debug target-dev-hub --json -> DEBUG = 'target-dev-hub'
// we need to delete the DEBUG level before being passed to OCLIF
delete process.argv[index + 1];
debug = lookahead;
}
process.argv.map((arg) => {
if (arg === '--dev-debug') {
let debug = '*';
const filterIndex = process.argv.indexOf('--dev-filter');
if (filterIndex > 0) {
debug = process.argv[filterIndex + 1];

// convert --dev-debug into a set of environment variables
process.env.DEBUG = debug;
process.env.SF_DEBUG = '1';
process.env.SF_ENV = 'development';
process.env.SFDX_DEBUG = '1';
process.env.SFDX_ENV = 'development';

return false;
process.argv.splice(filterIndex + 1, 1);
process.argv.splice(filterIndex, 1);
}
// convert --dev-debug into a set of environment variables
process.env.DEBUG = debug;
process.env.SF_DEBUG = '1';
process.env.SF_ENV = 'development';
process.env.SFDX_DEBUG = '1';
process.env.SFDX_ENV = 'development';

default: {
// retain all other cli args
return true;
}
// need to calculate indexOf --dev-debug here because it might've changed based on --dev-filter
process.argv.splice(process.argv.indexOf('--dev-debug'), 1);
}
});
}
Expand Down
48 changes: 19 additions & 29 deletions test/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,61 +44,51 @@ describe('CLI flags', () => {
expect(process.env.SF_ENV).to.equal('development');
expect(process.env.SFDX_DEBUG).to.equal('1');
expect(process.env.SFDX_ENV).to.equal('development');
expect(process.argv.length).to.equal(3);
});

it('should recognize --dev-debug with a DEBUG level', () => {
it('should recognize --dev-debug with a DEBUG filter', () => {
const process: ProcessLike = {
argv: ['--dev-debug', 'sf:config'],
argv: ['--dev-debug', '--dev-filter', 'sf:config'],
env: {},
};
preprocessCliFlags(process);
expect(process.argv).not.to.include('--dev-debug');
expect(process.argv).to.deep.equal([]);
expect(process.argv).not.to.include('sf:Config');
expect(process.env.DEBUG).to.equal('sf:config');
expect(process.env.SF_DEBUG).to.equal('1');
expect(process.env.SF_ENV).to.equal('development');
expect(process.env.SFDX_DEBUG).to.equal('1');
expect(process.env.SFDX_ENV).to.equal('development');
});

it('should recognize --dev-debug with a short flag after', () => {
it('should recognize --dev-debug with a DEBUG filter in any order', () => {
const process: ProcessLike = {
argv: ['--dev-debug', '-a', 'alias'],
argv: ['--setdefault', '--dev-filter', 'sf:Config', '--dev-debug'],
env: {},
};
preprocessCliFlags(process);
expect(process.argv).not.to.include('--dev-debug');
expect(process.env.DEBUG).to.equal('*');
expect(process.argv).to.deep.equal(['--setdefault']);
expect(process.argv).not.to.include('sf:Config');
expect(process.argv).to.include('--setdefault');
expect(process.env.DEBUG).to.equal('sf:Config');
expect(process.env.SF_DEBUG).to.equal('1');
expect(process.env.SF_ENV).to.equal('development');
expect(process.env.SFDX_DEBUG).to.equal('1');
expect(process.env.SFDX_ENV).to.equal('development');
});

it('should recognize --dev-debug with a long flag after', () => {
it('should only set DEBUG if --dev-debug is present', () => {
const process: ProcessLike = {
argv: ['--dev-debug', '--alias', 'alias'],
argv: ['--setdefault', '--dev-filter', 'sf:Config'],
env: {},
};
preprocessCliFlags(process);
expect(process.argv).not.to.include('--dev-debug');
expect(process.env.DEBUG).to.equal('*');
expect(process.env.SF_DEBUG).to.equal('1');
expect(process.env.SF_ENV).to.equal('development');
expect(process.env.SFDX_DEBUG).to.equal('1');
expect(process.env.SFDX_ENV).to.equal('development');
});

it('should recognize --dev-debug with a vararg after', () => {
const process: ProcessLike = {
argv: ['--dev-debug', 'var=arg'],
env: {},
};
preprocessCliFlags(process);
expect(process.argv).not.to.include('--dev-debug');
expect(process.env.DEBUG).to.equal('*');
expect(process.env.SF_DEBUG).to.equal('1');
expect(process.env.SF_ENV).to.equal('development');
expect(process.env.SFDX_DEBUG).to.equal('1');
expect(process.env.SFDX_ENV).to.equal('development');
expect(process.argv).to.deep.equal(['--setdefault', '--dev-filter', 'sf:Config']);
expect(process.env.DEBUG).to.undefined;
expect(process.env.SF_DEBUG).to.be.undefined;
expect(process.env.SF_ENV).to.undefined;
expect(process.env.SFDX_DEBUG).to.undefined;
expect(process.env.SFDX_ENV).to.undefined;
});
});

0 comments on commit eb2cd39

Please sign in to comment.