update-package-json.ts 872 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { readFileSync, writeFileSync } from 'fs'
  5. import { join } from 'path'
  6. interface Package {
  7. name?: string
  8. scripts?: Record<string, string>
  9. }
  10. export function updatePackageJson(
  11. appDirectory: string,
  12. appName: string,
  13. recipeShortName: string
  14. ): void {
  15. const pkgPath = join(appDirectory, 'package.json')
  16. const pkgString = readFileSync(pkgPath, 'utf8')
  17. const pkg = JSON.parse(pkgString) as Package
  18. const outputPkg = {
  19. ...pkg,
  20. name: appName,
  21. scripts: {
  22. ...pkg.scripts,
  23. start: `${recipeShortName === 'cra' ? 'cross-env BROWSER=none ' : ''}${
  24. pkg.scripts?.start as string
  25. }`,
  26. tauri: 'tauri'
  27. }
  28. }
  29. writeFileSync(pkgPath, JSON.stringify(outputPkg, undefined, 2))
  30. }