@@ -12,6 +12,30 @@ function walk(directory) {
1212}
1313walk ( path . join ( root , 'src' ) ) ;
1414const manifest = JSON . parse ( fs . readFileSync ( path . join ( root , 'manifest.json' ) , 'utf8' ) ) ;
15+ const localeReferences = [ ...JSON . stringify ( manifest ) . matchAll ( / _ _ M S G _ ( [ A - Z a - z 0 - 9 _ ] + ) _ _ / g) ] . map (
16+ ( match ) => match [ 1 ] ,
17+ ) ;
18+ const localeRoot = path . join ( root , '_locales' ) ;
19+ if ( ( localeReferences . length || fs . existsSync ( localeRoot ) ) && ! manifest . default_locale )
20+ throw new Error ( '使用语言包时必须声明 default_locale' ) ;
21+ if ( manifest . default_locale ) {
22+ if ( ! / ^ [ A - Z a - z 0 - 9 _ ] + $ / . test ( manifest . default_locale ) )
23+ throw new Error ( 'default_locale 必须是合法的语言目录名' ) ;
24+ const messagesPath = path . join ( localeRoot , manifest . default_locale , 'messages.json' ) ;
25+ if ( ! fs . existsSync ( messagesPath ) ) throw new Error ( '缺少默认语言包:' + messagesPath ) ;
26+ const messages = JSON . parse ( fs . readFileSync ( messagesPath , 'utf8' ) ) ;
27+ if ( ! messages || typeof messages !== 'object' || Array . isArray ( messages ) )
28+ throw new Error ( '语言包必须是消息名称到消息内容的对象' ) ;
29+ const normalizedMessages = new Map ( ) ;
30+ for ( const [ key , value ] of Object . entries ( messages ) ) {
31+ if ( ! value || typeof value . message !== 'string' || ! value . message . trim ( ) )
32+ throw new Error ( '语言包消息内容不能为空:' + key ) ;
33+ normalizedMessages . set ( key . toLowerCase ( ) , value . message ) ;
34+ }
35+ for ( const key of localeReferences )
36+ if ( ! normalizedMessages . has ( key . toLowerCase ( ) ) )
37+ throw new Error ( '默认语言包缺少 manifest 引用的消息:' + key ) ;
38+ }
1539const files = [
1640 manifest . background . service_worker ,
1741 manifest . action . default_popup ,
@@ -31,4 +55,4 @@ for (const [size, file] of Object.entries(manifest.icons)) {
3155 )
3256 throw new Error ( '扩展图标必须是正确尺寸的 PNG:' + file ) ;
3357}
34- console . log ( 'JavaScript 语法和扩展入口检查通过 。' ) ;
58+ console . log ( 'JavaScript 语法、扩展入口和语言包检查通过 。' ) ;
0 commit comments