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

Commit

Permalink
Generate new theme / clone slate repo (#7)
Browse files Browse the repository at this point in the history
* Clone a repo to your project folder

* Changes

* changes

* Changes

* Change

* refactor some code

* install dependencies

* remove random files

* git init and replace slate pkg

* add git/yeoman tpl files/env questions

* finish todos and start commenting code

* remove extra logs

* update comment on private function

* add last private function comment

* absolute dep

* commit stuff

* get generator files from slate repo

* pull templates from cache

* fix indent
  • Loading branch information
Ryan Macdonald authored and Thomas Kelly committed Jul 28, 2017
1 parent 1b14b01 commit 490bbd6
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 56 deletions.
119 changes: 119 additions & 0 deletions packages/slate-cli/generators/slate-shopify/includes/cloneRepo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
var NodeGit = require('nodegit');
var Promise = require('bluebird');
var path = require('path');
var rimraf = Promise.promisify(require('rimraf'));
var _ = require('lodash');

module.exports = {

/**
* Uses NodeGit to clone git repository and writes it to the destination path
*
* @param repo {String} - the location (url) of git repository
* @param destination {String} - the local destination path to write repository
*/
_cloneRepo: function(repo, destination) {
var cache = path.join(this.cacheRoot(), repo);
var options = {};

_.set(options, 'fetchOpts.callbacks.certificateCheck', checkCertificate);
_.set(options, 'fetchOpts.callbacks.credentials', getCredentials);

return rimraf(cache)
.then(function() {
return NodeGit.Clone(repo, cache, options);
})
.then(function() {
var glob = [
cache + '/**',
'!' + cache + '/.git/**',
'!' + cache + '/jsdoc-conf.json',
'!' + cache + '/docs/**',
'!' + cache + '/package.json',
'!' + cache + '/tasks/includes/config.js',
'!' + cache + '/generators/**'
];

this.fs.copy(glob, destination, {
globOptions: {
dot: true
}
});

this.fs.copyTpl(
path.join(cache, '/generators/config.yml.ejs'),
path.join(destination, '/config.yml'), {
environments: this.environments
}
);

this.fs.copyTpl(
path.join(cache, '/generators/package.json.ejs'),
path.join(destination, '/package.json'), {
name: this.dirname,
hasGitRepo: this.initGit,
repositoryUrl: this.repositoryUrl
}
);

this.fs.copyTpl(
path.join(cache, '/generators/tasks/includes/config.js.ejs'),
path.join(destination, '/tasks/includes/config.js'), {
env: this.defaultEnv
}
);
}.bind(this));
},

/**
* Uses NodeGit to initalize git repository
*
* @param repo {String} - the local path to the repository
* @param isBare {Boolean} - whether to use provided path as the working directory
*/
_initRepo: function(repo, isBare) {
return NodeGit.Repository.init(repo, isBare);
},

/**
* Uses NodeGit to add remote to git repository
*
* @param repo {String} - the local path to the repository
* @param name {String} - the name of the git remote
* @param url {String} - the url of the git remote
*/
_addRemote: function(repo, name, url) {
return NodeGit.Remote.create(repo, name, url)
.catch(function(err) {
this.log(err);
}.bind(this));
}
};

/**
* OS X libgit2 is unable to look up GitHub certificates correctly.
* In order to bypass this problem, we're going to passthrough
* the certificate check.
*
* @returns {Boolean}
* @private
*/
function checkCertificate() {
return 1;
}

/**
* In order to authorize the clone operation, we'll need to respond
* to a low-level callback that expects credentials to be passed.
* This function will respond back with the credentials from the
* agent. You'll notice we handle the second argument passed to
* credentials, userName.
*
* @param url {String}
* @param userName {String}
* @returns {String} - The newly created credential object.
* @private
*/
function getCredentials(url, userName) {
return NodeGit.Cred.sshKeyFromAgent(userName);
}
114 changes: 85 additions & 29 deletions packages/slate-cli/generators/slate-shopify/includes/questions.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
module.exports = {
theme: function(generator) {
var questions = [
{
type: 'confirm',
name: 'multiEnv',
message: 'Will you be deploying this theme to multiple environments?'
},
{
type: 'checkbox',
name: 'environments',
message: 'Which environments would you like to use?',
choices: [
{name: 'production', checked: true},
{name: 'staging', checked: true},
{name: 'development', checked: true},
{name: 'custom'}
],
when: hasMultipleEnvironments,
validate: function(answer) {
return answer.length < 1
? 'You must create at least one environment.'
: true;
}
},
{
type: 'input',
name: 'customEnv',
message: 'Enter the environment names you would like to create (comma separated)',
when: hasCustomEnvironments
var questions = [{
type: 'confirm',
name: 'multiEnv',
message: 'Will you be deploying this theme to multiple environments?'
}, {
type: 'checkbox',
name: 'environments',
message: 'Which environments would you like to use?',
choices: [{
name: 'production',
checked: true
}, {
name: 'staging',
checked: true
}, {
name: 'development',
checked: true
}, {
name: 'custom'
}],
when: hasMultipleEnvironments,
validate: requireEnv
}, {
type: 'input',
name: 'customEnv',
message: 'Enter the custom environment names you would like to create (comma separated)',
when: hasCustomEnvironments
}, {
type: 'list',
name: 'defaultEnv',
message: 'Which environment would you like to use as default?',
choices: getDefaultEnvSelect,
when: hasMultipleEnvironments,
validate: requireEnv
}, {
type: 'input',
name: 'repo',
message: 'Which repo would you like to start with?',
default: '[email protected]:Shopify/slate.git'
}, {
type: 'confirm',
name: 'initGit',
message: 'Will you be tracking this theme in git?'
}, {
type: 'input',
name: 'repositoryUrl',
message: 'Enter the URL of your git repository',
when: hasGitRepo,
validate: function(answer) {
return answer.length < 1
? 'You must provide a URL for your git repository.'
: true;
}
];
}];

if (!generator.dirname) {
questions.unshift({
Expand Down Expand Up @@ -77,3 +101,35 @@ function hasCustomEnvironments(answers) {
});
return hasCustom;
}

/**
*
* @param answers {Object}
* @returns {Boolean}
* @private
*/
function hasGitRepo(answers) {
return answers.initGit;
}

/**
*
* @param answers {Object}
* @returns {Boolean}
* @private
*/
function getDefaultEnvSelect(answers) {
return answers.environments;
}

/**
*
* @param answers {Object}
* @returns {Boolean}
* @private
*/
function requireEnv(answer) {
return answer.length < 1
? 'You must create at least one environment.'
: true;
}
10 changes: 0 additions & 10 deletions packages/slate-cli/generators/slate-shopify/templates/config.yml

This file was deleted.

59 changes: 43 additions & 16 deletions packages/slate-cli/generators/slate-shopify/theme.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
var generators = require('yeoman-generator');
var questions = require('./includes/questions.js');
var _ = require('lodash');
var open = require('open');
var questions = require('./includes/questions');
var fetchRepo = require('./includes/cloneRepo.js');

module.exports = generators.Base.extend({
var mainGenerator = generators.Base.extend({

constructor: function() {
generators.Base.apply(this, arguments);

this.argument('path', {type: String, required: true});
this.argument('dirname', {type: String, required: false});
this.argument('path', {
type: String,
required: true
});
this.argument('dirname', {
type: String,
required: false
});
this.environments = [];
},

initializing: function() {
this.log('Im initializing now');
},

prompting: function() {
return this.prompt(questions.theme(this))
.then(function(answers) {
Expand All @@ -25,32 +30,54 @@ module.exports = generators.Base.extend({
}

this.environments = env;
this.defaultEnv = answers.defaultEnv;

if (answers.dirname) {
this.dirname = answers.dirname;
}

this.repo = answers.repo;
this.initGit = answers.initGit;
this.repositoryUrl = answers.repositoryUrl;
}.bind(this));
},

configuring: function() {
this.destinationRoot(this.path + '/' + this.dirname);
this.config.set('environments', this.environments);
this.config.set('name', this.dirname);
this.config.save();
},

writing: function() {
var readFile = this.templatePath('config.yml');
var writeFile = this.destinationPath('config.yml');
this.fs.copyTpl(readFile, writeFile, {environments: this.environments});

// write `src` folder content for the theme
return this._cloneRepo(this.repo, this.destinationPath())
.then(function() {
if (this.initGit) {
return this._initRepo(this.destinationRoot(), 0)
.then(function(repo) {
return this._addRemote(repo, 'origin', this.repositoryUrl);
}.bind(this));
} else {
return true;
}
}.bind(this));
},

install: function() {
this.log('Im installing now');
// npm install, any other installs to run?
if (this.fs.exists(this.destinationPath('package.json'))) {
this.npmInstall();
}

if (this.fs.exists(this.destinationPath('bower.json'))) {
this.bowerInstall();
}
},

end: function() {
this.log('Im finished now');
open(this.destinationPath('config.yml'));
}
});

_.extend(mainGenerator.prototype, fetchRepo);

module.exports = mainGenerator;
2 changes: 1 addition & 1 deletion packages/slate-cli/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/slate-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
},
"dependencies": {
"bin-wrapper": "3.0.2",
"bluebird": "3.4.1",
"find-root": "1.0.0",
"global-npm": "0.3.0",
"lodash": "4.13.1",
"nodegit": "0.14.1",
"nopt": "3.0.6",
"open": "0.0.5",
"rimraf": "2.5.2",
"update-notifier": "0.7.0",
"yeoman-generator": "0.23.3",
"yo": "1.8.4"
Expand All @@ -50,6 +54,7 @@
"Promise": true
},
"rules": {
"no-unsafe-finally": 0,
"no-lonely-if": 0,
"comma-dangle": 0,
"spaced-comment": 0
Expand Down

0 comments on commit 490bbd6

Please sign in to comment.