Skip to content

Commit

Permalink
fix: forceIgnore does not work for `SFDX: Retrieve Source in Manifest…
Browse files Browse the repository at this point in the history
… from Org` in vscode extension (#413)

* fix: forceIgnore does not work with retrieve manifest

* fix: matchingContent subfolder
  • Loading branch information
violetyao authored Aug 18, 2021
1 parent 832955f commit af52e37
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/client/metadataApiRetrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export class MetadataApiRetrieve extends MetadataTransfer<
type: 'merge',
mergeWith: this.components.getSourceComponents(),
defaultDirectory: pkg.outputDir,
forceIgnoredPaths: this.components.forceIgnoredPaths ?? new Set<string>(),
}
: {
type: 'directory',
Expand All @@ -229,7 +230,9 @@ export class MetadataApiRetrieve extends MetadataTransfer<
.getSourceComponents()
.toArray();
const convertResult = await converter.convert(zipComponents, 'source', outputConfig);
components.push(...convertResult.converted);
if (convertResult) {
components.push(...convertResult.converted);
}
}
return new ComponentSet(components, registry);
}
Expand Down
4 changes: 4 additions & 0 deletions src/collections/componentSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
*/
public sourceApiVersion: string;
public fullName?: string;
public forceIgnoredPaths?: Set<string>;
private logger: Logger;
private registry: RegistryAccess;
private components = new Map<string, Map<string, SourceComponent>>();
Expand Down Expand Up @@ -137,6 +138,8 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
buildComponents(fsPaths, false);
buildComponents(fsDeletePaths, true);

set.forceIgnoredPaths = resolver.forceIgnoredPaths;

return set;
}

Expand Down Expand Up @@ -192,6 +195,7 @@ export class ComponentSet extends LazyCollection<MetadataComponent> {
include: resolveIncludeSet,
registry: options.registry,
});
result.forceIgnoredPaths = components.forceIgnoredPaths;
for (const component of components) {
result.add(component);
}
Expand Down
1 change: 1 addition & 0 deletions src/convert/metadataConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class MetadataConverter {
mergeSet.add(component.parent ?? component);
}
writer = new StandardWriter(output.defaultDirectory);
writer.forceIgnoredPaths = output.forceIgnoredPaths;
break;
}

Expand Down
20 changes: 17 additions & 3 deletions src/convert/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
*/
import { Archiver, create as createArchive } from 'archiver';
import { createWriteStream } from 'fs';
import { isAbsolute, join } from 'path';
import { basename, dirname, isAbsolute, join } from 'path';
import { pipeline as cbPipeline, Readable, Transform, Writable } from 'stream';
import { promisify } from 'util';
import { SourceComponent, MetadataResolver } from '../resolve';
import { SfdxFileFormat, WriteInfo, WriterFormat } from './types';
import { ensureFileExists } from '../utils/fileSystemHandler';
import { SourcePath, XML_DECL } from '../common';
import { META_XML_SUFFIX, SourcePath, XML_DECL } from '../common';
import { ConvertContext } from './convertContext';
import { MetadataTransformerFactory } from './transformers';
import { JsonMap } from '@salesforce/ts-types';
import { j2xParser } from 'fast-xml-parser';
import { ComponentSet } from '../collections';
import { LibraryError } from '../errors';
import { RegistryAccess } from '../registry';
import { Logger } from '@salesforce/core';
import { fs, Logger } from '@salesforce/core';
export const pipeline = promisify(cbPipeline);

