forked from mayswind/AriaNg-Native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-main-modules.js
111 lines (87 loc) · 3.24 KB
/
copy-main-modules.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
const fs = require('fs-extra');
const path = require('path');
const jsonfile = require('jsonfile');
const argv = require('yargs')
.usage('Usage: $0 -d <dist>')
.option('s', {
alias: 'source',
type: 'string',
describe: 'The directory where dependencies is.',
})
.option('d', {
alias: 'dist',
type: 'string',
describe: 'The directory where dependencies would be copies to.',
})
.option('r', {
alias: 'dryrun',
type: 'boolean',
describe: 'Find the dependencies and log to the screen only.',
})
.option('v', {
alias: 'verbose',
type: 'boolean',
describe: 'Enable verbose log.',
})
.parse();
const pkgfile = require('./package');
function getDependencies(allDependenciesDir, level, sourceDir, dependencies) {
for (let i = 0; i < dependencies.length; i++) {
let dependency = dependencies[i];
let dependencyDir = path.join(sourceDir, dependency);
if (!fs.existsSync(dependencyDir)) {
throw new Error('Dependency "' + dependency + '" does not exist!');
}
let dependencyPackageFile = path.join(dependencyDir, 'package.json');
if (!fs.existsSync(dependencyPackageFile)) {
throw new Error('Dependency "' + dependency + '" package file does not exist!');
}
let dependencyPackage = jsonfile.readFileSync(dependencyPackageFile, { throws: false });
if (!dependencyPackage) {
throw new Error('Dependency "' + dependency + '" package file loads failed!');
}
if (argv.verbose) {
let logPrefix = '';
for (let i = 0; i < level * 2; i++) {
logPrefix += '-';
}
console.log(logPrefix + (logPrefix && ' ') + 'find dependency ' + dependency);
}
if (dependencyPackage.dependencies) {
let subDependenciesDir = [];
let subDependencies = [];
for (subDependency in dependencyPackage.dependencies) {
if (!dependencyPackage.dependencies.hasOwnProperty(subDependency)) {
continue;
}
subDependencies.push(subDependency);
}
subDependenciesDir = getDependencies([], level + 1, sourceDir, subDependencies);
allDependenciesDir.push(...subDependenciesDir);
}
allDependenciesDir.push(dependencyDir);
}
return allDependenciesDir;
}
function copyDependencies(dependenciesDir, dist) {
let dependenciesDirSet = {};
for (let i = 0; i < dependenciesDir.length; i++) {
let dependencyDir = dependenciesDir[i];
dependenciesDirSet[dependencyDir] = true;
}
for (let dependencyDir in dependenciesDirSet) {
if (!dependenciesDirSet.hasOwnProperty(dependencyDir)) {
continue;
}
let srcDir = path.join(__dirname, dependencyDir);
let distDir = path.join(__dirname, dist, dependencyDir);
if (!fs.existsSync(distDir)) {
fs.mkdirpSync(distDir);
}
fs.copySync(srcDir, distDir);
}
}
let dependencies = getDependencies([], 0, argv.source, pkgfile.mainDependencies);
if (!argv.dryrun) {
copyDependencies(dependencies, argv.dist);
}