de-linting | test de-linting service | | Search

This code provides a function to format code within a project directory using WebStorm and potentially import project settings from a .idea directory.

Run example

npm run import -- "delint using webstorm"

delint using webstorm

// TODO: display a tree of require dependencies and make sure it all still compiles properly
var path = require('path');
var {ncp} = require('ncp');
var importer = require('../Core');
var execCmd = importer.import("spawn child process");
ncp.limit = 16;

var webstormWin = 'C:\\Program Files\\JetBrains\\WebStorm 2017.1.1\\bin\\format.bat';
var webstormMac = '/Applications/WebStorm.app/Contents/bin/format.sh';
var webstorm = process.platform === 'win32' ? webstormWin : webstormMac;
var idea = path.resolve(path.join(__dirname, '../.idea'));

function delint(project) {
    // output .idea to set the framework preferences?
    // I think I overwrote the default, some way to env variables for this?
    return util.promisify(ncp)(idea, project + '/.idea')
        .then(() => execCmd(`"${webstorm}" "${project}"`, {cwd: project}))
}

module.exports = delint;

What the code could have been:

// @description: Deletes lint in a project directory.
// @param {string} project - Path to the project directory.
// @return {Promise} - Resolves when the lint deletion is complete.

const path = require('path');
const { promisify } = require('util'); // Use util.promisify to avoid multiple imports
const ncp = require('ncp').promisify(); // Use promisify to avoid callback hell
const importer = require('../Core');
const { spawnChildProcess } = importer.import(); // Use destructuring to improve readability

// Define constants for WebStorm executable paths
const webstormExe = {
  win32: 'C:\\Program Files\\JetBrains\\WebStorm 2017.1.1\\bin\\format.bat',
  darwin: '/Applications/WebStorm.app/Contents/bin/format.sh', // Use darwin for macOS
};

// Define constants for IDEA directory path
const ideaDir = path.resolve(path.join(__dirname, '../.idea'));

/**
 * Deletes lint in a project directory using WebStorm's format tool.
 * @param {string} project - Path to the project directory.
 * @returns {Promise} - Resolves when the lint deletion is complete.
 */
function delint(project) {
  // Limit ncp to 16 concurrent operations
  ncp.limit = 16;

  // Use process.platform to determine the correct executable path
  const webstorm = webstormExe[process.platform];

  // Copy IDEA directory to project directory
  return ncp(ideaDir, path.join(project, '.idea'))
   .then(() => {
      // Execute WebStorm's format tool in the project directory
      return spawnChildProcess(`${webstorm} ${project}`, { cwd: project });
    });
}

module.exports = delint;

This code sets up a function delint that performs code formatting and potentially imports project settings from a .idea directory.

Here's a breakdown:

  1. Dependencies:

  2. WebStorm Path:

  3. .idea Directory:

  4. delint Function:

  5. Export:

In essence, this code aims to format code within a project directory using WebStorm and potentially import project-specific settings from a .idea directory.