update
This commit is contained in:
+14
@@ -2,6 +2,20 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
## [1.2.0](https://github.com/nuxt-contrib/fs-memo/compare/v1.1.0...v1.2.0) (2020-11-16)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* options.file ([813a517](https://github.com/nuxt-contrib/fs-memo/commit/813a517fa0f32b111b4d1e8453f1065a4ea23f2b))
|
||||
|
||||
## [1.1.0](https://github.com/nuxt-contrib/fs-memo/compare/v1.0.1...v1.1.0) (2020-11-16)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* write to node_modules/.cache/fs-memo/default.json ([4697250](https://github.com/nuxt-contrib/fs-memo/commit/46972506d0a655c1f2e96c3998fc8bc477abea27))
|
||||
|
||||
### [1.0.1](https://github.com/nuxt-contrib/fs-memo/compare/v1.0.0...v1.0.1) (2020-06-16)
|
||||
|
||||
## [1.0.0](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.6...v1.0.0) (2020-06-16)
|
||||
|
||||
+8
-10
@@ -34,7 +34,7 @@ FS loading silently bails if:
|
||||
- The process that made memo is still alive with different pid
|
||||
- Any fs error happens (like permission denied)
|
||||
|
||||
### `setMemo(eoptions)`
|
||||
### `setMemo(options)`
|
||||
|
||||
```ts
|
||||
setMemo(memo: object, options: MemoOptions): Promise<void>
|
||||
@@ -46,20 +46,18 @@ FS persistence silently bails if any error happens.
|
||||
|
||||
## Options
|
||||
|
||||
```ts
|
||||
interface MemoOptions {
|
||||
dir?: string
|
||||
name?: string
|
||||
}
|
||||
```
|
||||
|
||||
### `dir`
|
||||
|
||||
Specify directory where memo file should be stored. Default dir is `__dirname` (`node_modules/fs-memo/dist`)
|
||||
Specify directory where memo file should be stored. Default dir is `node_modules/.cache/fs-memo`
|
||||
|
||||
### `name`
|
||||
|
||||
Name of memo file. Default name is `.fs-memo`.
|
||||
Name of memo file. Default name is `default` (`.json` is appended to file name)
|
||||
|
||||
### `file`
|
||||
|
||||
Optionally provide full path to file (discards `dir` and `name` options)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
|
||||
+9
-7
@@ -1,7 +1,9 @@
|
||||
interface MemoOptions {
|
||||
dir?: string;
|
||||
name?: string;
|
||||
}
|
||||
export declare function getMemo(options: MemoOptions): Promise<any>;
|
||||
export declare function setMemo(memo: object, options: MemoOptions): Promise<void>;
|
||||
export {};
|
||||
interface MemoOptions {
|
||||
dir: string;
|
||||
name: string;
|
||||
file: string;
|
||||
}
|
||||
declare function getMemo(config: Partial<MemoOptions>): Promise<any>;
|
||||
declare function setMemo(memo: object, config: Partial<MemoOptions>): Promise<void>;
|
||||
|
||||
export { getMemo, setMemo };
|
||||
|
||||
+38
-110
@@ -2,125 +2,53 @@
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function _await(value, then, direct) {
|
||||
if (direct) {
|
||||
return then ? then(value) : value;
|
||||
}
|
||||
|
||||
if (!value || !value.then) {
|
||||
value = Promise.resolve(value);
|
||||
}
|
||||
|
||||
return then ? value.then(then) : value;
|
||||
}
|
||||
|
||||
function _empty() {}
|
||||
|
||||
function _catch(body, recover) {
|
||||
try {
|
||||
var result = body();
|
||||
} catch (e) {
|
||||
return recover(e);
|
||||
}
|
||||
|
||||
if (result && result.then) {
|
||||
return result.then(void 0, recover);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function _continue(value, then) {
|
||||
return value && value.then ? value.then(then) : then(value);
|
||||
}
|
||||
|
||||
function _async(f) {
|
||||
return function () {
|
||||
var arguments$1 = arguments;
|
||||
|
||||
for (var args = [], i = 0; i < arguments.length; i++) {
|
||||
args[i] = arguments$1[i];
|
||||
}
|
||||
|
||||
try {
|
||||
return Promise.resolve(f.apply(this, args));
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function _awaitIgnored(value, direct) {
|
||||
if (!direct) {
|
||||
return value && value.then ? value.then(_empty) : Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
function _continueIgnored(value) {
|
||||
if (value && value.then) {
|
||||
return value.then(_empty);
|
||||
}
|
||||
}
|
||||
|
||||
var setMemo = _async(function (memo, options) {
|
||||
var ref = getOptions(options);
|
||||
var file = ref.file; // Set local memo
|
||||
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid; // Try to persist
|
||||
|
||||
return _continueIgnored(_catch(function () {
|
||||
return _awaitIgnored(fs.promises.writeFile(file, JSON.stringify(_memo), 'utf-8'));
|
||||
}, _empty));
|
||||
});
|
||||
var getMemo = _async(function (options) {
|
||||
var ref = getOptions(options);
|
||||
var file = ref.file; // Try to load latest memo
|
||||
|
||||
return _continue(_catch(function () {
|
||||
return _await(fs.promises.readFile(file, 'utf-8'), function (_fs$readFile) {
|
||||
var memo = JSON.parse(_fs$readFile) || {};
|
||||
|
||||
if (!memo._pid) {
|
||||
throw new Error('InvalidMemo');
|
||||
}
|
||||
|
||||
if (memo._pid === _memo._pid || // fs is more reliable than require cache
|
||||
!isAlive(memo.pid) // RIP
|
||||
) {
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
}
|
||||
});
|
||||
}, _empty), function (_result) {
|
||||
return _memo;
|
||||
});
|
||||
});
|
||||
var _memo = {
|
||||
const _memo = {
|
||||
_pid: process.pid
|
||||
};
|
||||
var defaults = {
|
||||
dir: __dirname,
|
||||
name: '.fs-memo'
|
||||
};
|
||||
|
||||
function getOptions(options) {
|
||||
var opts = Object.assign({}, defaults, options, {
|
||||
file: ''
|
||||
});
|
||||
opts.file = path.resolve(opts.dir, opts.name);
|
||||
return opts;
|
||||
async function getMemo(config) {
|
||||
const options = getOptions(config);
|
||||
try {
|
||||
const memo = JSON.parse(await fs.promises.readFile(options.file, "utf-8")) || {};
|
||||
if (!memo._pid) {
|
||||
throw new Error("Memo lacks _pid");
|
||||
}
|
||||
if (memo._pid === _memo._pid || !isAlive(memo.pid)) {
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
return _memo;
|
||||
}
|
||||
async function setMemo(memo, config) {
|
||||
const options = getOptions(config);
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
try {
|
||||
await fs.promises.mkdir(options.dir);
|
||||
} catch (e) {
|
||||
}
|
||||
try {
|
||||
await fs.promises.writeFile(options.file, JSON.stringify(_memo), "utf-8");
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
function getOptions(config) {
|
||||
const options = {...config};
|
||||
options.name = options.name || "default";
|
||||
options.dir = options.dir || path.resolve(process.cwd(), "node_modules/.cache/fs-memo");
|
||||
options.file = options.file || path.resolve(options.dir, options.name + ".json");
|
||||
return options;
|
||||
}
|
||||
|
||||
function isAlive(pid) {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return e.code === 'EPERM';
|
||||
return e.code === "EPERM";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+19
-20
@@ -1,44 +1,43 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"fs-memo@1.0.1",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"fs-memo@1.2.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "fs-memo@1.0.1",
|
||||
"_id": "fs-memo@1.0.1",
|
||||
"_from": "fs-memo@1.2.0",
|
||||
"_id": "fs-memo@1.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-lV4dUPsyJHVgD3g0loRj+ClZQnb8eUQN0olNWgmYbq+ch4ponSMBI+l4l72l9krHakhq79QLCzPJMko+qpvmTQ==",
|
||||
"_integrity": "sha512-YEexkCpL4j03jn5SxaMHqcO6IuWuqm8JFUYhyCep7Ao89JIYmB8xoKhK7zXXJ9cCaNXpyNH5L3QtAmoxjoHW2w==",
|
||||
"_location": "/fs-memo",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "fs-memo@1.0.1",
|
||||
"raw": "fs-memo@1.2.0",
|
||||
"name": "fs-memo",
|
||||
"escapedName": "fs-memo",
|
||||
"rawSpec": "1.0.1",
|
||||
"rawSpec": "1.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.0.1"
|
||||
"fetchSpec": "1.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/get-port-please"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/fs-memo/-/fs-memo-1.0.1.tgz",
|
||||
"_spec": "1.0.1",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/fs-memo/-/fs-memo-1.2.0.tgz",
|
||||
"_spec": "1.2.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bugs": {
|
||||
"url": "https://github.com/nuxt-contrib/fs-memo/issues"
|
||||
},
|
||||
"description": "easy persisted memo object",
|
||||
"devDependencies": {
|
||||
"@nuxtjs/eslint-config-typescript": "^2.0.0",
|
||||
"@types/node": "^14.0.6",
|
||||
"bili": "^4.10.0",
|
||||
"eslint": "^7.1.0",
|
||||
"rollup-plugin-typescript2": "^0.27.1",
|
||||
"standard-version": "^8.0.0",
|
||||
"typescript": "^3.9.3"
|
||||
"@nuxtjs/eslint-config-typescript": "latest",
|
||||
"@types/node": "latest",
|
||||
"eslint": "latest",
|
||||
"siroc": "^0.4.0",
|
||||
"standard-version": "latest",
|
||||
"typescript": "latest"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
@@ -52,10 +51,10 @@
|
||||
"url": "git+https://github.com/nuxt-contrib/fs-memo.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bili src/index.ts --minimal",
|
||||
"build": "siroc build",
|
||||
"lint": "eslint --ext ts .",
|
||||
"release": "yarn build && standard-version && npm publish && git push --follow-tags"
|
||||
},
|
||||
"types": "dist/index.d.ts",
|
||||
"version": "1.0.1"
|
||||
"version": "1.2.0"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user