Skip to content

Commit 9e4b735

Browse files
committed
Minor refactors and fixes to install-run.
1 parent 6081dbf commit 9e4b735

2 files changed

Lines changed: 47 additions & 40 deletions

File tree

apps/rush-lib/src/scripts/install-run.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ function resolvePackageVersion(rushCommonFolder: string, { name, version }: IPac
6262
} else {
6363
// version resolves to
6464
try {
65-
const rushTempFolder: string = ensureAndResolveFolder(rushCommonFolder, 'temp');
66-
copyNpmrcIfItExists(rushCommonFolder, rushTempFolder);
65+
const rushTempFolder: string = ensureAndJoinPath(rushCommonFolder, 'temp');
66+
syncNpmrc(rushCommonFolder, rushTempFolder);
6767
const npmPath: string = getNpmPath();
6868

6969
// This returns something that looks like:
@@ -170,28 +170,28 @@ export function findRushJsonFolder(): string {
170170
* Does not support "." or ".." path segments.
171171
* Assumes the baseFolder exists.
172172
*/
173-
function ensureAndResolveFolder(baseFolder: string, ...pathSegments: string[]): string {
174-
let resolvedDirectory: string = baseFolder;
173+
function ensureAndJoinPath(baseFolder: string, ...pathSegments: string[]): string {
174+
let joinedPath: string = baseFolder;
175175
try {
176176
for (let pathSegment of pathSegments) {
177177
pathSegment = pathSegment.replace(/[\\\/]/g, '+');
178-
resolvedDirectory = path.resolve(resolvedDirectory, pathSegment);
179-
if (!fs.existsSync(resolvedDirectory)) {
180-
fs.mkdirSync(resolvedDirectory);
178+
joinedPath = path.join(joinedPath, pathSegment);
179+
if (!fs.existsSync(joinedPath)) {
180+
fs.mkdirSync(joinedPath);
181181
}
182182
}
183183
} catch (e) {
184-
throw new Error(`Error building local installation directory (${path.resolve(baseFolder, ...pathSegments)}): ${e}`);
184+
throw new Error(`Error building local installation folder (${path.join(baseFolder, ...pathSegments)}): ${e}`);
185185
}
186186

187-
return resolvedDirectory;
187+
return joinedPath;
188188
}
189189

190-
function copyNpmrcIfItExists(rushCommonFolder: string, packageInstallFolder: string): void {
190+
function syncNpmrc(rushCommonFolder: string, targetFolder: string): void {
191191
const npmrcPath: string = path.join(rushCommonFolder, 'config', 'rush', '.npmrc');
192-
const packageInstallNpmrcPath: string = path.join(packageInstallFolder, '.npmrc');
193-
if (fs.existsSync(npmrcPath)) {
194-
try {
192+
const targetNpmrcPath: string = path.join(targetFolder, '.npmrc');
193+
try {
194+
if (fs.existsSync(npmrcPath)) {
195195
let npmrcFileLines: string[] = fs.readFileSync(npmrcPath).toString().split('\n');
196196
npmrcFileLines = npmrcFileLines.map((line) => (line || '').trim());
197197
const resultLines: string[] = [];
@@ -216,10 +216,13 @@ function copyNpmrcIfItExists(rushCommonFolder: string, packageInstallFolder: str
216216
}
217217
}
218218

219-
fs.writeFileSync(packageInstallNpmrcPath, resultLines.join(os.EOL));
220-
} catch (e) {
221-
throw new Error(`Error reading or writing .npmrc file: ${e}`);
219+
fs.writeFileSync(targetNpmrcPath, resultLines.join(os.EOL));
220+
} else if (fs.existsSync(targetNpmrcPath)) {
221+
// If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
222+
fs.unlinkSync(targetNpmrcPath);
222223
}
224+
} catch (e) {
225+
throw new Error(`Error syncing .npmrc file: ${e}`);
223226
}
224227
}
225228

@@ -260,7 +263,7 @@ function cleanInstallFolder(rushCommonFolder: string, packageInstallFolder: stri
260263

261264
const nodeModulesFolder: string = path.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME);
262265
if (fs.existsSync(nodeModulesFolder)) {
263-
const rushRecyclerFolder: string = ensureAndResolveFolder(
266+
const rushRecyclerFolder: string = ensureAndJoinPath(
264267
rushCommonFolder,
265268
'temp',
266269
'rush-recycler',
@@ -349,7 +352,7 @@ export function installAndRun(
349352
): number {
350353
const rushJsonFolder: string = findRushJsonFolder();
351354
const rushCommonFolder: string = path.join(rushJsonFolder, 'common');
352-
const packageInstallFolder: string = ensureAndResolveFolder(
355+
const packageInstallFolder: string = ensureAndJoinPath(
353356
rushCommonFolder,
354357
'temp',
355358
'install-run',
@@ -359,7 +362,7 @@ export function installAndRun(
359362
if (!isPackageAlreadyInstalled(packageInstallFolder)) {
360363
// The package isn't already installed
361364
cleanInstallFolder(rushCommonFolder, packageInstallFolder);
362-
copyNpmrcIfItExists(rushCommonFolder, packageInstallFolder);
365+
syncNpmrc(rushCommonFolder, packageInstallFolder);
363366
createPackageJson(packageInstallFolder, packageName, packageVersion);
364367
installPackage(packageInstallFolder, packageName, packageVersion);
365368
writeFlagFile(packageInstallFolder);
@@ -418,7 +421,7 @@ function run(): void {
418421

419422
runWithErrorAndStatusCode(() => {
420423
const rushJsonFolder: string = findRushJsonFolder();
421-
const rushCommonFolder: string = ensureAndResolveFolder(rushJsonFolder, 'common');
424+
const rushCommonFolder: string = ensureAndJoinPath(rushJsonFolder, 'common');
422425

423426
const packageSpecifier: IPackageSpecifier = parsePackageSpecifier(rawPackageSpecifier);
424427
const name: string = packageSpecifier.name;

common/scripts/install-run.js

Lines changed: 24 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)