Skip to content

Commit

Permalink
feat: add flag to auto-delete oldest theme during push
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-NikitaPuzanenko committed Jan 14, 2020
1 parent ab5c5bc commit ea93793
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 3 additions & 1 deletion bin/stencil-push
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Program
.option('-f, --file [filename]', 'specify the filename of the bundle to upload')
.option('-s, --save [filename]', 'specify the filename to save the bundle as')
.option('-a, --activate [variationname]', 'specify the variation of the theme to activate')
.option('-d, --delete', 'delete oldest private theme if upload limit reached')
.parse(process.argv);

if (!versionCheck()) {
Expand All @@ -26,7 +27,8 @@ stencilPush(Object.assign({}, options, {
apiHost: Program.host || apiHost,
bundleZipPath: Program.file,
activate: Program.activate,
saveBundleName: Program.save
saveBundleName: Program.save,
deleteOldest: Program.delete,
}), (err, result) => {
if (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
Expand Down
11 changes: 9 additions & 2 deletions lib/stencil-push.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ utils.uploadBundle = (options, callback) => {
error.name = 'ThemeUploadError';
return callback(error);
}

callback(null, Object.assign({}, options, {
jobId: result.jobId,
themeLimitReached: !!result.themeLimitReached,
Expand All @@ -140,7 +139,7 @@ utils.uploadBundle = (options, callback) => {
};

utils.notifyUserOfThemeLimitReachedIfNecessary = (options, callback) => {
if (options.themeLimitReached) {
if (options.themeLimitReached && !options.deleteOldest) {
console.log('warning'.yellow + ` -- You have reached your upload limit. In order to proceed, you'll need to delete at least one theme.`);
}

Expand All @@ -152,6 +151,14 @@ utils.promptUserToDeleteThemesIfNecessary = (options, callback) => {
return async.nextTick(callback.bind(null, null, options));
}

if (options.deleteOldest) {
const oldestTheme = options.themes
.filter(theme => theme.is_private && !theme.is_active)
.map(theme => ({uuid: theme.uuid, updated_at : new Date(theme.updated_at).valueOf()}))
.reduce((prev, current) => prev.updated_at < current.updated_at ? prev : current)
return callback(null, Object.assign({}, options, {themeIdsToDelete: [oldestTheme.uuid]}));
}

const questions = [{
choices: options.themes.map(theme => ({
disabled: theme.is_active || !theme.is_private,
Expand Down

0 comments on commit ea93793

Please sign in to comment.