-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from x3cion/schematic-runner-pathfinding
refactor() make schematic runner more robust
- Loading branch information
Showing
2 changed files
with
77 additions
and
76 deletions.
There are no files selected for viewing
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,37 +1,25 @@ | ||
import { existsSync } from 'fs'; | ||
import { join, sep } from 'path'; | ||
import { join, resolve } from 'path'; | ||
import { AbstractRunner } from './abstract.runner'; | ||
|
||
export class SchematicRunner extends AbstractRunner { | ||
constructor() { | ||
super(`"${SchematicRunner.findClosestSchematicsBinary(__dirname)}"`); | ||
super(`"${SchematicRunner.findClosestSchematicsBinary()}"`); | ||
} | ||
|
||
public static findClosestSchematicsBinary(path: string): string { | ||
const segments = path.split(sep); | ||
const binaryPath = ['node_modules', '.bin', 'schematics']; | ||
|
||
const combineSegments = (pkgLastIndex: number) => [ | ||
sep, | ||
...segments.slice(0, pkgLastIndex), | ||
...binaryPath, | ||
]; | ||
const globalBinPathSegments = combineSegments( | ||
segments.lastIndexOf('cli') + 1, | ||
); | ||
const schematicsGlobalPath = join(...globalBinPathSegments); | ||
if (existsSync(schematicsGlobalPath)) { | ||
return schematicsGlobalPath; | ||
} | ||
public static getModulePaths() { | ||
return module.paths; | ||
} | ||
|
||
const localBinPathSegments = combineSegments( | ||
segments.lastIndexOf('node_modules'), | ||
); | ||
const schematicsLocalPath = join(...localBinPathSegments); | ||
if (existsSync(schematicsLocalPath)) { | ||
return schematicsLocalPath; | ||
public static findClosestSchematicsBinary(): string { | ||
const subPath = join('.bin', 'schematics'); | ||
for (const path of this.getModulePaths()) { | ||
const binaryPath = resolve(path, subPath); | ||
if (existsSync(binaryPath)) { | ||
return binaryPath; | ||
} | ||
} | ||
|
||
return join(__dirname, '../..', 'node_modules/.bin/schematics'); | ||
throw new Error("'schematics' binary path could not be found!"); | ||
} | ||
} |
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