@webiny/global-config 0.0.0-unstable.eb196ccd2f β†’ 0.0.0-unstable.eb81deee7e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +10 -0
  2. package/index.d.ts +6 -0
  3. package/index.js +27 -12
  4. package/package.json +11 -9
package/README.md CHANGED
@@ -1 +1,11 @@
1
1
  # @webiny/global-config
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ πŸ“˜ **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
package/index.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  export interface GlobalConfig {
2
+ /**
3
+ * Returns the whole config, creating it with defaults if it is missing or
4
+ * has no `id`. `isEnabled()` in @webiny/telemetry already calls it this way;
5
+ * the overload was just missing from these types.
6
+ */
7
+ get(): Record<string, any>;
2
8
  get(key: string): any;
3
9
  set(key: string, value: any): void;
4
10
  }
package/index.js CHANGED
@@ -1,33 +1,48 @@
1
- const os = require("os");
2
- const path = require("path");
3
- const { v4: uuidv4 } = require("uuid");
4
- const readJson = require("load-json-file");
5
- const writeJson = require("write-json-file");
1
+ import os from "os";
2
+ import path from "path";
3
+ import { randomUUID } from "crypto";
4
+ import { loadJsonFileSync } from "load-json-file";
5
+ import { writeJsonFileSync } from "write-json-file";
6
+ import { isCI } from "ci-info";
6
7
 
7
8
  const GLOBAL_CONFIG_PATH = path.join(os.homedir(), ".webiny", "config");
8
9
 
9
- module.exports.globalConfig = {
10
+ export const globalConfig = {
10
11
  __globalConfig: null,
11
12
  get(key) {
12
13
  try {
13
14
  if (!this.__globalConfig) {
14
- this.__globalConfig = readJson.sync(GLOBAL_CONFIG_PATH);
15
+ this.__globalConfig = loadJsonFileSync(GLOBAL_CONFIG_PATH);
15
16
  if (!this.__globalConfig.id) {
16
17
  throw Error("Invalid Webiny config!");
17
18
  }
18
19
  }
19
- } catch (e) {
20
+ } catch {
20
21
  // A new config file is written if it doesn't exist or is invalid.
21
22
  this.__globalConfig = {
22
- id: uuidv4(),
23
+ // Node's own v4 UUID. This used to come from `@webiny/stdlib`, whose root entry is
24
+ // about 30 modules, loaded on every CLI command just to generate this one ID.
25
+ id: randomUUID(),
23
26
  telemetry: true,
24
27
 
25
28
  // This flag is set to `false` the moment user successfully
26
29
  // deploys a Webiny project for the first time. Once they do,
27
30
  // they're considered no longer a "new user".
28
- newUser: true
31
+ // Also, in CI environments, we always set this to `false`.
32
+ newUser: isCI ? false : true
29
33
  };
30
- writeJson.sync(GLOBAL_CONFIG_PATH, this.__globalConfig);
34
+ writeJsonFileSync(GLOBAL_CONFIG_PATH, this.__globalConfig);
35
+ }
36
+
37
+ // Backfill `newUser` on configs that predate it, or that were written by something
38
+ // that didn't set it. Absent is not the same as `false`: the only thing that writes
39
+ // `false` is the first successful deploy, so a missing key means that never happened
40
+ // and the user is still new. Reading it as `Boolean(undefined)` reported every one of
41
+ // them as returning. Persisted so the state stops being ambiguous, and so every reader
42
+ // agrees without repeating this rule.
43
+ if (!("newUser" in this.__globalConfig)) {
44
+ this.__globalConfig.newUser = isCI ? false : true;
45
+ writeJsonFileSync(GLOBAL_CONFIG_PATH, this.__globalConfig);
31
46
  }
32
47
 
33
48
  return key ? this.__globalConfig[key] : this.__globalConfig;
@@ -35,7 +50,7 @@ module.exports.globalConfig = {
35
50
  set(key, value) {
36
51
  const globalConfig = this.get();
37
52
  globalConfig[key] = value;
38
- writeJson.sync(GLOBAL_CONFIG_PATH, globalConfig);
53
+ writeJsonFileSync(GLOBAL_CONFIG_PATH, globalConfig);
39
54
  return globalConfig;
40
55
  }
41
56
  };
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@webiny/global-config",
3
- "version": "0.0.0-unstable.eb196ccd2f",
4
- "main": "index.js",
3
+ "version": "0.0.0-unstable.eb81deee7e",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./*": "./*"
8
+ },
5
9
  "license": "MIT",
6
10
  "dependencies": {
7
- "load-json-file": "6.2.0",
8
- "uuid": "9.0.1",
9
- "write-json-file": "4.3.0"
11
+ "ci-info": "4.4.0",
12
+ "load-json-file": "7.0.1",
13
+ "write-json-file": "7.0.0"
10
14
  },
11
15
  "publishConfig": {
12
- "access": "public",
13
- "directory": "."
14
- },
15
- "gitHead": "eb196ccd2f32296e10f7add6dd7220d4e3abece4"
16
+ "access": "public"
17
+ }
16
18
  }