1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const deps_1 = require("./deps");
|
5 | class UserConfig {
|
6 | constructor(config) {
|
7 | this.config = config;
|
8 | this.needsSave = false;
|
9 | }
|
10 | get install() {
|
11 | return this.body.install || this.genInstall();
|
12 | }
|
13 | set install(install) {
|
14 | this.body.install = install;
|
15 | this.needsSave = true;
|
16 | }
|
17 | get skipAnalytics() {
|
18 | if (this.config.scopedEnvVar('SKIP_ANALYTICS') === '1')
|
19 | return false;
|
20 | if (typeof this.body.skipAnalytics !== 'boolean') {
|
21 | this.body.skipAnalytics = false;
|
22 | this.needsSave = true;
|
23 | }
|
24 | return this.body.skipAnalytics;
|
25 | }
|
26 | async init() {
|
27 | await this.saving;
|
28 | if (this._init)
|
29 | return this._init;
|
30 | return (this._init = (async () => {
|
31 | this.debug('init');
|
32 | this.body = (await this.read()) || { schema: 1 };
|
33 | if (!this.body.schema) {
|
34 | this.body.schema = 1;
|
35 | this.needsSave = true;
|
36 | }
|
37 | else if (this.body.schema !== 1)
|
38 | this.body = { schema: 1 };
|
39 |
|
40 | this.install;
|
41 |
|
42 | this.skipAnalytics;
|
43 | if (this.needsSave)
|
44 | await this.save();
|
45 | })());
|
46 | }
|
47 | get debug() {
|
48 | return require('debug')('heroku:user_config');
|
49 | }
|
50 | get file() {
|
51 | return path.join(this.config.dataDir, 'config.json');
|
52 | }
|
53 | async save() {
|
54 | if (!this.needsSave)
|
55 | return;
|
56 | this.needsSave = false;
|
57 | this.saving = (async () => {
|
58 | this.debug('saving');
|
59 | if (!await this.canWrite()) {
|
60 | throw new Error('file modified, cannot save');
|
61 | }
|
62 | await deps_1.default.file.outputJSON(this.file, this.body);
|
63 | })();
|
64 | }
|
65 | async read() {
|
66 | await this.migrate();
|
67 | try {
|
68 | this.mtime = await this.getLastUpdated();
|
69 | let body = await deps_1.default.file.readJSON(this.file);
|
70 | return body;
|
71 | }
|
72 | catch (err) {
|
73 | if (err.code !== 'ENOENT')
|
74 | throw err;
|
75 | this.debug('not found');
|
76 | }
|
77 | }
|
78 | async migrate() {
|
79 | if (await deps_1.default.file.exists(this.file))
|
80 | return;
|
81 | let old = path.join(this.config.configDir, 'config.json');
|
82 | if (!await deps_1.default.file.exists(old))
|
83 | return;
|
84 | this.debug('moving config into new place');
|
85 | await deps_1.default.file.rename(old, this.file);
|
86 | }
|
87 | async canWrite() {
|
88 | if (!this.mtime)
|
89 | return true;
|
90 | return (await this.getLastUpdated()) === this.mtime;
|
91 | }
|
92 | async getLastUpdated() {
|
93 | try {
|
94 | const stat = await deps_1.default.file.stat(this.file);
|
95 | return stat.mtime.getTime();
|
96 | }
|
97 | catch (err) {
|
98 | if (err.code !== 'ENOENT')
|
99 | throw err;
|
100 | }
|
101 | }
|
102 | genInstall() {
|
103 | const uuid = require('uuid/v4');
|
104 | this.install = uuid();
|
105 | return this.install;
|
106 | }
|
107 | }
|
108 | exports.default = UserConfig;
|