Skip to content

Commit

Permalink
fix(cli-generate-swagger): fix CliYaml implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Feb 23, 2023
1 parent f5129f7 commit c21626d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
23 changes: 21 additions & 2 deletions packages/cli-core/src/services/CliDockerComposeYaml.ts
Expand Up @@ -2,18 +2,37 @@ import {Inject, Injectable} from "@tsed/di";
import {snakeCase} from "change-case";
import {CliYaml} from "./CliYaml";
import {setValue} from "@tsed/core";
import {join} from "path";
import {CliFs} from "./CliFs";
import {ProjectPackageJson} from "./ProjectPackageJson";

@Injectable()
export class CliDockerComposeYaml {
@Inject()
protected cliYaml: CliYaml;

@Inject()
protected fs: CliFs;

@Inject()
protected projectPackageJson: ProjectPackageJson;

async read() {
return this.cliYaml.read("docker-compose.yml");
const path = "docker-compose.yml";
const file = !this.fs.exists(path) ? this.fs.findUpFile(this.projectPackageJson.dir, path) : path;

if (file) {
return this.cliYaml.read("docker-compose.yml");
}

return {};
}

async write(obj: any) {
return this.cliYaml.write("docker-compose.yml", obj);
const path = "docker-compose.yml";
const file = this.fs.findUpFile(this.projectPackageJson.dir, path) || join(this.projectPackageJson.dir, path);

return this.cliYaml.write(file, obj);
}

async addDatabaseService(name: string, database: string) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-core/src/services/CliFs.ts
Expand Up @@ -21,6 +21,10 @@ export class CliFs {
return this.raw.readFile(file, encoding) as any;
}

async writeJson(file: string | Buffer | number, data: any, options?: WriteFileOptions | string): Promise<any> {
await this.raw.writeFile(file, JSON.stringify(data, null, 2), options || ({encoding: "utf8"} as any));
}

writeFileSync(path: PathLike | number, data: any, options?: WriteFileOptions) {
return this.raw.writeFileSync(path, data, options);
}
Expand Down
19 changes: 3 additions & 16 deletions packages/cli-core/src/services/CliYaml.ts
@@ -1,34 +1,21 @@
import {Inject, Injectable} from "@tsed/di";
import JsYaml from "js-yaml";
import {join} from "path";
import {CliFs} from "./CliFs";
import {ProjectPackageJson} from "./ProjectPackageJson";

@Injectable()
export class CliYaml {
@Inject()
protected projectPackageJson: ProjectPackageJson;

@Inject()
protected fs: CliFs;

async read(path: string) {
const file = !this.fs.exists(path) ? this.fs.findUpFile(this.projectPackageJson.dir, path) : path;

if (file) {
const content = await this.fs.readFile(file, {encoding: "utf8"});
const content = await this.fs.readFile(path, {encoding: "utf8"});

return JsYaml.load(content);
}

return {};
return JsYaml.load(content);
}

async write(path: string, obj: any) {
const content = JsYaml.dump(obj);

const file = this.fs.findUpFile(this.projectPackageJson.dir, path) || join(this.projectPackageJson.dir, path);

return this.fs.writeFile(file, content, {encoding: "utf8"});
return this.fs.writeFile(path, content, {encoding: "utf8"});
}
}
Expand Up @@ -92,7 +92,6 @@ export class GenerateSwaggerCmd implements CommandProvider {

this.fs.ensureDirSync(path.dirname(fileJson));

this.fs.writeFile(fileJson, JSON.stringify(spec, null, 2), {encoding: "utf8"});
await this.cliYaml.write(fileYaml, spec);
await Promise.all([this.fs.writeJson(fileJson, spec), this.cliYaml.write(fileYaml, spec)]);
}
}

0 comments on commit c21626d

Please sign in to comment.