Skip to content

Instantly share code, notes, and snippets.

@dmihal
Created November 10, 2017 20:50
Show Gist options
  • Save dmihal/4c7ceb0db7650a0641e1a75e8fc1648a to your computer and use it in GitHub Desktop.
Save dmihal/4c7ceb0db7650a0641e1a75e8fc1648a to your computer and use it in GitHub Desktop.
Zip and upload files from the `build` directory to the Chrome Webstore
require('shelljs/global');
require('dotenv').config();
const fs = require('fs');
const archiver = require('archiver');
const webstore = require('chrome-webstore-upload');
const fetch = require('isomorphic-fetch');
function createZip(path) {
return new Promise((resolve, reject) => {
var output = fs.createWriteStream(path);
var archive = archiver.create('zip', {});
output.on('close', function() {
resolve();
});
archive.pipe(output);
archive
.directory(__dirname + '/../build/', false)
.finalize();
});
}
function getBranch() {
return process.env.CI_COMMIT_REF_NAME || exec('git rev-parse --abbrev-ref HEAD', {silent:true}).trim();
}
function getConfig() {
let configStr = process.env.DEPLOY_CONFIG;
if (!configStr) {
throw "Can not load deploy configuration.\n" +
"If running locally, make sure you have a .env file in the project root.";
}
return JSON.parse(configStr);
}
function errorHandler(e) {
console.error('Error', e);
process.exit(1);
}
(function main() {
let branch = getBranch();
const deployConfig = getConfig();
let config = deployConfig[branch];
if (!config) {
console.log(`No deploy target for branch "${branch}"`);
return;
}
let manifest = JSON.parse(require('fs').readFileSync(__dirname + '/../build/manifest.json', 'utf8'));
console.log(`Deploying version ${manifest.version} to extension '${config.extensionId}'`);
const webstoreExtenstion = webstore(config);
const buildPath = __dirname + '/../build.zip';
createZip(buildPath).then(() => {
const zipFile = fs.createReadStream(buildPath);
const target = 'default'; // Can also be 'trustedTesters'
webstoreExtenstion.fetchToken()
.then(token =>
webstoreExtenstion.uploadExisting(zipFile, token)
.then(res => {
if (res.uploadState == 'SUCCESS') {
webstoreExtenstion.publish(target, token)
.then(res => console.log('Extension publish status: ', res.status));
} else {
console.error('Upload failed', res);
process.exit(1);
}
}).catch(errorHandler)).catch(errorHandler);
}).catch(errorHandler);
})();
(END)
DEPLOY_CONFIG={"develop":{"extensionId": "DEV_EXTENSION_ID", "clientId": "CLIENT_ID", "clientSecret": "SECRET", "refreshToken": "TOKEN"}, "master":{"extensionId": "PROD_EXTENSION_ID", "clientId": "CLIENT_ID", "clientSecret": "SECRET", "refreshToken": "TOKEN"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment