[READ-ONLY] Mirror of https://github.com/danielroe/siroc. Zero-config build tooling for Node
bundle node package rollup typescript
0

Configure Feed

Select the types of activity you want to include in your feed.

fix(core): preserve package.json indent (#211)

Co-authored-by: lihbr <lihbr@users.noreply.github.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>

authored by

Lucie
lihbr
Daniel Roe
and committed by
GitHub
(Jun 23, 2021, 8:07 PM +0100) 14a7ffc4 b0f29c61

+23 -5
+1
package.json
··· 51 51 "chalk": "^4.1.1", 52 52 "consola": "^2.15.3", 53 53 "defu": "^5.0.0", 54 + "detect-indent": "^6.0.0", 54 55 "esbuild": "^0.12.5", 55 56 "execa": "^5.0.1", 56 57 "fs-extra": "^10.0.0",
+1
yarn.lock
··· 8208 8208 consola: ^2.15.3 8209 8209 conventional-changelog-conventionalcommits: ^4.6.0 8210 8210 defu: ^5.0.0 8211 + detect-indent: ^6.0.0 8211 8212 esbuild: ^0.12.5 8212 8213 eslint: ^7.27.0 8213 8214 eslint-config-prettier: ^8.3.0
+21 -5
src/core/package/index.ts
··· 2 2 3 3 import { bold } from 'chalk' 4 4 import consola, { Consola } from 'consola' 5 + import detectIndent from 'detect-indent' 5 6 import execa, { Options } from 'execa' 6 7 import { 7 8 copy, 8 9 existsSync, 9 - readJSONSync, 10 + readFileSync, 10 11 writeFile, 11 12 mkdirp, 12 13 chmod, ··· 83 84 * Whether to sort your `package.json` on build 84 85 */ 85 86 sortDependencies?: boolean 87 + /** 88 + * The indent to use when writing `package.json` 89 + * @default ` ` 90 + */ 91 + pkgIndent?: string 86 92 } 87 93 88 94 export type SirocOptions = Partial<DefaultPackageOptions> ··· 128 134 // Basic logger 129 135 this.logger = consola 130 136 131 - this.pkg = this.loadPackageJSON() 137 + // Get `package.json` as an object and its indentation type 138 + const blob = this.loadPackageJSON(false) 139 + this.pkg = JSON.parse(blob) 140 + this.options.pkgIndent = this.options.pkgIndent || detectIndent(blob).indent 132 141 133 142 // Use tagged logger 134 143 this.logger = consola.withTag(this.pkg.name) ··· 136 145 this.loadConfig() 137 146 } 138 147 139 - loadPackageJSON(): this['pkg'] { 148 + loadPackageJSON(): this['pkg'] 149 + loadPackageJSON(parse: false): string 150 + loadPackageJSON(parse = true) { 140 151 try { 141 - return readJSONSync(this.resolvePath('package.json')) 152 + const blob = readFileSync(this.resolvePath('package.json'), 'utf8') 153 + if (parse) return JSON.parse(blob) 154 + return blob 142 155 } catch { 143 156 if (this.options.rootDir === '/') { 144 157 this.logger.error( ··· 216 229 async writePackage() { 217 230 const pkgPath = this.resolvePath('package.json') 218 231 this.logger.debug('Writing', pkgPath) 219 - await writeFile(pkgPath, JSON.stringify(this.pkg, null, 2) + '\n') 232 + await writeFile( 233 + pkgPath, 234 + JSON.stringify(this.pkg, null, this.options.pkgIndent) + '\n' 235 + ) 220 236 } 221 237 222 238 /**