Skip to content

Commit

Permalink
Merge pull request #3 from pklaschka/v1.2.0
Browse files Browse the repository at this point in the history
V1.2.0
  • Loading branch information
pklaschka authored Jan 25, 2019
2 parents f2b24f4 + 25cab8b commit fe163b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.1.5",
"description": "A little helper to make storing key-value-pairs (e.g. settings) for plugins for Adobe XD CC easier.",
"main": "storage-helper.js",
"types": "storage-helper.d.ts",
"scripts": {
"test": "jest --coverage"
},
Expand Down
47 changes: 47 additions & 0 deletions storage-helper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2018 by Pablo Klaschka
*/

/**
* The xd-storage-helper module
* @module xd-storage-helper
*/
declare module 'xd-storage-helper' {
/**
* @class StorageHelper
* The main LocalizationHelper class
* @alias module:xd-storage-helper
* @static
* @hideconstructor
*/
export default class StorageHelper {
/**
* Retrieves a value from storage. Saves default value if none is set.
* @param {string} key The identifier
* @param {*} defaultValue The default value. Gets saved and returned if no value was previously set for the speciefied key.
* @return {Promise<*>} The value retrieved from storage. If none is saved, the `defaultValue` is returned.
*/
public static get(key:string, defaultValue:any): Promise<any>;

/**
* Saves a certain key-value-pair to the storage.
* @param {string} key The identifier
* @param {*} value The value that get's saved
* @return {Promise<void>}
*/
public static set(key: string, value:any): Promise<void>;

/**
* Deletes a certain key-value-pair from the storage
* @param {string} key The key of the deleted pair
* @return {Promise<void>}
*/
public static delete(key: string): Promise<void>;

/**
* Resets (i.e. purges) all stored settings.
* @returns {Promise<void>}
*/
public static reset(): Promise<void>;
}
}
24 changes: 15 additions & 9 deletions storage-helper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) 2018. by Pablo Klaschka
* Copyright (c) 2019. by Pablo Klaschka
*/

const storage = require('uxp').storage;
const fs = storage.localFileSystem;

let data;

class storageHelper {
/**
* Creates a data file if none was previously existent.
Expand All @@ -14,11 +16,14 @@ class storageHelper {
static async init() {
let dataFolder = await fs.getDataFolder();
try {
return await dataFolder.getEntry('storage.json');
let returnFile = await dataFolder.getEntry('storage.json');
data = JSON.parse((await returnFile.read({format: storage.formats.utf8})).toString());
return returnFile;
} catch (e) {
const file = await dataFolder.createEntry('storage.json', {type: storage.types.file, overwrite: true});
if (file.isFile) {
await file.write('{}', {append: false});
data = {};
return file;
} else {
throw new Error('Storage file storage.json was not a file.');
Expand All @@ -33,13 +38,15 @@ class storageHelper {
* @return {Promise<*>} The value retrieved from storage. If none is saved, the `defaultValue` is returned.
*/
static async get(key, defaultValue) {
const dataFile = await this.init();
let object = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
if (object[key] === undefined) {
if (!data) {
const dataFile = await this.init();
data = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
}
if (data[key] === undefined) {
await this.set(key, defaultValue);
return defaultValue;
} else {
return object[key];
return data[key];
}
}

Expand All @@ -51,9 +58,8 @@ class storageHelper {
*/
static async set(key, value) {
const dataFile = await this.init();
let object = JSON.parse((await dataFile.read({format: storage.formats.utf8})).toString());
object[key] = value;
return await dataFile.write(JSON.stringify(object), {append: false, format: storage.formats.utf8})
data[key] = value;
return await dataFile.write(JSON.stringify(data), {append: false, format: storage.formats.utf8})
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/storageHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ describe('storage helper', () => {
const storageHelper = require('../storage-helper');
mockFileExists = false;
expect(mockFileExists).toBe(false);
expect(await storageHelper.get('unknown', 3)).toBe(3);
expect(await storageHelper.get('unknown', 111)).toBe(111);
expect(await storageHelper.get('unknown', 5)).toBe(111);
expect(mockFileExists).toBe(true);
done();
});
Expand Down

0 comments on commit fe163b5

Please sign in to comment.