diff --git a/package-lock.json b/package-lock.json index dbaea87e..da269fe4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kontent-ai/data-ops", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@kontent-ai/data-ops", - "version": "2.0.0", + "version": "2.0.1", "license": "MIT", "dependencies": { "@kontent-ai/core-sdk": "^10.8.0", diff --git a/package.json b/package.json index b666273a..152dc792 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/modules/sync/diffEnvironments.ts b/src/modules/sync/diffEnvironments.ts index 6a53a0eb..a82f216b 100644 --- a/src/modules/sync/diffEnvironments.ts +++ b/src/modules/sync/diffEnvironments.ts @@ -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, @@ -40,9 +39,8 @@ export type SyncDiffParamsIntenal = Replace { 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) => { diff --git a/src/modules/sync/printDiff.ts b/src/modules/sync/printDiff.ts index 2ba198bb..27471dea 100644 --- a/src/modules/sync/printDiff.ts +++ b/src/modules/sync/printDiff.ts @@ -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 = ( @@ -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.`); diff --git a/src/modules/sync/utils/diffTemplate.html b/src/modules/sync/utils/diffTemplateHtml.ts similarity index 99% rename from src/modules/sync/utils/diffTemplate.html rename to src/modules/sync/utils/diffTemplateHtml.ts index 11d3a74b..5c6ce110 100644 --- a/src/modules/sync/utils/diffTemplate.html +++ b/src/modules/sync/utils/diffTemplateHtml.ts @@ -1,4 +1,4 @@ - +export const diffHtmlTemplate = ` @@ -372,4 +372,4 @@

Modified entities

}; - +`; diff --git a/src/modules/sync/utils/fileUtils.ts b/src/modules/sync/utils/fileUtils.ts index fa892562..5d9007fe 100644 --- a/src/modules/sync/utils/fileUtils.ts +++ b/src/modules/sync/utils/fileUtils.ts @@ -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`, ); @@ -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))}`); } @@ -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))}`,