diff --git a/lib/cli/cli.js b/lib/cli/cli.js index dee8e70dd4..233ff29a8e 100755 --- a/lib/cli/cli.js +++ b/lib/cli/cli.js @@ -18,6 +18,7 @@ const path = require('path'); const {loadOptions, YARGS_PARSER_CONFIG} = require('./options'); const commands = require('./commands'); const ansi = require('ansi-colors'); +const migrateOpts = require('../../scripts/migrate-opts-init'); const {repository, homepage, version, gitter} = require('../../package.json'); /** @@ -40,6 +41,7 @@ exports.main = (argv = process.argv.slice(2)) => { .scriptName('mocha') .command(commands.run) .command(commands.init) + .command(migrateOpts.init) .updateStrings({ 'Positionals:': 'Positional Arguments', 'Options:': 'Other Options', diff --git a/package-lock.json b/package-lock.json index be569422f6..b3f72ee361 100644 --- a/package-lock.json +++ b/package-lock.json @@ -485,8 +485,8 @@ "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", - "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", - "dev": true + "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", + "dev" : true }, "accepts": { "version": "1.3.5", @@ -2160,6 +2160,17 @@ "graceful-fs": "^4.1.2", "jsonfile": "^3.0.0", "universalify": "^0.1.0" + }, + "dependencies": { + "jsonfile": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-3.0.1.tgz", + "integrity": "sha1-pezG9l9T9mLEQVx2daAzHQmS7GY=", + "dev": true, + "requires": { + "graceful-fs": "^4.1.6" + } + } } }, "invert-kv": { @@ -3308,8 +3319,7 @@ "commander": { "version": "2.19.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", - "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==", - "dev": true + "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==" }, "comment-regex": { "version": "1.0.1", @@ -3428,7 +3438,6 @@ "version": "1.1.12", "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.12.tgz", "integrity": "sha512-a1eOIcu8+7lUInge4Rpf/n4Krkf3Dd9lqhljRzII1/Zno/kRtUWnznPO3jOKBmTEktkt3fkxisUcivoj0ebzoA==", - "dev": true, "requires": { "ini": "^1.3.4", "proto-list": "~1.2.1" @@ -4778,7 +4787,6 @@ "version": "0.15.3", "resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.3.tgz", "integrity": "sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==", - "dev": true, "requires": { "commander": "^2.19.0", "lru-cache": "^4.1.5", @@ -7352,8 +7360,7 @@ "graceful-fs": { "version": "4.1.15", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", - "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", - "dev": true + "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" }, "graceful-readlink": { "version": "1.0.1", @@ -8344,8 +8351,7 @@ "ini": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==", - "dev": true + "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" }, "inkscape": { "version": "2.0.0", @@ -9171,10 +9177,9 @@ "dev": true }, "js-beautify": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.10.0.tgz", - "integrity": "sha512-OMwf/tPDpE/BLlYKqZOhqWsd3/z2N3KOlyn1wsCRGFwViE8LOQTcDtathQvHvZc+q+zWmcNAbwKSC+iJoMaH2Q==", - "dev": true, + "version": "1.10.2", + "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.10.2.tgz", + "integrity": "sha512-ZtBYyNUYJIsBWERnQP0rPN9KjkrDfJcMjuVGcvXOUJrD1zmOGwhRwQ4msG+HJ+Ni/FA7+sRQEMYVzdTQDvnzvQ==", "requires": { "config-chain": "^1.1.12", "editorconfig": "^0.15.3", @@ -9376,13 +9381,21 @@ "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=", "dev": true }, + "json2yaml": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/json2yaml/-/json2yaml-1.1.0.tgz", + "integrity": "sha1-VBTZB/mBZYa4DFE+wuOusquBmmw=", + "requires": { + "remedial": "1.x" + } + }, "jsonfile": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-3.0.1.tgz", - "integrity": "sha1-pezG9l9T9mLEQVx2daAzHQmS7GY=", - "dev": true, + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-5.0.0.tgz", + "integrity": "sha512-NQRZ5CRo74MhMMC3/3r5g2k4fjodJ/wh8MxjFbCViWKFjxrnudWSY5vomh+23ZaXzAS7J3fBZIR2dV6WbmfM0w==", "requires": { - "graceful-fs": "^4.1.6" + "graceful-fs": "^4.1.6", + "universalify": "^0.1.2" } }, "jsonify": { @@ -10426,7 +10439,6 @@ "version": "4.1.5", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", - "dev": true, "requires": { "pseudomap": "^1.0.2", "yallist": "^2.1.2" @@ -11511,7 +11523,6 @@ "version": "4.0.1", "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.1.tgz", "integrity": "sha1-0NRoWv1UFRk8jHUFYC0NF81kR00=", - "dev": true, "requires": { "abbrev": "1", "osenv": "^0.1.4" @@ -12410,7 +12421,6 @@ "version": "0.1.5", "resolved": "https://registry.npmjs.org/osenv/-/osenv-0.1.5.tgz", "integrity": "sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==", - "dev": true, "requires": { "os-homedir": "^1.0.0", "os-tmpdir": "^1.0.0" @@ -13808,8 +13818,7 @@ "proto-list": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz", - "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=", - "dev": true + "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=" }, "prr": { "version": "1.0.1", @@ -13829,8 +13838,7 @@ "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", - "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", - "dev": true + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" }, "psl": { "version": "1.1.31", @@ -14471,6 +14479,11 @@ "autolinker": "~0.28.0" } }, + "remedial": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/remedial/-/remedial-1.0.8.tgz", + "integrity": "sha512-/62tYiOe6DzS5BqVsNpH/nkGlX45C/Sp6V+NtiN6JQNS1Viay7cWkazmRkrQrdFj2eshDe96SIQNIoMxqhzBOg==" + }, "remove-trailing-separator": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz", @@ -14980,8 +14993,7 @@ "semver": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", - "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", - "dev": true + "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==" }, "semver-compare": { "version": "1.0.0", @@ -15291,8 +15303,7 @@ "sigmund": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz", - "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=", - "dev": true + "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=" }, "signal-exit": { "version": "3.0.2", @@ -17559,8 +17570,7 @@ "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", - "dev": true + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" }, "unorm": { "version": "1.5.0", @@ -18266,8 +18276,7 @@ "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", - "dev": true + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" }, "yargs": { "version": "13.3.0", diff --git a/package-scripts.js b/package-scripts.js index 6d9985b90d..7fc523153c 100644 --- a/package-scripts.js +++ b/package-scripts.js @@ -77,6 +77,7 @@ module.exports = { 'test.node.exports', 'test.node.unit', 'test.node.integration', + 'test.node.migrateOpts', 'test.node.jsapi', 'test.node.requires', 'test.node.reporters', @@ -128,6 +129,11 @@ module.exports = { description: 'Run tests concerning mocha.opts', hiddenFromHelp: true }, + migrateOpts: { + script: test('migrateOpts', 'test/migrate-opts'), + description: 'Run tests concerning migrate mocha.opts script', + hiddenFromHelp: true + }, jsapi: { script: 'node test/jsapi', description: 'Run Node.js Mocha JavaScript API tests', diff --git a/package.json b/package.json index 3ea394b2e5..74304f2c81 100644 --- a/package.json +++ b/package.json @@ -525,7 +525,10 @@ "glob": "7.1.3", "growl": "1.10.5", "he": "1.2.0", + "js-beautify": "^1.10.2", "js-yaml": "3.13.1", + "json2yaml": "^1.1.0", + "jsonfile": "^5.0.0", "log-symbols": "2.2.0", "minimatch": "3.0.4", "mkdirp": "0.5.1", diff --git a/scripts/migrate-opts-init.js b/scripts/migrate-opts-init.js new file mode 100644 index 0000000000..4d7456464d --- /dev/null +++ b/scripts/migrate-opts-init.js @@ -0,0 +1,8 @@ +'use strict'; +/** + * Exports Yargs commands + * @see https://git.io/fpJ0G + * @private + * @module + */ +exports.init = require('./migrate-opts.js'); diff --git a/scripts/migrate-opts.js b/scripts/migrate-opts.js new file mode 100644 index 0000000000..ab5f522dd7 --- /dev/null +++ b/scripts/migrate-opts.js @@ -0,0 +1,59 @@ +'use strict'; +/** + * Command module for "migrate" command + * Interface : mocha migrate-opts -file ./test/mocha.opts --json + * Migrate opts + * Read opts file and change to othter file like yaml, yml, js, json + * @see https://mochajs.org/#configuring-mocha-nodejs + * @private + * @module + */ +const path = require('path'); +const YAML = require('json2yaml'); +const jsonfile = require('jsonfile'); +const beautify = require('js-beautify').js; +const fs = require('fs'); +const loadMochaOpts = require('../lib/cli/options.js').loadMochaOpts; +const setJsType = content => + beautify(`(module.exports = ${JSON.stringify(content)})`); +const setYamlType = content => YAML.stringify(content); +const writeFile = { + yaml: (content, _path) => + fs.writeFileSync(path.join(_path, `.mocharc.yaml`), setYamlType(content)), + yml: (content, _path) => + fs.writeFileSync(path.join(_path, `.mocharc.yml`), setYamlType(content)), + js: (content, _path) => + fs.writeFileSync(path.join(_path, `.mocharc.js`), setJsType(content)), + json: (content, _path) => + jsonfile.writeFileSync(path.join(_path, `.mocharc.json`), content, { + spaces: 1 + }) +}; +const writeConfig = (type, content, _path) => { + if (type === 'yaml') { + writeFile.yaml(content, _path); + } else if (type === 'yml') { + writeFile.yml(content, _path); + } else if (type === 'js') { + writeFile.js(content, _path); + } else { + writeFile.json(content, _path); + } + return content; +}; + +exports.init = (filepath, type, _path = process.cwd()) => { + const content = loadMochaOpts({opts: filepath}); + writeConfig(type, content, _path); +}; +exports.command = 'migrate-opts'; + +exports.description = 'Migrate opts file to type that user wanted'; + +exports.builder = yargs => yargs.option('file').option('type'); + +exports.handler = argv => { + const filepath = path.join(process.cwd(), argv.file[0]); + const type = argv.type; + this.init(filepath, type); +}; diff --git a/test/migrate-opts/index.js b/test/migrate-opts/index.js new file mode 100644 index 0000000000..b592d30348 --- /dev/null +++ b/test/migrate-opts/index.js @@ -0,0 +1,16 @@ +'use strict'; + +var Mocha = require('../../'); + +var mocha = new Mocha({ + ui: 'bdd', + globals: ['okGlobalA', 'okGlobalB', 'okGlobalC', 'callback*'], + growl: true, + timeout: 1000 +}); + +require('../setup'); + +mocha.addFile('test/migrate-opts/migrate-opts.spec.js'); + +mocha.run(function() {}); diff --git a/test/migrate-opts/migrate-opts.spec.js b/test/migrate-opts/migrate-opts.spec.js new file mode 100644 index 0000000000..ebe9c5d6f8 --- /dev/null +++ b/test/migrate-opts/migrate-opts.spec.js @@ -0,0 +1,35 @@ +'use strict'; +var fs = require('fs'); +var path = require('path'); +var expect = require('chai').expect; +var migrateOpts = require('../../scripts/migrate-opts'); +var types = ['json', 'js', 'yml', 'yaml']; + +describe('Test migrate-opts.js script', function() { + types.forEach(function(type) { + it('should be worked writeFile ' + type, function() { + var filepath = path.join(process.cwd(), './test/migrate-opts/mocha.opts'); + var _path = path.join(process.cwd(), './test/migrate-opts'); + migrateOpts.init(filepath, type, _path); + var __path = path.join( + process.cwd(), + './test/migrate-opts', + '.mocharc.' + type + ); + var isFile = fs.existsSync(__path); + expect(isFile).to.equal(true); + if (isFile) fs.unlinkSync(__path); + }); + }); + after(function() { + types.forEach(function(type) { + var __path = path.join( + process.cwd(), + './test/migrate-opts', + '.mocharc.' + type + ); + var isFile = fs.existsSync(__path); + if (isFile) fs.unlinkSync(__path); + }); + }); +}); diff --git a/test/migrate-opts/mocha.opts b/test/migrate-opts/mocha.opts new file mode 100644 index 0000000000..6c25f89f35 --- /dev/null +++ b/test/migrate-opts/mocha.opts @@ -0,0 +1,4 @@ +--reporter dot +--growl +--extension mjs +--extension js \ No newline at end of file