Skip to content

Commit

Permalink
Merge pull request #55 from leonhartX/support-manifest
Browse files Browse the repository at this point in the history
add support for manifest, fix github delete bug
  • Loading branch information
leonhartX authored Jan 19, 2018
2 parents f3b5ffb + 4cb6f59 commit c03b9e8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
31 changes: 19 additions & 12 deletions content/modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<span class="scm-config-modal-close modal-dialog-title-close"></span>
</div>
<div class="modal-dialog-content">
<div class="goog-inline-block" style="width: 600px; height: 100px;">
<div class="goog-inline-block" style="width: 600px; height: 120px;">
<div class="scm-config">
<table class="properties-data-dialog-table">
<tbody>
Expand All @@ -159,17 +159,24 @@
<th></th>
</tr>
<tr class="properties-data-dialog-table-row">
<td><div>File type to sync</div></td>
<td><div style="white-space: normal;">
<select id="filetype">
<option value=".gs">.gs</option>
<option value=".js">.js</option>
</select>
</div></td>
</tr>
<tr class="properties-data-dialog-table-row">
<td><div>Ignore file patterns (regex separated by ';')</div></td>
<td><input id="ignore-pattern" class="editable-row-input large-input" type="text"></td>
<td><div>File type to sync</div></td>
<td><div style="white-space: normal;">
<select id="filetype">
<option value=".gs">.gs</option>
<option value=".js">.js</option>
</select>
</div></td>
</tr>
<tr class="properties-data-dialog-table-row">
<td><div>Manage manifest file</div></td>
<td><div style="white-space: normal;">
<input id="manage-manifest" type="checkbox">
</div></td>
</tr>
<tr class="properties-data-dialog-table-row">
<td><div>Ignore file patterns (regex separated by ';')</div></td>
<td><input id="ignore-pattern" class="editable-row-input large-input" type="text"></td>
</tr>
</table>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.2.2",
"version": "3.3.0",
"manifest_version": 2,
"default_locale": "en",
"name": "__MSG_appName__",
Expand Down
8 changes: 6 additions & 2 deletions src/gas-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ class Gas {
const changed = $('.diff-file:checked').toArray().map(e => e.value);
const updatePromises = changed.filter(f => code.scm[f])
.map((file) => {
const match = file.match(/(.*?)\.(gs|html)$/);
const match = file.match(/(.*?)\.(gs|html|json)$/);
if (!match || !match[1] || !match[2]) {
showAlert('Unknow Error', LEVEL_ERROR);
showAlert('Unsupported file type.', LEVEL_ERROR);
return;
}
if (match[2] === 'json' && match[1] !== 'appsscript') {
showAlert('Unsupported file type', LEVEL_ERROR);
return;
}
const name = match[1];
const type = match[2];
Expand Down
51 changes: 36 additions & 15 deletions src/gas-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ function initContext() {
context.id = match[2];

return new Promise((resolve, reject) => {
chrome.storage.sync.get(['scm', 'token', 'user', 'baseUrl', 'bindRepo', 'bindBranch', 'bindType', 'bindPattern'], (item) => {
chrome.storage.sync.get([
'scm',
'token',
'user',
'baseUrl',
'bindRepo',
'bindBranch',
'bindType',
'bindPattern',
'bindConfig'
], (item) => {
if (!item.token) {
reject(new Error('need login'));
}
Expand All @@ -55,8 +65,11 @@ function initContext() {
context.bindBranch = item.bindBranch || {};
context.bindType = item.bindType || {};
context.bindPattern = item.bindPattern || {};
context.filetype = context.bindType[context.id] || '.gs';
context.ignorePattern = (context.bindPattern[context.id] || []).map(p => new RegExp(p));
context.bindConfig = item.bindConfig || {};
context.config = context.bindConfig[context.id] || {};
context.config.filetype = context.config.filetype || context.bindType[context.id] || '.gs';
context.config.ignorePattern = context.config.ignorePattern || context.bindPattern[context.id] || [];
context.config.manifestEnabled = context.config.manifestEnabled || false;
context.gist = context.bindRepo[context.id] && context.bindRepo[context.id].gist;
resolve(scm);
});
Expand Down Expand Up @@ -204,20 +217,19 @@ function initPageEvent() {
})

$(document).on('click', '#config-button', () => {
$('#filetype').val(context.filetype);
const pattern = context.bindPattern[context.id] || [];
$('#ignore-pattern').val(pattern.join(';'));
$('#filetype').val(context.config.filetype);
$('#manage-manifest').prop("checked", context.config.manifestEnabled);
$('#ignore-pattern').val(context.config.ignorePattern.join(';'));
changeModalState('config', true);
});

$(document).on('click', '#save-config', () => {
context.filetype = $('#filetype').val();
context.bindType[context.id] = context.filetype;
let pattern = $('#ignore-pattern').val().split(';').filter(p => p !== '');
context.bindPattern[context.id] = pattern;
context.config.filetype = $('#filetype').val();
context.config.manifestEnabled = $('#manage-manifest').prop( "checked" );
context.config.ignorePattern = $('#ignore-pattern').val().split(';').filter(p => p !== '');
context.bindConfig[context.id] = context.config;
try {
context.ignorePattern = pattern.map(p => new RegExp(p));
chrome.storage.sync.set({ bindType: context.bindType, bindPattern: context.bindPattern });
chrome.storage.sync.set({ bindConfig: context.bindConfig });
changeModalState('config', false);
} catch (err) {
showAlert(err.message, LEVEL_ERROR);
Expand Down Expand Up @@ -275,8 +287,11 @@ function initPageEvent() {
function prepareCode() {
return Promise.all([gas.getGasCode(), scm.getCode()])
.then((data) => {
const re = new RegExp(`\\${context.filetype}$`);
const re = new RegExp(`\\${context.config.filetype}$`);
const files = $('.item').toArray().reduce((hash, e) => {
if (context.config.manifestEnabled && e.innerText === 'appsscript.json') {
hash['appsscript'] = 'appsscript.json';
}
const match = e.innerText.match(/(.*?)\.(gs|html)$/);
if (!match || !match[1] || !match[2]) return hash;
hash[match[1]] = match[0];
Expand Down Expand Up @@ -313,8 +328,11 @@ function showDiff(code, type) {
})
.concat(gasFiles)
.filter(file => {
for (let i = 0; i < context.ignorePattern.length; i ++) {
let p = context.ignorePattern[i];
if (context.config.manifestEnabled && file === 'appsscript.json') {
return true;
}
for (let i = 0; i < context.config.ignorePattern.length; i ++) {
let p = new RegExp(context.config.ignorePattern[i]);
if (p.test(file)) return false;
}
const match = file.match(/(.*?)\.(gs|html)$/);
Expand All @@ -325,6 +343,9 @@ function showDiff(code, type) {
if (!oldCode[file]) {
mode = 'new file mode 100644';
} else if (!newCode[file]) {
if (file === 'appsscript.json') {
return diff; //can not delete manifest file
}
mode = 'deleted file mode 100644';
}
let fileDiff = JsDiff.createPatch(file, oldCode[file] || '', newCode[file] || '');
Expand Down
4 changes: 2 additions & 2 deletions src/scm/bitbucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Bitbucket {
push(code){
const changed = $('.diff-file:checked').toArray().map(elem => elem.value);
const files = changed.filter(f => code.gas[f]).map(f => {
return { name: f.replace(/\.gs$/, context.filetype), content: code.gas[f] }
return { name: f.replace(/\.gs$/, context.config.filetype), content: code.gas[f] }
});
const deleteFiles = changed.filter(f => !code.gas[f]);
const comment = $('#commit-comment').val();
Expand Down Expand Up @@ -287,7 +287,7 @@ class Bitbucket {
}).map(dir => {
return `${dir.links.self.href}?access_token=${data.token}`;
})
const re = new RegExp(`(\\${context.filetype}|\\.html)$`);
const re = new RegExp(`(\\${context.config.filetype}|\\.html${context.config.manifestEnabled ? '|^appsscript.json' : ''})$`);
const files = response.values.filter(src => {
return src.type === 'commit_file' && re.test(src.path);
});
Expand Down
10 changes: 4 additions & 6 deletions src/scm/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Github {

pushToRepo(code) {
const changed = $('.diff-file:checked').toArray().map(elem => elem.value);
const unchanged = Object.keys(code.gas).filter((f) => changed.indexOf(f) < 0 );
const promises = changed.filter(f => code.gas[f]).map((file) => {
const payload = {
content: code.gas[file],
Expand All @@ -41,7 +40,7 @@ class Github {
data: JSON.stringify(payload)
})
.then(response => {
return {file: file.replace(/\.gs$/, context.filetype), blob: response};
return {file: file.replace(/\.gs$/, context.config.filetype), blob: response};
})
});
if (changed.length === 0) {
Expand All @@ -65,7 +64,6 @@ class Github {
}
)
.then(baseTree => {
const re = new RegExp(`(\\${context.filetype}|\\.html)$`);
const tree = responses[0].map((data) => {
return {
path: data.file,
Expand All @@ -75,7 +73,7 @@ class Github {
}
})
.concat(baseTree.tree.filter((t) => {
return (t.type != 'tree') && (!re.test(t.path) || unchanged.indexOf(t.path) >= 0);
return (t.type != 'tree') && (changed.indexOf(t.path) < 0);
}));
return {
tree: tree
Expand Down Expand Up @@ -156,7 +154,7 @@ class Github {
files: {}
};
files.forEach(file => {
payload.files[file.replace(/\.gs$/, context.filetype)] = {
payload.files[file.replace(/\.gs$/, context.config.filetype)] = {
content: code.gas[file]
};
});
Expand Down Expand Up @@ -222,7 +220,7 @@ class Github {
);
})
.then(response => {
const re = new RegExp(`(\\${context.filetype}|\\.html)$`);
const re = new RegExp(`(\\${context.config.filetype}|\\.html${context.config.manifestEnabled ? '|^appsscript.json' : ''})$`);
const promises = response.tree.filter((tree) => {
return tree.type === 'blob' && re.test(tree.path);
})
Expand Down

0 comments on commit c03b9e8

Please sign in to comment.