summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/bin/node-scripts/update-ini.js
blob: 634cbc1de721cc60b2d404399ce74af0ee7bd3be (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

var path = require("path");
var cp = require("child_process");
var fs = require("fs");
var Promise = require("promise");
var parser = require("ini-parser");

var addonINI = path.resolve("./test/addons/jetpack-addon.ini");
var addonsDir = path.resolve("./test/addons/");
var packageINI = path.resolve("./test/jetpack-package.ini");
var packageDir = path.resolve("./test/");
var packageIgnorables = [ "addons", "preferences" ];
var packageSupportFiles = [
  "fixtures.js",
  "test-context-menu.html",
  "util.js"
]

function updateAddonINI() {
  return new Promise(function(resolve) {
    console.log("Start updating " + addonINI);

    makeAddonIniContent().
    then(function(contents) {
      fs.writeFileSync(addonINI, contents, { encoding: "utf8" });
      console.log("Done updating " + addonINI);
      resolve();
    });
  })
}
exports.updateAddonINI = updateAddonINI;

function makeAddonIniContent() {
  return new Promise(function(resolve) {
    var data = parser.parse(fs.readFileSync(addonINI, { encoding: "utf8" }).toString());
    var result = {};

    fs.readdir(addonsDir, function(err, files) {
      // get a list of folders
      var folders = files.filter(function(file) {
        return fs.statSync(path.resolve(addonsDir, file)).isDirectory();
      }).sort();

      // copy any related data from the existing ini
      folders.forEach(function(folder) {
        var oldData = data[folder + ".xpi"];
        result[folder] = oldData ? oldData : {};
      });

      // build a new ini file
      var contents = [];
      Object.keys(result).sort().forEach(function(key) {
        contents.push("[" + key + ".xpi]");
        Object.keys(result[key]).forEach(function(dataKey) {
          contents.push(dataKey + " = " + result[key][dataKey]);
        });
      });
      contents = contents.join("\n") + "\n";

      return resolve(contents);
    });
  });
}
exports.makeAddonIniContent = makeAddonIniContent;

function makePackageIniContent() {
  return new Promise(function(resolve) {
    var data = parser.parse(fs.readFileSync(packageINI, { encoding: "utf8" }).toString());
    var result = {};

    fs.readdir(packageDir, function(err, files) {
      // get a list of folders
      var folders = files.filter(function(file) {
        var ignore = (packageIgnorables.indexOf(file) >= 0);
        var isDir = fs.statSync(path.resolve(packageDir, file)).isDirectory();
        return (isDir && !ignore);
      }).sort();

      // get a list of "test-"" files
      var files = files.filter(function(file) {
        var ignore = !/^test\-.*\.js$/i.test(file);
        var isDir = fs.statSync(path.resolve(packageDir, file)).isDirectory();
        return (!isDir && !ignore);
      }).sort();

      // get a list of the support files
      var support_files = packageSupportFiles.map(function(file) {
        return "  " + file;
      });
      folders.forEach(function(folder) {
        support_files.push("  " + folder + "/**");
      });
      support_files = support_files.sort();

      // copy any related data from the existing ini
      files.forEach(function(file) {
        var oldData = data[file];
        result[file] = oldData ? oldData : {};
      });

      // build a new ini file
      var contents = [
        "[DEFAULT]",
        "support-files ="
      ];
      support_files.forEach(function(support_file) {
        contents.push(support_file);
      });
      contents.push("");

      Object.keys(result).sort().forEach(function(key) {
        contents.push("[" + key + "]");
        Object.keys(result[key]).forEach(function(dataKey) {
          contents.push(dataKey + " = " + result[key][dataKey]);
        });
      });
      contents = contents.join("\n") + "\n";

      return resolve(contents);
    });
  });
}
exports.makePackageIniContent = makePackageIniContent;

function updatePackageINI() {
  return new Promise(function(resolve) {
    console.log("Start updating " + packageINI);

    makeAddonIniContent().
    then(function(contents) {
      fs.writeFileSync(packageINI, contents, { encoding: "utf8" });
      console.log("Done updating " + packageINI);
      resolve();
    });
  })
}
exports.updatePackageINI = updatePackageINI;