Skip to content

Commit 434d8db

Browse files
authored
feat: support detecting origin indent and newline to generate pages.json (#170)
* chore: add detect-indent and detect-newline packages * feat: support using origin file style * fix: returns an empty string as the file content if the file is not found * test: update inline snapshot * fix: set eof by default * chore: update deps * fix: fix import * chore: update deps
1 parent efb6e0a commit 434d8db

File tree

9 files changed

+846
-1491
lines changed

9 files changed

+846
-1491
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"version": "0.2.23",
55
"private": true,
6-
"packageManager": "pnpm@9.1.0",
6+
"packageManager": "pnpm@9.4.0",
77
"description": "Use TypeScript to write pages.json of uni-app",
88
"author": "KeJun",
99
"license": "MIT",
@@ -31,15 +31,15 @@
3131
"lint:fix": "pnpm lint --fix"
3232
},
3333
"devDependencies": {
34-
"@antfu/eslint-config": "^2.17.0",
35-
"@types/node": "^20.12.11",
34+
"@antfu/eslint-config": "^2.21.1",
35+
"@types/node": "^20.14.8",
3636
"@uni-helper/volar-service-uni-pages": "workspace:*",
3737
"bumpp": "^9.4.1",
3838
"eslint": "^8.57.0",
3939
"rimraf": "^5.0.7",
40-
"typescript": "^5.4.5",
40+
"typescript": "^5.5.2",
4141
"unbuild": "^2.0.0",
42-
"vite": "^5.2.11",
42+
"vite": "^5.3.1",
4343
"vitest": "^1.6.0"
4444
}
4545
}

packages/core/package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@
4040
"stub": "unbuild --stub"
4141
},
4242
"dependencies": {
43-
"@uni-helper/uni-env": "^0.1.1",
44-
"@vue/compiler-sfc": "^3.4.27",
43+
"@uni-helper/uni-env": "^0.1.3",
44+
"@vue/compiler-sfc": "^3.4.30",
4545
"chokidar": "^3.6.0",
46-
"debug": "^4.3.4",
46+
"debug": "^4.3.5",
47+
"detect-indent": "^6.1.0",
48+
"detect-newline": "^3.1.0",
4749
"fast-glob": "^3.3.2",
4850
"json5": "^2.2.3",
4951
"lodash-unified": "^1.0.3",
5052
"magic-string": "^0.30.10",
5153
"unconfig": "^0.3.13",
52-
"yaml": "^2.4.2"
54+
"yaml": "^2.4.5"
5355
},
5456
"devDependencies": {
5557
"@antfu/utils": "^0.7.8",
5658
"@types/debug": "^4.1.12",
57-
"@types/node": "^20.12.11"
59+
"@types/node": "^20.14.8"
5860
}
5961
}

packages/core/src/context.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { loadConfig } from 'unconfig'
77
import { slash } from '@antfu/utils'
88
import dbg from 'debug'
99
import { platform } from '@uni-helper/uni-env'
10+
import detectIndent from 'detect-indent'
11+
import detectNewline from 'detect-newline'
1012
import type { PagesConfig } from './config/types'
1113
import type { PageMetaDatum, PagePath, ResolvedOptions, SubPageMetaDatum, UserOptions } from './types'
1214
import { writeDeclaration } from './declaration'
@@ -19,7 +21,7 @@ import {
1921
useCachedPages,
2022
} from './utils'
2123
import { resolveOptions } from './options'
22-
import { checkPagesJsonFile, getPageFiles, writeFileSync } from './files'
24+
import { checkPagesJsonFile, getPageFiles, readFileSync, writeFileSync } from './files'
2325
import { getRouteBlock, getRouteSfcBlock } from './customBlock'
2426
import { OUTPUT_NAME } from './constant'
2527

@@ -38,6 +40,9 @@ export class PageContext {
3840
subPageMetaData: SubPageMetaDatum[] = []
3941

4042
resolvedPagesJSONPath = ''
43+
resolvedPagesJSONIndent = ' '
44+
resolvedPagesJSONNewline = '\n'
45+
resolvedPagesJSONEofNewline = true
4146

4247
rawOptions: UserOptions
4348
root: string
@@ -59,6 +64,10 @@ export class PageContext {
5964
dbg.enable(`${prefix}${suffix}`)
6065
}
6166
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME)
67+
const resolvedPagesJSONContent = readFileSync(this.resolvedPagesJSONPath)
68+
this.resolvedPagesJSONIndent = detectIndent(resolvedPagesJSONContent).indent || ' '
69+
this.resolvedPagesJSONNewline = detectNewline(resolvedPagesJSONContent) || '\n'
70+
this.resolvedPagesJSONEofNewline = (resolvedPagesJSONContent.at(-1) ?? '\n') === this.resolvedPagesJSONNewline
6271
debug.options(this.options)
6372
}
6473

@@ -303,7 +312,13 @@ export class PageContext {
303312
subPackages: this.subPageMetaData,
304313
}
305314

306-
const pagesJson = JSON.stringify(data, null, this.options.minify ? undefined : 2)
315+
const pagesJson = JSON.stringify(
316+
data,
317+
null,
318+
this.options.minify ? undefined : this.resolvedPagesJSONIndent,
319+
) + (
320+
this.resolvedPagesJSONEofNewline ? this.resolvedPagesJSONNewline : ''
321+
)
307322
this.generateDeclaration()
308323
if (lsatPagesJson === pagesJson) {
309324
debug.pages('PagesJson Not have change')

packages/core/src/files.ts

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export function checkPagesJsonFile(path: string) {
3030
return true
3131
}
3232

33+
export function readFileSync(path: string) {
34+
try {
35+
return fs.readFileSync(path, { encoding: 'utf-8' })
36+
}
37+
catch {
38+
return ''
39+
}
40+
}
41+
3342
export function writeFileSync(path: string, content: string) {
3443
fs.writeFileSync(path, content, { encoding: 'utf-8' })
3544
}

packages/playground/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@dcloudio/uni-components": "3.0.0-4010520240507001",
1414
"@dcloudio/uni-h5": "3.0.0-4010520240507001",
1515
"@dcloudio/uni-mp-weixin": "3.0.0-4010520240507001",
16-
"vue": "^3.4.27",
16+
"vue": "^3.4.30",
1717
"vue-i18n": "^9.13.1"
1818
},
1919
"devDependencies": {

packages/schema/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
"prepublishOnly": "pnpm generate"
1919
},
2020
"devDependencies": {
21-
"ts-json-schema-generator": "^2.1.1"
21+
"ts-json-schema-generator": "^2.3.0"
2222
}
2323
}

packages/volar/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@
4141
},
4242
"dependencies": {
4343
"@uni-helper/pages-json-schema": "workspace:^",
44-
"ts-json-schema-generator": "^2.1.1",
45-
"vscode-json-languageservice": "^5.3.11",
44+
"vscode-json-languageservice": "^5.4.0",
4645
"vscode-languageserver-textdocument": "^1.0.11",
47-
"yaml-language-server": "^1.14.0"
46+
"yaml-language-server": "^1.15.0"
4847
},
4948
"devDependencies": {
5049
"@types/json-schema": "^7.0.15",

0 commit comments

Comments
 (0)