diff --git a/index.js b/index.js index e55fec8..512ced7 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const fs = require('fs'); const mri = require('mri'); const ora = require('ora'); const path = require('path'); +const prettier = require('prettier'); const util = require('util'); const Wizard = require('./lib/wizard'); @@ -28,7 +29,14 @@ const Wizard = require('./lib/wizard'); }, { compact: false, depth: null } ); - const config = `module.exports = ${moduleExports}\n`; + + let config = `module.exports = ${moduleExports}\n`; + + try { + config = prettier.format(config, { parser: 'babel', trailingComma: 'none' }); + } catch (error) { + ora('Could not format configuration file.').warn(); + } console.log(''); @@ -48,8 +56,8 @@ const Wizard = require('./lib/wizard'); ora(`Configuration saved to ${chalk.bold(configPath)}.\n`).succeed(); } } catch (error) { + ora('ERROR: Could not create configuration file.').fail(); console.log(error); - console.log('ERROR: Could not create configuration file.'); process.exit(1); } @@ -57,7 +65,7 @@ const Wizard = require('./lib/wizard'); // Writing env file. try { const envFileData = Object.keys(envFileOptions) - .map(key => `${key}=${envFileOptions[key]}`) + .map((key) => `${key}=${envFileOptions[key]}`) .join('\n'); if (envFileData.length > 0) { diff --git a/lib/wizard.js b/lib/wizard.js index ebf9d92..5e03eaf 100644 --- a/lib/wizard.js +++ b/lib/wizard.js @@ -66,7 +66,7 @@ ${chalk.dim(`v${pkg.version}`)} } installPlugins(pluginNames) { - const installablePluginNames = pluginNames.filter(pluginName => { + const installablePluginNames = pluginNames.filter((pluginName) => { return pluginName && pluginName[0] !== path.sep && pluginName[0] !== '.'; }); const modules = ['sourcebit'].concat(installablePluginNames).join(' '); @@ -85,7 +85,7 @@ ${chalk.dim(`v${pkg.version}`)} loadPlugins(pluginNames) { const plugins = {}; - pluginNames.forEach(pluginName => { + pluginNames.forEach((pluginName) => { // Ideally we would `require(pluginName)`, but that fails. Presumably // because the module has just been installed in a child process, so // the main process isn't able to resolve its name just yet. To get @@ -140,7 +140,7 @@ ${chalk.dim(`v${pkg.version}`)} name: 'stage1Plugins', message: `Select the source plugins. ${chalk.reset.dim('They define the sources of your data.')}`, choices: pluginsByType.source || [], - validate: value => (value.length > 0 ? true : 'You must select at least one source plugin.') + validate: (value) => (value.length > 0 ? true : 'You must select at least one source plugin.') }, { when: Boolean(pluginsByType.transform), @@ -165,10 +165,7 @@ ${chalk.dim(`v${pkg.version}`)} const { stage1Plugins } = await inquirer.prompt(questions[0]); const { stage2Plugins } = await inquirer.prompt(questions[1]); const { stage3Plugins } = await inquirer.prompt(questions[2]); - const allPlugins = stage1Plugins - .concat(stage2Plugins) - .concat(stage3Plugins) - .filter(Boolean); + const allPlugins = stage1Plugins.concat(stage2Plugins).concat(stage3Plugins).filter(Boolean); console.log(''); activeSpinner = ora('Installing plugins\n').start(); @@ -179,7 +176,7 @@ ${chalk.dim(`v${pkg.version}`)} activeSpinner.succeed(); const pluginModules = this.loadPlugins(allPlugins); - const pluginBlocks = allPlugins.map(pluginName => ({ + const pluginBlocks = allPlugins.map((pluginName) => ({ module: pluginModules[pluginName], options: {} })); @@ -198,7 +195,7 @@ ${chalk.dim(`v${pkg.version}`)} }); allPlugins.forEach((pluginName, pluginIndex, plugins) => { - setupData = setupData.then(async setupData => { + setupData = setupData.then(async (setupData) => { console.log(`\nConfiguring plugin ${pluginIndex + 1} of ${plugins.length}: ${chalk.bold(pluginName)}\n`); const data = await sourcebit.transform(); @@ -211,7 +208,7 @@ ${chalk.dim(`v${pkg.version}`)} } const currentPluginBlock = (currentConfig.plugins || []).find( - plugin => plugin.module && plugin.module.name === pluginModules[pluginName].name + (plugin) => plugin.module && plugin.module.name === pluginModules[pluginName].name ); const currentOptions = (currentPluginBlock && currentPluginBlock.options) || {}; @@ -254,7 +251,7 @@ ${chalk.dim(`v${pkg.version}`)} const privateOptions = {}; // Finding private/public options and environment variables. - Object.keys(pluginOptions).forEach(key => { + Object.keys(pluginOptions).forEach((key) => { const schema = optionsSchema[key] || {}; if (schema.env) { diff --git a/lib/wrapped-function.js b/lib/wrapped-function.js index ead04d1..ea113c5 100644 --- a/lib/wrapped-function.js +++ b/lib/wrapped-function.js @@ -1,23 +1,16 @@ const util = require('util'); -Function.prototype[util.inspect.custom] = function(_, { indentationLvl }) { +Function.prototype[util.inspect.custom] = function (_, { indentationLvl }) { const fnSource = this.toString(); const indentedSource = fnSource .split('\n') - .map((line, index, lines) => { - // Oddly, the `toString` method adds a line break after the last - // parameter in the function signature. We get rid of it here. + .map((line, index) => { if (index === 0) { - return (line + (lines[1] || '')).replace('function anonymous', 'function'); + return line.replace('function anonymous', 'function'); } - if (index === 1) return null; - - const isLastLine = index === lines.length - 1; - - return ' '.repeat(indentationLvl + (isLastLine ? 0 : 2)) + line; + return line; }) - .filter(Boolean) .join('\n'); return indentedSource; diff --git a/package-lock.json b/package-lock.json index 7e36e20..f0984ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "create-sourcebit", - "version": "0.9.0", + "version": "0.10.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1676,10 +1676,9 @@ } }, "prettier": { - "version": "1.19.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", - "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==", - "dev": true + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", + "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==" }, "pump": { "version": "3.0.0", diff --git a/package.json b/package.json index a2f5769..64b0dc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-sourcebit", - "version": "0.9.0", + "version": "0.10.0", "description": "Create and configure a Sourcebit installation", "main": "index.js", "bin": { @@ -13,13 +13,13 @@ "inquirer": "^7.0.3", "mri": "^1.1.4", "ora": "^4.0.3", + "prettier": "^2.0.5", "sourcebit": "^0.2.2" }, "devDependencies": { "@stackbit/prettier-config": "^1.0.0", "husky": "^4.2.0", - "lint-staged": "^9.5.0", - "prettier": "^1.19.1" + "lint-staged": "^9.5.0" }, "scripts": { "test": "npm run format",