Skip to content

Commit

Permalink
feat: add Generate Project Key, and don't generate it by default
Browse files Browse the repository at this point in the history
  • Loading branch information
luandro committed Oct 23, 2024
1 parent b5edf3c commit b3b9a09
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
24 changes: 24 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ function onOpen() {
ui.createMenu("CoMapeo Tools")
.addItem("Translate CoMapeo Category", "translateCoMapeoCategory")
.addItem("Generate Category Icons", "generateIcons")
.addItem("Generate Project Key", "generateProjectKey")
.addSeparator()
.addItem("Generate CoMapeo Category", "generateCoMapeoCategory")
.addSeparator()
.addItem("Lint Sheets", "lintAllSheets")
.addItem("Reset Spreadsheet", "cleanAllSheets")
.addItem("Help", "openHelpPage")
Expand Down Expand Up @@ -57,6 +60,27 @@ function generateIcons() {
}
}

function generateProjectKey() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
"Generate Project Key",
"This will generate a project key for your CoMapeo Category. Continue?",
ui.ButtonSet.YES_NO,
);

if (result === ui.Button.YES) {
try {
generateProjectKeyConfig();
} catch (error) {
ui.alert(
"Error",
`An error occurred while generating the configuration: ${error.message}`,
ui.ButtonSet.OK,
);
}
}
}

function generateCoMapeoCategory() {
const ui = SpreadsheetApp.getUi();
const result = ui.alert(
Expand Down
2 changes: 1 addition & 1 deletion src/apiService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function sendDataToApiAndGetZip(zipFile: GoogleAppsScript.Base.Blob, metadata: { name: string, version: string }) {
const fileName = `${metadata.name}.comapeocat`
const fileName = `${metadata.name}-${metadata.version}.comapeocat`
const apiUrl = "http://137.184.153.36:3000/";
console.log('Posting zip to API URL:', apiUrl);
const form = {
Expand Down
3 changes: 2 additions & 1 deletion src/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ function showHelpDialog() {
<li>Check the generated icons in the icons folder and modify them using the <br /><a href="https://icons.earthdefenderstoolkit.com" target="_blank">Icon Generator App</a> if necessary.</li>
<li>Copy the shared link for each icon and paste it into the corresponding icon cell in the spreadsheet.</li>
<li>Use the "Lint Sheets" option to ensure proper formatting and capitalization of your data.</li>
<li>Repeat steps 1-7 as needed, updating translations and icons until you're satisfied with the results.</li>
<li>Use the "Generate Project Key" option to create a unique key for your project. This key ensures that your configuration can only be synced with projects using the same key, enhancing security.</li>
<li>Repeat steps 1-8 as needed, updating translations, icons, and the project key until you're satisfied with the results.</li>
<li>When ready, use the "Generate CoMapeo Category" option to create your final configuration. This process may take a few minutes and will produce a zip file containing your .comapeocat file, ready for use with the CoMapeo app.</li>
</ol>
<p>For more detailed information, visit our GitHub repository:</p>
Expand Down
43 changes: 38 additions & 5 deletions src/generateConfig/processMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function generateProjectKeyConfig(): void {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
let metadataSheet = spreadsheet.getSheetByName('Metadata');

if (!metadataSheet) {
metadataSheet = createMetadataSheet(spreadsheet);
}

const projectKey = generateRandomBytes(64);
getOrSetValue(metadataSheet, 'projectKey', projectKey);
}

function processMetadata(data) {
const { documentName } = data;
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
Expand All @@ -13,32 +25,53 @@ function processMetadata(data) {
return { metadata, packageJson };
}

function createMetadataSheet(spreadsheet) {
function createMetadataSheet(spreadsheet: GoogleAppsScript.Spreadsheet.Spreadsheet): GoogleAppsScript.Spreadsheet.Sheet {
const sheet = spreadsheet.insertSheet('Metadata');
const headerRange = sheet.getRange(1, 1, 1, 2);
headerRange.setValues([['Key', 'Value']]);
headerRange.setFontWeight('bold');
return sheet;
}

function getOrCreateMetadata(sheet, documentName) {
const metadata = {
function getOrCreateMetadata(sheet: GoogleAppsScript.Spreadsheet.Sheet, documentName: string): CoMapeoMetadata {
const metadata: CoMapeoMetadata = {
dataset_id: getOrSetValue(sheet, 'dataset_id', `comapeo-${slugify(documentName)}`),
name: getOrSetValue(sheet, 'name', `config-${slugify(documentName)}`),
version: getOrSetValue(sheet, 'version', Utilities.formatDate(new Date(), 'UTC', 'yy.MM.dd')),
projectKey: getOrSetValue(sheet, 'projectKey', generateRandomBytes(64))
version: getOrSetValue(sheet, 'version', Utilities.formatDate(new Date(), 'UTC', 'yy.MM.dd'))
};
return metadata;
}

/**
* Retrieves or sets a value in the metadata sheet.
*
* @param sheet - The metadata sheet to operate on
* @param key - The key to search for or set
* @param defaultValue - The default value to set if the key is not found
* @returns The existing value if found, or the default value if not found
*/
function getOrSetValue(sheet, key, defaultValue) {
// Get all data from the sheet
const data = sheet.getDataRange().getValues();

// Search for the key in the existing data
for (let i = 1; i < data.length; i++) {
if (data[i][0] === key) {
// If key is found, return its corresponding value
if (key === 'version') {
// Always update version with current date
const newVersion = Utilities.formatDate(new Date(), 'UTC', 'yy.MM.dd');
sheet.getRange(i + 1, 2).setValue(newVersion);
return newVersion;
}
return data[i][1];
}
}

// If key is not found, append a new row with the key and default value
sheet.appendRow([key, defaultValue]);

// Return the default value
return defaultValue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface CoMapeoMetadata {
dataset_id: string;
name: string;
version: string;
projectKey: string;
projectKey?: string;
}

interface CoMapeoPackageJson {
Expand Down

0 comments on commit b3b9a09

Please sign in to comment.