azur
Version:
Azure Web App Deployment Automation
123 lines (102 loc) • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _debug = _interopRequireDefault(require("debug"));
var _del = _interopRequireDefault(require("del"));
var _nodegit = _interopRequireDefault(require("nodegit"));
var tmp = _interopRequireWildcard(require("./lib/tmp"));
var _extract = _interopRequireDefault(require("./lib/extract"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const log = (0, _debug.default)('azur');
log.log = console.log.bind(console); // eslint-disable-line no-console
class Application {
constructor({
appName,
username,
password,
gitName,
gitEmail
}) {
this.appName = appName;
this.credentials = {
username,
password
};
this.gitName = gitName;
this.gitEmail = gitEmail;
this.gitUrl = `https://${username}:${password}@${appName}.scm.azurewebsites.net:443/${appName}.git`;
}
async deploy({
archiveFilePath
}) {
log('Starting deployment...');
log('Creating tmp directory...');
const {
path,
cleanup
} = await tmp.dir();
log('Created new tmp dir:', path);
let error = null;
try {
log('Cloning Azure Git Repository...');
const repo = await _nodegit.default.Clone(this.gitUrl, path, {
fetchOpts: {
callbacks: {
credentials: () => _nodegit.default.Cred.userpassPlaintextNew(this.credentials.username, this.credentials.password)
}
}
});
log('Cloned successfully');
log('Cleaning repository...');
await (0, _del.default)([`${path}/**/*`, `!${path}/.git`], {
force: true
});
log('Cleaned repository successfully');
log('Extracting new application version...');
await (0, _extract.default)(archiveFilePath, path);
log('Extracted new application successfully');
log('Tracking files in Git...');
const statuses = await repo.getStatusExt();
const paths = statuses.map(status => status.path()); // eslint-disable-next-line no-bitwise
const isBare = repo.isBare() & _nodegit.default.Repository.INIT_FLAG.BARE;
const index = await (isBare ? repo.refreshIndex() : repo.index());
if (!isBare) await index.read(1);
await index.addAll();
paths.forEach(p => {
// eslint-disable-next-line no-bitwise
if (_nodegit.default.Status.file(repo, p) & _nodegit.default.Status.STATUS.WT_DELETED) {
index.removeByPath(p);
} else {
index.addByPath(p);
}
});
await index.write();
log('Tracked files in Git successfully');
log('Writing commit...');
const oid = await index.writeTree();
let parent = null;
if (!isBare) {
const head = await _nodegit.default.Reference.nameToId(repo, 'HEAD');
parent = await repo.getCommit(head);
}
const signature = _nodegit.default.Signature.create(this.gitName, this.gitEmail, Date.now(), 0);
await repo.createCommit('HEAD', signature, signature, 'Deployment', oid, isBare ? [] : [parent]);
log('Wrote commit successfully');
const remote = await repo.getRemote('origin');
log('Pushing changes to Azure App Service... (this may take a while)');
await remote.push(['refs/heads/master:refs/heads/master']);
log('Finished deployment');
} catch (e) {
error = e;
} finally {
cleanup();
}
if (error) throw error;
}
}
exports.default = Application;
//# sourceMappingURL=application.js.map