Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
validate theme_id on deploy tasks (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanPJF authored Apr 7, 2017
1 parent e7bdfe3 commit ca62195
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ gulp.task('zip', (done) => {
* @static
*/
gulp.task('watch', () => {
runSequence('build:config', defineWatchTasks());
runSequence('validate:id', 'build:config', defineWatchTasks());
});

function defineWatchTasks() {
Expand All @@ -77,7 +77,7 @@ function defineWatchTasks() {
* @static
*/
gulp.task('deploy', (done) => {
runSequence('build', 'deploy:replace', done);
runSequence('validate:id', 'build', 'deploy:replace', done);
});

/**
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/deploy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ gulp.task('deploy:sync-init', () => {
const envObj = tkConfig[environment];
let proxyTarget = `https://${envObj.store}`;

if (envObj.theme_id && (envObj.theme_id === parseInt(envObj.theme_id, 10))) {
proxyTarget += `?preview_theme_id=${envObj.theme_id}`;
}
// break theme preview cache by always setting a preview parameter
const previewParam = (envObj.theme_id !== 'live') ? envObj.theme_id : '';
proxyTarget += `?preview_theme_id=${previewParam}`;

debug(proxyTarget);

Expand Down
61 changes: 61 additions & 0 deletions src/tasks/deploy-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,67 @@ function deploy(env) {
});
}

/**
* Validate theme_id used for the environment
* @param {Object} - settings of theme_id and environment
* @returns {Promise}
* @private
*/
function validateId(settings) {
return new Promise((resolve, reject) => {
// Only string allowed is "live"
if (settings.themeId === 'live') {
resolve();
}

const id = Number(settings.themeId);

if (isNaN(id)) {
reject(settings);
} else {
resolve();
}
});
}

/**
* Validate the config.yml theme_id is an integer or "live"
* @function validate:id
* @memberof slate-cli.tasks.watch, slate-cli.tasks.deploy
* @private
*/

gulp.task('validate:id', () => {
const file = fs.readFileSync(config.tkConfig, 'utf8');
const tkConfig = yaml.safeLoad(file);
let envObj;

const environments = config.environment.split(/\s*,\s*|\s+/);
const promises = [];

environments.forEach((environment) => {
function factory() {
envObj = tkConfig[environment];
const envSettings = {
themeId: envObj.theme_id,
environment,
};

return validateId(envSettings);
}
promises.push(factory);
});

return utils.promiseSeries(promises)
.catch((result) => {
// stop process to prevent deploy defaulting to published theme
messages.invalidThemeId(result.themeId, result.environment);
const exitCode = 2;
return process.exit(exitCode);
});

});

/**
* Replace your existing theme using ThemeKit.
*
Expand Down
7 changes: 7 additions & 0 deletions src/tasks/includes/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ const messages = {
' and run a full <slate deploy> as a result.';
},

invalidThemeId: (themeId, env) => {
gutil.log('Invalid theme id for',
gutil.colors.cyan(`${env}: ${themeId}`),
gutil.colors.yellow('`theme_id` must be an integer or "live".'),
);
},

configError: () => {
return '`config.yml` does not exist. You need to add a config file before you can upload your theme to Shopify.';
},
Expand Down

0 comments on commit ca62195

Please sign in to comment.