Skip to content

Commit 3d5f006

Browse files
committed
Move cp for static files from system to FileSystemWriter
1 parent fb0f10b commit 3d5f006

7 files changed

Lines changed: 395 additions & 7 deletions

File tree

src/api/writer-generator/csharp/csharp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class CSharp extends Writer<CSharpGeneratorOptions> {
107107
tabSize: 4,
108108
withDebugComment: false,
109109
commentLinePrefix: "//",
110+
resolveAssets: options.resolveAssets ?? resolveCSharpAssets,
110111
...options,
111112
});
112113
}
@@ -357,9 +358,8 @@ export class CSharp extends Writer<CSharpGeneratorOptions> {
357358
}
358359

359360
private copyStaticFiles(): void {
360-
if (this.opts.inMemoryOnly) return;
361-
const sourceDir = resolveCSharpAssets("");
362-
fs.cpSync(sourceDir, this.opts.outputDir, { recursive: true });
361+
this.cp("Client.cs", "Client.cs");
362+
this.cp("Helper.cs", "Helper.cs");
363363
}
364364

365365
private generateHelperFile(): void {

src/api/writer-generator/python.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
150150
private tsIndex: TypeSchemaIndex | undefined;
151151

152152
constructor(options: PythonGeneratorOptions) {
153-
super(options);
153+
super({ ...options, resolveAssets: options.resolveAssets ?? resolvePyAssets });
154154
this.nameFormatFunction = this.getFieldFormatFunction(options.fieldFormat);
155155
}
156156

@@ -166,7 +166,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
166166

167167
private generateRootPackages(groups: TypeSchemaPackageGroups): void {
168168
this.generateRootInitFile(groups);
169-
fs.cpSync(resolvePyAssets("requirements.txt"), Path.resolve(this.opts.outputDir, "requirements.txt"));
169+
this.cp(resolvePyAssets("requirements.txt"), "requirements.txt");
170170
}
171171

172172
private generateSDKPackages(groups: TypeSchemaPackageGroups): void {

src/api/writer-generator/writer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type FileSystemWriterOptions = {
77
outputDir: string;
88
inMemoryOnly?: boolean;
99
logger?: CodegenLogger;
10+
resolveAssets?: (fn: string) => string;
1011
};
1112

1213
export type WriterOptions = FileSystemWriterOptions & {
@@ -104,6 +105,19 @@ export abstract class FileSystemWriter<T extends FileSystemWriterOptions = FileS
104105
buf.tokens.push(str);
105106
}
106107

108+
cp(source: string, destination: string) {
109+
if (!this.opts.resolveAssets) throw new Error("resolveAssets is not defined");
110+
source = Path.resolve(this.opts.resolveAssets(source));
111+
destination = Path.normalize(`${this.currentDir ?? this.opts.outputDir}/${destination}`);
112+
const content = fs.readFileSync(source, "utf8");
113+
this.writtenFilesBuffer[destination] = {
114+
relPath: destination,
115+
absPath: Path.resolve(destination),
116+
tokens: [content],
117+
};
118+
fs.cpSync(source, destination);
119+
}
120+
107121
abstract generate(_tsIndex: TypeSchemaIndex): Promise<void>;
108122

109123
writtenFiles(): FileBuffer[] {

0 commit comments

Comments
 (0)