-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
139 lines (122 loc) · 3.92 KB
/
index.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
const fs = require('fs');
const path = require('path');
const xml2js = require('xml2js');
const co = require('co');
exports.cwd = path.join(__dirname, '../../');
const prefix = '$MODULE_DIR$/';
let targetList = [];
// XML 转 Object
exports.parseString = function*(xml) {
return new Promise(function(resolve, reject) {
new xml2js.Parser().parseString(xml, function(err, result) {
if (err) return reject(err);
resolve(result);
});
});
};
// Object 转 XML
exports.buildObject = function(obj) {
return new xml2js.Builder().buildObject(obj);
};
// 设置忽略
exports.setExcludeFolder = function(exclude, use) {
targetList.forEach((dir, index) => {
if (use[ index ]) {
return;
}
exclude.push({
$: {
url: 'file://' + path.join(prefix, dir)
}
});
});
};
// 更新项目.iml文件
exports.updateProjectIml = function*(file, cancel) {
if (!fs.existsSync(file)) {
return;
}
const xml = yield exports.parseString(fs.readFileSync(file, 'utf-8').toString());
const content = xml.module.component[ 0 ].content[ 0 ];
content.excludeFolder = content.excludeFolder || [];
if (!cancel) {
const haveExclude = [];
// 检查是否已经含有 node_modules 忽略
content.excludeFolder.forEach(function(exclude) {
const filePath = exclude.$.url;
const dir = filePath.substring(filePath.lastIndexOf('$/') + 1);
const index = targetList.indexOf(dir);
if (index >= 0) {
haveExclude[ index ] = true;
}
});
exports.setExcludeFolder(content.excludeFolder, haveExclude);
} else {
content.excludeFolder.forEach(function(exclude, index) {
const filePath = exclude.$.url;
const dir = filePath.substring(filePath.lastIndexOf('$/') + 1);
if (targetList.indexOf(dir) >= 0) {
content.excludeFolder.splice(index);
}
});
}
// 返流
fs.writeFileSync(file, exports.buildObject(xml));
};
// 设置 node_modules 为 exclude
exports.doTargetDir = function(dirList, cancel) {
const projectName = path.basename(exports.cwd);
const ideaConfigPath = path.join(exports.cwd, '.idea');
const modulesFile = path.join(ideaConfigPath, 'modules.xml');
if (!isDirectory(ideaConfigPath) || !(dirList instanceof Array)) {
return;
}
// 标准化 dirList
dirList.forEach((dir, index) => {
targetList[ index ] = /^\//ig.test(dir) ? dir : '/' + dir;
});
// 解析 modules.xml
co(function*() {
// 检测是否含有 modules.xml 文件
if (!fs.existsSync(modulesFile)) {
const modulesTemplateFile = path.join(__dirname, 'data/modules.xml');
const projectTemplateFile = path.join(__dirname, 'data/project.iml');
const projectFile = path.join(ideaConfigPath, `${projectName}.iml`);
const modulesTemplateText = fs.readFileSync(modulesTemplateFile).toString().replace(/\{\{([a-z0-9_-]+)\}\}/ig, (s, p1) => {
if (p1 === 'project_name') {
return projectName;
}
});
fs.writeFileSync(modulesFile, modulesTemplateText);
fs.writeFileSync(projectFile, fs.readFileSync(projectTemplateFile));
return;
}
const modules = yield exports.parseString(fs.readFileSync(modulesFile, 'utf-8').toString());
let iml;
modules.project.component.forEach(component => {
if (component.$.name === 'ProjectModuleManager') {
component.modules.forEach(current => {
current.module.some(item => {
const reg = /^\$PROJECT_DIR\$\/(.*\.iml)$/;
const isIml = reg.test(item.$.filepath);
if (isIml) {
iml = item.$.filepath.replace(reg, `${exports.cwd}/$1`);
return true;
}
});
});
iml = path.join(iml);
}
});
iml && (yield exports.updateProjectIml(iml, cancel));
});
};
function isDirectory(path) {
try {
const stat = fs.lstatSync(path);
return stat.isDirectory();
} catch (e) {
return false;
}
}