Skip to content

Commit 8b86499

Browse files
authored
Merge pull request #35 from modos189/fix/no-filename
Added filename generation for plugin if no value is specified for some reason
2 parents 9dc7c01 + 7948fba commit 8b86499

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lib-iitc-manager",
3-
"version": "1.9.1",
3+
"version": "1.9.2",
44
"description": "Library for managing IITC plugins",
55
"main": "src/index.js",
66
"type": "module",

src/backup.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
22

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

66
/**
@@ -123,7 +123,10 @@ export const exportExternalPlugins = (all_storage) => {
123123
// Loop through each plugin UID in the current key's storage data
124124
for (const plugin_uid in all_storage[key]) {
125125
// Get the plugin's filename and code from the storage data and add to the external_plugins object
126-
const plugin_filename = all_storage[key][plugin_uid]['filename'];
126+
let plugin_filename = all_storage[key][plugin_uid]['filename'];
127+
if (!plugin_filename) {
128+
plugin_filename = sanitizeFileName(`${all_storage[key][plugin_uid]['name']}.user.js`);
129+
}
127130
external_plugins[channel][plugin_filename] = all_storage[key][plugin_uid]['code'];
128131
}
129132
}

src/helpers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,22 @@ export function clearWait() {
212212
export function isSet(value) {
213213
return typeof value !== 'undefined' && value !== null;
214214
}
215+
216+
/**
217+
* Processes a string by removing invalid characters for the file system and limiting its length.
218+
*
219+
* @param {string} input - The original string to be converted into a file name.
220+
* @param {number} maxLength - The maximum length of the file name (default is 255 characters).
221+
* @returns {string} - The processed string.
222+
*/
223+
export function sanitizeFileName(input, maxLength = 255) {
224+
const invalidChars = /[/\\:*?"<>|]/g;
225+
let sanitized = input.replace(invalidChars, '');
226+
227+
// Truncate the length to maxLength characters
228+
if (sanitized.length > maxLength) {
229+
sanitized = sanitized.slice(0, maxLength);
230+
}
231+
232+
return sanitized;
233+
}

src/manager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Worker } from './worker.js';
44
import * as migrations from './migrations.js';
5-
import { getUID, isSet } from './helpers.js';
5+
import { getUID, isSet, sanitizeFileName } from './helpers.js';
66
import * as backup from './backup.js';
77

88
/**
@@ -310,6 +310,7 @@ export class Manager extends Worker {
310310
plugins_user[plugin_uid] = Object.assign(meta, {
311311
uid: plugin_uid,
312312
status: 'on',
313+
filename: meta['filename'] ? meta['filename'] : sanitizeFileName(`${meta['name']}.user.js`),
313314
code: code,
314315
});
315316

test/manager.2.external.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe('manage.js external plugins integration tests', function () {
9797
category: 'Controls',
9898
status: 'on',
9999
user: true,
100+
filename: 'Bookmarks for maps and portals.user.js',
100101
code: external_code,
101102
};
102103
const run = await manager.addUserScripts(scripts);
@@ -145,6 +146,7 @@ describe('manage.js external plugins integration tests', function () {
145146
category: 'Misc',
146147
status: 'on',
147148
user: true,
149+
filename: 'Bookmarks2 for maps and portals.user.js',
148150
code: external_code,
149151
};
150152
const run = await manager.addUserScripts(scripts);
@@ -356,6 +358,7 @@ describe('manage.js external plugins integration tests', function () {
356358
category: 'Controls',
357359
status: 'on',
358360
user: true,
361+
filename: 'Bookmarks for maps and portals.user.js',
359362
code: external_code,
360363
};
361364
const run = await manager.addUserScripts(scripts);

test/manager.9.backup.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('getBackupData and setBackupData', function () {
2121
custom: {},
2222
release: {
2323
'total-conversion-build.user.js': external_iitc_code,
24-
'bookmarks1.user.js': external_code,
24+
'Bookmarks for maps and portals.user.js': external_code,
2525
},
2626
},
2727
data: {
@@ -131,7 +131,6 @@ describe('getBackupData and setBackupData', function () {
131131
namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion',
132132
name: 'Bookmarks for maps and portals',
133133
category: 'Controls',
134-
filename: 'bookmarks1.user.js',
135134
},
136135
code: external_code,
137136
},
@@ -146,10 +145,10 @@ describe('getBackupData and setBackupData', function () {
146145
status: 'on',
147146
user: true,
148147
code: external_code,
149-
filename: 'bookmarks1.user.js',
150148
},
151149
};
152150
const run = await manager.addUserScripts(scripts);
151+
delete run['Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion']['filename'];
153152
expect(run).to.deep.equal(installed);
154153
});
155154
it('Add plugin settings data', async function () {

0 commit comments

Comments
 (0)