Skip to content

Commit

Permalink
feat(app-webpack): preserve JSON config file formatting when updating (
Browse files Browse the repository at this point in the history
…fix: quasarframework#16808)

ported from app-vite quasarframework#17754
  • Loading branch information
yusufkandemir committed Jan 17, 2025
1 parent d07b4fe commit 7eb866c
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 193 deletions.
2 changes: 1 addition & 1 deletion app-vite/lib/app-extension/api-classes/InstallAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import fs from 'fs-extra'
import path from 'node:path'
import { merge } from 'webpack-merge'
import semver from 'semver'
import { parseJSON, stringifyJSON } from 'confbox'

import { warn, fatal } from '../../utils/logger.js'
import { getPackageJson } from '../../utils/get-package-json.js'
import { getCallerPath } from '../../utils/get-caller-path.js'
import { BaseAPI } from './BaseAPI.js'
import { parseJSON, stringifyJSON } from 'confbox'

/**
* API for extension's /install.js script
Expand Down
2 changes: 1 addition & 1 deletion app-vite/lib/modes/ssr/ssr-builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'node:path'
import { writeFileSync } from 'node:fs'
import { merge } from 'webpack-merge'
import { stringifyJSON } from 'confbox'

import { AppBuilder } from '../../app-builder.js'
import { quasarSsrConfig } from './ssr-config.js'
Expand All @@ -9,7 +10,6 @@ import { getFixedDeps } from '../../utils/get-fixed-deps.js'
import { getProdSsrTemplateFn, transformProdSsrPwaOfflineHtml } from '../../utils/html-template.js'

import { injectPwaManifest, buildPwaServiceWorker } from '../pwa/utils.js'
import { stringifyJSON } from 'confbox'

export class QuasarModeBuilder extends AppBuilder {
async build () {
Expand Down
2 changes: 1 addition & 1 deletion app-vite/lib/utils/get-pkg.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync, statSync } from 'node:fs'
import { parseJSON } from 'confbox'

import { warning } from './logger.js'
import { getPackageJson } from '../utils/get-package-json.js'
import { parseJSON } from 'confbox'

export function getPkg (appPaths) {
const { appDir, cliDir } = appPaths
Expand Down
10 changes: 7 additions & 3 deletions app-webpack/lib/app-extension/api-classes/InstallAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs-extra')
const path = require('node:path')
const { merge } = require('webpack-merge')
const semver = require('semver')
const { stringifyJSON, parseJSON } = require('confbox')

const { warn, fatal } = require('../../utils/logger.js')
const { getPackageJson } = require('../../utils/get-package-json.js')
Expand Down Expand Up @@ -177,7 +178,7 @@ module.exports.InstallAPI = class InstallAPI extends BaseAPI {

fs.writeFileSync(
this.resolve.app('package.json'),
JSON.stringify(pkg, null, 2),
stringifyJSON(pkg),
'utf-8'
)

Expand Down Expand Up @@ -208,12 +209,15 @@ module.exports.InstallAPI = class InstallAPI extends BaseAPI {
// which usually means we are dealing with an extended JSON flavour,
// for example JSON with comments or JSON5.
// Notable examples are TS 'tsconfig.json' or VSCode 'settings.json'
// TODO: use parseJSONC/stringifyJSONC from confbox
try {
const data = merge({}, fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : {}, newData)
const existingData = fs.existsSync(filePath) ? parseJSON(fs.readFileSync(filePath, 'utf-8')) : {}
const data = merge({}, existingData, newData)

fs.writeFileSync(
this.resolve.app(file),
JSON.stringify(data, null, 2),
// if file exists, preserve indentation, otherwise use 2 spaces
stringifyJSON(data, { indent: Object.keys(existingData).length > 0 ? undefined : 2 }),
'utf-8'
)
}
Expand Down
6 changes: 4 additions & 2 deletions app-webpack/lib/app-extension/create-app-ext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { existsSync, readFileSync, writeFileSync } = require('node:fs')
const { merge } = require('webpack-merge')
const { parseJSON, stringifyJSON } = require('confbox')

const { log, fatal } = require('../utils/logger.js')
const { AppExtensionInstance } = require('./AppExtensionInstance.js')
Expand All @@ -10,7 +11,7 @@ function readJson (file) {
}

try {
return JSON.parse(
return parseJSON(
readFileSync(file, 'utf-8')
)
}
Expand All @@ -24,7 +25,8 @@ function getAppExtJson ({ file, json, onListUpdate }) {
function save () {
writeFileSync(
file,
JSON.stringify(json, null, 2),
// if file exists, preserve indentation, otherwise use 2 spaces
stringifyJSON(json, { indent: fileExists ? undefined : 2 }),
'utf-8'
)
}
Expand Down
12 changes: 7 additions & 5 deletions app-webpack/lib/modes/capacitor/config-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('node:fs')
const { basename, dirname, join } = require('node:path')
const { globSync } = require('tinyglobby')
const { stringifyJSON, parseJSON } = require('confbox')

const { log, warn } = require('../../utils/logger.js')
const { ensureConsistency } = require('./ensure-consistency.js')
Expand Down Expand Up @@ -74,8 +75,9 @@ module.exports.CapacitorConfigFile = class CapacitorConfigFile {

this.#tamperedFiles = []

// TODO: support other formats: .js and .ts
const capJsonPath = appPaths.resolve.capacitor('capacitor.config.json')
const capJson = JSON.parse(
const capJson = parseJSON(
fs.readFileSync(capJsonPath, 'utf-8')
)

Expand All @@ -85,7 +87,7 @@ module.exports.CapacitorConfigFile = class CapacitorConfigFile {
path: capJsonPath,
name: 'capacitor.config.json',
content: this.#updateCapJson(quasarConf, capJson, capVersion, target),
originalContent: JSON.stringify(capJson, null, 2)
originalContent: stringifyJSON(capJson)
})

this.#save()
Expand Down Expand Up @@ -136,7 +138,7 @@ module.exports.CapacitorConfigFile = class CapacitorConfigFile {
}
}

return JSON.stringify(capJson, null, 2)
return stringifyJSON(capJson)
}

#updateCapPkg (quasarConf) {
Expand All @@ -146,7 +148,7 @@ module.exports.CapacitorConfigFile = class CapacitorConfigFile {
} = this.#ctx

const capPkgPath = appPaths.resolve.capacitor('package.json')
const capPkg = JSON.parse(
const capPkg = parseJSON(
fs.readFileSync(capPkgPath, 'utf-8')
)

Expand All @@ -157,7 +159,7 @@ module.exports.CapacitorConfigFile = class CapacitorConfigFile {
author: appPkg.author
})

fs.writeFileSync(capPkgPath, JSON.stringify(capPkg, null, 2), 'utf-8')
fs.writeFileSync(capPkgPath, stringifyJSON(capPkg), 'utf-8')
}

#updateSSL (quasarConf, target, capVersion) {
Expand Down
3 changes: 2 additions & 1 deletion app-webpack/lib/modes/ssr/ssr-builder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { merge } = require('webpack-merge')
const { stringifyJSON } = require('confbox')

const { AppBuilder } = require('../../app-builder.js')
const { quasarSsrConfig } = require('./ssr-config.js')
Expand Down Expand Up @@ -92,7 +93,7 @@ module.exports.QuasarModeBuilder = class QuasarModeBuilder extends AppBuilder {
this.quasarConf.ssr.extendPackageJson(pkg)
}

this.writeFile('package.json', JSON.stringify(pkg, null, 2))
this.writeFile('package.json', stringifyJSON(pkg, { indent: 2 }))
}

async #writeRenderTemplate () {
Expand Down
4 changes: 3 additions & 1 deletion app-webpack/lib/utils/get-pkg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { readFileSync, statSync } = require('node:fs')
const { parseJSON } = require('confbox')

const { warning } = require('./logger.js')
const { getPackageJson } = require('../utils/get-package-json.js')
Expand All @@ -16,7 +17,8 @@ module.exports.getPkg = function getPkg (appPaths) {
if (mtime !== lastAppPkgModifiedTime) {
lastAppPkgModifiedTime = mtime
try {
appPkg = JSON.parse(
// This may get updated and written, so use parseJSON to preserve formatting
appPkg = parseJSON(
readFileSync(appPkgPath, 'utf-8')
)
}
Expand Down
1 change: 1 addition & 0 deletions app-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"chokidar": "^4.0.0",
"ci-info": "^4.0.0",
"compression-webpack-plugin": "^11.1.0",
"confbox": "^0.1.8",
"copy-webpack-plugin": "^12.0.2",
"cross-spawn": "^7.0.6",
"css-loader": "^7.1.2",
Expand Down
Loading

0 comments on commit 7eb866c

Please sign in to comment.