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

fix syncDiff as public version used wrong directory path #92

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@kontent-ai/data-ops",
"version": "2.0.0",
"version": "2.0.1",
"description": "",
"type": "module",
"scripts": {
"build": "tsc && cp ./src/modules/sync/utils/diffTemplate.html build/src/modules/sync/utils/diffTemplate.html",
"build": "tsc",
"checkTests": "tsc --project tsconfig.tests.jsonc",
"lint": "eslint . --ext ts,tsx",
"fmt": "dprint fmt",
Expand Down
6 changes: 2 additions & 4 deletions src/modules/sync/diffEnvironments.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chalk from "chalk";
import { resolve } from "path";

import { logInfo, LogOptions } from "../../log.js";
import { createClient } from "../../utils/client.js";
import { Replace } from "../../utils/types.js";
import { syncEntityChoices, syncEntityDependencies, SyncEntityName } from "./constants/entities.js";
import { diff } from "./diff.js";
import { readHtmlFile } from "./utils/fileUtils.js";
import { diffHtmlTemplate } from "./utils/diffTemplateHtml.js";
import {
fetchSourceSyncModel,
getSourceItemAndAssetCodenames,
Expand Down Expand Up @@ -40,9 +39,8 @@ export type SyncDiffParamsIntenal = Replace<SyncDiffParams, { entities: Readonly
export const syncDiff = async (params: SyncDiffParams) => {
const resolvedParams = { ...params, entities: params.entities ?? syncEntityChoices };
const diffModel = await syncDiffInternal(resolvedParams, "diff-API");
const templateString = readHtmlFile(resolve(import.meta.dirname, "./utils/diffTemplate.html"));

return resolveHtmlTemplate(templateString, { ...diffModel, ...resolvedParams });
return resolveHtmlTemplate(diffHtmlTemplate, { ...diffModel, ...resolvedParams });
};

export const syncDiffInternal = async (params: SyncDiffParamsIntenal, commandName: string) => {
Expand Down
14 changes: 4 additions & 10 deletions src/modules/sync/printDiff.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import chalk from "chalk";
import { existsSync } from "fs";
import { dirname, resolve } from "path";
import { dirname } from "path";
import { match } from "ts-pattern";

import { logInfo, LogOptions } from "../../log.js";
import { SyncEntityName } from "./constants/entities.js";
import { DiffModel, DiffObject } from "./types/diffModel.js";
import { PatchOperation } from "./types/patchOperation.js";
import {
createOutputDirectory,
createOutputFile,
openOutputFile,
readHtmlFile,
resolveOutputPath,
} from "./utils/fileUtils.js";
import { diffHtmlTemplate } from "./utils/diffTemplateHtml.js";
import { createOutputDirectory, createOutputFile, openOutputFile, resolveOutputPath } from "./utils/fileUtils.js";
import { DiffData, resolveHtmlTemplate } from "./utils/htmlRenderers.js";

export const printDiff = (
Expand Down Expand Up @@ -130,8 +125,7 @@ const printDiffEntity = (
export const createAdvancedDiffFile = (diffData: DiffData) => {
const logOptions: LogOptions = diffData;
const resolvedPath = diffData.outPath ? resolveOutputPath(diffData.outPath) : false;
const templateString = readHtmlFile(resolve(import.meta.dirname, "./utils/diffTemplate.html"));
const resolvedTemplate = resolveHtmlTemplate(templateString, diffData);
const resolvedTemplate = resolveHtmlTemplate(diffHtmlTemplate, diffData);

if (!resolvedPath) {
throw new Error(`Output path not specified.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
export const diffHtmlTemplate = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -372,4 +372,4 @@ <h2>Modified entities</h2>
};
</script>
</body>
</html>
</html>`;
23 changes: 5 additions & 18 deletions src/modules/sync/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@ import * as path from "path";

import { logError, logInfo, LogOptions } from "../../../log.js";

const { resolve } = path;
const { mkdirSync, readFileSync, writeFileSync } = fs;

export const readHtmlFile = (templatePath: string): string => {
try {
return readFileSync(resolve(templatePath), "utf-8");
} catch (err) {
throw new Error(
`Failed reading a file at '${templatePath}': ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`,
);
}
};

export const resolveOutputPath = (outputPath: string) => {
const hasExtension = resolve(outputPath).includes(".");
const hasExtension = path.resolve(outputPath).includes(".");

return hasExtension
? resolve(outputPath)
: resolve(
? path.resolve(outputPath)
: path.resolve(
outputPath,
`diff_${new Date().toISOString().replace(/[:.-]/g, "_")}.html`,
);
Expand All @@ -36,7 +23,7 @@ export const createOutputFile = (path: string, content: string, logOptions: LogO
"standard",
chalk.yellow(`Generating a diff file at ${path}`),
);
writeFileSync(path, content);
fs.writeFileSync(path, content);
} catch (err) {
throw new Error(`Failed writing a diff file: ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`);
}
Expand All @@ -63,7 +50,7 @@ export const createOutputDirectory = (path: string, logOptions: LogOptions) => {
"standard",
chalk.yellow(`Creating a directory '${path}'`),
);
mkdirSync(path, { recursive: true });
fs.mkdirSync(path, { recursive: true });
} catch (err) {
throw new Error(
`Failed to create directory '${path}': ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`,
Expand Down