Skip to content

Commit 238d62c

Browse files
authored
Merge pull request #124 from amtrack/feat/ignorefile
feat: add --ignorefile flag
2 parents fe74462 + 4500fad commit 238d62c

File tree

7 files changed

+79
-24
lines changed

7 files changed

+79
-24
lines changed

src/cli.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFile } from "fs/promises";
12
import type { FileProperties } from "jsforce/api/metadata";
23
import { toMetadataComponentName } from "./metadata-component";
34

@@ -35,3 +36,22 @@ export function formatFileProperties(
3536
const entries = fileProperties.map(mapFn).sort();
3637
return outputType.endsWith("-csv") ? entries.join(",") : entries.join("\n");
3738
}
39+
40+
export async function getNonEmptyLinesFromFile(
41+
filePath: string
42+
): Promise<string[]> {
43+
const content = await readFile(filePath, "utf8");
44+
return (
45+
parseNewLineSeparatedValues(content)
46+
// ignore comments
47+
.filter((x) => !x.startsWith("#"))
48+
);
49+
}
50+
51+
export async function getNonEmptyLinesFromFiles(
52+
filePaths: string[]
53+
): Promise<string[]> {
54+
const promises = filePaths.map((fp) => getNonEmptyLinesFromFile(fp));
55+
const results = await Promise.all(promises);
56+
return results.flat().filter(Boolean);
57+
}

