mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
* first action! (#1)
This commit is contained in:
parent
8deacc95b1
commit
ace1e6a69a
3750 changed files with 1155519 additions and 0 deletions
106
node_modules/configstore/index.js
generated
vendored
Normal file
106
node_modules/configstore/index.js
generated
vendored
Normal file
|
@ -0,0 +1,106 @@
|
|||
'use strict';
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const fs = require('graceful-fs');
|
||||
const makeDir = require('make-dir');
|
||||
const xdgBasedir = require('xdg-basedir');
|
||||
const writeFileAtomic = require('write-file-atomic');
|
||||
const dotProp = require('dot-prop');
|
||||
const uniqueString = require('unique-string');
|
||||
|
||||
const configDir = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
|
||||
const permissionError = 'You don\'t have access to this file.';
|
||||
const makeDirOptions = {mode: 0o0700};
|
||||
const writeFileOptions = {mode: 0o0600};
|
||||
|
||||
class Configstore {
|
||||
constructor(id, defaults, options = {}) {
|
||||
const pathPrefix = options.globalConfigPath ?
|
||||
path.join(id, 'config.json') :
|
||||
path.join('configstore', `${id}.json`);
|
||||
|
||||
this.path = options.configPath || path.join(configDir, pathPrefix);
|
||||
|
||||
if (defaults) {
|
||||
this.all = Object.assign({}, defaults, this.all);
|
||||
}
|
||||
}
|
||||
|
||||
get all() {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(this.path, 'utf8'));
|
||||
} catch (error) {
|
||||
// Create directory if it doesn't exist
|
||||
if (error.code === 'ENOENT') {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Improve the message of permission errors
|
||||
if (error.code === 'EACCES') {
|
||||
error.message = `${error.message}\n${permissionError}\n`;
|
||||
}
|
||||
|
||||
// Empty the file if it encounters invalid JSON
|
||||
if (error.name === 'SyntaxError') {
|
||||
writeFileAtomic.sync(this.path, '', writeFileOptions);
|
||||
return {};
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
set all(value) {
|
||||
try {
|
||||
// Make sure the folder exists as it could have been deleted in the meantime
|
||||
makeDir.sync(path.dirname(this.path), makeDirOptions);
|
||||
|
||||
writeFileAtomic.sync(this.path, JSON.stringify(value, null, '\t'), writeFileOptions);
|
||||
} catch (error) {
|
||||
// Improve the message of permission errors
|
||||
if (error.code === 'EACCES') {
|
||||
error.message = `${error.message}\n${permissionError}\n`;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
get size() {
|
||||
return Object.keys(this.all || {}).length;
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return dotProp.get(this.all, key);
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
const config = this.all;
|
||||
|
||||
if (arguments.length === 1) {
|
||||
for (const k of Object.keys(key)) {
|
||||
dotProp.set(config, k, key[k]);
|
||||
}
|
||||
} else {
|
||||
dotProp.set(config, key, value);
|
||||
}
|
||||
|
||||
this.all = config;
|
||||
}
|
||||
|
||||
has(key) {
|
||||
return dotProp.has(this.all, key);
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
const config = this.all;
|
||||
dotProp.delete(config, key);
|
||||
this.all = config;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.all = {};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Configstore;
|
Loading…
Add table
Add a link
Reference in a new issue