@rushstack/lockfile-explorer
Version:
Rush Lockfile Explorer: The UI for solving version conflicts quickly in a large monorepo
77 lines • 4.28 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
// This function will read the current directory and try to figure out if it's a rush project or regular pnpm workspace
// Currently it will throw error if neither can be determined
import path from 'node:path';
import { FileSystem, Path } from '@rushstack/node-core-library';
import { RushConfiguration } from '@microsoft/rush-lib/lib/api/RushConfiguration';
import * as lockfilePath from '../graph/lockfilePath';
export const init = (options) => {
const { lockfileExplorerProjectRoot, appVersion, debugMode, subspaceName } = options;
const currentWorkingDirectory = path.resolve(process.cwd());
let appState;
let currentFolder = Path.convertToSlashes(currentWorkingDirectory);
while (currentFolder.includes('/')) {
// Look for a rush.json [rush project] or pnpm-lock.yaml file [regular pnpm workspace]
const rushJsonPath = currentFolder + '/rush.json';
const pnpmLockPath = currentFolder + '/pnpm-lock.yaml';
if (FileSystem.exists(rushJsonPath)) {
console.log('Found a Rush workspace: ', rushJsonPath);
const rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonPath);
const subspace = rushConfiguration.getSubspace(subspaceName);
const commonTempFolder = subspace.getSubspaceTempFolderPath();
const pnpmLockfileAbsolutePath = path.join(commonTempFolder, 'pnpm-lock.yaml');
const relativeCommonTempFolder = Path.convertToSlashes(path.relative(currentFolder, subspace.getSubspaceTempFolderPath()));
const pnpmLockfileRelativePath = lockfilePath.join(relativeCommonTempFolder, 'pnpm-lock.yaml');
const pnpmFileRelativePath = lockfilePath.join(relativeCommonTempFolder, '.pnpmfile.cjs');
const relativeCommonConfigFolder = Path.convertToSlashes(path.relative(currentFolder, subspace.getSubspaceConfigFolderPath()));
const rushPnpmFileRelativePath = lockfilePath.join(relativeCommonConfigFolder, '.pnpmfile.cjs');
appState = {
currentWorkingDirectory,
appVersion,
debugMode,
lockfileExplorerProjectRoot,
pnpmLockfileLocation: pnpmLockfileAbsolutePath,
pnpmfileLocation: commonTempFolder + '/.pnpmfile.cjs',
projectRoot: currentFolder,
lfxWorkspace: {
workspaceRootFullPath: currentFolder,
pnpmLockfilePath: Path.convertToSlashes(pnpmLockfileRelativePath),
pnpmLockfileFolder: Path.convertToSlashes(path.dirname(pnpmLockfileRelativePath)),
pnpmfilePath: Path.convertToSlashes(pnpmFileRelativePath),
rushConfig: {
rushVersion: rushConfiguration.rushConfigurationJson.rushVersion,
subspaceName: subspaceName !== null && subspaceName !== void 0 ? subspaceName : '',
rushPnpmfilePath: rushPnpmFileRelativePath
}
}
};
break;
}
else if (FileSystem.exists(pnpmLockPath)) {
appState = {
currentWorkingDirectory,
appVersion,
debugMode,
lockfileExplorerProjectRoot,
pnpmLockfileLocation: currentFolder + '/pnpm-lock.yaml',
pnpmfileLocation: currentFolder + '/.pnpmfile.cjs',
projectRoot: currentFolder,
lfxWorkspace: {
workspaceRootFullPath: currentFolder,
pnpmLockfilePath: Path.convertToSlashes(path.relative(currentFolder, pnpmLockPath)),
pnpmLockfileFolder: '',
pnpmfilePath: '.pnpmfile.cjs',
rushConfig: undefined
}
};
break;
}
currentFolder = currentFolder.substring(0, currentFolder.lastIndexOf('/'));
}
if (!appState) {
throw new Error('Could not find a Rush or PNPM workspace!');
}
return appState;
};
//# sourceMappingURL=init.js.map