Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on ShellJS #729

Merged
merged 6 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
10 changes: 0 additions & 10 deletions src/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'fs';
import path from 'path';

export {
executeShellCommand,
getParsedJsonFromFile,
getParsedPackageJsonFromPath,
isArray,
Expand All @@ -11,15 +10,6 @@ export {
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 Down
6 changes: 3 additions & 3 deletions src/configLoader/loader.js
Original file line number Diff line number Diff line change
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
12 changes: 6 additions & 6 deletions src/git/add.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import childProcess from 'child_process';

export {
addPath,
addFile
Expand All @@ -6,15 +8,13 @@ export {
/**
* Synchronously adds a path to git staging
*/
function addPath (sh, repoPath) {
sh.cd(repoPath);
sh.exec('git add .');
function addPath (repoPath) {
childProcess.spawnSync('git', ['add', '.'], { cwd: repoPath });
}

/**
* Synchronously adds a file to git staging
*/
function addFile (sh, repoPath, filename) {
sh.cd(repoPath);
sh.exec('git add ' + filename)
function addFile (repoPath, filename) {
childProcess.spawnSync('git', ['add', filename], { cwd: repoPath });
}
4 changes: 2 additions & 2 deletions src/git/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { commit };
/**
* Asynchronously git commit at a given path with a message
*/
function commit (sh, repoPath, message, options, done) {
function commit (repoPath, message, options, done) {
let called = false;

// commit the file by spawning a git process, unless the --hook
Expand Down Expand Up @@ -39,7 +39,7 @@ function commit (sh, repoPath, message, options, done) {
if (code === 128) {
console.warn(`
Git exited with code 128. Did you forget to run:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
`)
Expand Down
Loading