1 | var path = require('path');
|
2 | var fs = require('fs');
|
3 | var readline = require('readline');
|
4 | var chalk = require("chalk");
|
5 | module.exports = Tools;
|
6 |
|
7 | function Tools() {}
|
8 |
|
9 | Tools.copy = function(src, dest) {
|
10 | var content = fs.readFileSync(src, 'utf8');
|
11 | fs.writeFileSync(dest, content);
|
12 | }
|
13 | Tools.mergeFile = function(src, dest, override) {
|
14 | var buffer = fs.readFileSync(dest, 'UTF-8');
|
15 | const rl = readline.createInterface({
|
16 | input: fs.createReadStream(src)
|
17 | });
|
18 | rl.on('line', (line) => {
|
19 | if(!buffer.includes(line)){
|
20 | fs.appendFile(dest, '\n'+line, 'utf8', function (argument) {
|
21 | console.log("+"+chalk.green.bold(line))
|
22 | });
|
23 | }
|
24 | });
|
25 | }
|
26 | Tools.packs = {};
|
27 | Tools.mkdir = function(dir) {
|
28 |
|
29 |
|
30 |
|
31 | dir = path.resolve(dir);
|
32 | var paths = dir.split(path.sep);
|
33 | paths[paths.length - 1] = paths[paths.length - 1].replace(":", "$");
|
34 | var root = paths[0];
|
35 |
|
36 | for (var i = 1; i < paths.length; i++) {
|
37 | root = root + path.sep + paths[i];
|
38 | if (!fs.existsSync(root)) {
|
39 | fs.mkdirSync(root);
|
40 | }
|
41 | }
|
42 |
|
43 | return dir;
|
44 | }
|
45 | Tools.mkfile = function(filePath, contentFun) {
|
46 |
|
47 | filePath = path.resolve(filePath);
|
48 | if (!fs.existsSync(filePath)) {
|
49 | fs.writeFileSync(filePath, contentFun());
|
50 | }
|
51 | }
|
52 | Tools.checkProject = function() {
|
53 | if (!fs.existsSync(path.resolve(".aics"))) {
|
54 | console.log("\nNot a aics projcet.\n");
|
55 | process.exit(1);
|
56 | }
|
57 | }
|
58 | Tools.projectConfig = function(configPath) {
|
59 | configPath = path.resolve(configPath);
|
60 | if (!fs.statSync(configPath)) {
|
61 | console.log("未找到配置文件: " + configPath);
|
62 | process.exit(1);
|
63 | }
|
64 | return require(configPath);
|
65 | }
|
66 |
|
67 | Tools.updateProjectConfig = function(config) {
|
68 | fs.writeFileSync(path.resolve("./project.json"), JSON.stringify(config));
|
69 | }
|
70 | Tools.getAics = function() {
|
71 | var configPath = require(path.resolve(".aics/packages.json"));
|
72 | return configPath;
|
73 | }
|
74 | Tools.setAics = function(name, version) {
|
75 | var configPath = require(path.resolve(".aics/packages.json"));
|
76 | configPath[name] = version;
|
77 | fs.writeFileSync(path.resolve(".aics/packages.json"), JSON.stringify(configPath, null, 2));
|
78 | }
|
79 | Tools.confRemove = function(name) {
|
80 | var packageConf = require(path.resolve(".aics/packages.json"));
|
81 | var projConf = require(path.resolve(".aics/project.json"));
|
82 |
|
83 | delete packageConf[name.split("@")[0]];
|
84 | delete projConf.packages[name.split("@")[0]];
|
85 |
|
86 | fs.writeFileSync(path.resolve(".aics/packages.json"), JSON.stringify(packageConf, null, 2));
|
87 | fs.writeFileSync(path.resolve(".aics/project.json"), JSON.stringify(projConf, null, 2));
|
88 | }
|
89 | Tools.isExist = function(path, showLog) {
|
90 | try {
|
91 | var states = fs.statSync(path);
|
92 | if (states) {
|
93 | return true;
|
94 | } else {
|
95 | return false;
|
96 | }
|
97 | } catch (e) {
|
98 | showLog ? console.log(path + ' is not Exist.') : "";
|
99 | return false;
|
100 | }
|
101 | }
|