-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to allow pulling live config files (#550)
* Add .nvmrc file for easy NVM version switching * feat: add pull command to pull live config settings * fix: eslint issues * fix: downgrade language features used to be compatible with test suite * fix: remove erroneous semi-colon * fix: remove more erroneous semi-colons * fix: remove yet another erroneous semi-colon
- Loading branch information
1 parent
d4fce7b
commit 43558fc
Showing
11 changed files
with
248 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"parserOptions": { | ||
"ecmaVersion": 6 | ||
"ecmaVersion": 8 | ||
}, | ||
"ecmaFeatures": { | ||
"modules": true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v10.16.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env node | ||
|
||
require('colors'); | ||
const apiHost = 'https://api.bigcommerce.com'; | ||
const dotStencilFilePath = './.stencil'; | ||
const options = { dotStencilFilePath }; | ||
const pkg = require('../package.json'); | ||
const Program = require('commander'); | ||
const stencilPull = require('../lib/stencil-pull'); | ||
const versionCheck = require('../lib/version-check'); | ||
const themeApiClient = require('../lib/theme-api-client'); | ||
|
||
Program | ||
.version(pkg.version) | ||
.option('--host [hostname]', 'specify the api host', apiHost) | ||
.option('--save [filename]', 'specify the filename to save the config as', 'config.json') | ||
.parse(process.argv); | ||
|
||
if (!versionCheck()) { | ||
return; | ||
} | ||
|
||
stencilPull(Object.assign({}, options, { | ||
apiHost: Program.host || apiHost, | ||
saveConfigName: Program.save, | ||
}), (err, result) => { | ||
if (err) { | ||
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`); | ||
themeApiClient.printErrorMessages(err.messages); | ||
console.log('If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.'); | ||
} else { | ||
console.log('ok'.green + ` -- Pulled active theme config to ${result.saveConfigName}`); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
const async = require('async'); | ||
let stencilPushUtils = require('./stencil-push.utils'); | ||
let stencilPullUtils = require('./stencil-pull.utils'); | ||
|
||
module.exports = stencilPull; | ||
|
||
function stencilPull(options, callback) { | ||
options = options || {}; | ||
async.waterfall([ | ||
async.constant(options), | ||
stencilPushUtils.readStencilConfigFile, | ||
stencilPushUtils.getStoreHash, | ||
stencilPushUtils.getThemes, | ||
stencilPullUtils.selectActiveTheme, | ||
stencilPullUtils.startThemeDownloadJob, | ||
stencilPushUtils.pollForJobCompletion(({ download_url: downloadUrl }) => ({ downloadUrl })), | ||
stencilPullUtils.downloadThemeConfig, | ||
], callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const themeApiClient = require('./theme-api-client'); | ||
const request = require("request"); | ||
const yauzl = require('yauzl'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const tmp = require('tmp'); | ||
const utils = {}; | ||
|
||
module.exports = utils; | ||
|
||
utils.selectActiveTheme = (options, callback) => { | ||
const [activeTheme] = options.themes.filter(theme => theme.is_active).map(theme => theme.uuid); | ||
|
||
callback(null, Object.assign({}, options, { activeTheme })); | ||
}; | ||
|
||
utils.startThemeDownloadJob = (options, callback) => { | ||
const config = options.config; | ||
|
||
themeApiClient.downloadTheme({ | ||
accessToken: config.accessToken, | ||
apiHost: options.apiHost, | ||
themeId: options.activeTheme, | ||
clientId: 'stencil-cli', | ||
storeHash: options.storeHash, | ||
}, (error, result) => { | ||
if (error) { | ||
error.name = 'ThemeUploadError'; | ||
return callback(error); | ||
} | ||
|
||
callback(null, Object.assign({}, options, { | ||
jobId: result.jobId, | ||
})); | ||
}); | ||
}; | ||
|
||
utils.downloadThemeConfig = (options, callback) => { | ||
tmp.file(function _tempFileCreated(err, tempThemePath, fd, cleanupCallback) { | ||
if (err) { | ||
callback(err); | ||
} | ||
|
||
( | ||
new Promise( | ||
(resolve, reject) => | ||
request(options.downloadUrl) | ||
.pipe(fs.createWriteStream(tempThemePath)) | ||
.on('finish', () => resolve(tempThemePath)) | ||
.on('error', reject) | ||
) | ||
) | ||
.then(tempThemePath => | ||
new Promise( | ||
(resolve, reject) => | ||
yauzl.open(tempThemePath, { lazyEntries: true }, (error, zipFile) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
zipFile.readEntry(); | ||
zipFile.on('entry', entry => { | ||
if (!/config\.json/.test(entry.fileName)) { | ||
zipFile.readEntry(); | ||
return; | ||
} | ||
|
||
zipFile.openReadStream(entry, (readStreamError, readStream) => { | ||
if (readStreamError) { | ||
return reject(readStreamError); | ||
} | ||
|
||
let configFileData = ''; | ||
|
||
readStream.on('end', () => { | ||
resolve(JSON.parse(configFileData)); | ||
zipFile.close(); | ||
}); | ||
readStream.on('data', chunk => { | ||
configFileData += chunk; | ||
}); | ||
}); | ||
}); | ||
}) | ||
) | ||
) | ||
.then( | ||
liveStencilConfig => | ||
new Promise( | ||
(resolve, reject) => | ||
fs.writeFile(path.resolve(options.saveConfigName), JSON.stringify(liveStencilConfig, null, 2), error => { | ||
if (error) { | ||
reject(error); | ||
} | ||
|
||
resolve(); | ||
}) | ||
) | ||
) | ||
.then(() => { | ||
cleanupCallback(); | ||
callback(null, options); | ||
}) | ||
.catch(callback); | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.