forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue2Code.js
More file actions
92 lines (79 loc) · 1.8 KB
/
vue2Code.js
File metadata and controls
92 lines (79 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const {
readdirSync,
readFile,
writeFile,
createReadStream,
createWriteStream,
existsSync,
mkdirSync,
} = require('fs');
const { join } = require('path');
// ignore file
const ignoreFiles = [
'node_modules',
'.DS_Store',
'dist',
'.prettierrc',
'yarn.lock',
'README.md',
'package-lock.json',
'package.json',
'tsconfig.json',
];
// TODO replace files
const replaceFiles = [];
const handleError = err => err && console.error(err);
/**
* replace file
*
* @param {*} fromFile
* @param {*} toFile
* @param {*} options
* @returns
*/
const replaceArgsToFile = (fromFile, toFile, options) => {
if (!options) return;
readFile(fromFile, 'utf8', (err, data) => {
if (err) {
handleError(err);
return;
}
// TODO replace template code
writeFile(toFile, data, 'utf8', handleError);
});
};
/**
* copy file
*
* @param {*} fromPath
* @param {*} toPath
* @param {*} options
*/
const generateVueCode = (fromPath, toPath, options) => {
if (!existsSync(fromPath)) return;
// not exist and mkdir
!existsSync(toPath) && mkdirSync(toPath);
const files = readdirSync(fromPath, { withFileTypes: true });
for (const file of files) {
if (!ignoreFiles.includes(file.name)) {
const fromFilePath = join(fromPath, file.name);
const toFilePath = join(toPath, file.name);
if (file.isFile()) {
if (replaceFiles.includes(file.name)) {
// replace args
replaceArgsToFile(fromFilePath, toFilePath, options);
} else {
// copy file
const reader = createReadStream(fromFilePath);
const writer = createWriteStream(toFilePath);
reader.pipe(writer);
}
} else {
generateVueCode(fromFilePath, toFilePath);
}
}
}
};
module.exports = {
generateVueCode,
};