├── .eslintignore ├── .eslintrc ├── .gitignore ├── README.md ├── index.js └── package.json /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "commonjs": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": "eslint:recommended" 8 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | node_modules 3 | package-lock.json 4 | .eslintcache 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # electron-localStorage 2 | 在eletron主线程也能使用的localStorage 3 | 4 | 5 | electron主进程中是不能获取到浏览器的window对象的,所以我们不能像在渲染进程中一样使用浏览器为我们提供的localstorage对象。 6 | 7 | 但是主进程中有可能也需要这样的需求,比如我们在本地存储了当前的环境(dev/beta/prod),主进程需要根据不同的开发环境来load不同的url。 8 | 9 | 于是手动封装了一个可以在主进程中调用的localstorage。 10 | 11 | # 1.安装 12 | 13 | ``` 14 | npm install electron-localstorage 15 | ``` 16 | # 2.引用: 17 | ``` 18 | const storage = require('electron-localstorage'); 19 | ``` 20 | 21 | # 3.使用 22 | 23 | ## 3.1完美支持所有localStorage的所有api: 24 | 25 | 存储数据 26 | ``` 27 | storage.setItem(`myCat`, `Tom`); 28 | ``` 29 | 获取数据 30 | ``` 31 | let cat = storage.getItem(`myCat`); 32 | ``` 33 | 移除某个数据 34 | ``` 35 | storage.removeItem(`myCat`); 36 | ``` 37 | 移除所有数据 38 | ``` 39 | storage.clear(); 40 | ``` 41 | 42 | ## 3.2 扩展方法 43 | 44 | 获取当前所有存储的项 45 | ``` 46 | storage.getAll(); 47 | ``` 48 | 自定义存储路径 49 | ``` 50 | storage.setStoragePath(path.join(__dirname,'test.json')); 51 | ``` 52 | 获取当前数据存储路径 53 | ``` 54 | storage.getStoragePath(); 55 | ``` 56 | 57 | # 4.示例程序 58 | 59 | https://github.com/ConardLi/electron-localstorage-demo 60 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * 读取本地配置 5 | */ 6 | const fs = require('fs') 7 | const path = require('path') 8 | /** 9 | * 判断配置文件是否存在 10 | */ 11 | function isExit() { 12 | let success = true; 13 | if (!(localConfig.config && typeof localConfig.config === 'object')) { 14 | success = initConfig(); 15 | } 16 | return success; 17 | } 18 | /** 19 | * 初始化config 20 | */ 21 | function initConfig() { 22 | try { 23 | const config = readConfig(); 24 | if (config) { 25 | localConfig.config = JSON.parse(config); 26 | return true; 27 | } 28 | const defalutConfig = {}; 29 | const content = JSON.stringify(defalutConfig); 30 | fs.writeFileSync(localConfig.configUrl, content); 31 | localConfig.config = defalutConfig; 32 | return true; 33 | } catch (e) { 34 | return false; 35 | } 36 | } 37 | /** 38 | * 读取文件 39 | */ 40 | function readConfig() { 41 | try { 42 | const result = fs.readFileSync(localConfig.configUrl); 43 | return result; 44 | } catch (error) { 45 | return false; 46 | } 47 | } 48 | /** 49 | * 写入文件 50 | */ 51 | function writeConfig(value) { 52 | try { 53 | const content = JSON.stringify(value); 54 | fs.writeFileSync(localConfig.configUrl, content); 55 | return true; 56 | } catch (e) { 57 | return false; 58 | } 59 | } 60 | 61 | 62 | const localConfig = { 63 | config: null, 64 | configUrl: path.join(__dirname, './localConfig.json'), 65 | setStoragePath: (path) => { 66 | localConfig.configUrl = path; 67 | }, 68 | getStoragePath: () => { 69 | return localConfig.configUrl; 70 | }, 71 | getItem: (key) => { 72 | const success = isExit(); 73 | if (success) { 74 | const result = localConfig.config[key]; 75 | return result ? result : ''; 76 | } 77 | return null; 78 | }, 79 | setItem: (key, value) => { 80 | let success = isExit(); 81 | if (success) { 82 | const config = Object.assign({}, localConfig.config); 83 | config[key] = value; 84 | const suc = writeConfig(config); 85 | if (suc) { 86 | localConfig.config = config; 87 | return true; 88 | } 89 | } 90 | return false; 91 | }, 92 | getAll: () => { 93 | let success = isExit(); 94 | if (success) { 95 | return localConfig.config; 96 | } 97 | return null; 98 | }, 99 | removeItem: (key) => { 100 | const value = localConfig.getItem(key); 101 | if (value) { 102 | const config = Object.assign({}, localConfig.config); 103 | delete config[key]; 104 | const suc = writeConfig(config); 105 | if (suc) { 106 | localConfig.config = config; 107 | return true; 108 | } 109 | } 110 | return false; 111 | }, 112 | clear: () => { 113 | let success = isExit(); 114 | if (success) { 115 | const suc = writeConfig({}); 116 | if (suc) { 117 | localConfig.config = {}; 118 | return true; 119 | } 120 | } 121 | return false; 122 | } 123 | } 124 | 125 | module.exports = localConfig; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-localstorage", 3 | "version": "1.0.5", 4 | "description": "在eletron主线程也能使用的localStorage", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "echo \"代码检测中...\" && eslint ./src *.js --cache" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ConardLi/electron-localStorage.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/ConardLi/electron-localStorage/issues" 17 | }, 18 | "homepage": "https://github.com/ConardLi/electron-localStorage#readme", 19 | "devDependencies": { 20 | "eslint": "3.19.0", 21 | "eslint-config-standard": "10.2.1", 22 | "eslint-plugin-import": "2.8.0", 23 | "eslint-plugin-markdown": "1.0.0-beta.6", 24 | "eslint-plugin-node": "5.2.1", 25 | "eslint-plugin-promise": "3.6.0", 26 | "eslint-plugin-standard": "3.0.1" 27 | } 28 | } --------------------------------------------------------------------------------