summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/addon/folder.js
blob: 4b590c55a73ad3a21bed25010df3300a0d57c867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';

const JETPACK_DIR_BASENAME = "jetpack";
const PATH_TEST = /^[\s\.\\\/]/;

const { Cc, Ci } = require('chrome');
const file = require('sdk/io/file');
const jpSelf = require('sdk/self');

let storeFile = Cc['@mozilla.org/file/directory_service;1']
                  .getService(Ci.nsIProperties)
                  .get('ProfD', Ci.nsIFile);
storeFile.append(JETPACK_DIR_BASENAME);
storeFile.append(jpSelf.id);
storeFile.append('addon-folder');

const ADDON_FOLDER_PATH = storeFile.path + '/';

// make the addon-folder container folder
file.mkpath(ADDON_FOLDER_PATH);

function ioFileWrap(funcName, preMode) {
  preMode = preMode || "";
  return function(filepath, mode) {
    filepath = filepath || '';
    if (PATH_TEST.test(filepath)) {
      throw 'The provided filepath "' + filepath + '"" is not valid';
    }
    return file[funcName](ADDON_FOLDER_PATH + filepath, preMode + mode);
  }
}
exports.isFile = ioFileWrap('isFile');
exports.exists = ioFileWrap('exists');

exports.remove = function(filepath) {
  if (exports.isFile(filepath)) {
  	file.remove(ADDON_FOLDER_PATH + filepath);
  }
  else {
  	file.rmdir(ADDON_FOLDER_PATH + filepath);
  }
};
exports.read = ioFileWrap('read');
exports.write = ioFileWrap('open', 'w');
exports.mkpath = ioFileWrap('mkpath');
exports.list = ioFileWrap('list');

exports.destroy = function destroy() {
  // remove the addon-folder container folder
  file.rmdir(ADDON_FOLDER_PATH);
}