-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
139 additions
and
132 deletions.
There are no files selected for viewing
271 changes: 139 additions & 132 deletions
271
lib/tools/node-modules/node-modules-dependencies-builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,154 @@ | ||
import * as path from "path"; | ||
import { PACKAGE_JSON_FILE_NAME, } from "../../constants"; | ||
import { PACKAGE_JSON_FILE_NAME } from "../../constants"; | ||
import { INodeModulesDependenciesBuilder } from "../../definitions/platform"; | ||
import { IDependencyData } from "../../declarations"; | ||
import { IFileSystem } from "../../common/declarations"; | ||
import * as _ from "lodash"; | ||
import { injector } from "../../common/yok"; | ||
|
||
interface IDependencyDescription { | ||
parent: IDependencyDescription; | ||
parentDir: string; | ||
name: string; | ||
depth: number; | ||
parent: IDependencyDescription; | ||
parentDir: string; | ||
name: string; | ||
depth: number; | ||
} | ||
|
||
export class NodeModulesDependenciesBuilder | ||
implements INodeModulesDependenciesBuilder { | ||
public constructor(private $fs: IFileSystem) { | ||
} | ||
|
||
public getProductionDependencies(projectPath: string, ignore?: string[]): IDependencyData[] { | ||
const projectPackageJsonPath = path.join( | ||
projectPath, | ||
PACKAGE_JSON_FILE_NAME | ||
); | ||
const packageJsonContent = this.$fs.readJson(projectPackageJsonPath); | ||
const dependencies = packageJsonContent && packageJsonContent.dependencies; | ||
|
||
const resolvedDependencies: IDependencyData[] = []; | ||
|
||
const queue: IDependencyDescription[] = _.keys(dependencies).map( | ||
(dependencyName) => ({ | ||
parent: null, | ||
parentDir: projectPath, | ||
name: dependencyName, | ||
depth: 0, | ||
}) | ||
); | ||
|
||
while (queue.length) { | ||
const currentModule = queue.shift(); | ||
const resolvedDependency = this.findModule( | ||
currentModule, | ||
resolvedDependencies | ||
); | ||
|
||
if ( | ||
resolvedDependency && | ||
!_.some( | ||
resolvedDependencies, | ||
(r) => r.directory === resolvedDependency.directory | ||
) | ||
) { | ||
_.each(resolvedDependency.dependencies, (d) => { | ||
const dependency: IDependencyDescription = { | ||
parent: currentModule, | ||
name: d, | ||
parentDir: resolvedDependency.directory, | ||
depth: resolvedDependency.depth + 1, | ||
}; | ||
|
||
const shouldAdd = !_.some( | ||
queue, | ||
(element) => | ||
element.parent === dependency.parent && | ||
element.name === dependency.name && | ||
element.parentDir === dependency.parentDir && | ||
element.depth === dependency.depth | ||
); | ||
|
||
if (shouldAdd) { | ||
queue.push(dependency); | ||
} | ||
}); | ||
|
||
resolvedDependencies.push(resolvedDependency); | ||
} | ||
} | ||
if (ignore && ignore.length > 0) { | ||
return resolvedDependencies.filter(d => ignore.indexOf(d.name) === -1); | ||
} | ||
return resolvedDependencies; | ||
} | ||
|
||
private findModule( | ||
depDescription: IDependencyDescription, | ||
resolvedDependencies: IDependencyData[], | ||
): IDependencyData { | ||
try { | ||
const parentModulesPath = depDescription?.parentDir ?? depDescription?.parent?.parentDir; | ||
const modulePath = require.resolve(`${depDescription.name}/package.json`, { | ||
paths: [parentModulesPath] | ||
}).replace('/package.json', '') | ||
|
||
// if we already resolved this dependency, we return null to avoid a duplicate resolution | ||
if (resolvedDependencies.some(r => { | ||
return r.name === depDescription.name && r.directory === modulePath | ||
})) { | ||
return null; | ||
} | ||
|
||
return this.getDependencyData( | ||
depDescription.name, | ||
modulePath, | ||
depDescription.depth | ||
); | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
|
||
private getDependencyData( | ||
name: string, | ||
directory: string, | ||
depth: number | ||
): IDependencyData { | ||
const dependency: IDependencyData = { | ||
name, | ||
directory, | ||
depth, | ||
version: null, | ||
}; | ||
|
||
const packageJsonPath = path.join(directory, PACKAGE_JSON_FILE_NAME); | ||
const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile(); | ||
|
||
if (packageJsonExists) { | ||
const packageJsonContents = this.$fs.readJson(packageJsonPath); | ||
|
||
dependency.version = packageJsonContents.version; | ||
if (!!packageJsonContents.nativescript) { | ||
// add `nativescript` property, necessary for resolving plugins | ||
dependency.nativescript = packageJsonContents.nativescript; | ||
} | ||
|
||
dependency.dependencies = _.keys(packageJsonContents.dependencies); | ||
return dependency; | ||
} | ||
|
||
return null; | ||
} | ||
implements INodeModulesDependenciesBuilder { | ||
public constructor(private $fs: IFileSystem) {} | ||
|
||
public getProductionDependencies( | ||
projectPath: string, | ||
ignore?: string[] | ||
): IDependencyData[] { | ||
const projectPackageJsonPath = path.join( | ||
projectPath, | ||
PACKAGE_JSON_FILE_NAME | ||
); | ||
const packageJsonContent = this.$fs.readJson(projectPackageJsonPath); | ||
const dependencies = packageJsonContent && packageJsonContent.dependencies; | ||
|
||
const resolvedDependencies: IDependencyData[] = []; | ||
|
||
const queue: IDependencyDescription[] = _.keys(dependencies).map( | ||
(dependencyName) => ({ | ||
parent: null, | ||
parentDir: projectPath, | ||
name: dependencyName, | ||
depth: 0, | ||
}) | ||
); | ||
|
||
while (queue.length) { | ||
const currentModule = queue.shift(); | ||
const resolvedDependency = this.findModule( | ||
currentModule, | ||
resolvedDependencies | ||
); | ||
|
||
if ( | ||
resolvedDependency && | ||
!_.some( | ||
resolvedDependencies, | ||
(r) => r.directory === resolvedDependency.directory | ||
) | ||
) { | ||
_.each(resolvedDependency.dependencies, (d) => { | ||
const dependency: IDependencyDescription = { | ||
parent: currentModule, | ||
name: d, | ||
parentDir: resolvedDependency.directory, | ||
depth: resolvedDependency.depth + 1, | ||
}; | ||
|
||
const shouldAdd = !_.some( | ||
queue, | ||
(element) => | ||
element.parent === dependency.parent && | ||
element.name === dependency.name && | ||
element.parentDir === dependency.parentDir && | ||
element.depth === dependency.depth | ||
); | ||
|
||
if (shouldAdd) { | ||
queue.push(dependency); | ||
} | ||
}); | ||
|
||
resolvedDependencies.push(resolvedDependency); | ||
} | ||
} | ||
if (ignore && ignore.length > 0) { | ||
return resolvedDependencies.filter((d) => ignore.indexOf(d.name) === -1); | ||
} | ||
return resolvedDependencies; | ||
} | ||
|
||
private findModule( | ||
depDescription: IDependencyDescription, | ||
resolvedDependencies: IDependencyData[] | ||
): IDependencyData { | ||
try { | ||
const parentModulesPath = | ||
depDescription?.parentDir ?? depDescription?.parent?.parentDir; | ||
const modulePath = require | ||
.resolve(`${depDescription.name}/package.json`, { | ||
paths: [parentModulesPath], | ||
}) | ||
.replace("package.json", ""); | ||
|
||
// if we already resolved this dependency, we return null to avoid a duplicate resolution | ||
if ( | ||
resolvedDependencies.some((r) => { | ||
return r.name === depDescription.name && r.directory === modulePath; | ||
}) | ||
) { | ||
return null; | ||
} | ||
|
||
return this.getDependencyData( | ||
depDescription.name, | ||
modulePath, | ||
depDescription.depth | ||
); | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
|
||
private getDependencyData( | ||
name: string, | ||
directory: string, | ||
depth: number | ||
): IDependencyData { | ||
const dependency: IDependencyData = { | ||
name, | ||
directory, | ||
depth, | ||
version: null, | ||
}; | ||
|
||
const packageJsonPath = path.join(directory, PACKAGE_JSON_FILE_NAME); | ||
const packageJsonExists = this.$fs.getLsStats(packageJsonPath).isFile(); | ||
|
||
if (packageJsonExists) { | ||
const packageJsonContents = this.$fs.readJson(packageJsonPath); | ||
|
||
dependency.version = packageJsonContents.version; | ||
if (!!packageJsonContents.nativescript) { | ||
// add `nativescript` property, necessary for resolving plugins | ||
dependency.nativescript = packageJsonContents.nativescript; | ||
} | ||
|
||
dependency.dependencies = _.keys(packageJsonContents.dependencies); | ||
return dependency; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
injector.register( | ||
"nodeModulesDependenciesBuilder", | ||
NodeModulesDependenciesBuilder | ||
"nodeModulesDependenciesBuilder", | ||
NodeModulesDependenciesBuilder | ||
); |