@@ -4,69 +4,80 @@ const path = require('path');
44const glob = require ( 'glob' ) ;
55const micromatch = require ( 'micromatch' ) ;
66const args = require ( 'minimist' ) ( process . argv . slice ( 2 ) ,
7- buildOptions ( { platform : {
8- type : 'string' ,
9- alias : 'p'
10- }
7+ buildOptions ( {
8+ platform : {
9+ type : 'string' ,
10+ alias : 'p'
11+ }
1112 } ) ) ;
1213
1314const platformsConfigs = {
14- react : './react-config.js'
15+ react : './react-config.js' ,
16+ angular : './angular-config.js' ,
17+ vue : './vue-config.js'
1518} ;
1619
1720const commands = args [ '_' ] ;
18- if ( commands . length ) {
19- throw new Error ( `Unexpected command(s) '${ args . _ } '` ) ;
20- }
21+ if ( commands . length ) {
22+ throw new Error ( `Unexpected command(s) '${ args . _ } '` ) ;
23+ }
2124
22- if ( args . platform in platformsConfigs ) {
23- generateTemplate ( args . platform ) ;
24- } else if ( ! args . platform ) {
25- for ( let platform in platformsConfigs ) {
26- generateTemplate ( platform ) ;
27- }
28- } else {
29- throw new Error ( 'Platform doesn\'t exist' ) ;
25+ if ( args . platform in platformsConfigs ) {
26+ generateTemplate ( args . platform ) ;
27+ } else if ( ! args . platform ) {
28+ for ( let platform in platformsConfigs ) {
29+ generateTemplate ( platform ) ;
3030 }
31+ } else {
32+ throw new Error ( 'Platform doesn\'t exist' ) ;
33+ }
3134
32- function generateTemplate ( platform ) {
33- const config = require ( platformsConfigs [ platform ] , 'utf8' ) ;
34- const relativePaths = glob . sync ( config . sourceGlob , {
35- cwd : config . sourcePath ,
36- ignore : config . ignoreList
37- } ) ;
38- relativePaths . forEach ( relativePath => {
39- let content = fs . readFileSync ( `${ config . sourcePath } ${ relativePath } ` , 'utf8' ) ;
40- content = updateContent ( relativePath , content , config . updateRules ) ;
35+ function generateTemplate ( platform ) {
36+ const config = require ( platformsConfigs [ platform ] , 'utf8' ) ;
37+ const relativePaths = glob . sync ( config . sourceGlob , {
38+ cwd : config . sourcePath ,
39+ ignore : config . ignoreList
40+ } ) ;
41+ relativePaths . forEach ( relativePath => {
42+ let content = fs . readFileSync ( `${ config . sourcePath } ${ relativePath } ` , 'utf8' ) ;
43+ content = updateContent ( relativePath , content , config . replaceRules , config . removeRules ) ;
4144
42- writeFile ( relativePath , content , config ) ;
43- } ) ;
44- }
45+ writeFile ( relativePath , content , config ) ;
46+ } ) ;
47+ }
4548
46- function updateContent ( relativePath , content , updateRules ) {
47- const rules = updateRules . filter ( info => micromatch . isMatch ( relativePath , info . glob ) ) ;
48- rules . forEach ( rule => {
49- rule . definitions . forEach ( definition => {
49+ function updateContent ( relativePath , content , replaceRules , removeRules ) {
50+ replaceRules . forEach ( replacement => {
51+ if ( micromatch . isMatch ( relativePath , replacement . glob ) ) {
52+ replacement . definitions . forEach ( definition => {
5053 content = content . replace ( definition . before , definition . after ) ;
5154 } ) ;
52- } ) ;
55+ }
56+ } ) ;
5357
54- return content ;
55- }
58+ removeRules . forEach ( removal => {
59+ if ( micromatch . isMatch ( relativePath , removal . glob ) ) {
60+ removal . definitions . forEach ( definition => {
61+ content = content . replace ( definition , '' ) ;
62+ } ) ;
63+ }
64+ } ) ;
65+ return content ;
66+ }
5667
57- function writeFile ( relativePath , content , { moveRules, targetPath } ) {
58- const rule = moveRules . find ( rule => micromatch . isMatch ( relativePath , rule . glob ) ) ;
59- const fullPath = rule
60- ? `${ rule . definition . targetPath } ${ relativePath . replace ( rule . definition . sourcePath , '' ) } `
61- : `${ targetPath } ${ relativePath } ` ;
68+ function writeFile ( relativePath , content , { moveRules, targetPath } ) {
69+ const rule = moveRules . find ( rule => micromatch . isMatch ( relativePath , rule . glob ) ) ;
70+ const fullPath = rule
71+ ? `${ rule . definition . targetPath } ${ relativePath . replace ( rule . definition . sourcePath , '' ) } `
72+ : `${ targetPath } ${ relativePath } ` ;
6273
63- createNestedFolder ( fullPath , content ) ;
64- fs . writeFileSync ( fullPath , content ) ;
65- }
74+ createNestedFolder ( fullPath , content ) ;
75+ fs . writeFileSync ( fullPath , content ) ;
76+ }
6677
67- function createNestedFolder ( fullPath ) {
68- const dirName = path . dirname ( fullPath ) ;
69- if ( ! fs . existsSync ( dirName ) ) {
70- fs . mkdirSync ( dirName , { recursive : true } ) ;
71- }
78+ function createNestedFolder ( fullPath ) {
79+ const dirName = path . dirname ( fullPath ) ;
80+ if ( ! fs . existsSync ( dirName ) ) {
81+ fs . mkdirSync ( dirName , { recursive : true } ) ;
7282 }
83+ }
0 commit comments