Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conf.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const ConfigChain = require('config-chain').ConfigChain;
  5. const util = require('./util');
  6. class Conf extends ConfigChain {
  7. // https://github.com/npm/npm/blob/latest/lib/config/core.js#L208-L222
  8. constructor(base) {
  9. super(base);
  10. this.root = base;
  11. }
  12. // https://github.com/npm/npm/blob/latest/lib/config/core.js#L332-L342
  13. add(data, marker) {
  14. try {
  15. for (const x of Object.keys(data)) {
  16. data[x] = util.parseField(data[x], x);
  17. }
  18. } catch (err) {
  19. throw err;
  20. }
  21. return super.add(data, marker);
  22. }
  23. // https://github.com/npm/npm/blob/latest/lib/config/core.js#L312-L325
  24. addFile(file, name) {
  25. name = name || file;
  26. const marker = {__source__: name};
  27. this.sources[name] = {path: file, type: 'ini'};
  28. this.push(marker);
  29. this._await();
  30. try {
  31. const contents = fs.readFileSync(file, 'utf8');
  32. this.addString(contents, file, 'ini', marker);
  33. } catch (err) {
  34. this.add({}, marker);
  35. }
  36. return this;
  37. }
  38. // https://github.com/npm/npm/blob/latest/lib/config/core.js#L344-L360
  39. addEnv(env) {
  40. env = env || process.env;
  41. const conf = {};
  42. Object.keys(env)
  43. .filter(x => /^npm_config_/i.test(x))
  44. .forEach(x => {
  45. if (!env[x]) {
  46. return;
  47. }
  48. const p = x.toLowerCase()
  49. .replace(/^npm_config_/, '')
  50. .replace(/(?!^)_/g, '-');
  51. conf[p] = env[x];
  52. });
  53. return super.addEnv('', conf, 'env');
  54. }
  55. // https://github.com/npm/npm/blob/latest/lib/config/load-prefix.js
  56. loadPrefix() {
  57. const cli = this.list[0];
  58. Object.defineProperty(this, 'prefix', {
  59. enumerable: true,
  60. set: prefix => {
  61. const g = this.get('global');
  62. this[g ? 'globalPrefix' : 'localPrefix'] = prefix;
  63. },
  64. get: () => {
  65. const g = this.get('global');
  66. return g ? this.globalPrefix : this.localPrefix;
  67. }
  68. });
  69. Object.defineProperty(this, 'globalPrefix', {
  70. enumerable: true,
  71. set: prefix => {
  72. this.set('prefix', prefix);
  73. },
  74. get: () => {
  75. return path.resolve(this.get('prefix'));
  76. }
  77. });
  78. let p;
  79. Object.defineProperty(this, 'localPrefix', {
  80. enumerable: true,
  81. set: prefix => {
  82. p = prefix;
  83. },
  84. get: () => {
  85. return p;
  86. }
  87. });
  88. if (Object.prototype.hasOwnProperty.call(cli, 'prefix')) {
  89. p = path.resolve(cli.prefix);
  90. } else {
  91. try {
  92. const prefix = util.findPrefix(process.cwd());
  93. p = prefix;
  94. } catch (err) {
  95. throw err;
  96. }
  97. }
  98. return p;
  99. }
  100. // https://github.com/npm/npm/blob/latest/lib/config/load-cafile.js
  101. loadCAFile(file) {
  102. if (!file) {
  103. return;
  104. }
  105. try {
  106. const contents = fs.readFileSync(file, 'utf8');
  107. const delim = '-----END CERTIFICATE-----';
  108. const output = contents
  109. .split(delim)
  110. .filter(x => Boolean(x.trim()))
  111. .map(x => x.trimLeft() + delim);
  112. this.set('ca', output);
  113. } catch (err) {
  114. if (err.code === 'ENOENT') {
  115. return;
  116. }
  117. throw err;
  118. }
  119. }
  120. // https://github.com/npm/npm/blob/latest/lib/config/set-user.js
  121. loadUser() {
  122. const defConf = this.root;
  123. if (this.get('global')) {
  124. return;
  125. }
  126. if (process.env.SUDO_UID) {
  127. defConf.user = Number(process.env.SUDO_UID);
  128. return;
  129. }
  130. const prefix = path.resolve(this.get('prefix'));
  131. try {
  132. const stats = fs.statSync(prefix);
  133. defConf.user = stats.uid;
  134. } catch (err) {
  135. if (err.code === 'ENOENT') {
  136. return;
  137. }
  138. throw err;
  139. }
  140. }
  141. }
  142. module.exports = Conf;