Skip to content

Commit

Permalink
Fix npm run local to support projects with different NPM name than it…
Browse files Browse the repository at this point in the history
…s directory (#78)
  • Loading branch information
kpajdzik authored Nov 12, 2018
1 parent 71b0b77 commit 9358628
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions .scripts/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export function resolvePath(...paths: string[]): string {
return path.resolve(...paths).split("\\").join("/");
}

function exists(path: string): boolean {
function exists(path: string | undefined): boolean {
if (!path) {
return false;
}

return fs.existsSync(path);
}

Expand Down Expand Up @@ -109,13 +113,33 @@ function getPackageJsonFilePath(packageFolder: string): string {
return resolvePath(packageFolder, "package.json");
}

function getPackageNameFromPackageJson(packageJsonFilePath: string): string {
const packageJson: { name: string } = getPackageJson(packageJsonFilePath);
return packageJson.name;
}

/**
* Get the absolute path to the local clone of the repository with the provided name.
* @param {string} repoName The name of the repository.
* @returns {string} The absolute path to the local clone of the repository.
*/
export function getLocalRepositoryPath(repoName: string): string {
return resolvePath(getThisRepositoryFolderPath(), "..", repoName);
export function getLocalRepositoryPath(repoName: string): string | undefined {
const repositoriesRoot: string = resolvePath(getThisRepositoryFolderPath(), "..");
const repositoryPaths: string[] = fs.readdirSync(repositoriesRoot).map(childDir => resolvePath(repositoriesRoot, childDir));

for (const repositoryPath of repositoryPaths) {
const packageJsonPath = getPackageJsonFilePath(repositoryPath);
if (!fs.existsSync(packageJsonPath)) {
continue;
}

const packageName = getPackageNameFromPackageJson(packageJsonPath);
if (packageName === repoName) {
return repositoryPath;
}
}

return undefined;
}

/**
Expand Down Expand Up @@ -158,7 +182,11 @@ function getClonedRepositories(dependencies?: { [packageName: string]: string })
* @returns {void}
*/
export function runLocalRepositoryNPMScript(repoName: string, scriptName: string): void {
const repoFolderPath: string = getLocalRepositoryPath(repoName);
const repoFolderPath: string | undefined = getLocalRepositoryPath(repoName);
if (!repoFolderPath) {
return;
}

const packageJsonFilePath: string = getPackageJsonFilePath(repoFolderPath);
const packageJson: any = getPackageJson(packageJsonFilePath);
const repoScripts: any = packageJson.scripts;
Expand Down

0 comments on commit 9358628

Please sign in to comment.