diff --git a/apps/backend/scripts/generate-docs.ts b/apps/backend/scripts/generate-docs.ts index d10f1b210..d56722a51 100644 --- a/apps/backend/scripts/generate-docs.ts +++ b/apps/backend/scripts/generate-docs.ts @@ -1,9 +1,9 @@ import { parseOpenAPI, parseWebhookOpenAPI } from '@/lib/openapi'; import { isSmartRouteHandler } from '@/route-handlers/smart-route-handler'; import { webhookEvents } from '@stackframe/stack-shared/dist/interface/webhooks'; +import { writeFileSyncIfChanged } from '@stackframe/stack-shared/dist/utils/fs'; import { HTTP_METHODS } from '@stackframe/stack-shared/dist/utils/http'; import { typedKeys } from '@stackframe/stack-shared/dist/utils/objects'; -import fs from 'fs'; import { glob } from 'glob'; import path from 'path'; import yaml from 'yaml'; @@ -33,12 +33,12 @@ async function main() { }))), audience, })); - fs.writeFileSync(`../../docs/fern/openapi/${audience}.yaml`, openAPISchema); + writeFileSyncIfChanged(`../../docs/fern/openapi/${audience}.yaml`, openAPISchema); const webhookOpenAPISchema = yaml.stringify(parseWebhookOpenAPI({ webhooks: webhookEvents, })); - fs.writeFileSync(`../../docs/fern/openapi/webhooks.yaml`, webhookOpenAPISchema); + writeFileSyncIfChanged(`../../docs/fern/openapi/webhooks.yaml`, webhookOpenAPISchema); } console.log("Successfully updated docs schemas"); } diff --git a/packages/stack-shared/src/utils/fs.tsx b/packages/stack-shared/src/utils/fs.tsx index 09426c7bf..613c2cb1e 100644 --- a/packages/stack-shared/src/utils/fs.tsx +++ b/packages/stack-shared/src/utils/fs.tsx @@ -21,3 +21,13 @@ export async function listRecursively(p: string, options: { excludeDirectories?: }))).flat(), ]; } + +export function writeFileSyncIfChanged(path: string, content: string): void { + if (stackFs.existsSync(path)) { + const existingContent = stackFs.readFileSync(path, "utf-8"); + if (existingContent === content) { + return; + } + } + stackFs.writeFileSync(path, content); +} diff --git a/packages/template/scripts/merge-quetzal-translations.ts b/packages/template/scripts/merge-quetzal-translations.ts index d7aacae2e..582ec30a6 100644 --- a/packages/template/scripts/merge-quetzal-translations.ts +++ b/packages/template/scripts/merge-quetzal-translations.ts @@ -1,3 +1,4 @@ +import { writeFileSyncIfChanged } from "@stackframe/stack-shared/dist/utils/fs"; import { deindent, stringCompare } from "@stackframe/stack-shared/dist/utils/strings"; import * as fs from "fs"; import * as path from "path"; @@ -34,7 +35,7 @@ async function main() { } } - fs.writeFileSync("src/generated/quetzal-translations.ts", deindent` + writeFileSyncIfChanged("src/generated/quetzal-translations.ts", deindent` // THIS FILE IS AUTO-GENERATED BY THE \`merge-quetzal-translations.ts\` SCRIPT. // DO NOT EDIT IT BY HAND. diff --git a/packages/template/scripts/process-css.ts b/packages/template/scripts/process-css.ts index 4600f1e98..3d091f911 100644 --- a/packages/template/scripts/process-css.ts +++ b/packages/template/scripts/process-css.ts @@ -1,3 +1,4 @@ +import { writeFileSyncIfChanged } from '@stackframe/stack-shared/dist/utils/fs'; import { replaceAll } from '@stackframe/stack-shared/dist/utils/strings'; import autoprefixer from 'autoprefixer'; import * as fs from 'fs'; @@ -45,12 +46,12 @@ async function main() { } // output css file for debugging - fs.writeFileSync(outputPath.replace(/\.ts$/, '.css'), content); + writeFileSyncIfChanged(outputPath.replace(/\.ts$/, '.css'), content); content = JSON.stringify(content); content = `export const globalCSS = ${content};\n`; - fs.writeFileSync(outputPath, content); + writeFileSyncIfChanged(outputPath, content); } diff --git a/scripts/generate-from-template.ts b/scripts/generate-from-template.ts index 8cc79df78..dcf90a190 100644 --- a/scripts/generate-from-template.ts +++ b/scripts/generate-from-template.ts @@ -1,3 +1,5 @@ +// This file needs to run without any dependencies. + import fs from "fs"; import path from "path"; @@ -448,8 +450,6 @@ function writeFileSyncIfChanged(path: string, content: string): void { fs.writeFileSync(path, content); } - - // Copy package-template.json to package.json and apply macros const packageTemplateContent = fs.readFileSync(path.join(srcDir, 'package-template.json'), 'utf-8'); const processedPackageJson = processMacros(packageTemplateContent, allEnvs);