Skip to content

Commit

Permalink
feat: support detecting origin indent and newline to generate pages.j…
Browse files Browse the repository at this point in the history
…son (#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
  • Loading branch information
ModyQyW authored Jun 25, 2024
1 parent efb6e0a commit 434d8db
Show file tree
Hide file tree
Showing 9 changed files with 846 additions and 1,491 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.2.23",
"private": true,
"packageManager": "pnpm@9.1.0",
"packageManager": "pnpm@9.4.0",
"description": "Use TypeScript to write pages.json of uni-app",
"author": "KeJun",
"license": "MIT",
Expand Down Expand Up @@ -31,15 +31,15 @@
"lint:fix": "pnpm lint --fix"
},
"devDependencies": {
"@antfu/eslint-config": "^2.17.0",
"@types/node": "^20.12.11",
"@antfu/eslint-config": "^2.21.1",
"@types/node": "^20.14.8",
"@uni-helper/volar-service-uni-pages": "workspace:*",
"bumpp": "^9.4.1",
"eslint": "^8.57.0",
"rimraf": "^5.0.7",
"typescript": "^5.4.5",
"typescript": "^5.5.2",
"unbuild": "^2.0.0",
"vite": "^5.2.11",
"vite": "^5.3.1",
"vitest": "^1.6.0"
}
}
12 changes: 7 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@
"stub": "unbuild --stub"
},
"dependencies": {
"@uni-helper/uni-env": "^0.1.1",
"@vue/compiler-sfc": "^3.4.27",
"@uni-helper/uni-env": "^0.1.3",
"@vue/compiler-sfc": "^3.4.30",
"chokidar": "^3.6.0",
"debug": "^4.3.4",
"debug": "^4.3.5",
"detect-indent": "^6.1.0",
"detect-newline": "^3.1.0",
"fast-glob": "^3.3.2",
"json5": "^2.2.3",
"lodash-unified": "^1.0.3",
"magic-string": "^0.30.10",
"unconfig": "^0.3.13",
"yaml": "^2.4.2"
"yaml": "^2.4.5"
},
"devDependencies": {
"@antfu/utils": "^0.7.8",
"@types/debug": "^4.1.12",
"@types/node": "^20.12.11"
"@types/node": "^20.14.8"
}
}
19 changes: 17 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { loadConfig } from 'unconfig'
import { slash } from '@antfu/utils'
import dbg from 'debug'
import { platform } from '@uni-helper/uni-env'
import detectIndent from 'detect-indent'
import detectNewline from 'detect-newline'
import type { PagesConfig } from './config/types'
import type { PageMetaDatum, PagePath, ResolvedOptions, SubPageMetaDatum, UserOptions } from './types'
import { writeDeclaration } from './declaration'
Expand All @@ -19,7 +21,7 @@ import {
useCachedPages,
} from './utils'
import { resolveOptions } from './options'
import { checkPagesJsonFile, getPageFiles, writeFileSync } from './files'
import { checkPagesJsonFile, getPageFiles, readFileSync, writeFileSync } from './files'
import { getRouteBlock, getRouteSfcBlock } from './customBlock'
import { OUTPUT_NAME } from './constant'

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

resolvedPagesJSONPath = ''
resolvedPagesJSONIndent = ' '
resolvedPagesJSONNewline = '\n'
resolvedPagesJSONEofNewline = true

rawOptions: UserOptions
root: string
Expand All @@ -59,6 +64,10 @@ export class PageContext {
dbg.enable(`${prefix}${suffix}`)
}
this.resolvedPagesJSONPath = path.join(this.root, this.options.outDir, OUTPUT_NAME)
const resolvedPagesJSONContent = readFileSync(this.resolvedPagesJSONPath)
this.resolvedPagesJSONIndent = detectIndent(resolvedPagesJSONContent).indent || ' '
this.resolvedPagesJSONNewline = detectNewline(resolvedPagesJSONContent) || '\n'
this.resolvedPagesJSONEofNewline = (resolvedPagesJSONContent.at(-1) ?? '\n') === this.resolvedPagesJSONNewline
debug.options(this.options)
}

Expand Down Expand Up @@ -303,7 +312,13 @@ export class PageContext {
subPackages: this.subPageMetaData,
}

const pagesJson = JSON.stringify(data, null, this.options.minify ? undefined : 2)
const pagesJson = JSON.stringify(
data,
null,
this.options.minify ? undefined : this.resolvedPagesJSONIndent,
) + (
this.resolvedPagesJSONEofNewline ? this.resolvedPagesJSONNewline : ''
)
this.generateDeclaration()
if (lsatPagesJson === pagesJson) {
debug.pages('PagesJson Not have change')
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export function checkPagesJsonFile(path: string) {
return true
}

export function readFileSync(path: string) {
try {
return fs.readFileSync(path, { encoding: 'utf-8' })
}
catch {
return ''
}
}

export function writeFileSync(path: string, content: string) {
fs.writeFileSync(path, content, { encoding: 'utf-8' })
}
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@dcloudio/uni-components": "3.0.0-4010520240507001",
"@dcloudio/uni-h5": "3.0.0-4010520240507001",
"@dcloudio/uni-mp-weixin": "3.0.0-4010520240507001",
"vue": "^3.4.27",
"vue": "^3.4.30",
"vue-i18n": "^9.13.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"prepublishOnly": "pnpm generate"
},
"devDependencies": {
"ts-json-schema-generator": "^2.1.1"
"ts-json-schema-generator": "^2.3.0"
}
}
5 changes: 2 additions & 3 deletions packages/volar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
},
"dependencies": {
"@uni-helper/pages-json-schema": "workspace:^",
"ts-json-schema-generator": "^2.1.1",
"vscode-json-languageservice": "^5.3.11",
"vscode-json-languageservice": "^5.4.0",
"vscode-languageserver-textdocument": "^1.0.11",
"yaml-language-server": "^1.14.0"
"yaml-language-server": "^1.15.0"
},
"devDependencies": {
"@types/json-schema": "^7.0.15",
Expand Down
Loading

0 comments on commit 434d8db

Please sign in to comment.