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

Default to https when cloning new Slate theme #445

Merged
merged 3 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/create-slate-theme/__mocks__/hosted-git-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const TEST_INFO = {
ssh() {
return 'git@github.com:shopify/test-repo.git';
},
https() {
return 'https://github.com/shopify/test-repo';
},
};

function fromUrl(repo) {
Expand Down
21 changes: 17 additions & 4 deletions packages/create-slate-theme/__tests__/create-slate-theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ const config = require('../create-slate-theme.config');
const TEST_PROJECT = 'test-project';
const TEST_STARTER = 'test-repo';
const TEST_COMMITTISH = '123456';
const CLONE_COMMAND = `git clone
const CLONE_HTTPS_COMMAND = `git clone
https://github.com/shopify/${TEST_STARTER}
${path.resolve(TEST_PROJECT)}
--single-branch`;
const CLONE_SSH_COMMAND = `git clone
git@github.com:shopify/${TEST_STARTER}.git
${path.resolve(TEST_PROJECT)}
--single-branch`;
const CLONE_BRANCH_COMMAND = `git clone
-b ${TEST_COMMITTISH}
git@github.com:shopify/${TEST_STARTER}.git
https://github.com/shopify/${TEST_STARTER}
${path.resolve(TEST_PROJECT)}
--single-branch`;

Expand All @@ -33,15 +37,24 @@ beforeEach(() => {
execa().mockClear();
});

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

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

expect(fs.existsSync('test-project/package.json')).toBeTruthy();
expect(execa()).toHaveBeenCalledWith(file, args, {stdio: 'pipe'});
});

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

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

expect(fs.existsSync('test-project/package.json')).toBeTruthy();
expect(execa()).toHaveBeenCalledWith(file, args, {stdio: 'pipe'});
});

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

Expand Down
15 changes: 15 additions & 0 deletions packages/create-slate-theme/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ test('Calls createSlateTheme with the default repo if process.argv[3] is undefin
process.argv = args;
});

test('Registers an --ssh flag and passes it to Create Slate Theme', () => {
const config = Object.assign({}, require('../create-slate-theme.config'));
const mockArgs = ['node', 'index.js', 'test-project', '--ssh'];

config.defaultOptions = Object.assign({}, config.defaultOptions, {ssh: true});
process.argv = mockArgs;

require('./../index.js');
expect(require('../create-slate-theme')).toHaveBeenCalledWith(
mockArgs[2],
config.defaultStarter,
config.defaultOptions,
);
});

test('Exits if Node version is lower than 8.9.4', () => {
Object.defineProperty(process.versions, 'node', {
value: '8.9.3',
Expand Down
1 change: 1 addition & 0 deletions packages/create-slate-theme/create-slate-theme.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
},
defaultOptions: {
skipInstall: false,
ssh: false,
},
validFiles: [
'.DS_Store',
Expand Down
25 changes: 14 additions & 11 deletions packages/create-slate-theme/create-slate-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const packageJson = require('./package.json');

module.exports = async function createSlateTheme(name, starter, flags) {
const root = path.resolve(name);
const options = Object.assign(config.defaultOptions, flags);
const options = Object.assign({}, config.defaultOptions, flags);

checkAppName(name);
fs.ensureDirSync(root);
Expand All @@ -22,12 +22,12 @@ module.exports = async function createSlateTheme(name, starter, flags) {
version: packageJson,
starter,
skipInstall: options.skipInstall,
verbose: options.verbose,
ssh: options.ssh,
});

console.log(`\nCreating a new Slate theme in: ${chalk.green(root)}.`);

await getStarterTheme(root, starter, options.verbose);
await getStarterTheme(root, starter, options.ssh);
await env.create({root});
await installThemeDeps(root, options);

Expand Down Expand Up @@ -123,19 +123,22 @@ function copyFromDir(starter, root) {
}

// Clones starter from URI.
function cloneFromGit(hostInfo, root, verbose) {
const url = hostInfo.ssh({noCommittish: true});
function cloneFromGit(hostInfo, root, ssh) {
const branch = hostInfo.committish ? `-b ${hostInfo.committish}` : '';
const options = {stdio: 'pipe'};
let url;

if (verbose) {
options.stdio = 'inherit';
if (ssh) {
url = hostInfo.ssh({noCommittish: true});
} else {
url = hostInfo.https({noCommittish: true, noGitPlus: true});
}

console.log(`Cloning theme from a git repo: ${chalk.green(url)}`);

return utils
.spawn(`git clone ${branch} ${url} ${root} --single-branch`, options)
.spawn(`git clone ${branch} ${url} ${root} --single-branch`, {
stdio: 'pipe',
})
.then(() => {
return fs.remove(path.join(root, '.git'));
})
Expand All @@ -149,11 +152,11 @@ function cloneFromGit(hostInfo, root, verbose) {
});
}

function getStarterTheme(root, starter, verbose) {
function getStarterTheme(root, starter, ssh) {
const hostedInfo = hostedGitInfo.fromUrl(starter);

if (hostedInfo) {
return cloneFromGit(hostedInfo, root, verbose);
return cloneFromGit(hostedInfo, root, ssh);
} else {
return copyFromDir(starter, root);
}
Expand Down
48 changes: 27 additions & 21 deletions packages/create-slate-theme/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
#!/usr/bin/env node
/* eslint-disable */

const chalk = require('chalk');
const program = require('commander');
const semver = require('semver');
const createSlateTheme = require('./create-slate-theme');
const packageJson = require('./package.json');
const config = require('./create-slate-theme.config');
/* This file must remain compatible for as many legacy versions of Node as possible. Avoid ES6+ language. */

const currentNodeVersion = process.versions.node;
var chalk = require('chalk');
var program = require('commander');
var semver = require('semver');
var packageJson = require('./package.json');
var config = require('./create-slate-theme.config');

var currentNodeVersion = process.versions.node;

if (!semver.satisfies(currentNodeVersion, '>=8.9.4')) {
console.log(
chalk.red(
`You are running Node ${currentNodeVersion}\n\`` +
`Create Slate Theme requires Node 8.9.4 or higher. \n` +
`Please update your version of Node.`,
),
'You are running Node ' +
currentNodeVersion +
'\n' +
'Create Slate Theme requires Node 8.9.4 or higher. \n' +
'Please update your version of Node.'
)
);

