Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow copied files to be mapped prior to writing #568

Merged
merged 2 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/components/source-repositories/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export const sourceRepositoryRootDirectory = 'src';
export interface SourceRepositoryDefinition {
title: string;
}
export interface BasicFile {
path: string;
content: any;
}
const NoOpFileMap = (file: BasicFile) => file;

export class SourceRepository extends Component {
public static BUNDLE_PATH = sourceRepositoryRootDirectory;
Expand Down Expand Up @@ -60,7 +65,15 @@ export class SourceRepository extends Component {
* @returns
*/
substitute?: { [key: string]: string };

/**
* Allows iteration across all copied files. This is run after substitutions and final path resolution and prior to initializing the file object.
* @param file
* @returns file
*/
map?: (file: BasicFile) => BasicFile;
}) {
const fileMapper = options?.map || NoOpFileMap;
let from = options?.from ? path.join('', options?.from) : '';
if (from === '.') {
from = '';
Expand All @@ -75,9 +88,17 @@ export class SourceRepository extends Component {
for (const asset of SubstitionAsset.findAll(fromGlob)) {
const relativeLocation = asset.path().replace(from, '');
if (isBinary(asset.content())) {
new File(this, path.join(to, relativeLocation), asset.content());
const mapped = fileMapper({
path: path.join(to, relativeLocation),
content: asset.content(),
});
new File(this, mapped.path, mapped.content);
} else {
new SourceFile(this, path.join(to, relativeLocation), options?.substitute ? asset.substitute(options.substitute) : asset.toString());
const mapped = fileMapper({
path: path.join(to, relativeLocation),
content: options?.substitute ? asset.substitute(options.substitute) : asset.toString(),
});
new SourceFile(this, mapped.path, mapped.content);
}
}
}
Expand Down
Loading