1
1
var fs = require ( 'fs' ) ;
2
+ var path = require ( 'path' ) ;
2
3
var removePosition = require ( 'unist-util-remove-position' ) ;
3
4
var u = require ( 'unist-builder' ) ;
4
5
var _ = require ( 'lodash' ) ;
@@ -12,6 +13,8 @@ var demoHeaders = {
12
13
'en-US' : 'Demos'
13
14
}
14
15
16
+ var I18N_KEY_REGEXP = / \{ i 1 8 n \. ( [ ^ \} ] * ) \} / ;
17
+
15
18
// var beautyConsole = require('./lib').beautyConsole;
16
19
17
20
/**
@@ -55,8 +58,9 @@ function getDemoID(filename) {
55
58
56
59
function extractDemos ( demosPath , i18n , ctx ) {
57
60
var styles = [ ] ;
58
- var demos = fs . readdirSync ( demosPath + '/demos' ) . map ( filename => {
59
- var filePath = demosPath + '/demos/' + filename ;
61
+ var demoDir = path . join ( demosPath , 'demos' ) ;
62
+ var demos = fs . readdirSync ( demoDir ) . map ( filename => {
63
+ var filePath = path . join ( demosDir , filename ) ;
60
64
ctx . addDependency ( filePath )
61
65
var demoAST = markdownParser . parse ( fs . readFileSync ( filePath , { encoding : 'utf-8' } ) ) ,
62
66
yamlPart = ( demoAST && demoAST . children && demoAST . children [ 0 ] ) || { } ,
@@ -67,13 +71,13 @@ function extractDemos(demosPath, i18n, ctx) {
67
71
if ( yamlPart . type === 'yaml' ) {
68
72
yaml = parseYAML ( yamlPart . value ) ;
69
73
} else {
70
- ctx . emitWarning ( ` \nYaml header in demo.md is required, ${ demosPath + '/demos/' + filename } wasn't appended.\n` ) ;
74
+ ctx . emitWarning ( ' \nYaml header in demo.md is required, ' + filePath + ' wasn\ 't appended.\n' ) ;
71
75
return null ;
72
76
}
73
77
74
78
// validate main content
75
79
if ( mainContent . type !== 'code' ) {
76
- ctx . emitWarning ( ` \nThe first 2 sections of demo.md must be yaml header and code block, ${ demosPath + '/demos/' + filename } wasn't appended.\n` ) ;
80
+ ctx . emitWarning ( ' \nThe first 2 sections of demo.md must be yaml header and code block, ' + filePath + ' wasn\ 't appended.\n' ) ;
77
81
return null ;
78
82
}
79
83
@@ -96,9 +100,11 @@ function extractDemos(demosPath, i18n, ctx) {
96
100
} ) ;
97
101
98
102
// check if any {i18n.xxx} exists after replace
99
- if ( / \{ i 1 8 n \. [ ^ \} ] * \} / . test ( mainContent . value ) ) {
103
+ var i18nMatchResult = I18N_KEY_REGEXP . exec ( mainContent . value ) ;
104
+ if ( i18nMatchResult ) {
105
+ var i18nMissingKey = i18nMatchResult [ 1 ] ;
100
106
ctx . emitError ( new Error (
101
- ` \nUnrecognized i18n variable {i18n.${ / \{ i 1 8 n \. ( [ ^ \} ] * ) \} / . exec ( mainContent . value ) [ 1 ] } } in ${ demosPath + '/demos/ ' + filename } `
107
+ ' \nUnrecognized i18n variable {i18n.' + i18nMissingKey + '} in ' + filePath
102
108
) ) ;
103
109
return null ;
104
110
}
0 commit comments