Skip to content

Commit

Permalink
feature: remove dependency on ShellJS (#729)
Browse files Browse the repository at this point in the history
* feature: avoid dependning on shelljs

* refactor: remove shelljs from tests

* refactor: simplify configLoader call

* fix: use shell flag when spawning npm init

Co-authored-by: hdmr14 <58992133+hdmr14@users.noreply.github.com>

* refactor: remove unused function isArray

* refactor: remove unused function isString

Co-authored-by: hdmr14 <58992133+hdmr14@users.noreply.github.com>
  • Loading branch information
LinusU and hdmr14 committed May 4, 2020
1 parent ce1042e commit 6ef8afa
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 287 deletions.
27 changes: 3 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -84,7 +84,6 @@
"is-utf8": "^0.2.1",
"lodash": "4.17.15",
"minimist": "1.2.5",
"shelljs": "0.7.6",
"strip-bom": "4.0.0",
"strip-json-comments": "3.0.1"
},
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commitizen.js
@@ -1,6 +1,5 @@
import { init } from '../commitizen';
import { commitizen as commitizenParser } from './parsers';
import * as sh from 'shelljs';

let { parse } = commitizenParser;

Expand All @@ -27,7 +26,7 @@ function bootstrap (environment = {}, argv = process.argv) {
if (adapterNpmName) {
console.log(`Attempting to initialize using the npm package ${adapterNpmName}`);
try {
init(sh, process.cwd(), adapterNpmName, parsedArgs);
init(process.cwd(), adapterNpmName, parsedArgs);
} catch (e) {
console.error(`Error: ${e}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/cli/strategies/git-cz.js
@@ -1,4 +1,3 @@
import sh from 'shelljs';
import inquirer from 'inquirer';
import findRoot from 'find-root';
import { getParsedPackageJsonFromPath } from '../../common/util';
Expand Down Expand Up @@ -56,7 +55,7 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
let adapterPackageJson = getParsedPackageJsonFromPath(resolvedAdapterRootPath);
let cliPackageJson = getParsedPackageJsonFromPath(environment.cliPath);
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
commit(sh, inquirer, process.cwd(), prompter, {
commit(inquirer, process.cwd(), prompter, {
args: parsedGitCzArgs,
disableAppendPaths: true,
emitData: true,
Expand Down
2 changes: 0 additions & 2 deletions src/commitizen.js
Expand Up @@ -2,15 +2,13 @@
import * as adapter from './commitizen/adapter';
import * as cache from './commitizen/cache';
import commit from './commitizen/commit';
import * as configLoader from './commitizen/configLoader';
import init from './commitizen/init';
import * as staging from './commitizen/staging';

export {
adapter,
cache,
commit,
configLoader,
init,
staging
};
12 changes: 6 additions & 6 deletions src/commitizen/adapter.js
@@ -1,9 +1,9 @@
import childProcess from 'child_process';
import path from 'path';
import fs from 'fs';
import findNodeModules from 'find-node-modules';
import _ from 'lodash';
import detectIndent from 'detect-indent';
import sh from 'shelljs';

import { isFunction } from '../common/util';

Expand Down Expand Up @@ -32,7 +32,7 @@ export {
* Modifies the package.json, sets config.commitizen.path to the path of the adapter
* Must be passed an absolute path to the cli's root
*/
function addPathToAdapterConfig (sh, cliPath, repoPath, adapterNpmName) {
function addPathToAdapterConfig (cliPath, repoPath, adapterNpmName) {

let commitizenAdapterConfig = {
config: {
Expand All @@ -42,7 +42,7 @@ function addPathToAdapterConfig (sh, cliPath, repoPath, adapterNpmName) {
}
};

let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
let packageJsonPath = path.join(getNearestProjectRootDirectory(repoPath), 'package.json');
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
// tries to detect the indentation and falls back to a default if it can't
let indent = detectIndent(packageJsonString).indent || ' ';
Expand Down Expand Up @@ -111,8 +111,8 @@ function getNearestNodeModulesDirectory (options) {
/**
* Gets the nearest project root directory
*/
function getNearestProjectRootDirectory (options) {
return path.join(process.cwd(), getNearestNodeModulesDirectory(options), '/../');
function getNearestProjectRootDirectory (repoPath, options) {
return path.join(repoPath, getNearestNodeModulesDirectory(options), '/../');
}

/**
Expand Down Expand Up @@ -180,5 +180,5 @@ function resolveAdapterPath (inboundAdapterPath) {
}

function getGitRootPath () {
return sh.exec('git rev-parse --show-toplevel', { silent: true }).stdout.trim();
return childProcess.spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' }).stdout.trim();
}
10 changes: 5 additions & 5 deletions src/commitizen/commit.js
Expand Up @@ -10,17 +10,17 @@ export default commit;
/**
* Takes all of the final inputs needed in order to make dispatch a git commit
*/
function dispatchGitCommit (sh, repoPath, template, options, overrideOptions, done) {
function dispatchGitCommit (repoPath, template, options, overrideOptions, done) {
// Commit the user input -- side effect that we'll test
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function (error) {
gitCommit(repoPath, template, { ...options, ...overrideOptions }, function (error) {
done(error, template);
});
}

/**
* Asynchronously commits files using commitizen
*/
function commit (sh, inquirer, repoPath, prompter, options, done) {
function commit (inquirer, repoPath, prompter, options, done) {
var cacheDirectory = cacheDir('commitizen');
var cachePath = path.join(cacheDirectory, 'commitizen.json');

Expand All @@ -40,7 +40,7 @@ function commit (sh, inquirer, repoPath, prompter, options, done) {
overrideOptions: retryOverrideOptions,
template: retryTemplate
} = cache.getCacheValueSync(cachePath, repoPath);
dispatchGitCommit(sh, repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);
dispatchGitCommit(repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);

} else {
// Get user input -- side effect that is hard to test
Expand All @@ -59,7 +59,7 @@ function commit (sh, inquirer, repoPath, prompter, options, done) {

// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
dispatchGitCommit(repoPath, template, options, overrideOptions, done);
});
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/commitizen/configLoader.js

This file was deleted.

40 changes: 12 additions & 28 deletions src/commitizen/init.js
@@ -1,7 +1,7 @@
import childProcess from 'child_process';
import path from 'path';
import * as configLoader from './configLoader';
import { executeShellCommand } from '../common/util';
import * as adapter from './adapter';
import * as configLoader from '../configLoader';

let {
addPathToAdapterConfig,
Expand All @@ -15,6 +15,9 @@ export default init;

const CLI_PATH = path.normalize(path.join(__dirname, '../../'));

/** Configuration sources in priority order. */
const LOADER_CONFIGS = ['.czrc', '.cz.json', 'package.json'];

/**
* CZ INIT
*
Expand Down Expand Up @@ -48,7 +51,7 @@ const defaultInitOptions = {
/**
* Runs npm install for the adapter then modifies the config.commitizen as needed
*/
function init (sh, repoPath, adapterNpmName, {
function init (repoPath, adapterNpmName, {
save = false,
saveDev = true,
saveExact = false,
Expand All @@ -60,13 +63,10 @@ function init (sh, repoPath, adapterNpmName, {
} = defaultInitOptions) {

// Don't let things move forward if required args are missing
checkRequiredArguments(sh, repoPath, adapterNpmName);

// Move to the correct directory so we can run commands
sh.cd(repoPath);
checkRequiredArguments(repoPath, adapterNpmName);

// Load the current adapter config
let adapterConfig = loadAdapterConfig();
let adapterConfig = configLoader.loader(LOADER_CONFIGS, null, repoPath);

// Get the npm string mappings based on the arguments provided
let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force);
Expand All @@ -88,11 +88,11 @@ function init (sh, repoPath, adapterNpmName, {
}

try {
executeShellCommand(sh, repoPath, installAdapterCommand);
childProcess.execSync(installAdapterCommand, { cwd: repoPath });
if(includeCommitizen) {
executeShellCommand(sh, repoPath, installCommitizenCommand);
childProcess.execSync(installCommitizenCommand, { cwd: repoPath });
}
addPathToAdapterConfig(sh, CLI_PATH, repoPath, adapterNpmName);
addPathToAdapterConfig(CLI_PATH, repoPath, adapterNpmName);
} catch (e) {
console.error(e);
}
Expand All @@ -102,27 +102,11 @@ function init (sh, repoPath, adapterNpmName, {
* Checks to make sure that the required arguments are passed
* Throws an exception if any are not.
*/
function checkRequiredArguments (sh, path, adapterNpmName) {
if (!sh) {
throw new Error("You must pass an instance of shelljs when running init.");
}
function checkRequiredArguments (path, adapterNpmName) {
if (!path) {
throw new Error("Path is required when running init.");
}
if (!adapterNpmName) {
throw new Error("The adapter's npm name is required when running init.");
}
}

/**
* CONFIG
* Loads and returns the adapter config at key config.commitizen, if it exists
*/
function loadAdapterConfig () {
let config = configLoader.load();
if (config) {
return config;
} else {

}
}
2 changes: 1 addition & 1 deletion src/commitizen/staging.js
Expand Up @@ -8,7 +8,7 @@ export { isClean };
function isClean (repoPath, done) {
exec('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', {
maxBuffer: Infinity,
cwd: repoPath || process.cwd()
cwd: repoPath
}, function (error, stdout) {
if (error) {
return done(error);
Expand Down
40 changes: 0 additions & 40 deletions src/common/util.js
Expand Up @@ -2,24 +2,12 @@ import fs from 'fs';
import path from 'path';

export {
executeShellCommand,
getParsedJsonFromFile,
getParsedPackageJsonFromPath,
isArray,
isFunction,
isString,
isInTest
}

/**
* Executes the command passed to it at the path requested
* using the instance of shelljs passed in
*/
function executeShellCommand (sh, path, installCommand) {
sh.cd(path);
sh.exec(installCommand);
}

/**
* Gets the parsed contents of a json file
*/
Expand All @@ -39,20 +27,6 @@ function getParsedPackageJsonFromPath (path) {
return getParsedJsonFromFile(path, 'package.json');
}

/**
* Test if the passed argument is an array
*/
function isArray (arr) {
if (typeof arr === "undefined")
{
return false;
} else if (arr === null) {
return false;
} else {
return arr.constructor === Array;
}
}

/**
* Test if the passed argument is a function
*/
Expand All @@ -68,20 +42,6 @@ function isFunction (functionToCheck) {
}
}

/**
* Test if the passed argument is a string
*/
function isString (str) {
if (typeof str === "undefined")
{
return false;
} else if (str === null) {
return false;
} else {
return Object.prototype.toString.call(str) === '[object String]';
}
}

function isInTest () {
return typeof global.it === 'function';
}
6 changes: 3 additions & 3 deletions src/configLoader/loader.js
Expand Up @@ -13,13 +13,13 @@ export default loader;

/**
* Get content of the configuration file
* @param {String} config - partial path to configuration file
* @param {String} [cwd = process.cwd()] - directory path which will be joined with config argument
* @param {String|null} config - partial path to configuration file
* @param {String} cwd - directory path which will be joined with config argument
* @return {Object|undefined}
*/
function loader (configs, config, cwd) {
var content;
var directory = cwd || process.cwd();
var directory = cwd;

// If config option is given, attempt to load it
if (config) {
Expand Down

0 comments on commit 6ef8afa

Please sign in to comment.