-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dependency.js
171 lines (146 loc) · 5.74 KB
/
Dependency.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const fs = require(`fs-extra`);
const path = require(`path`);
const _ = require(`lodash`);
const NODE_MODULES = `node_modules`;
const THE_ROOT_MODULE = `__THE_ROOT_MODULE__`;
class Dependency {
constructor( {parent, name, directory, root }) {
this.name = name;
this.parent = parent;
this.directory = directory;
this.root = root;
}
/**
* Get directories that need to be copied to target.
* @param {boolean} [includeOptional=true] - Include optional dependencies?
* @param {boolean} [includePeers=true] - Include peer dependencies?
* @returns {Promise<string[]>} Full set of directories to copy.
*/
getDirectoriesToCopy({ includeOptional = false, includePeers = false }) {
const childrenNames = this.gatherChildren({ includeOptional, includePeers });
if (!childrenNames) {
return []; // Ignore this directory
}
if (childrenNames.length === 0) {
if (this.name !== THE_ROOT_MODULE) {
return [ this.directory ]; // just need our own directory!
} else {
return [];
}
}
const children = childrenNames.map(name => this.resolve(name));
const allDirs = children.map(child => child.getDirectoriesToCopy(includeOptional, includePeers));
// flatten allDirs down to single Array
const flattened = allDirs.reduce((acc, val) => acc.concat(val), []); // TODO: replace with flat() call once Node 11+
if (this.name !== THE_ROOT_MODULE) {
flattened.push(this.directory); // We need to include our own directory
}
return flattened;
}
/**
* Gather a list of all child dependencies.
* @param {boolean} [includeOptional] - Include optional dependencies?
* @param {boolean} [includePeers] - Include peer dependencies?
* @returns {Promise<string[]>} Set of dependency names.
*/
gatherChildren( {includeOptional = false, includePeers = false }) {
const packageJson = fs.readJsonSync(path.join(this.directory, `package.json`));
const result = {
};
result.includeParent = !_.get(packageJson, 'titanium.ignore', false);
const titaniumDependencies = _.get(packageJson, 'titanium.dependencies');
if (!result.includeParent && this.name !== THE_ROOT_MODULE) {
let main;
if (packageJson.main) {
if (fs.existsSync(path.join(this.directory, packageJson.main))) {
main = path.join(this.directory, packageJson.main).substring(this.root.length);
} else if (fs.existsSync(path.join(this.directory, `${packageJson.main}.js`))) {
main = path.join(this.directory, `${packageJson.main}.js`).substring(this.root.length);
} else if (fs.existsSync(path.join(this.directory, `${packageJson.main}.json`))) {
main = path.join(this.directory, `${packageJson.main}.json`).substring(this.root.length);
} else if (fs.existsSync(path.join(this.directory, `index.js`))) {
main = path.join(this.directory, `index.js`).substring(this.root.length);
} else if (fs.existsSync(path.join(this.directory, `index.json`))) {
main = path.join(this.directory, `index.json`).substring(this.root.length);
}
}
copier.package_registry.push({
name: packageJson.name,
version: packageJson.version,
directory: this.directory,
main,
});
}
const aliases = _.get(packageJson, `titanium.aliases`);
if (_.isObject(aliases)) {
for (const alias in aliases) {
let main = aliases[alias];
if (!main.startsWith(`/`)) {
main = path.join(this.directory, main).substring(this.root.length);
}
copier.package_registry.push({
alias,
version: packageJson.version,
directory: this.directory,
main,
});
}
}
let dependencies = {};
if(packageJson.titanium && packageJson.titanium. )
dependencies = Object.keys(packageJson.dependencies || {});
// include optional dependencies too?
if (includeOptional && packageJson.optionalDependencies) {
dependencies.push(...Object.keys(packageJson.optionalDependencies));
}
if (includePeers && packageJson.peerDependencies) {
dependencies.push(...Object.keys(packageJson.peerDependencies));
}
if (packageJson.titanium) {
if (packageJson.titanium.type === `native-module`) {
copier.nativeModulePaths.push(this.directory);
// Just add ios and android if type: native-module
copier.nativeModulePlatformPaths.push(path.join(this.directory, `ios`));
copier.nativeModulePlatformPaths.push(path.join(this.directory, `android`));
} else if (packageJson.titanium.type === `widget`) {
const widgetDir = path.join(this.directory, packageJson.titanium.widgetDir || `.`);
copier.widgetDirectories.push(widgetDir);
const widgetManifest = {
dir: widgetDir,
manifest: {
id: packageJson.titanium.widgetId || packageJson.id,
platforms: packageJson.titanium.platforms || `ios,android`,
},
};
copier.widgetManifests.push(widgetManifest);
}
}
return dependencies;
}
/**
* Attempts to resolve a given module by id to the correct.
* @param {string} subModule - Id of a module that is it's dependency.
* @returns {Promise<Dependency>} The resolved dependency.
*/
async resolve(subModule) {
try {
// First try underneath the current module
const source_directory = path.join(this.directory, NODE_MODULES, subModule);
// const target_directory = path.join(this.target, NODE_MODULES, subModule);
const packageJsonExists = await fs.exists(path.join(source_directory, `package.json`));
if (packageJsonExists) {
return new Dependency( { parent: this, name: subModule, directory: source_directory, root: this.root });
}
} catch (err) {
// this is the root and we still didn't find it, fail!
if (this.parent === null) {
throw err;
}
}
if (this.parent === null) {
throw new Error(`Could not find dependency: ${subModule}`);
}
return this.parent.resolve(subModule); // Try the parent (recursively)
}
}
module.exports = Dependency;