All files / platform/cli/src/commands unlinkPackage.js

0% Statements 0/25
0% Branches 0/4
0% Functions 0/4
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                                                                                                     
import { execa } from 'execa';
import fs from 'fs';
import path from 'path';
import { validateYarn, removeExtensionFromConfig, removeModeFromConfig } from './utils/index.js';
 
const linkPackage = async (packageName, options, removeFromConfig) => {
  const { viewerDirectory } = options;
 
  // make sure yarn is installed
  await validateYarn();
 
  // change directory to OHIF Platform root and execute yarn link
  process.chdir(viewerDirectory);
 
  const results = await execa(`yarn`, ['unlink', packageName]);
  console.log(results.stdout);
 
  const webpackPwaPath = path.join(viewerDirectory, '.webpack', 'webpack.pwa.js');
 
  await removePathFromWebpackConfig(webpackPwaPath, packageName);
 
  //update the plugin.json file
  removeFromConfig(packageName);
 
  // run prettier on the webpack config
  await execa(`yarn`, ['prettier', '--write', webpackPwaPath]);
};
 
async function removePathFromWebpackConfig(webpackConfigPath, packageName) {
  const fileContent = await fs.promises.readFile(webpackConfigPath, 'utf8');
 
  const packageNameSubstring = `${packageName}/node_modules`;
  const pathResolveStart = 'path.resolve(';
  const closingParenthesis = ')';
 
  let startIndex = fileContent.indexOf(packageNameSubstring);
 
  if (startIndex === -1) {
    return;
  }
 
  // Find the start of the "path.resolve" line.
  startIndex = fileContent.lastIndexOf(pathResolveStart, startIndex);
 
  // Find the end of the line with the closing parenthesis.
  let endIndex = fileContent.indexOf(closingParenthesis, startIndex) + 1;
 
  // Check if there's a comma after the closing parenthesis and remove it as well.
  if (fileContent[endIndex] === ',') {
    endIndex++;
  }
 
  const modifiedFileContent = fileContent.slice(0, startIndex) + fileContent.slice(endIndex);
 
  await fs.promises.writeFile(webpackConfigPath, modifiedFileContent);
}
 
function unlinkExtension(extensionName, options) {
  linkPackage(extensionName, options, removeExtensionFromConfig);
}
 
function unlinkMode(modeName, options) {
  linkPackage(modeName, options, removeModeFromConfig);
}
 
export { unlinkExtension, unlinkMode };