Skip to content

Commit a32e9fb

Browse files
authored
Add vue config (#174)
1 parent 957035e commit a32e9fb

File tree

3 files changed

+201
-59
lines changed

3 files changed

+201
-59
lines changed

packages/devextreme-cli/templates/vue/application/src/router.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ import simpleLayout from "./layouts/single-card";
1212
Vue.use(Router);
1313

1414
const router = new Router({
15-
routes: [<%=^empty%>
15+
routes: [<%=#empty%>
1616
{
17+
path: "/",
18+
components: {
19+
layout: defaultLayout
20+
}
21+
},<%=/empty%>
22+
<%=^empty%>{
1723
path: "/home",
1824
name: "home",
1925
meta: { requiresAuth: true },
@@ -39,14 +45,7 @@ const router = new Router({
3945
layout: defaultLayout,
4046
content: DisplayData
4147
}
42-
},<%=/empty%>
43-
<%=#empty%>{
44-
path: "/",
45-
components: {
46-
layout: defaultLayout
47-
}
48-
},<%=/empty%>
49-
{
48+
},<%=/empty%>{
5049
path: "/login-form",
5150
name: "login-form",
5251
meta: { requiresAuth: false },
@@ -58,8 +57,8 @@ const router = new Router({
5857
content: () =>
5958
import(/* webpackChunkName: "login" */ "./views/login-form")
6059
}
61-
},<%=^empty%>
62-
{
60+
},
61+
<%=^empty%>{
6362
path: "/",
6463
redirect: "/home"
6564
},

templates-generator/index.js

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,80 @@ const path = require('path');
44
const glob = require('glob');
55
const micromatch = require('micromatch');
66
const 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

1314
const platformsConfigs = {
14-
react: './react-config.js'
15+
react: './react-config.js',
16+
angular: './angular-config.js',
17+
vue: './vue-config.js'
1518
};
1619

1720
const 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+
}

templates-generator/vue-config.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
module.exports = {
2+
sourcePath: 'packages/devextreme-cli/testing/sandbox/vue/my-app/',
3+
targetPath: 'packages/devextreme-cli/templates/vue/',
4+
sourceGlob: '**/*.{js,scss,json,vue}',
5+
ignoreList: [
6+
'{node_modules,public,src/themes/generated}/**/*.*',
7+
'src/{App.test,setupTests,serviceWorker,index}.js',
8+
'src/components/HelloWorld.vue',
9+
'{package,package-lock}.json',
10+
'babel.config.js'
11+
],
12+
replaceRules: [
13+
{
14+
glob: 'src/themes/metadata.additional.json',
15+
definitions: [
16+
{
17+
before: /"baseTheme": "[^"]*"/,
18+
after: '"baseTheme": "material.orange.dark"'
19+
}
20+
]
21+
},
22+
{
23+
glob: 'src/themes/metadata.base.json',
24+
definitions: [
25+
{
26+
before: /"baseTheme": "[^"]*"/,
27+
after: '"baseTheme": "material.orange.light"'
28+
}
29+
]
30+
},
31+
{
32+
glob: 'src/themes/metadata.*.json',
33+
definitions: [
34+
{
35+
before: /"items": \[[^\]]*]/,
36+
after: '"items": []'
37+
}
38+
]
39+
},
40+
{
41+
glob: 'src/app-info.js',
42+
definitions: [
43+
{
44+
before: 'My App',
45+
after: '<%=project%>'
46+
}
47+
],
48+
},
49+
{
50+
glob: 'src/app-navigation.js',
51+
definitions: [
52+
{
53+
before: /\[(.*?)\];/s,
54+
after: '[<%=^empty%>$1<%=/empty%>];'
55+
}
56+
]
57+
},
58+
{
59+
glob: 'src/router.js',
60+
definitions: [
61+
{
62+
before: /((import (Home|Profile|DisplayData).*\n)+)/,
63+
after: '<%=^empty%>$1<%=/empty%>'
64+
},
65+
{
66+
before: /side-nav-(inner|outer)-toolbar/,
67+
after: '<%=layout%>'
68+
},
69+
{
70+
before: /routes: \[\s+/,
71+
after: `routes: [<%=#empty%>
72+
{
73+
path: "/",
74+
components: {
75+
layout: defaultLayout
76+
}
77+
},<%=/empty%>
78+
`
79+
},
80+
{
81+
before: /({\s+path: "\/home".*content: DisplayData(\s+})+,)\s+/s,
82+
after: '<%=^empty%>$1<%=/empty%>'
83+
},
84+
{
85+
before: /({[^}]*redirect: "\/home"[^\]]*})\s+]/,
86+
after: `<%=^empty%>$1<%=/empty%>
87+
<%=#empty%>{
88+
path: "*",
89+
redirect: "/"
90+
}<%=/empty%>
91+
]`
92+
}
93+
]
94+
}
95+
],
96+
removeRules: [
97+
{
98+
glob: 'src/main.js',
99+
definitions: [
100+
'import \'devextreme/dist/css/dx.common.css\';\n',
101+
'import \'./themes/generated/theme.base.css\';\n',
102+
'import \'./themes/generated/theme.additional.css\';\n',
103+
]
104+
}
105+
],
106+
moveRules: [
107+
{
108+
glob: 'src/{!(views)/*.*,views/login-form.vue,*.*}',
109+
definition:
110+
{
111+
sourcePath: '',
112+
targetPath: 'packages/devextreme-cli/templates/vue/application/'
113+
}
114+
},
115+
{
116+
glob: 'devextreme.json',
117+
definition:
118+
{
119+
sourcePath: '',
120+
targetPath: 'packages/devextreme-cli/templates/vue/application/'
121+
}
122+
},
123+
{
124+
glob: 'src/views/**/*.*',
125+
definition:
126+
{
127+
sourcePath: 'src/views/',
128+
targetPath: 'packages/devextreme-cli/templates/vue/sample-pages/'
129+
}
130+
}
131+
]
132+
};

0 commit comments

Comments
 (0)