-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
125 lines (107 loc) · 3.12 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
var fs = require('fs');
var path = require('path');
var excludePatterns = [
'*.map',
'src/',
'src/**/*',
'examples/',
'examples/**/*',
'example/',
'example/**/*',
'demo/**/*',
'spec/',
'spec/**/*',
'docs/',
'docs/**/*',
'tests/',
'tests/**/*',
'test/',
'test/**/*',
'Gruntfile.js',
'gulpfile.js',
'package.json',
'package-lock.json',
'bower.json',
'composer.json',
'yarn.lock',
'webpack.config.js',
'README',
'LICENSE',
'CHANGELOG',
'*.yml',
'*.md',
'*.coffee',
'*.ts',
'*.scss',
'*.less'
];
module.exports = function (config) {
config = config || {};
var copyUnminified = config.copyUnminified || false;
var replaceDefaultExcludes = config.replaceDefaultExcludes || false;
var nodeModulesPath = config.nodeModulesPath || false;
var packageJsonPath = config.packageJsonPath || false;
var excludes = config.excludes || [];
var workingDir = process.cwd();
var nodeModDir = nodeModulesPath ? path.join(workingDir, nodeModulesPath) : '.';
var packageJsonFile = packageJsonPath ? path.join(workingDir, packageJsonPath, 'package.json') : 'package.json';
var buffer = fs.readFileSync(packageJsonFile);
var packageJson = JSON.parse(buffer.toString().trim());
var packages = [];
if (!replaceDefaultExcludes) {
excludes = excludes.concat(excludePatterns);
}
for (lib in packageJson.dependencies) {
var mainFileDir = path.join(nodeModDir, 'node_modules', lib);
var libFiles = [];
if (fs.existsSync(mainFileDir + '/dist')) {
mainFileDir = mainFileDir + '/dist';
} else {
var depPackageBuffer = fs.readFileSync(mainFileDir + '/package.json');
var depPackage = JSON.parse(depPackageBuffer.toString());
if (depPackage.main) {
var mainFile = mainFileDir + '/' + depPackage.main;
var distDirPos;
distDirPos = mainFile.lastIndexOf('/dist/');
if (distDirPos !== -1) {
mainFileDir = mainFile.substring(0, distDirPos) + '/dist';
}
}
}
function readLibFilesRecursively(target) {
try {
fs.readdirSync(target).forEach(function (path) {
var fullPath = target + '/' + path;
if (fs.lstatSync(fullPath).isDirectory()) {
readLibFilesRecursively(fullPath);
}
libFiles.push(fullPath);
});
} catch (err) {
console.log(err);
}
}
readLibFilesRecursively(mainFileDir);
// Includes
packages.push(mainFileDir + '/**/*');
//Excludes
excludes.map(function (value) {
packages.push('!' + mainFileDir + '/**/' + value);
});
if (copyUnminified === false) {
// Delete unminified versions
for (var i = 0; i < libFiles.length; i++) {
var target;
if (libFiles[i].indexOf('.min.js') > -1) {
target = libFiles[i].replace(/\.min\.js/, '.js');
packages.push('!' + libFiles[libFiles.indexOf(target)]);
}
if (libFiles[i].indexOf('.min.css') > -1) {
target = libFiles[i].replace(/\.min\.css/, '.css');
packages.push('!' + libFiles[libFiles.indexOf(target)]);
}
}
}
}
return packages;
};