@ixon-cdk/core 1.1.0-next.2 → 1.1.0-next.3

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.
@@ -5,6 +5,19 @@ const ConfigService = require('../config/config.service');
5
5
  module.exports = class ApiBaseService {
6
6
  _configSrv = new ConfigService();
7
7
 
8
+ /**
9
+ * Returns the amount of seconds between when the `.accesstoken` file was last modified relative
10
+ * to now, or `-1` when the file doesn't exist.
11
+ * */
12
+ _getAccessTokenFileModifiedRelativeToNow() {
13
+ if (fs.existsSync(this._getAccessTokenFilePath())) {
14
+ const stats = fs.statSync(this._getAccessTokenFilePath());
15
+ return Math.floor((new Date().getTime() - stats.mtimeMs) / 1000);
16
+ }
17
+ return -1;
18
+ }
19
+
20
+ /** Returns an absolute path to the `.accesstoken` file. */
8
21
  _getAccessTokenFilePath() {
9
22
  return path.join(require('../utils').getRootDir(), '.accesstoken');
10
23
  }
@@ -28,9 +41,11 @@ module.exports = class ApiBaseService {
28
41
  _getSecretId() {
29
42
  let secretId;
30
43
  try {
31
- secretId = fs.readFileSync(this._getAccessTokenFilePath(), {
32
- encoding: 'utf-8',
33
- });
44
+ // If the `.accesstoken` file's modified time exceeds the standard expiry time of 24 hours,
45
+ // we'll make the assumption that it is no longer a valid access-token.
46
+ if (this._getAccessTokenFileModifiedRelativeToNow() < 86400) {
47
+ secretId = fs.readFileSync(this._getAccessTokenFilePath(), { encoding: 'utf-8' });
48
+ }
34
49
  } catch {
35
50
  // do nothing
36
51
  }
@@ -6,21 +6,32 @@ const { getRootDir, logFileCrudMessage, logErrorMessage } = require('../utils');
6
6
  module.exports = class ConfigService {
7
7
  _config = { components: {} };
8
8
 
9
- constructor(configFile = 'config.json') {
10
- this._path = path.join(getRootDir(), configFile);
9
+ constructor(configFile) {
10
+ // When the CDK is part of a larger project workspace it may be desirable to have a more explicit config-file name. Therefore, unless
11
+ // a specific config file path was defined in the contructor, we'll first try to find `cdk.config.json`, before falling back to the
12
+ // default file name `config.json`.
13
+ if (configFile !== undefined) {
14
+ this._path = path.join(getRootDir(), configFile);
15
+ } else {
16
+ if (fs.existsSync(path.join(getRootDir(), 'cdk.config.json'))) {
17
+ this._path = path.join(getRootDir(), 'cdk.config.json');
18
+ } else {
19
+ this._path = path.join(getRootDir(), 'config.json');
20
+ }
21
+ }
11
22
 
12
23
  let config;
13
24
 
14
25
  if (!fs.existsSync(this._path)) {
15
26
  logErrorMessage('No config file.');
16
- process.exit();
27
+ process.exit(1);
17
28
  }
18
29
 
19
30
  try {
20
31
  config = require(this._path);
21
32
  } catch {
22
33
  logErrorMessage("Couldn't parse config file.");
23
- process.exit();
34
+ process.exit(1);
24
35
  }
25
36
 
26
37
  this._config = config;
@@ -38,6 +49,10 @@ module.exports = class ConfigService {
38
49
  return this._config.newComponentRoot || 'components';
39
50
  }
40
51
 
52
+ getOutputPath() {
53
+ return this._config.outputPath || 'dist';
54
+ }
55
+
41
56
  getPrefix() {
42
57
  return this._config.prefix || null;
43
58
  }
@@ -60,7 +75,11 @@ module.exports = class ConfigService {
60
75
  }
61
76
 
62
77
  extendComponent(name, config) {
63
- this._config.components[name] = merge({}, this._config.components[name], config);
78
+ this._config.components[name] = merge(
79
+ {},
80
+ this._config.components[name],
81
+ config,
82
+ );
64
83
  this._sync();
65
84
  }
66
85
 
@@ -7,6 +7,11 @@ export interface schema {
7
7
  * @default "components"
8
8
  */
9
9
  newComponentRoot?: string;
10
+ /**
11
+ * The path where the build output will be written, relative to the current workspace root.
12
+ * @default "dist"
13
+ */
14
+ outputPath?: string;
10
15
  /**
11
16
  * API application ID
12
17
  * @default "LtDdZKEPa5lK"
@@ -7,6 +7,11 @@
7
7
  "description": "The path where new components will be created, relative to the workspace root.",
8
8
  "default": "components"
9
9
  },
10
+ "outputPath": {
11
+ "type": "string",
12
+ "description": "The path where the build output will be written, relative to the current workspace root.",
13
+ "default": "dist"
14
+ },
10
15
  "apiApplication": {
11
16
  "type": "string",
12
17
  "description": "API application ID",
package/http-request.js CHANGED
@@ -11,14 +11,14 @@ module.exports = function request(options) {
11
11
 
12
12
  if (typeof customRequest !== 'function') {
13
13
  logErrorMessage('The custom "http-request.js" file must export a function.');
14
- process.exit();
14
+ process.exit(1);
15
15
  }
16
16
 
17
17
  const promise = customRequest(options);
18
18
 
19
19
  if (typeof promise !== 'object' || typeof promise.then !== 'function') {
20
20
  logErrorMessage('The exported function in the custom "http-request.js" file must return a Promise.');
21
- process.exit();
21
+ process.exit(1);
22
22
  }
23
23
 
24
24
  return promise;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ixon-cdk/core",
3
- "version": "1.1.0-next.2",
3
+ "version": "1.1.0-next.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "",
@@ -11,6 +11,7 @@
11
11
  "axios": "^0.26.1",
12
12
  "chalk": "^4.1.2",
13
13
  "chokidar": "^3.5.3",
14
+ "cors": "^2.8.5",
14
15
  "crypto-js": "^4.1.1",
15
16
  "express": "^4.17.3",
16
17
  "glob": "^8.0.1",
package/server/index.js CHANGED
@@ -1,12 +1,15 @@
1
1
  const path = require('path');
2
2
  const express = require('express');
3
+ const cors = require('cors');
3
4
  const liveReload = require('livereload');
5
+ const ApiBaseService = require('../api/base.service');
4
6
  const ConfigService = require('../config/config.service');
5
7
 
6
8
  const LR_PORT_DEFAULT = 35729;
7
9
 
8
10
  module.exports = class Server {
9
11
  constructor(opts) {
12
+ this._apiSrv = new ApiBaseService();
10
13
  this._configSrv = new ConfigService();
11
14
  this._rootDir = require('../utils').getRootDir();
12
15
  this._opts = {
@@ -63,6 +66,14 @@ module.exports = class Server {
63
66
  app.get(`/${this._opts.componentBasePath}/*`, (req, res) => {
64
67
  res.sendStatus(404);
65
68
  });
69
+ app.get(
70
+ '/access-token.txt',
71
+ cors({ origin: `http://localhost:${this._opts.port}` }),
72
+ (req, res) => {
73
+ res.type('text/plain');
74
+ res.send(this._apiSrv._getSecretId() || '');
75
+ },
76
+ );
66
77
  app.get('/config.json', (req, res) => {
67
78
  res.send({
68
79
  api: {
@@ -118,12 +129,12 @@ module.exports = class Server {
118
129
  if (name) {
119
130
  const prefix = this._configSrv.getPrefix();
120
131
  const tag = prefix ? `${prefix}-${name}` : name;
121
- return `dist/${name}/${tag}.min.js`;
132
+ return `${this._getOutputDir(name)}/${tag}.min.js`;
122
133
  }
123
134
  return null;
124
135
  }
125
136
 
126
137
  _getOutputDir(name) {
127
- return name ? `dist/${name}` : null;
138
+ return name ? `${this._configSrv.getOutputPath()}/${name}` : null;
128
139
  }
129
140
  };
@@ -28,7 +28,7 @@ module.exports = class TemplateService {
28
28
 
29
29
  if (!fs.existsSync(examplesRoot)) {
30
30
  logErrorMessage("Package 'ixoncloud/component-examples' is not installed.");
31
- process.exit();
31
+ process.exit(1);
32
32
  }
33
33
 
34
34
  const examplesConfigSrv = new ConfigService('node_modules/component-examples/config.json');
@@ -36,7 +36,7 @@ module.exports = class TemplateService {
36
36
 
37
37
  if (!Object.keys(components).length) {
38
38
  logErrorMessage('No examples found.');
39
- process.exit();
39
+ process.exit(1);
40
40
  }
41
41
 
42
42
  const exampleComponentRoot = examplesConfigSrv.getNewComponentRoot();
@@ -52,10 +52,10 @@ module.exports = class TemplateService {
52
52
  const exampleName = result.name;
53
53
 
54
54
  if (!exampleName) {
55
- process.exit();
55
+ process.exit(1);
56
56
  } else if (!components[exampleName]) {
57
57
  logErrorMessage(`Example '${exampleName}' is not configured.`);
58
- process.exit();
58
+ process.exit(1);
59
59
  }
60
60
 
61
61
  const { build } = components[exampleName].runner;
package/utils.js CHANGED
@@ -80,7 +80,7 @@ const pascalCase = flow(camelCase, upperFirst);
80
80
  function apiHttpErrorToMessage(error) {
81
81
  if (typeof error === 'string') {
82
82
  logErrorMessage(error);
83
- process.exit();
83
+ process.exit(1);
84
84
  }
85
85
  if (error && error.response && error.response.data) {
86
86
  const errors = error.response.data.data;
@@ -90,12 +90,12 @@ function apiHttpErrorToMessage(error) {
90
90
  .map((err) => (err.propertyName ? `${err.propertyName}: ${err.message}` : err.message))
91
91
  .join('\n'),
92
92
  );
93
- process.exit();
93
+ process.exit(1);
94
94
  }
95
95
  }
96
96
  logErrorMessage('Unexpected error.');
97
97
  console.log(error);
98
- process.exit();
98
+ process.exit(1);
99
99
  }
100
100
 
101
101
  function generatePreviewHash(templateId, templateName, versionId, versionNumber, versionMainPath) {