generated from hemengke1997/ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
autoDetectI18nConfig
option
1. autoDetectI18nConfig is enable default. If enable, plugin will find up vscode settings until reach `stopAt` dir. 2. deprecate `dotVscodePath` because it seems useless. Now use `root` instead of `dotVscodePath`
- Loading branch information
1 parent
cabd91d
commit 9ccaa91
Showing
12 changed files
with
137 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { findUpSync } from 'find-up' | ||
import JSON5 from 'json5' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { debug } from './debugger' | ||
|
||
export class I18nAllyVscodeSetting { | ||
static I18N_ALLY_KEY = 'i18n-ally.' | ||
static SETTING_FILE = '.vscode/settings.json' | ||
public i18nConfig: Record<string, any> | undefined | ||
|
||
constructor( | ||
private readonly _root: string, | ||
private readonly _stopAt: string = _root, | ||
) { | ||
debug('I18nAllyVscodeSetting - root:', _root) | ||
debug('I18nAllyVscodeSetting - stopAt:', _stopAt) | ||
} | ||
|
||
findUp() { | ||
const settingFile = findUpSync(I18nAllyVscodeSetting.SETTING_FILE, { | ||
type: 'file', | ||
cwd: this._root, | ||
stopAt: this._stopAt, | ||
}) | ||
|
||
debug('findup - settingFile:', settingFile) | ||
return settingFile | ||
} | ||
|
||
init() { | ||
const settings = this.readJsonFile(this.findUp()) | ||
|
||
if (settings) { | ||
const removePrefix = (key: string) => key.replace(I18nAllyVscodeSetting.I18N_ALLY_KEY, '') | ||
const filteredConfig = Object.keys(settings) | ||
.filter((key) => key.startsWith(I18nAllyVscodeSetting.I18N_ALLY_KEY)) | ||
.reduce( | ||
(obj, key) => { | ||
if (key === `${I18nAllyVscodeSetting.I18N_ALLY_KEY}localesPaths`) { | ||
if (Array.isArray(settings[key])) { | ||
obj[removePrefix(key)] = settings[key].map((p: string) => path.resolve(this._root, p)) | ||
} | ||
} else { | ||
obj[removePrefix(key)] = settings[key] | ||
} | ||
return obj | ||
}, | ||
{} as Record<string, any>, | ||
) | ||
if (Object.keys(filteredConfig).length) { | ||
this.i18nConfig = filteredConfig | ||
} | ||
} | ||
return this.i18nConfig | ||
} | ||
|
||
readJsonFile(filePath: string | undefined) { | ||
if (filePath) { | ||
try { | ||
return JSON5.parse(fs.readFileSync(filePath, { encoding: 'utf-8' })) | ||
} catch { | ||
return '' | ||
} | ||
} | ||
return '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters