Skip to content

Commit

Permalink
Merge pull request #6236 from sergeyzwezdin/next
Browse files Browse the repository at this point in the history
Forked CRA support on Windows
  • Loading branch information
shilman committed Mar 23, 2019
1 parent e2abc09 commit b67002f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/react/src/server/cra-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,37 @@ export function getReactScriptsPath({ noCache } = {}) {
if (reactScriptsPath && !noCache) return reactScriptsPath;

const appDirectory = fs.realpathSync(process.cwd());
const reactScriptsScriptPath = fs.realpathSync(
let reactScriptsScriptPath = fs.realpathSync(
path.join(appDirectory, '/node_modules/.bin/react-scripts')
);

try {
// Note: Since there is no symlink for .bin/react-scripts on Windows
// we'll parse react-scripts file to find actual package path.
// This is important if you use fork of CRA.

const pathIsNotResolved = /node_modules[\\/]\.bin[\\/]react-scripts/i.test(
reactScriptsScriptPath
);

if (pathIsNotResolved) {
const content = fs.readFileSync(reactScriptsScriptPath, 'utf8');
const packagePathMatch = content.match(
/"\$basedir[\\/]([^\s]+?[\\/]bin[\\/]react-scripts\.js")/i
);

if (packagePathMatch && packagePathMatch.length > 1) {
reactScriptsScriptPath = path.join(
appDirectory,
'/node_modules/.bin/',
packagePathMatch[1]
);
}
}
} catch (e) {
console.warn('Error occured during react-scripts package path resolving.', e);
}

reactScriptsPath = path.join(reactScriptsScriptPath, '../..');
const scriptsPkgJson = path.join(reactScriptsPath, 'package.json');

Expand Down
33 changes: 33 additions & 0 deletions app/react/src/server/cra-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mockConfig from './__mocks__/mockConfig';

jest.mock('fs', () => ({
realpathSync: jest.fn(),
readFileSync: jest.fn(),
existsSync: () => true,
}));
jest.mock('mini-css-extract-plugin', () => {});
Expand Down Expand Up @@ -46,6 +47,38 @@ describe('cra-config', () => {
});
});

describe('when used with a custom react-scripts package without symlinks in .bin folder', () => {
beforeEach(() => {
// In case of .bin/react-scripts is not symlink (like it happens on Windows),
// realpathSync() method does not translate the path.
fs.realpathSync.mockImplementationOnce(filePath => filePath);

fs.readFileSync.mockImplementationOnce(
() => `#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case \`uname\` in
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../custom-react-scripts/bin/react-scripts.js" "$@"
ret=$?
else
node "$basedir/../custom-react-scripts/bin/react-scripts.js" "$@"
ret=$?
fi
exit $ret`
);
});

it('should locate the react-scripts package', () => {
expect(getReactScriptsPath({ noCache: true })).toEqual(
'/test-project/node_modules/custom-react-scripts'
);
});
});

describe('when used with TypeScript', () => {
it('should return the correct config', () => {
// Normalise the return, as we know our new rules object will be an array, whereas a string is expected.
Expand Down

0 comments on commit b67002f

Please sign in to comment.