Skip to content

Commit 6ef8afa

Browse files
LinusUhdmr14
andauthored
feature: remove dependency on ShellJS (#729)
* 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 <[email protected]> * refactor: remove unused function isArray * refactor: remove unused function isString Co-authored-by: hdmr14 <[email protected]>
1 parent ce1042e commit 6ef8afa

24 files changed

+131
-287
lines changed

package-lock.json

+3-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
"is-utf8": "^0.2.1",
8585
"lodash": "4.17.15",
8686
"minimist": "1.2.5",
87-
"shelljs": "0.7.6",
8887
"strip-bom": "4.0.0",
8988
"strip-json-comments": "3.0.1"
9089
},

src/cli/commitizen.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { init } from '../commitizen';
22
import { commitizen as commitizenParser } from './parsers';
3-
import * as sh from 'shelljs';
43

54
let { parse } = commitizenParser;
65

@@ -27,7 +26,7 @@ function bootstrap (environment = {}, argv = process.argv) {
2726
if (adapterNpmName) {
2827
console.log(`Attempting to initialize using the npm package ${adapterNpmName}`);
2928
try {
30-
init(sh, process.cwd(), adapterNpmName, parsedArgs);
29+
init(process.cwd(), adapterNpmName, parsedArgs);
3130
} catch (e) {
3231
console.error(`Error: ${e}`);
3332
}

src/cli/strategies/git-cz.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sh from 'shelljs';
21
import inquirer from 'inquirer';
32
import findRoot from 'find-root';
43
import { getParsedPackageJsonFromPath } from '../../common/util';
@@ -56,7 +55,7 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
5655
let adapterPackageJson = getParsedPackageJsonFromPath(resolvedAdapterRootPath);
5756
let cliPackageJson = getParsedPackageJsonFromPath(environment.cliPath);
5857
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
59-
commit(sh, inquirer, process.cwd(), prompter, {
58+
commit(inquirer, process.cwd(), prompter, {
6059
args: parsedGitCzArgs,
6160
disableAppendPaths: true,
6261
emitData: true,

src/commitizen.js

-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
import * as adapter from './commitizen/adapter';
33
import * as cache from './commitizen/cache';
44
import commit from './commitizen/commit';
5-
import * as configLoader from './commitizen/configLoader';
65
import init from './commitizen/init';
76
import * as staging from './commitizen/staging';
87

98
export {
109
adapter,
1110
cache,
1211
commit,
13-
configLoader,
1412
init,
1513
staging
1614
};

src/commitizen/adapter.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import childProcess from 'child_process';
12
import path from 'path';
23
import fs from 'fs';
34
import findNodeModules from 'find-node-modules';
45
import _ from 'lodash';
56
import detectIndent from 'detect-indent';
6-
import sh from 'shelljs';
77

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

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

3737
let commitizenAdapterConfig = {
3838
config: {
@@ -42,7 +42,7 @@ function addPathToAdapterConfig (sh, cliPath, repoPath, adapterNpmName) {
4242
}
4343
};
4444

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

118118
/**
@@ -180,5 +180,5 @@ function resolveAdapterPath (inboundAdapterPath) {
180180
}
181181

182182
function getGitRootPath () {
183-
return sh.exec('git rev-parse --show-toplevel', { silent: true }).stdout.trim();
183+
return childProcess.spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' }).stdout.trim();
184184
}

src/commitizen/commit.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export default commit;
1010
/**
1111
* Takes all of the final inputs needed in order to make dispatch a git commit
1212
*/
13-
function dispatchGitCommit (sh, repoPath, template, options, overrideOptions, done) {
13+
function dispatchGitCommit (repoPath, template, options, overrideOptions, done) {
1414
// Commit the user input -- side effect that we'll test
15-
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function (error) {
15+
gitCommit(repoPath, template, { ...options, ...overrideOptions }, function (error) {
1616
done(error, template);
1717
});
1818
}
1919

2020
/**
2121
* Asynchronously commits files using commitizen
2222
*/
23-
function commit (sh, inquirer, repoPath, prompter, options, done) {
23+
function commit (inquirer, repoPath, prompter, options, done) {
2424
var cacheDirectory = cacheDir('commitizen');
2525
var cachePath = path.join(cacheDirectory, 'commitizen.json');
2626

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

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

6060
// We don't want to add retries to the cache, only actual commands
6161
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
62-
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
62+
dispatchGitCommit(repoPath, template, options, overrideOptions, done);
6363
});
6464
}
6565
}

src/commitizen/configLoader.js

-10
This file was deleted.

src/commitizen/init.js

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import childProcess from 'child_process';
12
import path from 'path';
2-
import * as configLoader from './configLoader';
3-
import { executeShellCommand } from '../common/util';
43
import * as adapter from './adapter';
4+
import * as configLoader from '../configLoader';
55

66
let {
77
addPathToAdapterConfig,
@@ -15,6 +15,9 @@ export default init;
1515

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

18+
/** Configuration sources in priority order. */
19+
const LOADER_CONFIGS = ['.czrc', '.cz.json', 'package.json'];
20+
1821
/**
1922
* CZ INIT
2023
*
@@ -48,7 +51,7 @@ const defaultInitOptions = {
4851
/**
4952
* Runs npm install for the adapter then modifies the config.commitizen as needed
5053
*/
51-
function init (sh, repoPath, adapterNpmName, {
54+
function init (repoPath, adapterNpmName, {
5255
save = false,
5356
saveDev = true,
5457
saveExact = false,
@@ -60,13 +63,10 @@ function init (sh, repoPath, adapterNpmName, {
6063
} = defaultInitOptions) {
6164

6265
// Don't let things move forward if required args are missing
63-
checkRequiredArguments(sh, repoPath, adapterNpmName);
64-
65-
// Move to the correct directory so we can run commands
66-
sh.cd(repoPath);
66+
checkRequiredArguments(repoPath, adapterNpmName);
6767

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

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

9090
try {
91-
executeShellCommand(sh, repoPath, installAdapterCommand);
91+
childProcess.execSync(installAdapterCommand, { cwd: repoPath });
9292
if(includeCommitizen) {
93-
executeShellCommand(sh, repoPath, installCommitizenCommand);
93+
childProcess.execSync(installCommitizenCommand, { cwd: repoPath });
9494
}
95-
addPathToAdapterConfig(sh, CLI_PATH, repoPath, adapterNpmName);
95+
addPathToAdapterConfig(CLI_PATH, repoPath, adapterNpmName);
9696
} catch (e) {
9797
console.error(e);
9898
}
@@ -102,27 +102,11 @@ function init (sh, repoPath, adapterNpmName, {
102102
* Checks to make sure that the required arguments are passed
103103
* Throws an exception if any are not.
104104
*/
105-
function checkRequiredArguments (sh, path, adapterNpmName) {
106-
if (!sh) {
107-
throw new Error("You must pass an instance of shelljs when running init.");
108-
}
105+
function checkRequiredArguments (path, adapterNpmName) {
109106
if (!path) {
110107
throw new Error("Path is required when running init.");
111108
}
112109
if (!adapterNpmName) {
113110
throw new Error("The adapter's npm name is required when running init.");
114111
}
115112
}
116-
117-
/**
118-
* CONFIG
119-
* Loads and returns the adapter config at key config.commitizen, if it exists
120-
*/
121-
function loadAdapterConfig () {
122-
let config = configLoader.load();
123-
if (config) {
124-
return config;
125-
} else {
126-
127-
}
128-
}

src/commitizen/staging.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { isClean };
88
function isClean (repoPath, done) {
99
exec('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', {
1010
maxBuffer: Infinity,
11-
cwd: repoPath || process.cwd()
11+
cwd: repoPath
1212
}, function (error, stdout) {
1313
if (error) {
1414
return done(error);

src/common/util.js

-40
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@ import fs from 'fs';
22
import path from 'path';
33

44
export {
5-
executeShellCommand,
65
getParsedJsonFromFile,
76
getParsedPackageJsonFromPath,
8-
isArray,
97
isFunction,
10-
isString,
118
isInTest
129
}
1310

14-
/**
15-
* Executes the command passed to it at the path requested
16-
* using the instance of shelljs passed in
17-
*/
18-
function executeShellCommand (sh, path, installCommand) {
19-
sh.cd(path);
20-
sh.exec(installCommand);
21-
}
22-
2311
/**
2412
* Gets the parsed contents of a json file
2513
*/
@@ -39,20 +27,6 @@ function getParsedPackageJsonFromPath (path) {
3927
return getParsedJsonFromFile(path, 'package.json');
4028
}
4129

42-
/**
43-
* Test if the passed argument is an array
44-
*/
45-
function isArray (arr) {
46-
if (typeof arr === "undefined")
47-
{
48-
return false;
49-
} else if (arr === null) {
50-
return false;
51-
} else {
52-
return arr.constructor === Array;
53-
}
54-
}
55-
5630
/**
5731
* Test if the passed argument is a function
5832
*/
@@ -68,20 +42,6 @@ function isFunction (functionToCheck) {
6842
}
6943
}
7044

71-
/**
72-
* Test if the passed argument is a string
73-
*/
74-
function isString (str) {
75-
if (typeof str === "undefined")
76-
{
77-
return false;
78-
} else if (str === null) {
79-
return false;
80-
} else {
81-
return Object.prototype.toString.call(str) === '[object String]';
82-
}
83-
}
84-
8545
function isInTest () {
8646
return typeof global.it === 'function';
8747
}

src/configLoader/loader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export default loader;
1313

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

2424
// If config option is given, attempt to load it
2525
if (config) {

0 commit comments

Comments
 (0)