export class ComponentReader extends Readable {
Expand Down Expand Up @@ -126,6 +126,7 @@ export class ComponentConverter extends Transform {
}

export abstract class ComponentWriter extends Writable {
public forceIgnoredPaths?: Set<string> = new Set<string>();
protected rootDestination?: SourcePath;

constructor(rootDestination?: SourcePath) {
Expand Down Expand Up @@ -158,6 +159,19 @@ export class StandardWriter extends ComponentWriter {
const fullDest = isAbsolute(info.output)
? info.output
: join(this.rootDestination, info.output);
if (!fs.fileExistsSync(fullDest)) {
for (const ignoredPath of this.forceIgnoredPaths) {
if (
dirname(ignoredPath).includes(dirname(fullDest)) &&
basename(ignoredPath).includes(basename(fullDest))
) {
return;
}
}
}
if (this.forceIgnoredPaths.has(fullDest)) {
return;
}
// if there are children, resolve each file. o/w just pick one of the files to resolve
if (toResolve.length === 0 || chunk.component.type.children) {
// This is a workaround for a server side ListViews bug where
Expand Down
1 change: 1 addition & 0 deletions src/convert/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type MergeConfig = {
* Location to store components that aren't merged.
*/
defaultDirectory: SourcePath;
forceIgnoredPaths?: Set<string>;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions src/resolve/metadataResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { MetadataType } from '../registry';
* @internal
*/
export class MetadataResolver {
public forceIgnoredPaths: Set<string>;
private forceIgnore: ForceIgnore;
private sourceAdapterFactory: SourceAdapterFactory;
private tree: TreeContainer;
Expand All @@ -33,6 +34,7 @@ export class MetadataResolver {
this.registry = registry;
this.tree = tree;
this.sourceAdapterFactory = new SourceAdapterFactory(this.registry, tree);
this.forceIgnoredPaths = new Set<string>();
}

/**
Expand Down Expand Up @@ -118,6 +120,7 @@ export class MetadataResolver {
private resolveComponent(fsPath: string, isResolvingSource: boolean): SourceComponent {
if (this.forceIgnore.denies(fsPath)) {
// don't resolve the component if the path is denied
this.forceIgnoredPaths.add(fsPath);
return;
}
const type = this.resolveType(fsPath);
Expand Down
16 changes: 16 additions & 0 deletions test/client/metadataApiRetrieve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ describe('MetadataApiRetrieve', async () => {
type: 'merge',
mergeWith: toRetrieve.getSourceComponents(),
defaultDirectory: MOCK_DEFAULT_OUTPUT,
forceIgnoredPaths: new Set<string>(),
})
).to.be.true;
});
Expand All @@ -355,6 +356,21 @@ describe('MetadataApiRetrieve', async () => {
expect(result).to.deep.equal(expected);
});

it('should construct a result object with no components when components are forceIgnored', async () => {
const toRetrieve = new ComponentSet([COMPONENT], mockRegistry);
toRetrieve.forceIgnoredPaths = new Set([COMPONENT.xml, COMPONENT.content]);
const { operation } = await stubMetadataRetrieve(env, {
toRetrieve,
merge: true,
successes: toRetrieve,
});

await operation.start();
const result = await operation.pollStatus();

expect(result.components.size).to.equal(0);
});

it('should construct a result object with no components when no components are retrieved', async () => {
const toRetrieve = new ComponentSet([COMPONENT], mockRegistry);
const { operation, response } = await stubMetadataRetrieve(env, {
Expand Down
9 changes: 8 additions & 1 deletion test/mock/client/transferOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export async function stubMetadataRetrieve(
type: 'merge',
mergeWith: retrievedComponents.getSourceComponents(),
defaultDirectory: MOCK_DEFAULT_OUTPUT,
forceIgnoredPaths: retrievedComponents.forceIgnoredPaths ?? new Set<string>(),
});
converted = source;

Expand All @@ -266,6 +267,7 @@ export async function stubMetadataRetrieve(
type: 'merge',
mergeWith: retrievedComponents.getSourceComponents(),
defaultDirectory: pkg.outputDir,
forceIgnoredPaths: retrievedComponents.forceIgnoredPaths ?? new Set<string>(),
})
);
} else {
Expand Down Expand Up @@ -297,7 +299,12 @@ export async function stubMetadataRetrieve(
}
const convertStub = sandbox.stub(MetadataConverter.prototype, 'convert');
outputConfigs.forEach((outputCfg) => {
convertStub.withArgs(match.any, 'source', outputCfg).resolves({ converted });
const notForceIgnoredConverted = converted.filter(
(component) => !retrievedComponents.forceIgnoredPaths ?? [].includes(component.xml)
);
convertStub
.withArgs(match.any, 'source', outputCfg)
.resolves({ converted: notForceIgnoredConverted });
});

return {
Expand Down

0 comments on commit af52e37

Please sign in to comment.