@contentstack/cli-utilities 1.0.0 → 1.0.1

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.
@@ -8,15 +8,65 @@ const ENC_KEY = 'encryptionKey';
8
8
  const ENCRYPT_CONF = true;
9
9
  const CONFIG_NAME = 'contentstack_cli';
10
10
  const ENC_CONFIG_NAME = 'contentstack_cli_obfuscate';
11
+ const OLD_CONFIG_BACKUP_FLAG = 'isOldConfigBackup';
12
+ const xdgBasedir = require('xdg-basedir');
13
+ const path = require('path');
14
+ const os = require('os');
15
+ const uniqueString = require('unique-string');
16
+ const oldConfigDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
17
+ const pathPrefix = path.join('configstore', `${CONFIG_NAME}.json`);
18
+ const oldConfigPath = path.join(oldConfigDirectory, pathPrefix);
11
19
  class Config {
12
20
  constructor() {
13
21
  this.init();
22
+ this.importOldConfig();
14
23
  }
15
24
  init() {
16
25
  return ENCRYPT_CONF === true
17
26
  ? this.getEncryptedConfig()
18
27
  : this.getDecryptedConfig();
19
28
  }
29
+ importOldConfig() {
30
+ if (!this.get(OLD_CONFIG_BACKUP_FLAG)) {
31
+ try {
32
+ const oldConfigStoreData = this.getOldConfig();
33
+ if (oldConfigStoreData) {
34
+ this.setOldConfigStoreData(oldConfigStoreData, '');
35
+ this.removeOldConfigStoreFile();
36
+ }
37
+ }
38
+ catch (error) {
39
+ console.log("No data to be imported from Old config file");
40
+ }
41
+ this.set(OLD_CONFIG_BACKUP_FLAG, true);
42
+ }
43
+ }
44
+ // Recursive function to migrate from the old config
45
+ setOldConfigStoreData(data, _path = '') {
46
+ for (const key in data) {
47
+ const value = data[key];
48
+ const setPath = _path ? _path + '.' + key : key;
49
+ if (typeof (value) == "object") {
50
+ this.setOldConfigStoreData(value, setPath);
51
+ }
52
+ else {
53
+ this.set(setPath, value);
54
+ }
55
+ }
56
+ }
57
+ removeOldConfigStoreFile() {
58
+ if ((0, fs_1.existsSync)(oldConfigPath)) {
59
+ (0, fs_1.unlinkSync)(oldConfigPath); // NOTE remove old configstore file
60
+ }
61
+ }
62
+ getOldConfig() {
63
+ try {
64
+ return JSON.parse((0, fs_1.readFileSync)(oldConfigPath, 'utf8'));
65
+ }
66
+ catch (error) {
67
+ return undefined;
68
+ }
69
+ }
20
70
  fallbackInit() {
21
71
  return new conf_1.default({ configName: CONFIG_NAME, encryptionKey: ENC_KEY });
22
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentstack/cli-utilities",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Utilities for contentstack projects",
5
5
  "main": "lib/index.js",
6
6
  "types": "./types/index.d.ts",
@@ -34,7 +34,9 @@
34
34
  "debug": "^4.1.1",
35
35
  "inquirer": "^8.0.0",
36
36
  "ora": "^5.4.0",
37
- "winston": "^3.7.2"
37
+ "unique-string": "^2.0.0",
38
+ "winston": "^3.7.2",
39
+ "xdg-basedir": "^4.0.0"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@oclif/plugin-help": "^5.1.12",
@@ -1,23 +1,78 @@
1
1
  import Conf from 'conf';
2
2
  import { v4 as uuid } from 'uuid';
3
- import { existsSync, unlinkSync } from 'fs'
3
+ import { existsSync, unlinkSync, readFileSync } from 'fs';
4
4
 
5
5
  const ENC_KEY = 'encryptionKey';
6
6
  const ENCRYPT_CONF: boolean = true
7
7
  const CONFIG_NAME = 'contentstack_cli';
8
8
  const ENC_CONFIG_NAME = 'contentstack_cli_obfuscate';
9
+ const OLD_CONFIG_BACKUP_FLAG = 'isOldConfigBackup'
10
+
11
+ const xdgBasedir = require('xdg-basedir');
12
+ const path = require('path');
13
+ const os = require('os');
14
+ const uniqueString = require('unique-string');
15
+ const oldConfigDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
16
+ const pathPrefix = path.join('configstore', `${CONFIG_NAME}.json`);
17
+ const oldConfigPath = path.join(oldConfigDirectory, pathPrefix);
9
18
 
10
19
  class Config {
11
20
  private config: Conf;
12
21
 
13
22
  constructor() {
14
23
  this.init()
24
+ this.importOldConfig()
15
25
  }
16
26
 
17
27
  init() {
18
28
  return ENCRYPT_CONF === true
19
29
  ? this.getEncryptedConfig()
20
30
  : this.getDecryptedConfig()
31
+
32
+ }
33
+
34
+ importOldConfig() {
35
+ if (!this.get(OLD_CONFIG_BACKUP_FLAG)) {
36
+ try {
37
+ const oldConfigStoreData = this.getOldConfig()
38
+ if (oldConfigStoreData) {
39
+ this.setOldConfigStoreData(oldConfigStoreData, '')
40
+ this.removeOldConfigStoreFile()
41
+ }
42
+ } catch (error) {
43
+ console.log("No data to be imported from Old config file");
44
+
45
+ }
46
+ this.set(OLD_CONFIG_BACKUP_FLAG, true)
47
+ }
48
+ }
49
+
50
+ // Recursive function to migrate from the old config
51
+ setOldConfigStoreData(data, _path = '') {
52
+ for (const key in data) {
53
+ const value = data[key];
54
+ const setPath = _path ? _path + '.' + key : key
55
+ if (typeof (value) == "object") {
56
+ this.setOldConfigStoreData(value, setPath)
57
+ }
58
+ else {
59
+ this.set(setPath, value)
60
+ }
61
+ }
62
+ }
63
+
64
+ removeOldConfigStoreFile() {
65
+ if (existsSync(oldConfigPath)) {
66
+ unlinkSync(oldConfigPath) // NOTE remove old configstore file
67
+ }
68
+ }
69
+
70
+ private getOldConfig() {
71
+ try {
72
+ return JSON.parse(readFileSync(oldConfigPath, 'utf8'));
73
+ } catch (error) {
74
+ return undefined;
75
+ }
21
76
  }
22
77
 
23
78
  private fallbackInit(): Conf {