Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #513 from Shopify/fix-create-slate-theme-spaces
Browse files Browse the repository at this point in the history
Make sure paths with spaces don't break Create Slate Theme
  • Loading branch information
t-kelly authored Apr 25, 2018
2 parents 8169df9 + 46cb0a1 commit 1a7dc23
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
15 changes: 8 additions & 7 deletions packages/create-slate-theme/__tests__/create-slate-theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ const path = require('path');
const execa = require('execa');
const createSlateTheme = require('../create-slate-theme');
const config = require('../create-slate-theme.config');
const {splitCommandString} = require('../utils');

const TEST_PROJECT = 'test-project';
const TEST_STARTER = 'test-repo';
const TEST_COMMITTISH = '123456';
const CLONE_HTTPS_COMMAND = `git clone
https://github.com/shopify/${TEST_STARTER}
${path.resolve(TEST_PROJECT)}
"${path.resolve(TEST_PROJECT)}"
--single-branch`;
const CLONE_SSH_COMMAND = `git clone
[email protected]:shopify/${TEST_STARTER}.git
${path.resolve(TEST_PROJECT)}
"${path.resolve(TEST_PROJECT)}"
--single-branch`;
const CLONE_BRANCH_COMMAND = `git clone
-b ${TEST_COMMITTISH}
https://github.com/shopify/${TEST_STARTER}
${path.resolve(TEST_PROJECT)}
"${path.resolve(TEST_PROJECT)}"
--single-branch`;

jest.mock('@shopify/slate-env', () => {
Expand All @@ -27,7 +28,7 @@ jest.mock('@shopify/slate-env', () => {

beforeAll(() => {
// Mock process.exit since it terminates the test runner
process.exit = jest.fn(code => {
process.exit = jest.fn((code) => {
throw new Error(`Process exit with code: ${code}`);
});
});
Expand All @@ -38,7 +39,7 @@ beforeEach(() => {
});

test('can clone a theme from a Git repo using HTTPS', async () => {
const [file, ...args] = CLONE_HTTPS_COMMAND.split(/\s+/);
const [file, ...args] = splitCommandString(CLONE_HTTPS_COMMAND);

await createSlateTheme('test-project', 'shopify/test-repo');

Expand All @@ -47,7 +48,7 @@ test('can clone a theme from a Git repo using HTTPS', async () => {
});

test('can clone a theme from a Git repo using SSH', async () => {
const [file, ...args] = CLONE_SSH_COMMAND.split(/\s+/);
const [file, ...args] = splitCommandString(CLONE_SSH_COMMAND);

await createSlateTheme('test-project', 'shopify/test-repo', {ssh: true});

Expand All @@ -56,7 +57,7 @@ test('can clone a theme from a Git repo using SSH', async () => {
});

test('can clone a theme from a Github repo with a specified commitish (branch)', async () => {
const [file, ...args] = CLONE_BRANCH_COMMAND.split(/\s+/);
const [file, ...args] = splitCommandString(CLONE_BRANCH_COMMAND);

await createSlateTheme('test-project', 'shopify/test-repo#123456');

Expand Down
10 changes: 5 additions & 5 deletions packages/create-slate-theme/create-slate-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function checkAppName(name) {

function printValidationResults(results) {
if (typeof results !== 'undefined') {
results.forEach(error => {
results.forEach((error) => {
console.error(chalk.red(` * ${error}`));
});
}
Expand All @@ -64,7 +64,7 @@ function printValidationResults(results) {
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
function checkDirForConflicts(root) {
const files = fs.readdirSync(root);
const conflicts = files.filter(file => !config.validFiles.includes(file));
const conflicts = files.filter((file) => !config.validFiles.includes(file));

if (conflicts.length > 0) {
console.log();
Expand Down Expand Up @@ -116,7 +116,7 @@ function copyFromDir(starter, root) {
`Creating new theme from local starter: ${chalk.green(starter)}`,
);
return fs.copy(starter, root, {
filter: file =>
filter: (file) =>
!/^\.(git|hg)$/.test(path.basename(file)) && !/node_modules/.test(file),
});
});
Expand All @@ -136,13 +136,13 @@ function cloneFromGit(hostInfo, root, ssh) {
console.log(`Cloning theme from a git repo: ${chalk.green(url)}`);

return utils
.spawn(`git clone ${branch} ${url} ${root} --single-branch`, {
.spawn(`git clone ${branch} ${url} "${root}" --single-branch`, {
stdio: 'pipe',
})
.then(() => {
return fs.remove(path.join(root, '.git'));
})
.catch(error => {
.catch((error) => {
console.log();
console.log(chalk.red('There was an error while cloning the git repo:'));
console.log('');
Expand Down
20 changes: 19 additions & 1 deletion packages/create-slate-theme/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ const {execSync} = require('child_process');
const execa = require('execa');

function spawn(cmd, options = {stdio: 'inherit'}) {
const [file, ...args] = cmd.split(/\s+/);
const [file, ...args] = splitCommandString(cmd);
return execa(file, args, options);
}

function splitCommandString(cmd) {
const regexp = /[^\s"]+|"([^"]*)"/gi;
const parts = [];

do {
// Each call to exec returns the next regex match as an array
var match = regexp.exec(cmd);
if (match != null) {
// Index 1 in the array is the captured group if it exists
// Index 0 is the matched text, which we use if no captured group exists
parts.push(match[1] ? match[1] : match[0]);
}
} while (match != null);

return parts;
}

// Checks the existence of yarn package
// We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
// Refer to https://github.com/yarnpkg/yarn/issues/673
Expand All @@ -23,4 +40,5 @@ function shouldUseYarn() {
module.exports = {
spawn,
shouldUseYarn,
splitCommandString,
};

0 comments on commit 1a7dc23

Please sign in to comment.