Skip to content

Commit

Permalink
Fix running mix CLI on Windows
Browse files Browse the repository at this point in the history
This is not ideal but I don’t have another solution at the moment. The bug is not with mix but with NPM’s generated files under node_modules/.bin. With the way those commands are run process.argv gets split on spaces even when they’re quoted.
  • Loading branch information
thecrypticace committed Jan 11, 2021
1 parent ff32a82 commit 2248853
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
22 changes: 15 additions & 7 deletions bin/cli.js
Expand Up @@ -2,6 +2,7 @@

const { Command } = require('commander');
const { spawn } = require('child_process');
const path = require('path');

run();

Expand Down Expand Up @@ -46,18 +47,25 @@ async function run() {
* @param {string[]} args
*/
async function executeScript(cmd, opts, args = []) {
const env =
opts.production
? 'production'
: (isTesting() && process.env.NODE_ENV === 'test') || !process.env.NODE_ENV
? 'development'
: process.env.NODE_ENV;
const env = opts.production
? 'production'
: (isTesting() && process.env.NODE_ENV === 'test') || !process.env.NODE_ENV
? 'development'
: process.env.NODE_ENV;

// We MUST use a relative path because the files
// created by npm dont correctly handle paths
// containg spaces on Windows (yarn does)
const configPath = path.relative(
process.cwd(),
require.resolve('../setup/webpack.config.js')
);

let script = [
`cross-env NODE_ENV=${env}`,
`MIX_FILE="${opts.mixConfig}"`,
commandScript(cmd, opts),
`--config="${require.resolve('../setup/webpack.config.js')}"`,
`--config="${configPath}"`,
...quoteArgs(args)
].join(' ');

Expand Down
29 changes: 20 additions & 9 deletions test/unit/cli.js
@@ -1,14 +1,25 @@
import test from 'ava';
import path from 'path';
import { cli } from '../helpers/cli';

const mix = cli({ testing: true });

/** @type {string} */
let configPath = '';

test.before(() => {
configPath = path.relative(
process.cwd(),
require.resolve('../../setup/webpack.config.js')
);
});

test('it calls webpack in development mode', async t => {
let { stdout } = await mix();

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack --progress --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand All @@ -19,7 +30,7 @@ test('it calls webpack in production mode', async t => {

t.is(
'cross-env NODE_ENV=production MIX_FILE="webpack.mix" npx webpack --progress --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand All @@ -30,7 +41,7 @@ test('it calls webpack with watch mode', async t => {

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack --progress --watch --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand All @@ -41,7 +52,7 @@ test('it calls webpack with watch mode using polling', async t => {

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack --progress --watch --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"' +
' --watch-poll',
stdout
Expand All @@ -53,7 +64,7 @@ test('it calls webpack with hot reloading', async t => {

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack serve --hot --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand All @@ -64,7 +75,7 @@ test('it calls webpack with hot reloading using polling', async t => {

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack serve --hot --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"' +
' --watch-poll',
stdout
Expand All @@ -76,7 +87,7 @@ test('it calls webpack with quoted key value pair command arguments', async t =>

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack --progress --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"' +
' --env foo="bar baz" foo="bar=baz"',
stdout
Expand All @@ -94,7 +105,7 @@ test('it calls webpack with custom node_env', async t => {

t.is(
'cross-env NODE_ENV=foobar MIX_FILE="webpack.mix" npx webpack --progress --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand All @@ -107,7 +118,7 @@ test('it disables progress reporting when not using a terminal', async t => {

t.is(
'cross-env NODE_ENV=development MIX_FILE="webpack.mix" npx webpack --config="' +
require.resolve('../../setup/webpack.config.js') +
configPath +
'"',
stdout
);
Expand Down

0 comments on commit 2248853

Please sign in to comment.