From e65110f3f6c27ce79d6027fa6abc3d7c930d64ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barth=C3=A9l=C3=A9my=20Ledoux?= Date: Tue, 27 Mar 2018 16:25:58 -0500 Subject: [PATCH] fix(invoke): issue #1037 invoke binary files (#1038) Using `vue invoke` was breaking all binary files because of encoding issues --- packages/@vue/cli/lib/invoke.js | 40 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/@vue/cli/lib/invoke.js b/packages/@vue/cli/lib/invoke.js index 740e77f991..226253e49d 100644 --- a/packages/@vue/cli/lib/invoke.js +++ b/packages/@vue/cli/lib/invoke.js @@ -4,6 +4,7 @@ const execa = require('execa') const chalk = require('chalk') const globby = require('globby') const inquirer = require('inquirer') +const isBinary = require('isbinaryfile') const Generator = require('./Generator') const { loadOptions } = require('./options') const { installDeps } = require('./util/installDeps') @@ -27,7 +28,10 @@ async function readFiles (context) { }) const res = {} for (const file of files) { - res[file] = fs.readFileSync(path.resolve(context, file), 'utf-8') + const name = path.resolve(context, file) + res[file] = isBinary.sync(name) + ? fs.readFileSync(name) + : fs.readFileSync(name, 'utf-8') } return res } @@ -48,11 +52,11 @@ async function invoke (pluginName, options = {}, context = process.cwd()) { if (!deps) return let name // official - if (deps[name = `@vue/cli-plugin-${pluginName}`]) { + if (deps[(name = `@vue/cli-plugin-${pluginName}`)]) { return name } // full id, scoped short, or default short - if (deps[name = resolvePluginId(pluginName)]) { + if (deps[(name = resolvePluginId(pluginName))]) { return name } } @@ -61,7 +65,7 @@ async function invoke (pluginName, options = {}, context = process.cwd()) { if (!id) { throw new Error( `Cannot resolve plugin ${chalk.yellow(pluginName)} from package.json. ` + - `Did you forget to install it?` + `Did you forget to install it?` ) } @@ -102,14 +106,14 @@ async function invoke (pluginName, options = {}, context = process.cwd()) { const newDeps = generator.pkg.dependencies const newDevDeps = generator.pkg.devDependencies - const depsChanged = ( + const depsChanged = JSON.stringify(newDeps) !== JSON.stringify(pkg.dependencies) || JSON.stringify(newDevDeps) !== JSON.stringify(pkg.devDependencies) - ) if (!isTestOrDebug && depsChanged) { logWithSpinner('📦', `Installing additional dependencies...`) - const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm') + const packageManager = + loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm') await installDeps(context, packageManager) } @@ -125,14 +129,30 @@ async function invoke (pluginName, options = {}, context = process.cwd()) { log() log(` Successfully invoked generator for plugin: ${chalk.cyan(id)}`) if (!process.env.VUE_CLI_TEST && hasGit()) { - const { stdout } = await execa('git', ['ls-files', '--exclude-standard', '--modified', '--others']) + const { stdout } = await execa('git', [ + 'ls-files', + '--exclude-standard', + '--modified', + '--others' + ]) if (stdout.trim()) { log(` The following files have been updated / added:\n`) - log(chalk.red(stdout.split(/\r?\n/g).map(line => ` ${line}`).join('\n'))) + log( + chalk.red( + stdout + .split(/\r?\n/g) + .map(line => ` ${line}`) + .join('\n') + ) + ) log() } } - log(` You should review these changes with ${chalk.cyan(`git diff`)} and commit them.`) + log( + ` You should review these changes with ${chalk.cyan( + `git diff` + )} and commit them.` + ) log() generator.printExitLogs()