process.exit(1);
}

let themeName;
let themeStarter;
var themeName;
var themeStarter;

program
.version(packageJson.version)
.usage(`${chalk.green('<theme-directory>')} [starter-theme] [options]`)
.usage(chalk.green('<theme-directory>') + '[starter-theme] [options]')
.arguments('<name> [repo]')
.option('--skipInstall', 'skip installing theme dependencies')
.option('--ssh', 'uses SSH to clone git repo')
.option('--verbose', 'print additional logs')
.action((name, starter = config.defaultStarter) => {
.action((name, starter) => {
themeName = name;
themeStarter = starter;
themeStarter = starter || config.defaultStarter;
})
.parse(process.argv);

if (typeof themeName === 'undefined') {
console.error('Please specify the theme directory:');
console.log(
`${chalk.cyan(program.name())} ${chalk.green('<theme-directory>')}`,
`${chalk.cyan(program.name())} ${chalk.green('<theme-directory>')}`
);
console.log();
console.log('For example:');
console.log(`${chalk.cyan(program.name())} ${chalk.green('my-theme')}`);
console.log(chalk.cyan(program.name()) + ' ' + chalk.green('my-theme'));
console.log();
console.log(
`Run${chalk.cyan(`${program.name()} --help`)} to see all options.`,
'Run ' + chalk.cyan(program.name() + ' --help') + ' to see all options.'
);
process.exit(1);
}
Expand All @@ -57,8 +62,9 @@ function assignOption(key) {
: program[key];
}

const options = {
var options = {
skipInstall: assignOption('skipInstall'),
ssh: assignOption('ssh'),
};

createSlateTheme(themeName, themeStarter, options);
require('./create-slate-theme')(themeName, themeStarter, options);