1 | const {
|
2 | readFileSync,
|
3 | lstatSync,
|
4 | ensureFileSync,
|
5 | writeFileSync
|
6 | } = require('fs-extra');
|
7 | const { relative } = require('path');
|
8 | const chalk = require('chalk');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const getFileContent = function(path) {
|
15 | return readFileSync(path, 'utf-8');
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const isDirectory = function(path) {
|
24 | return lstatSync(path).isDirectory();
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const writeFile = function(path, content, rootPath) {
|
35 | ensureFileSync(path);
|
36 | console.log(chalk.green('Write'), relative(rootPath, path));
|
37 | writeFileSync(path, content, 'utf-8');
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | const getCurrentDirectoryPath = function(dirname, root = 'src') {
|
47 | const rootPosition = dirname.indexOf(root);
|
48 | if (rootPosition === -1) {
|
49 | throw new Error(`Current directory ${dirname} is not under ${root}`);
|
50 | }
|
51 | return dirname.slice(rootPosition + root.length);
|
52 | };
|
53 |
|
54 | module.exports = {
|
55 | isDirectory,
|
56 | getFileContent,
|
57 | writeFile,
|
58 | getCurrentDirectoryPath
|
59 | };
|