From c7af218c59b09beab272e7f29e691ad5aa0dd0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Thu, 30 Apr 2020 21:57:52 +0100 Subject: [PATCH 1/6] feature: avoid dependning on shelljs --- package-lock.json | 13 +++++++++---- package.json | 2 +- src/cli/commitizen.js | 3 +-- src/cli/strategies/git-cz.js | 3 +-- src/commitizen/adapter.js | 6 +++--- src/commitizen/commit.js | 10 +++++----- src/commitizen/init.js | 20 +++++++------------- src/common/util.js | 10 ---------- src/git/add.js | 12 ++++++------ src/git/commit.js | 4 ++-- src/git/init.js | 7 ++++--- test/tests/adapter.js | 8 ++++---- test/tests/commit.js | 32 ++++++++++++++++---------------- test/tests/init.js | 34 +++++++++++++++++----------------- test/tests/staging.js | 4 ++-- 15 files changed, 78 insertions(+), 90 deletions(-) diff --git a/package-lock.json b/package-lock.json index d34da3bd..25072565 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6228,9 +6228,10 @@ } }, "interpret": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.1.0.tgz", - "integrity": "sha1-ftGxQQxqDg94z5XTuEQMY/eLhhQ=" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.2.0.tgz", + "integrity": "sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==", + "dev": true }, "into-stream": { "version": "5.1.1", @@ -12867,7 +12868,8 @@ "path-parse": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", - "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=" + "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", + "dev": true }, "path-to-regexp": { "version": "1.7.0", @@ -13187,6 +13189,7 @@ "version": "0.6.2", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", + "dev": true, "requires": { "resolve": "^1.1.6" } @@ -13377,6 +13380,7 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.5.0.tgz", "integrity": "sha512-hgoSGrc3pjzAPHNBg+KnFcK2HwlHTs/YrAGUr6qgTVUZmXv1UEXXl0bZNBKMA9fud6lRYFdPGz0xXxycPzmmiw==", + "dev": true, "requires": { "path-parse": "^1.0.5" } @@ -13754,6 +13758,7 @@ "version": "0.7.6", "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.7.6.tgz", "integrity": "sha1-N5zM+1a5HIYB5HkzVutTgpJN6a0=", + "dev": true, "requires": { "glob": "^7.0.0", "interpret": "^1.0.0", diff --git a/package.json b/package.json index a394ec45..dbd58df6 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "proxyquire": "^2.1.0", "semantic-release": "15.13.18", "semver": "6.2.0", + "shelljs": "0.7.6", "sinon": "^6.3.4", "uuid": "3.3.2" }, @@ -84,7 +85,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" }, diff --git a/src/cli/commitizen.js b/src/cli/commitizen.js index ef74daa6..45e8b39d 100644 --- a/src/cli/commitizen.js +++ b/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; @@ -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}`); } diff --git a/src/cli/strategies/git-cz.js b/src/cli/strategies/git-cz.js index 2eda9dde..df9e2df5 100644 --- a/src/cli/strategies/git-cz.js +++ b/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'; @@ -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, diff --git a/src/commitizen/adapter.js b/src/commitizen/adapter.js index 68789467..9df26c35 100644 --- a/src/commitizen/adapter.js +++ b/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'; @@ -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: { @@ -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(); } diff --git a/src/commitizen/commit.js b/src/commitizen/commit.js index 6dac0552..f57cc769 100644 --- a/src/commitizen/commit.js +++ b/src/commitizen/commit.js @@ -10,9 +10,9 @@ 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); }); } @@ -20,7 +20,7 @@ function dispatchGitCommit (sh, repoPath, template, options, overrideOptions, do /** * 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'); @@ -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 @@ -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); }); } } diff --git a/src/commitizen/init.js b/src/commitizen/init.js index 624d5c4c..2281b4d6 100644 --- a/src/commitizen/init.js +++ b/src/commitizen/init.js @@ -1,6 +1,6 @@ +import childProcess from 'child_process'; import path from 'path'; import * as configLoader from './configLoader'; -import { executeShellCommand } from '../common/util'; import * as adapter from './adapter'; let { @@ -48,7 +48,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, @@ -60,10 +60,7 @@ 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(); @@ -88,11 +85,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); } @@ -102,10 +99,7 @@ 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."); } diff --git a/src/common/util.js b/src/common/util.js index db652bb0..9039c747 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -2,7 +2,6 @@ import fs from 'fs'; import path from 'path'; export { - executeShellCommand, getParsedJsonFromFile, getParsedPackageJsonFromPath, isArray, @@ -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 */ diff --git a/src/git/add.js b/src/git/add.js index 09ce9c37..4aad8750 100644 --- a/src/git/add.js +++ b/src/git/add.js @@ -1,3 +1,5 @@ +import childProcess from 'child_process'; + export { addPath, addFile @@ -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 }); } diff --git a/src/git/commit.js b/src/git/commit.js index 71d6eeb4..a193458a 100644 --- a/src/git/commit.js +++ b/src/git/commit.js @@ -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 @@ -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 "you@example.com" git config --global user.name "Your Name" `) diff --git a/src/git/init.js b/src/git/init.js index 43b4c1fe..754eb22a 100644 --- a/src/git/init.js +++ b/src/git/init.js @@ -1,9 +1,10 @@ +import childProcess from 'child_process'; + export { init }; /** * Synchronously creates a new git repo at a path */ -function init (sh, repoPath) { - sh.cd(repoPath); - sh.exec('git init'); +function init (repoPath) { + childProcess.spawnSync('git', ['init'], { cwd: repoPath }); } diff --git a/test/tests/adapter.js b/test/tests/adapter.js index 6c982a76..5d79d427 100644 --- a/test/tests/adapter.js +++ b/test/tests/adapter.js @@ -54,7 +54,7 @@ describe('adapter', function () { }; // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog'); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog'); // TEST expect(function () { adapter.resolveAdapterPath('IAMANIMPOSSIBLEPATH'); }).to.throw(Error); @@ -95,7 +95,7 @@ describe('adapter', function () { }; // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, '@commitizen/cz-conventional-changelog'); + commitizenInit(config.paths.endUserRepo, '@commitizen/cz-conventional-changelog'); // TEST expect(function () { adapter.resolveAdapterPath('IAMANIMPOSSIBLEPATH'); }).to.throw(Error); @@ -131,7 +131,7 @@ describe('adapter', function () { }; // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', {includeCommitizen: true}); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', {includeCommitizen: true}); // TEST expect(function () { adapter.getPrompter('IAMANIMPOSSIBLEPATH'); }).to.throw(Error); @@ -167,7 +167,7 @@ describe('adapter', function () { }; // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog-default-export'); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog-default-export'); // TEST expect(function () { adapter.getPrompter('IAMANIMPOSSIBLEPATH'); }).to.throw(Error); diff --git a/test/tests/commit.js b/test/tests/commit.js index f3cfca9e..9557a0ed 100644 --- a/test/tests/commit.js +++ b/test/tests/commit.js @@ -59,12 +59,12 @@ describe('commit', function () { }; // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage); // TEST // Pass in inquirer but it never gets used since we've mocked out a different // version of prompter. - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { log(repoConfig.path, function (logOutput) { expect(logOutput).to.have.string(dummyCommitMessage); done(); @@ -103,12 +103,12 @@ describe('commit', function () { }; // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage); // TEST // Pass in inquirer but it never gets used since we've mocked out a different // version of prompter. - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { log(repoConfig.path, function (logOutput) { expect(logOutput).to.have.string(dummyCommitMessage); done(); @@ -152,12 +152,12 @@ ${(os.platform === 'win32') ? '' : ' '} }; // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage); // TEST // Pass in inquirer but it never gets used since we've mocked out a different // version of prompter. - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true }, function () { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true }, function () { log(repoConfig.path, function (logOutput) { expect(logOutput).to.have.string(dummyCommitMessage); done(); @@ -201,12 +201,12 @@ ${(os.platform === 'win32') ? '' : ' '} }; // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage, options); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage, options); // TEST // Pass in inquirer but it never gets used since we've mocked out a different // version of prompter. - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { log(repoConfig.path, function (logOutput) { expect(logOutput).to.have.string(author); expect(logOutput).to.have.string(dummyCommitMessage); @@ -255,12 +255,12 @@ ${(os.platform === 'win32') ? '' : ' '} }; // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage, options); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage, options); // TEST // Pass in inquirer but it never gets used since we've mocked out a different // version of prompter. - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function () { log(repoConfig.path, function (logOutput) { expect(logOutput).to.have.string(dummyCommitMessage); }); @@ -297,11 +297,11 @@ ${(os.platform === 'win32') ? '' : ' '} } // Quick setup the repos, adapter, and grab a simple prompter - let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage); + let prompter = quickPrompterSetup(repoConfig, adapterConfig, dummyCommitMessage); // TEST // This is a successful commit directly to .git/COMMIT_EDITMSG - commitizenCommit(sh, inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true, hookMode: true }, function (err) { + commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true, hookMode: true }, function (err) { const commitFilePath = path.join(repoConfig.path, '.git/COMMIT_EDITMSG') const commitFile = fs.openSync(commitFilePath, 'r+') let commitContents = fs.readFileSync(commitFile, { flags: 'r+' }).toString(); @@ -330,9 +330,9 @@ after(function () { * This is just a helper for testing. NOTE that prompter * prompter is overriden for testing purposes. */ -function quickPrompterSetup (sh, repoConfig, adapterConfig, commitMessage, options = {}) { +function quickPrompterSetup (repoConfig, adapterConfig, commitMessage, options = {}) { - commitizenInit(sh, repoConfig.path, adapterConfig.npmName); + commitizenInit(repoConfig.path, adapterConfig.npmName); // NOTE: // In our real code we'd use this here but since we're testing, @@ -342,14 +342,14 @@ function quickPrompterSetup (sh, repoConfig, adapterConfig, commitMessage, optio commit(commitMessage, options); } - gitInit(sh, repoConfig.path); + gitInit(repoConfig.path); writeFilesToPath(repoConfig.files, repoConfig.path); for (let key in repoConfig.files) { let file = repoConfig.files[key]; if (file.add !== false) { - gitAddFile(sh, repoConfig.path, file.filename); + gitAddFile(repoConfig.path, file.filename); } } diff --git a/test/tests/init.js b/test/tests/init.js index a2f51e76..fda38531 100644 --- a/test/tests/init.js +++ b/test/tests/init.js @@ -31,7 +31,7 @@ describe('init', function () { // SETUP // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog'); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog'); // TEST @@ -48,7 +48,7 @@ describe('init', function () { // SETUP // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { save: true, saveDev: false }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { save: true, saveDev: false }); // TEST @@ -66,13 +66,13 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); // TEST sh.cd(config.paths.endUserRepo); // Adding a second adapter expect(function () { - commitizenInit(sh, config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true }); }).to.throw(/already configured/); // Check resulting json @@ -91,13 +91,13 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); // TEST // Adding a second adapter expect(function () { - commitizenInit(sh, config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true, force: true }); + commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true, force: true }); }).to.not.throw(); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -114,7 +114,7 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog'); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog'); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); // TEST @@ -140,7 +140,7 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { saveExact: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveExact: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); // TEST @@ -163,7 +163,7 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { includeCommitizen: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { includeCommitizen: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); // TEST @@ -186,7 +186,7 @@ describe('init', function () { // SETUP // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true }); // TEST @@ -203,7 +203,7 @@ describe('init', function () { // SETUP // Install an adapter - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); // TEST @@ -221,13 +221,13 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); // TEST sh.cd(config.paths.endUserRepo); // Adding a second adapter expect(function () { - commitizenInit(sh, config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true }); }).to.throw(/already configured/); // Check resulting json @@ -246,13 +246,13 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); // TEST // Adding a second adapter expect(function () { - commitizenInit(sh, config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true, force: true }); + commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true, force: true }); }).to.not.throw(); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -269,7 +269,7 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); // TEST @@ -295,7 +295,7 @@ describe('init', function () { // Add a first adapter sh.cd(config.paths.endUserRepo); - commitizenInit(sh, config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true, exact: true }); + commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true, exact: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); // TEST diff --git a/test/tests/staging.js b/test/tests/staging.js index 2921cf24..483fa530 100644 --- a/test/tests/staging.js +++ b/test/tests/staging.js @@ -44,7 +44,7 @@ describe('staging', function () { } }; - gitInit(sh, repoConfig.path); + gitInit(repoConfig.path); staging.isClean('./@this-actually-does-not-exist', function (stagingError) { expect(stagingError).to.be.an.instanceof(Error); @@ -55,7 +55,7 @@ describe('staging', function () { writeFilesToPath(repoConfig.files, repoConfig.path); - gitAdd(sh, repoConfig.path); + gitAdd(repoConfig.path); staging.isClean(repoConfig.path, function (afterWriteStagingIsCleanError, afterWriteStagingIsClean) { expect(afterWriteStagingIsCleanError).to.be.null; From 88aa26fb23b8e7ec241b49231124aa4e869301ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Sat, 2 May 2020 18:46:00 +0100 Subject: [PATCH 2/6] refactor: remove shelljs from tests --- package-lock.json | 26 -------------------------- package.json | 1 - src/commitizen/adapter.js | 6 +++--- src/commitizen/init.js | 6 +++--- src/commitizen/staging.js | 2 +- src/configLoader/loader.js | 4 ++-- test/config.js | 6 ------ test/tester.js | 7 +------ test/tests/adapter.js | 10 +++++----- test/tests/commit.js | 10 +++++----- test/tests/init.js | 21 +++++---------------- test/tests/staging.js | 10 +++++----- test/tools/clean.js | 36 ++++++++++++++++++------------------ test/tools/repo.js | 14 ++++++++------ 14 files changed, 56 insertions(+), 103 deletions(-) diff --git a/package-lock.json b/package-lock.json index 25072565..dc8b827c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6227,12 +6227,6 @@ } } }, - "interpret": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.2.0.tgz", - "integrity": "sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==", - "dev": true - }, "into-stream": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/into-stream/-/into-stream-5.1.1.tgz", @@ -13185,15 +13179,6 @@ "set-immediate-shim": "^1.0.1" } }, - "rechoir": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", - "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", - "dev": true, - "requires": { - "resolve": "^1.1.6" - } - }, "redent": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/redent/-/redent-2.0.0.tgz", @@ -13754,17 +13739,6 @@ "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", "dev": true }, - "shelljs": { - "version": "0.7.6", - "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.7.6.tgz", - "integrity": "sha1-N5zM+1a5HIYB5HkzVutTgpJN6a0=", - "dev": true, - "requires": { - "glob": "^7.0.0", - "interpret": "^1.0.0", - "rechoir": "^0.6.2" - } - }, "signal-exit": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", diff --git a/package.json b/package.json index dbd58df6..6fc29519 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,6 @@ "proxyquire": "^2.1.0", "semantic-release": "15.13.18", "semver": "6.2.0", - "shelljs": "0.7.6", "sinon": "^6.3.4", "uuid": "3.3.2" }, diff --git a/src/commitizen/adapter.js b/src/commitizen/adapter.js index 9df26c35..717f7341 100644 --- a/src/commitizen/adapter.js +++ b/src/commitizen/adapter.js @@ -42,7 +42,7 @@ function addPathToAdapterConfig (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 || ' '; @@ -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), '/../'); } /** diff --git a/src/commitizen/init.js b/src/commitizen/init.js index 2281b4d6..91ec5b7d 100644 --- a/src/commitizen/init.js +++ b/src/commitizen/init.js @@ -63,7 +63,7 @@ function init (repoPath, adapterNpmName, { checkRequiredArguments(repoPath, adapterNpmName); // Load the current adapter config - let adapterConfig = loadAdapterConfig(); + let adapterConfig = loadAdapterConfig(repoPath); // Get the npm string mappings based on the arguments provided let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force); @@ -112,8 +112,8 @@ function checkRequiredArguments (path, adapterNpmName) { * CONFIG * Loads and returns the adapter config at key config.commitizen, if it exists */ -function loadAdapterConfig () { - let config = configLoader.load(); +function loadAdapterConfig (cwd) { + let config = configLoader.load(null, cwd); if (config) { return config; } else { diff --git a/src/commitizen/staging.js b/src/commitizen/staging.js index 70fc15b5..c485b5dd 100644 --- a/src/commitizen/staging.js +++ b/src/commitizen/staging.js @@ -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); diff --git a/src/configLoader/loader.js b/src/configLoader/loader.js index e4472d8e..ba34f509 100644 --- a/src/configLoader/loader.js +++ b/src/configLoader/loader.js @@ -14,12 +14,12 @@ 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} 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) { diff --git a/test/config.js b/test/config.js index d46c69f2..efb6da47 100644 --- a/test/config.js +++ b/test/config.js @@ -33,12 +33,6 @@ let config = { */ maxTimeout: 240000, - /** - * Whether shelljs should suppress output, should be true - * unless debugging. - */ - silent: true, - /** * Whether or not to keep the artifacts of the tests after * they've run. diff --git a/test/tester.js b/test/tester.js index e8f50a3f..8e99f768 100644 --- a/test/tester.js +++ b/test/tester.js @@ -3,7 +3,6 @@ import * as clean from './tools/clean'; import * as files from './tools/files'; import * as util from '../src/common/util'; import { config as userConfig } from './config'; -import * as sh from 'shelljs'; // local instance import _ from 'lodash'; // Clone the user's config so we don't get caught w/our pants down @@ -11,13 +10,9 @@ let patchedConfig = _.cloneDeep(userConfig); function bootstrap () { - // Patch any shelljs specific config settings - sh.config.silent = patchedConfig.silent || true; - - // Return the patched config and shelljs instance + // Return the patched config return { config: patchedConfig, - sh, repo, clean, util, diff --git a/test/tests/adapter.js b/test/tests/adapter.js index 5d79d427..b23ea8e6 100644 --- a/test/tests/adapter.js +++ b/test/tests/adapter.js @@ -12,16 +12,16 @@ import { isFunction } from '../../src/common/util'; import { bootstrap } from '../tester'; // Destructure some things based on the bootstrap process -let { config, sh, repo, clean } = bootstrap(); +let { config, repo, clean } = bootstrap(); before(function () { // Creates the temp path - clean.before(sh, config.paths.tmp); + clean.before(config.paths.tmp); }); beforeEach(function () { this.timeout(config.maxTimeout); // this could take a while - repo.createEndUser(sh, config.paths.endUserRepo); + repo.createEndUser(config.paths.endUserRepo); }); describe('adapter', function () { @@ -180,12 +180,12 @@ describe('adapter', function () { afterEach(function () { this.timeout(config.maxTimeout); // this could take a while // All this should do is archive the tmp path to the artifacts - clean.afterEach(sh, config.paths.tmp, config.preserve); + clean.afterEach(config.paths.tmp, config.preserve); }); after(function () { this.timeout(config.maxTimeout); // this could take a while // Once everything is done, the artifacts should be cleaned up based on // the preserve setting in the config - clean.after(sh, config.paths.tmp, config.preserve); + clean.after(config.paths.tmp, config.preserve); }); diff --git a/test/tests/commit.js b/test/tests/commit.js index 9557a0ed..f9c7fa86 100644 --- a/test/tests/commit.js +++ b/test/tests/commit.js @@ -13,18 +13,18 @@ import { addFile as gitAddFile, init as gitInit, log, whatChanged } from '../../ import { commit as commitizenCommit, init as commitizenInit } from '../../src/commitizen'; // Destructure some things for cleaner tests -let { config, sh, repo, clean, files } = bootstrap(); +let { config, repo, clean, files } = bootstrap(); let { writeFilesToPath } = files; before(function () { // Creates the temp path - clean.before(sh, config.paths.tmp); + clean.before(config.paths.tmp); }); beforeEach(function () { this.timeout(config.maxTimeout); // this could take a while /* istanbul ignore next */ - repo.createEndUser(sh, config.paths.endUserRepo); + repo.createEndUser(config.paths.endUserRepo); }); describe('commit', function () { @@ -316,14 +316,14 @@ ${(os.platform === 'win32') ? '' : ' '} afterEach(function () { this.timeout(config.maxTimeout); // this could take a while // All this should do is archive the tmp path to the artifacts - clean.afterEach(sh, config.paths.tmp, config.preserve); + clean.afterEach(config.paths.tmp, config.preserve); }); after(function () { this.timeout(config.maxTimeout); // this could take a while // Once everything is done, the artifacts should be cleaned up based on // the preserve setting in the config - clean.after(sh, config.paths.tmp, config.preserve); + clean.after(config.paths.tmp, config.preserve); }); /** diff --git a/test/tests/init.js b/test/tests/init.js index fda38531..4d465e43 100644 --- a/test/tests/init.js +++ b/test/tests/init.js @@ -10,16 +10,16 @@ import { init as commitizenInit } from '../../src/commitizen'; import { bootstrap } from '../tester'; // Destructure some things based on the bootstrap process -let { config, sh, repo, clean, util } = bootstrap(); +let { config, repo, clean, util } = bootstrap(); before(function () { // Creates the temp path - clean.before(sh, config.paths.tmp); + clean.before(config.paths.tmp); }); beforeEach(function () { this.timeout(config.maxTimeout); // this could take a while - repo.createEndUser(sh, config.paths.endUserRepo); + repo.createEndUser(config.paths.endUserRepo); }); describe('init', function () { @@ -65,11 +65,9 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); // TEST - sh.cd(config.paths.endUserRepo); // Adding a second adapter expect(function () { commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true }); @@ -90,7 +88,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true }); // TEST @@ -113,7 +110,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog'); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -139,7 +135,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveExact: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -162,7 +157,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { includeCommitizen: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -220,11 +214,9 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); // TEST - sh.cd(config.paths.endUserRepo); // Adding a second adapter expect(function () { commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true }); @@ -245,7 +237,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); // TEST @@ -268,7 +259,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -294,7 +284,6 @@ describe('init', function () { // SETUP // Add a first adapter - sh.cd(config.paths.endUserRepo); commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true, exact: true }); let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo); @@ -315,12 +304,12 @@ describe('init', function () { afterEach(function () { this.timeout(config.maxTimeout); // this could take a while // All this should do is archive the tmp path to the artifacts - clean.afterEach(sh, config.paths.tmp, config.preserve); + clean.afterEach(config.paths.tmp, config.preserve); }); after(function () { this.timeout(config.maxTimeout); // this could take a while // Once everything is done, the artifacts should be cleaned up based on // the preserve setting in the config - clean.after(sh, config.paths.tmp, config.preserve); + clean.after(config.paths.tmp, config.preserve); }); diff --git a/test/tests/staging.js b/test/tests/staging.js index 483fa530..02e8c4fd 100644 --- a/test/tests/staging.js +++ b/test/tests/staging.js @@ -8,17 +8,17 @@ import { init as gitInit, addPath as gitAdd } from '../../src/git'; import { staging } from '../../src/commitizen'; // Destructure some things for cleaner tests -let { config, sh, repo, clean, files } = bootstrap(); +let { config, repo, clean, files } = bootstrap(); let { writeFilesToPath } = files; before(function () { // Creates the temp path - clean.before(sh, config.paths.tmp); + clean.before(config.paths.tmp); }); beforeEach(function () { this.timeout(config.maxTimeout); // this could take a while - repo.createEndUser(sh, config.paths.endUserRepo); + repo.createEndUser(config.paths.endUserRepo); }); describe('staging', function () { @@ -83,12 +83,12 @@ describe('staging', function () { afterEach(function () { this.timeout(config.maxTimeout); // this could take a while // All this should do is archive the tmp path to the artifacts - clean.afterEach(sh, config.paths.tmp, config.preserve); + clean.afterEach(config.paths.tmp, config.preserve); }); after(function () { this.timeout(config.maxTimeout); // this could take a while // Once everything is done, the artifacts should be cleaned up based on // the preserve setting in the config - clean.after(sh, config.paths.tmp, config.preserve); + clean.after(config.paths.tmp, config.preserve); }); diff --git a/test/tools/clean.js b/test/tools/clean.js index 5241dd67..d8d39439 100644 --- a/test/tools/clean.js +++ b/test/tools/clean.js @@ -12,22 +12,22 @@ export { let testSuiteRunId = uuidv4(); // At the beginning of a run purge .tmp -function before (sh, tmpPath) { - cleanPath(sh, tmpPath); - // clean(sh, tmpPath, 'all'); +function before (tmpPath) { + cleanPath(tmpPath); + // clean(tmpPath, 'all'); } -function afterEach (sh, tmpPath, preserve) { +function afterEach (tmpPath, preserve) { if (preserve !== false) { - archive(sh, tmpPath, testSuiteRunId); + archive(tmpPath, testSuiteRunId); } - cleanPath(sh, tmpPath); + cleanPath(tmpPath); } // After should listen to the user via the config // Before should always purge .tmp irregardless of config -function after (sh, tmpPath, preserve) { - clean(sh, tmpPath, preserve); +function after (tmpPath, preserve) { + clean(tmpPath, preserve); } /** @@ -36,10 +36,10 @@ function after (sh, tmpPath, preserve) { * * Generally should be run in afterEach() */ -function archive (sh, tmpPath, testSuiteRunId) { +function archive (tmpPath, testSuiteRunId) { let destinationPath = path.resolve(tmpPath + '/../artifacts/' + testSuiteRunId + '/' + uuidv4()); - sh.mkdir('-p', destinationPath); - sh.cp('-Rf', tmpPath + '/*', destinationPath); + fs.mkdirSync(destinationPath, { recursive: true }); + fs.copySync(tmpPath, destinationPath); } /** @@ -47,7 +47,7 @@ function archive (sh, tmpPath, testSuiteRunId) { * * Generally called in after() */ -function clean (sh, tmpPath, preserve) { +function clean (tmpPath, preserve) { /** * If preserve is a normal integer over 0 thats how many results to keep. @@ -85,11 +85,11 @@ function clean (sh, tmpPath, preserve) { }); // Keep only the number of files defined in the config setting 'preserve'. - keep(sh, artifactsBasePath, artifactFolders, preserve); + keep(artifactsBasePath, artifactFolders, preserve); } // Always purge tmp, it needs to be empty for next run - cleanPath(sh, tmpPath); + cleanPath(tmpPath); } function isNormalNonZeroInteger (str) { @@ -108,14 +108,14 @@ function isNormalNonZeroInteger (str) { * * n is the (1 indexed) count of files to keep. */ -function keep (sh, basePath, paths, n) { +function keep (basePath, paths, n) { for (let i = paths.length; i > n; i--) { fs.removeSync(path.resolve(basePath, paths[i - 1])); } } -function cleanPath (sh, tmpPath) { - sh.rm('-rf', tmpPath + '/*'); - sh.mkdir(tmpPath); +function cleanPath (tmpPath) { + fs.removeSync(tmpPath); + fs.mkdirSync(tmpPath); } diff --git a/test/tools/repo.js b/test/tools/repo.js index 88ab2e8f..19cce5b5 100644 --- a/test/tools/repo.js +++ b/test/tools/repo.js @@ -1,3 +1,6 @@ +import childProcess from 'child_process'; +import fs from 'fs'; + export { createEmpty, createEndUser @@ -6,15 +9,14 @@ export { /** * Create an empty repo */ -function createEmpty (sh, path) { - sh.mkdir(path); - sh.cd(path); - sh.exec('npm init --force --yes'); +function createEmpty (path) { + fs.mkdirSync(path, { recursive: true }); + childProcess.spawnSync('npm', ['init', '--force', '--yes'], { cwd: path }); } /** * Create a new repo to hold an end user app */ -function createEndUser (sh, path) { - createEmpty(sh, path); +function createEndUser (path) { + createEmpty(path); } From 468e92417dbcdacb2cbcf06fdcc582eca423df4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Sun, 3 May 2020 13:34:21 +0100 Subject: [PATCH 3/6] refactor: simplify configLoader call --- src/commitizen.js | 2 -- src/commitizen/configLoader.js | 10 ---------- src/commitizen/init.js | 20 +++++--------------- src/configLoader/loader.js | 2 +- 4 files changed, 6 insertions(+), 28 deletions(-) delete mode 100644 src/commitizen/configLoader.js diff --git a/src/commitizen.js b/src/commitizen.js index 018bd681..1f031445 100644 --- a/src/commitizen.js +++ b/src/commitizen.js @@ -2,7 +2,6 @@ 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'; @@ -10,7 +9,6 @@ export { adapter, cache, commit, - configLoader, init, staging }; diff --git a/src/commitizen/configLoader.js b/src/commitizen/configLoader.js deleted file mode 100644 index 02355d78..00000000 --- a/src/commitizen/configLoader.js +++ /dev/null @@ -1,10 +0,0 @@ -import { loader } from '../configLoader'; - -export { load }; - -// Configuration sources in priority order. -var configs = ['.czrc', '.cz.json', 'package.json']; - -function load (config, cwd) { - return loader(configs, config, cwd); -} diff --git a/src/commitizen/init.js b/src/commitizen/init.js index 91ec5b7d..4ff40f85 100644 --- a/src/commitizen/init.js +++ b/src/commitizen/init.js @@ -1,7 +1,7 @@ import childProcess from 'child_process'; import path from 'path'; -import * as configLoader from './configLoader'; import * as adapter from './adapter'; +import * as configLoader from '../configLoader'; let { addPathToAdapterConfig, @@ -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 * @@ -63,7 +66,7 @@ function init (repoPath, adapterNpmName, { checkRequiredArguments(repoPath, adapterNpmName); // Load the current adapter config - let adapterConfig = loadAdapterConfig(repoPath); + 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); @@ -107,16 +110,3 @@ function checkRequiredArguments (path, 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 (cwd) { - let config = configLoader.load(null, cwd); - if (config) { - return config; - } else { - - } -} diff --git a/src/configLoader/loader.js b/src/configLoader/loader.js index ba34f509..a6b63117 100644 --- a/src/configLoader/loader.js +++ b/src/configLoader/loader.js @@ -13,7 +13,7 @@ export default loader; /** * Get content of the configuration file - * @param {String} config - partial path to configuration file + * @param {String|null} config - partial path to configuration file * @param {String} cwd - directory path which will be joined with config argument * @return {Object|undefined} */ From 3771d0944d05efa2a35bbd20cd340acceca07f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 4 May 2020 20:38:43 +0100 Subject: [PATCH 4/6] fix: use shell flag when spawning npm init Co-authored-by: hdmr14 <58992133+hdmr14@users.noreply.github.com> --- test/tools/repo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tools/repo.js b/test/tools/repo.js index 19cce5b5..2357f3a7 100644 --- a/test/tools/repo.js +++ b/test/tools/repo.js @@ -11,7 +11,7 @@ export { */ function createEmpty (path) { fs.mkdirSync(path, { recursive: true }); - childProcess.spawnSync('npm', ['init', '--force', '--yes'], { cwd: path }); + childProcess.spawnSync('npm', ['init', '--force', '--yes'], { cwd: path, shell: true }); } /** From 746a3519e6404a4ac258e94cf9339cc8d3234730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 4 May 2020 22:30:09 +0100 Subject: [PATCH 5/6] refactor: remove unused function isArray --- src/common/util.js | 15 --------------- test/tests/util.js | 23 +---------------------- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/src/common/util.js b/src/common/util.js index 9039c747..9b743520 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -4,7 +4,6 @@ import path from 'path'; export { getParsedJsonFromFile, getParsedPackageJsonFromPath, - isArray, isFunction, isString, isInTest @@ -29,20 +28,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 */ diff --git a/test/tests/util.js b/test/tests/util.js index 0dd648ec..f6efc2e4 100644 --- a/test/tests/util.js +++ b/test/tests/util.js @@ -1,29 +1,8 @@ import { expect } from 'chai'; -import { isArray, isFunction, isString } from '../../src/common/util'; +import { isFunction, isString } from '../../src/common/util'; describe('common util', function () { - it('isArray determines if array is passed', function () { - - // Truthies - expect(isArray([])).to.be.true; - expect(isArray([1, 2, 3])).to.be.true; - expect(isArray([1, , 3])).to.be.true; - expect(isArray(new Array())).to.be.true; - - // Falsies - expect(isArray(undefined)).to.be.false; - expect(isArray(null)).to.be.false; - expect(isArray(49)).to.be.false; - expect(isArray(function () {})).to.be.false; - expect(isArray({})).to.be.false; - expect(isArray("asdf")).to.be.false; - expect(isArray(true)).to.be.false; - expect(isArray(false)).to.be.false; - expect(isArray(Symbol('test'))).to.be.false; - - }); - it('isFunction determines if a function is passed', function () { // Truthies From 4b7afb52f661643f5488bfcdf9471e3dcf641f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 4 May 2020 22:31:33 +0100 Subject: [PATCH 6/6] refactor: remove unused function isString --- src/common/util.js | 15 --------------- test/tests/util.js | 26 +------------------------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/src/common/util.js b/src/common/util.js index 9b743520..8f3f1cd3 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -5,7 +5,6 @@ export { getParsedJsonFromFile, getParsedPackageJsonFromPath, isFunction, - isString, isInTest } @@ -43,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'; } diff --git a/test/tests/util.js b/test/tests/util.js index f6efc2e4..f2368e94 100644 --- a/test/tests/util.js +++ b/test/tests/util.js @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { isFunction, isString } from '../../src/common/util'; +import { isFunction } from '../../src/common/util'; describe('common util', function () { @@ -22,28 +22,4 @@ describe('common util', function () { }); - it('isString determines if string is passed', function () { - - // Truthies - expect(isString('a single quoted string')).to.be.true; - expect(isString("a double quoted string")).to.be.true; - expect(isString(` - a multi - line - string` - )).to.be.true; - expect(isString(new String())).to.be.true; - - // Falsies - expect(isString(function () {})).to.be.false; - expect(isString(undefined)).to.be.false; - expect(isString(null)).to.be.false; - expect(isString(49)).to.be.false; - expect(isString([])).to.be.false; - expect(isString({})).to.be.false; - expect(isString(true)).to.be.false; - expect(isString(false)).to.be.false; - expect(isString(Symbol('test'))).to.be.false; - - }); });