Skip to content

Commit c7af218

Browse files
committed
feature: avoid dependning on shelljs
1 parent ce1042e commit c7af218

File tree

15 files changed

+78
-90
lines changed

15 files changed

+78
-90
lines changed

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"proxyquire": "^2.1.0",
6969
"semantic-release": "15.13.18",
7070
"semver": "6.2.0",
71+
"shelljs": "0.7.6",
7172
"sinon": "^6.3.4",
7273
"uuid": "3.3.2"
7374
},
@@ -84,7 +85,6 @@
8485
"is-utf8": "^0.2.1",
8586
"lodash": "4.17.15",
8687
"minimist": "1.2.5",
87-
"shelljs": "0.7.6",
8888
"strip-bom": "4.0.0",
8989
"strip-json-comments": "3.0.1"
9090
},

src/cli/commitizen.js

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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/adapter.js

Lines changed: 3 additions & 3 deletions
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: {
@@ -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

Lines changed: 5 additions & 5 deletions
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/init.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import childProcess from 'child_process';
12
import path from 'path';
23
import * as configLoader from './configLoader';
3-
import { executeShellCommand } from '../common/util';
44
import * as adapter from './adapter';
55

66
let {
@@ -48,7 +48,7 @@ const defaultInitOptions = {
4848
/**
4949
* Runs npm install for the adapter then modifies the config.commitizen as needed
5050
*/
51-
function init (sh, repoPath, adapterNpmName, {
51+
function init (repoPath, adapterNpmName, {
5252
save = false,
5353
saveDev = true,
5454
saveExact = false,
@@ -60,10 +60,7 @@ function init (sh, repoPath, adapterNpmName, {
6060
} = defaultInitOptions) {
6161

6262
// 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);
63+
checkRequiredArguments(repoPath, adapterNpmName);
6764

6865
// Load the current adapter config
6966
let adapterConfig = loadAdapterConfig();
@@ -88,11 +85,11 @@ function init (sh, repoPath, adapterNpmName, {
8885
}
8986

9087
try {
91-
executeShellCommand(sh, repoPath, installAdapterCommand);
88+
childProcess.execSync(installAdapterCommand, { cwd: repoPath });
9289
if(includeCommitizen) {
93-
executeShellCommand(sh, repoPath, installCommitizenCommand);
90+
childProcess.execSync(installCommitizenCommand, { cwd: repoPath });
9491
}
95-
addPathToAdapterConfig(sh, CLI_PATH, repoPath, adapterNpmName);
92+
addPathToAdapterConfig(CLI_PATH, repoPath, adapterNpmName);
9693
} catch (e) {
9794
console.error(e);
9895
}
@@ -102,10 +99,7 @@ function init (sh, repoPath, adapterNpmName, {
10299
* Checks to make sure that the required arguments are passed
103100
* Throws an exception if any are not.
104101
*/
105-
function checkRequiredArguments (sh, path, adapterNpmName) {
106-
if (!sh) {
107-
throw new Error("You must pass an instance of shelljs when running init.");
108-
}
102+
function checkRequiredArguments (path, adapterNpmName) {
109103
if (!path) {
110104
throw new Error("Path is required when running init.");
111105
}

src/common/util.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'fs';
22
import path from 'path';
33

44
export {
5-
executeShellCommand,
65
getParsedJsonFromFile,
76
getParsedPackageJsonFromPath,
87
isArray,
@@ -11,15 +10,6 @@ export {
1110
isInTest
1211
}
1312

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-
2313
/**
2414
* Gets the parsed contents of a json file
2515
*/

src/git/add.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import childProcess from 'child_process';
2+
13
export {
24
addPath,
35
addFile
@@ -6,15 +8,13 @@ export {
68
/**
79
* Synchronously adds a path to git staging
810
*/
9-
function addPath (sh, repoPath) {
10-
sh.cd(repoPath);
11-
sh.exec('git add .');
11+
function addPath (repoPath) {
12+
childProcess.spawnSync('git', ['add', '.'], { cwd: repoPath });
1213
}
1314

1415
/**
1516
* Synchronously adds a file to git staging
1617
*/
17-
function addFile (sh, repoPath, filename) {
18-
sh.cd(repoPath);
19-
sh.exec('git add ' + filename)
18+
function addFile (repoPath, filename) {
19+
childProcess.spawnSync('git', ['add', filename], { cwd: repoPath });
2020
}

src/git/commit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { commit };
1111
/**
1212
* Asynchronously git commit at a given path with a message
1313
*/
14-
function commit (sh, repoPath, message, options, done) {
14+
function commit (repoPath, message, options, done) {
1515
let called = false;
1616

1717
// commit the file by spawning a git process, unless the --hook
@@ -39,7 +39,7 @@ function commit (sh, repoPath, message, options, done) {
3939
if (code === 128) {
4040
console.warn(`
4141
Git exited with code 128. Did you forget to run:
42-
42+
4343
git config --global user.email "you@example.com"
4444
git config --global user.name "Your Name"
4545
`)

src/git/init.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import childProcess from 'child_process';
2+
13
export { init };
24

35
/**
46
* Synchronously creates a new git repo at a path
57
*/
6-
function init (sh, repoPath) {
7-
sh.cd(repoPath);
8-
sh.exec('git init');
8+
function init (repoPath) {
9+
childProcess.spawnSync('git', ['init'], { cwd: repoPath });
910
}

test/tests/adapter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('adapter', function () {
5454
};
5555

5656
// Install an adapter
57-
commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog');
57+
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog');
5858

5959
// TEST
6060
expect(function () { adapter.resolveAdapterPath('IAMANIMPOSSIBLEPATH'); }).to.throw(Error);
@@ -95,7 +95,7 @@ describe('adapter', function () {
9595
};
9696

9797
// Install an adapter
98-
commitizenInit(sh, config.paths.endUserRepo, '@commitizen/cz-conventional-changelog');
98+
commitizenInit(config.paths.endUserRepo, '@commitizen/cz-conventional-changelog');
9999

100100
// TEST
101101
expect(function () { adapter.resolveAdapterPath('IAMANIMPOSSIBLEPATH'); }).to.throw(Error);
@@ -131,7 +131,7 @@ describe('adapter', function () {
131131
};
132132

133133
// Install an adapter
134-
commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', {includeCommitizen: true});
134+
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', {includeCommitizen: true});
135135

136136
// TEST
137137
expect(function () { adapter.getPrompter('IAMANIMPOSSIBLEPATH'); }).to.throw(Error);
@@ -167,7 +167,7 @@ describe('adapter', function () {
167167
};
168168

169169
// Install an adapter
170-
commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog-default-export');
170+
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog-default-export');
171171

172172
// TEST
173173
expect(function () { adapter.getPrompter('IAMANIMPOSSIBLEPATH'); }).to.throw(Error);

0 commit comments

Comments
 (0)