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
5 changed files
with
131 additions
and
27 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
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,41 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import * as path from 'path'; | ||
import { VirtualTreeContainer, VirtualDirectory } from '@salesforce/source-deploy-retrieve'; | ||
|
||
/** | ||
* Designed for recreating virtual files from deleted files where the only information we have is the file's former location | ||
* Any use of MetadataResolver was trying to access the non-existent files and throwing | ||
* | ||
* @param filenames full paths to files | ||
* @returns VirtualTreeContainer to use with MetadataResolver | ||
*/ | ||
export const filenamesToVirtualTree = (filenames: string[]): VirtualTreeContainer => { | ||
const virtualDirectoryByFullPath = new Map<string, VirtualDirectory>(); | ||
filenames.map((filename) => { | ||
const splits = filename.split(path.sep); | ||
for (let i = 0; i < splits.length - 1; i++) { | ||
const fullPathSoFar = splits.slice(0, i + 1).join(path.sep); | ||
if (virtualDirectoryByFullPath.has(fullPathSoFar)) { | ||
const existing = virtualDirectoryByFullPath.get(fullPathSoFar) as VirtualDirectory; | ||
// only add to children if we don't already have it | ||
if (!existing.children.includes(splits[i + 1])) { | ||
virtualDirectoryByFullPath.set(fullPathSoFar, { | ||
dirPath: existing.dirPath, | ||
children: [...existing.children, splits[i + 1]], | ||
}); | ||
} | ||
} else { | ||
virtualDirectoryByFullPath.set(fullPathSoFar, { | ||
dirPath: fullPathSoFar, | ||
children: [splits[i + 1]], | ||
}); | ||
} | ||
} | ||
}); | ||
return new VirtualTreeContainer(Array.from(virtualDirectoryByFullPath.values())); | ||
}; |
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,37 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/* eslint-disable no-console */ | ||
|
||
import { expect } from 'chai'; | ||
import { MetadataResolver } from '@salesforce/source-deploy-retrieve'; | ||
import { filenamesToVirtualTree } from '../../src/shared/filenamesToVirtualTree'; | ||
|
||
describe('two deleted files from an apex class', () => { | ||
const tree = filenamesToVirtualTree([ | ||
'force-app/main/default/classes/TestOrderController.cls', | ||
'force-app/main/default/classes/TestOrderController.cls-meta.xml', | ||
]); | ||
|
||
it('tree has expected structure', () => { | ||
expect(tree.isDirectory('force-app'), 'force-app').to.equal(true); | ||
expect(tree.isDirectory('force-app/main'), 'force-app/main').to.equal(true); | ||
expect(tree.isDirectory('force-app/main/default'), 'force-app/main/default').to.equal(true); | ||
expect(tree.isDirectory('force-app/main/default/classes'), 'force-app/main/default/classes').to.equal(true); | ||
expect(tree.readDirectory('force-app/main/default/classes')).to.deep.equal([ | ||
'TestOrderController.cls', | ||
'TestOrderController.cls-meta.xml', | ||
]); | ||
}); | ||
|
||
it('tree resolves to a class', () => { | ||
const resolver = new MetadataResolver(undefined, tree); | ||
const resolved = resolver.getComponentsFromPath('force-app'); | ||
expect(resolved.length).to.equal(1); | ||
expect(resolved[0].type.name).to.equal('ApexClass'); | ||
}); | ||
}); |