src/commands/force/mdapi/listallmetadata.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { flags, SfdxCommand } from "@salesforce/command";
22
import { promises as fs } from "fs";
33
import {
44
formatFileProperties,
5+
getNonEmptyLinesFromFiles,
56
parseCommaSeparatedValues,
67
parseNewLineSeparatedValues,
78
} from "../../../cli";
@@ -88,6 +89,10 @@ export default class MdapiListAllMetadataCommand extends SfdxCommand {
8889
description: `comma-separated list of metadata component name expressions to ignore
8990
Example: 'InstalledPackage:*,Profile:*,Report:unfiled$public/*,CustomField:Account.*'`,
9091
}),
92+
ignorefile: flags.filepath({
93+
description: `same as --ignore, but instead read from a file containing one ignore pattern per line`,
94+
multiple: true
95+
}),
9196
unlocked: flags.boolean({
9297
description: `list metadata components from Unlocked Packages`,
9398
default: null,
@@ -151,7 +156,10 @@ export default class MdapiListAllMetadataCommand extends SfdxCommand {
151156
: parseCommaSeparatedValues(this.flags.metadata);
152157
allowPatterns = allowPatterns.length ? allowPatterns : ["*:*", "*:**/*"];
153158
allowPatterns = allowPatterns.map(ensureMetadataComponentPattern);
154-
const ignorePatterns = parseCommaSeparatedValues(this.flags.ignore);
159+
const ignorePatterns = [
160+
...(await getNonEmptyLinesFromFiles(this.flags.ignorefile)),
161+
...parseCommaSeparatedValues(this.flags.ignore)
162+
];
155163
const allowFunctions = [];
156164
const ignoreFunctions = [];
157165
const flag2FunctionName = {

src/commands/package.xml/generate.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { flags, SfdxCommand } from '@salesforce/command';
22
import { promises as fs } from 'fs';
3-
import { parseCommaSeparatedValues } from '../../cli';
3+
import {
4+
getNonEmptyLinesFromFiles,
5+
parseCommaSeparatedValues
6+
} from '../../cli';
47
import { match } from '../../match';
58
import { toMetadataComponentName } from '../../metadata-component';
69
import PackageXml from '../../package-xml';
@@ -58,9 +61,10 @@ export default class PackageXmlGenerateCommand extends SfdxCommand {
5861
description: `comma-separated list of metadata component name expressions to ignore
5962
Example: 'InstalledPackage:*,Profile:*,Report:unfiled$public/*,CustomField:Account.*'`
6063
}),
61-
// ignorefile: flags.filepath({
62-
// description: `same as --ignore, just one ignore pattern per line in a file`
63-
// }),
64+
ignorefile: flags.filepath({
65+
description: `same as --ignore, but instead read from a file containing one ignore pattern per line`,
66+
multiple: true
67+
}),
6468
defaultignore: flags.array({
6569
description: 'ignored by default, to disable use --defaultignore ""',
6670
default: ['InstalledPackage:*']
@@ -82,13 +86,11 @@ export default class PackageXmlGenerateCommand extends SfdxCommand {
8286
if (this.flags.apiversion) {
8387
meta['version'] = this.flags.apiversion;
8488
}
85-
const ignorePatterns = [];
86-
if (this.flags.ignore) {
87-
ignorePatterns.push(...parseCommaSeparatedValues(this.flags.ignore));
88-
}
89-
if (this.flags.defaultignore) {
90-
ignorePatterns.push(...this.flags.defaultignore);
91-
}
89+
const ignorePatterns = [
90+
...(await getNonEmptyLinesFromFiles(this.flags.ignorefile)),
91+
...parseCommaSeparatedValues(this.flags.ignore),
92+
...this.flags.defaultignore
93+
];
9294
const [, keep] = match(
9395
fileProperties,
9496
ignorePatterns,

test/cli.test.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
import { expect } from 'chai';
1+
import { expect } from "chai";
2+
import { join } from "path";
23
import {
4+
getNonEmptyLinesFromFiles,
35
parseCommaSeparatedValues,
4-
parseNewLineSeparatedValues
5-
} from '../src/cli';
6+
parseNewLineSeparatedValues,
7+
} from "../src/cli";
68

7-
describe('cli', () => {
8-
describe('parseCommaSeparatedValues()', () => {
9-
it('parses comma-separated values', () => {
9+
describe("cli", () => {
10+
describe("parseCommaSeparatedValues()", () => {
11+
it("parses comma-separated values", () => {
1012
expect(
11-
parseCommaSeparatedValues(' CustomObject:Account , ApexPage:Foo')
12-
).to.deep.equal(['CustomObject:Account', 'ApexPage:Foo']);
13+
parseCommaSeparatedValues(" CustomObject:Account , ApexPage:Foo")
14+
).to.deep.equal(["CustomObject:Account", "ApexPage:Foo"]);
1315
});
1416
});
15-
describe('parseNewLineSeparatedValues()', () => {
16-
it('parses newline-separated values', () => {
17+
describe("parseNewLineSeparatedValues()", () => {
18+
it("parses newline-separated values", () => {
1719
const input = `CustomObject:Account
1820
ApexPage:Foo
1921
`;
2022
expect(parseNewLineSeparatedValues(input)).to.deep.equal([
21-
'CustomObject:Account',
22-
'ApexPage:Foo'
23+
"CustomObject:Account",
24+
"ApexPage:Foo",
25+
]);
26+
});
27+
});
28+
describe("getNonEmptyLinesFromFiles()", () => {
29+
it("parses multiple files", async () => {
30+
const fixtures = join("test", "fixtures", "ignore-files");
31+
const ignoreFiles = [
32+
join(fixtures, "full.txt"),
33+
join(fixtures, "empty.txt"),
34+
join(fixtures, "min.txt"),
35+
];
36+
expect(await getNonEmptyLinesFromFiles(ignoreFiles)).to.deep.equal([
37+
"ApexClass:Full",
38+
"ApexPage:Full",
39+
"ApexClass:Min",
40+
"ApexPage:Min",
2341
]);
2442
});
2543
});

test/fixtures/ignore-files/empty.txt

Whitespace-only changes.

test/fixtures/ignore-files/full.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This is a comment
2+
3+
ApexClass:Full
4+
5+
ApexPage:Full

test/fixtures/ignore-files/min.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ApexClass:Min
2+
ApexPage:Min

0 commit comments

Comments
 (0)