Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filename generation for plugin if no value is specified for some reason #35

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lib-iitc-manager",
"version": "1.9.1",
"version": "1.9.2",
"description": "Library for managing IITC plugins",
"main": "src/index.js",
"type": "module",
Expand Down
7 changes: 5 additions & 2 deletions src/backup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { isSet, parseMeta } from './helpers.js';
import { isSet, parseMeta, sanitizeFileName } from './helpers.js';
import deepmerge from '@bundled-es-modules/deepmerge';

/**
Expand Down Expand Up @@ -123,7 +123,10 @@ export const exportExternalPlugins = (all_storage) => {
// Loop through each plugin UID in the current key's storage data
for (const plugin_uid in all_storage[key]) {
// Get the plugin's filename and code from the storage data and add to the external_plugins object
const plugin_filename = all_storage[key][plugin_uid]['filename'];
let plugin_filename = all_storage[key][plugin_uid]['filename'];
if (!plugin_filename) {
plugin_filename = sanitizeFileName(`${all_storage[key][plugin_uid]['name']}.user.js`);
}
external_plugins[channel][plugin_filename] = all_storage[key][plugin_uid]['code'];
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,22 @@ export function clearWait() {
export function isSet(value) {
return typeof value !== 'undefined' && value !== null;
}

/**
* Processes a string by removing invalid characters for the file system and limiting its length.
*
* @param {string} input - The original string to be converted into a file name.
* @param {number} maxLength - The maximum length of the file name (default is 255 characters).
* @returns {string} - The processed string.
*/
export function sanitizeFileName(input, maxLength = 255) {
const invalidChars = /[/\\:*?"<>|]/g;
let sanitized = input.replace(invalidChars, '');

// Truncate the length to maxLength characters
if (sanitized.length > maxLength) {
sanitized = sanitized.slice(0, maxLength);
}

return sanitized;
}
3 changes: 2 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Worker } from './worker.js';
import * as migrations from './migrations.js';
import { getUID, isSet } from './helpers.js';
import { getUID, isSet, sanitizeFileName } from './helpers.js';
import * as backup from './backup.js';

/**
Expand Down Expand Up @@ -310,6 +310,7 @@ export class Manager extends Worker {
plugins_user[plugin_uid] = Object.assign(meta, {
uid: plugin_uid,
status: 'on',
filename: meta['filename'] ? meta['filename'] : sanitizeFileName(`${meta['name']}.user.js`),
code: code,
});

Expand Down
3 changes: 3 additions & 0 deletions test/manager.2.external.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Controls',
status: 'on',
user: true,
filename: 'Bookmarks for maps and portals.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down Expand Up @@ -145,6 +146,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Misc',
status: 'on',
user: true,
filename: 'Bookmarks2 for maps and portals.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down Expand Up @@ -356,6 +358,7 @@ describe('manage.js external plugins integration tests', function () {
category: 'Controls',
status: 'on',
user: true,
filename: 'Bookmarks for maps and portals.user.js',
code: external_code,
};
const run = await manager.addUserScripts(scripts);
Expand Down
5 changes: 2 additions & 3 deletions test/manager.9.backup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('getBackupData and setBackupData', function () {
custom: {},
release: {
'total-conversion-build.user.js': external_iitc_code,
'bookmarks1.user.js': external_code,
'Bookmarks for maps and portals.user.js': external_code,
},
},
data: {
Expand Down Expand Up @@ -131,7 +131,6 @@ describe('getBackupData and setBackupData', function () {
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
name: 'Bookmarks for maps and portals',
category: 'Controls',
filename: 'bookmarks1.user.js',
},
code: external_code,
},
Expand All @@ -146,10 +145,10 @@ describe('getBackupData and setBackupData', function () {
status: 'on',
user: true,
code: external_code,
filename: 'bookmarks1.user.js',
},
};
const run = await manager.addUserScripts(scripts);
delete run['Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion']['filename'];
expect(run).to.deep.equal(installed);
});
it('Add plugin settings data', async function () {
Expand Down
Loading