|
1 | 1 | import * as path from "path";
|
2 |
| -import { PACKAGE_JSON_FILE_NAME, } from "../../constants"; |
| 2 | +import { PACKAGE_JSON_FILE_NAME } from "../../constants"; |
3 | 3 | import { INodeModulesDependenciesBuilder } from "../../definitions/platform";
|
4 | 4 | import { IDependencyData } from "../../declarations";
|
5 | 5 | import { IFileSystem } from "../../common/declarations";
|
6 | 6 | import * as _ from "lodash";
|
7 | 7 | import { injector } from "../../common/yok";
|
8 | 8 |
|
9 | 9 | interface IDependencyDescription {
|
10 |
| - parent: IDependencyDescription; |
11 |
| - parentDir: string; |
12 |
| - name: string; |
13 |
| - depth: number; |
| 10 | + parent: IDependencyDescription; |
| 11 | + parentDir: string; |
| 12 | + name: string; |
| 13 | + depth: number; |
14 | 14 | }
|
15 | 15 |
|
16 | 16 | export class NodeModulesDependenciesBuilder
|
17 |
| - implements INodeModulesDependenciesBuilder { |
18 |
| - public constructor(private $fs: IFileSystem) { |
19 |
| - } |
20 |
| - |
21 |
| - public getProductionDependencies(projectPath: string, ignore?: string[]): IDependencyData[] { |
22 |
| - const projectPackageJsonPath = path.join( |
23 |
| - projectPath, |
24 |
| - PACKAGE_JSON_FILE_NAME |
25 |
| - ); |
26 |
| - const packageJsonContent = this.$fs.readJson(projectPackageJsonPath); |
27 |
| - const dependencies = packageJsonContent && packageJsonContent.dependencies; |
28 |
| - |
29 |
| - const resolvedDependencies: IDependencyData[] = []; |
30 |
| - |
31 |
| - const queue: IDependencyDescription[] = _.keys(dependencies).map( |
32 |
| - (dependencyName) => ({ |
33 |
| - parent: null, |
34 |
| - parentDir: projectPath, |
35 |
| - name: dependencyName, |
36 |
| - depth: 0, |
37 |
| - }) |
38 |
| - ); |
39 |
| - |
40 |
| - while (queue.length) { |
41 |
| - const currentModule = queue.shift(); |
42 |
| - const resolvedDependency = this.findModule( |
43 |
| - currentModule, |
44 |
| - resolvedDependencies |
45 |
| - ); |
46 |
| - |
47 |
| - if ( |
48 |
| - resolvedDependency && |
49 |
| - !_.some( |
50 |
| - resolvedDependencies, |
51 |
| - (r) => r.directory === resolvedDependency.directory |
52 |
| - ) |
53 |
| - ) { |
54 |
| - _.each(resolvedDependency.dependencies, (d) => { |
55 |
| - const dependency: IDependencyDescription = { |
56 |
| - parent: currentModule, |
57 |
| - name: d, |
58 |
| - parentDir: resolvedDependency.directory, |
59 |
| - depth: resolvedDependency.depth + 1, |
60 |
| - }; |
61 |
| - |
62 |
| - const shouldAdd = !_.some( |
63 |
| - queue, |
64 |
| - (element) => |
65 |
| - element.parent === dependency.parent && |
66 |
| - element.name === dependency.name && |
67 |
| - element.parentDir === dependency.parentDir && |
68 |
| - element.depth === dependency.depth |
69 |
| - ); |
70 |
| - |
71 |
| - if (shouldAdd) { |
72 |
| - queue.push(dependency); |
73 |
| - } |
74 |
| - }); |
75 |
| - |
76 |
| - resolvedDependencies.push(resolvedDependency); |
77 |
| - } |
78 |
| - } |
79 |
| - if (ignore && ignore.length > 0) { |
80 |
| - return resolvedDependencies.filter(d => ignore.indexOf(d.name) === -1); |
81 |
| - } |
82 |
| - return resolvedDependencies; |
83 |
| - } |
84 |
| - |
85 |
| - private findModule( |
86 |
| - depDescription: IDependencyDescription, |
87 |
| - resolvedDependencies: IDependencyData[], |
88 |
| - ): IDependencyData { |
89 |
| - try { |
90 |
| - const parentModulesPath = depDescription?.parentDir ?? depDescription?.parent?.parentDir; |
91 |
| - const modulePath = require.resolve(`${depDescription.name}/package.json`, { |
92 |
| - paths: [parentModulesPath] |
93 |
| - }).replace('/package.json', '') |
94 |
| - |
95 |
| - // if we already resolved this dependency, we return null to avoid a duplicate resolution |
96 |
| - if (resolvedDependencies.some(r => { |
97 |
| - return r.name === depDescription.name && r.directory === modulePath |
98 |
| - })) { |
99 |
| - return null; |
100 |
| - } |
101 |
| - |
102 |
| - return this.getDependencyData( |
103 |
| - depDescription.name, |
104 |
| - modulePath, |
105 |
| - depDescription.depth |
106 |
| - ); |
107 |
| - } catch (err) { |
108 |
| - return null; |
109 |
| - } |
110 |
| - } |
111 |
| - |
112 |
| - private getDependencyData( |
113 |
| - name: string, |
114 |
| - directory: string, |
115 |
| - depth: number |
116 |
| - ): IDependencyData { |
117 |
| - const dependency: IDependencyData = { |
118 |
| - name, |
119 |
| - directory, |
120 |
| - depth, |
121 |
| - version: null, |
122 |
| - }; |
123 |
| - |
124 |
| - const packageJsonPath = path.join(directory, PACKAGE_JSON_FILE_NAME); |
125 |
| - const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile(); |
126 |
| - |
127 |
| - if (packageJsonExists) { |
128 |
| - const packageJsonContents = this.$fs.readJson(packageJsonPath); |
129 |
| - |
130 |
| - dependency.version = packageJsonContents.version; |
131 |
| - if (!!packageJsonContents.nativescript) { |
132 |
| - // add `nativescript` property, necessary for resolving plugins |
133 |
| - dependency.nativescript = packageJsonContents.nativescript; |
134 |
| - } |
135 |
| - |
136 |
| - dependency.dependencies = _.keys(packageJsonContents.dependencies); |
137 |
| - return dependency; |
138 |
| - } |
139 |
| - |
140 |
| - return null; |
141 |
| - } |
| 17 | + implements INodeModulesDependenciesBuilder { |
| 18 | + public constructor(private $fs: IFileSystem) {} |
| 19 | + |
| 20 | + public getProductionDependencies( |
| 21 | + projectPath: string, |
| 22 | + ignore?: string[] |
| 23 | + ): IDependencyData[] { |
| 24 | + const projectPackageJsonPath = path.join( |
| 25 | + projectPath, |
| 26 | + PACKAGE_JSON_FILE_NAME |
| 27 | + ); |
| 28 | + const packageJsonContent = this.$fs.readJson(projectPackageJsonPath); |
| 29 | + const dependencies = packageJsonContent && packageJsonContent.dependencies; |
| 30 | + |
| 31 | + const resolvedDependencies: IDependencyData[] = []; |
| 32 | + |
| 33 | + const queue: IDependencyDescription[] = _.keys(dependencies).map( |
| 34 | + (dependencyName) => ({ |
| 35 | + parent: null, |
| 36 | + parentDir: projectPath, |
| 37 | + name: dependencyName, |
| 38 | + depth: 0, |
| 39 | + }) |
| 40 | + ); |
| 41 | + |
| 42 | + while (queue.length) { |
| 43 | + const currentModule = queue.shift(); |
| 44 | + const resolvedDependency = this.findModule( |
| 45 | + currentModule, |
| 46 | + resolvedDependencies |
| 47 | + ); |
| 48 | + |
| 49 | + if ( |
| 50 | + resolvedDependency && |
| 51 | + !_.some( |
| 52 | + resolvedDependencies, |
| 53 | + (r) => r.directory === resolvedDependency.directory |
| 54 | + ) |
| 55 | + ) { |
| 56 | + _.each(resolvedDependency.dependencies, (d) => { |
| 57 | + const dependency: IDependencyDescription = { |
| 58 | + parent: currentModule, |
| 59 | + name: d, |
| 60 | + parentDir: resolvedDependency.directory, |
| 61 | + depth: resolvedDependency.depth + 1, |
| 62 | + }; |
| 63 | + |
| 64 | + const shouldAdd = !_.some( |
| 65 | + queue, |
| 66 | + (element) => |
| 67 | + element.parent === dependency.parent && |
| 68 | + element.name === dependency.name && |
| 69 | + element.parentDir === dependency.parentDir && |
| 70 | + element.depth === dependency.depth |
| 71 | + ); |
| 72 | + |
| 73 | + if (shouldAdd) { |
| 74 | + queue.push(dependency); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + resolvedDependencies.push(resolvedDependency); |
| 79 | + } |
| 80 | + } |
| 81 | + if (ignore && ignore.length > 0) { |
| 82 | + return resolvedDependencies.filter((d) => ignore.indexOf(d.name) === -1); |
| 83 | + } |
| 84 | + return resolvedDependencies; |
| 85 | + } |
| 86 | + |
| 87 | + private findModule( |
| 88 | + depDescription: IDependencyDescription, |
| 89 | + resolvedDependencies: IDependencyData[] |
| 90 | + ): IDependencyData { |
| 91 | + try { |
| 92 | + const parentModulesPath = |
| 93 | + depDescription?.parentDir ?? depDescription?.parent?.parentDir; |
| 94 | + const modulePath = require |
| 95 | + .resolve(`${depDescription.name}/package.json`, { |
| 96 | + paths: [parentModulesPath], |
| 97 | + }) |
| 98 | + .replace("package.json", ""); |
| 99 | + |
| 100 | + // if we already resolved this dependency, we return null to avoid a duplicate resolution |
| 101 | + if ( |
| 102 | + resolvedDependencies.some((r) => { |
| 103 | + return r.name === depDescription.name && r.directory === modulePath; |
| 104 | + }) |
| 105 | + ) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return this.getDependencyData( |
| 110 | + depDescription.name, |
| 111 | + modulePath, |
| 112 | + depDescription.depth |
| 113 | + ); |
| 114 | + } catch (err) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private getDependencyData( |
| 120 | + name: string, |
| 121 | + directory: string, |
| 122 | + depth: number |
| 123 | + ): IDependencyData { |
| 124 | + const dependency: IDependencyData = { |
| 125 | + name, |
| 126 | + directory, |
| 127 | + depth, |
| 128 | + version: null, |
| 129 | + }; |
| 130 | + |
| 131 | + const packageJsonPath = path.join(directory, PACKAGE_JSON_FILE_NAME); |
| 132 | + const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile(); |
| 133 | + |
| 134 | + if (packageJsonExists) { |
| 135 | + const packageJsonContents = this.$fs.readJson(packageJsonPath); |
| 136 | + |
| 137 | + dependency.version = packageJsonContents.version; |
| 138 | + if (!!packageJsonContents.nativescript) { |
| 139 | + // add `nativescript` property, necessary for resolving plugins |
| 140 | + dependency.nativescript = packageJsonContents.nativescript; |
| 141 | + } |
| 142 | + |
| 143 | + dependency.dependencies = _.keys(packageJsonContents.dependencies); |
| 144 | + return dependency; |
| 145 | + } |
| 146 | + |
| 147 | + return null; |
| 148 | + } |
142 | 149 | }
|
143 | 150 |
|
144 | 151 | injector.register(
|
145 |
| - "nodeModulesDependenciesBuilder", |
146 |
| - NodeModulesDependenciesBuilder |
| 152 | + "nodeModulesDependenciesBuilder", |
| 153 | + NodeModulesDependenciesBuilder |
147 | 154 | );
|
0 commit comments