Skip to content

Commit

Permalink
fix(core): preserve package.json indent (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: lihbr <lihbr@users.noreply.github.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
3 people authored Jun 23, 2021
1 parent b0f29c6 commit 14a7ffc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"chalk": "^4.1.1",
"consola": "^2.15.3",
"defu": "^5.0.0",
"detect-indent": "^6.0.0",
"esbuild": "^0.12.5",
"execa": "^5.0.1",
"fs-extra": "^10.0.0",
Expand Down
26 changes: 21 additions & 5 deletions src/core/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { dirname, relative, resolve } from 'upath'

import { bold } from 'chalk'
import consola, { Consola } from 'consola'
import detectIndent from 'detect-indent'
import execa, { Options } from 'execa'
import {
copy,
existsSync,
readJSONSync,
readFileSync,
writeFile,
mkdirp,
chmod,
Expand Down Expand Up @@ -83,6 +84,11 @@ export interface DefaultPackageOptions {
* Whether to sort your `package.json` on build
*/
sortDependencies?: boolean
/**
* The indent to use when writing `package.json`
* @default ` `
*/
pkgIndent?: string
}

export type SirocOptions = Partial<DefaultPackageOptions>
Expand Down Expand Up @@ -128,17 +134,24 @@ export class Package {
// Basic logger
this.logger = consola

this.pkg = this.loadPackageJSON()
// Get `package.json` as an object and its indentation type
const blob = this.loadPackageJSON(false)
this.pkg = JSON.parse(blob)
this.options.pkgIndent = this.options.pkgIndent || detectIndent(blob).indent

// Use tagged logger
this.logger = consola.withTag(this.pkg.name)

this.loadConfig()
}

loadPackageJSON(): this['pkg'] {
loadPackageJSON(): this['pkg']
loadPackageJSON(parse: false): string
loadPackageJSON(parse = true) {
try {
return readJSONSync(this.resolvePath('package.json'))
const blob = readFileSync(this.resolvePath('package.json'), 'utf8')
if (parse) return JSON.parse(blob)
return blob
} catch {
if (this.options.rootDir === '/') {
this.logger.error(
Expand Down Expand Up @@ -216,7 +229,10 @@ export class Package {
async writePackage() {
const pkgPath = this.resolvePath('package.json')
this.logger.debug('Writing', pkgPath)
await writeFile(pkgPath, JSON.stringify(this.pkg, null, 2) + '\n')
await writeFile(
pkgPath,
JSON.stringify(this.pkg, null, this.options.pkgIndent) + '\n'
)
}

/**
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8208,6 +8208,7 @@ __metadata:
consola: ^2.15.3
conventional-changelog-conventionalcommits: ^4.6.0
defu: ^5.0.0
detect-indent: ^6.0.0
esbuild: ^0.12.5
eslint: ^7.27.0
eslint-config-prettier: ^8.3.0
Expand Down

0 comments on commit 14a7ffc

Please sign in to comment.