@superblocksteam/util 1.13.0 → 1.14.0

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.
@@ -41,55 +41,51 @@ exports.getComponentConfigs = getComponentConfigs;
41
41
  const node_child_process_1 = require("node:child_process");
42
42
  const node_os_1 = __importDefault(require("node:os"));
43
43
  const node_path_1 = __importDefault(require("node:path"));
44
+ const util = __importStar(require("node:util"));
44
45
  const fs = __importStar(require("fs-extra"));
45
46
  const mimeTypes = __importStar(require("mime-types"));
46
47
  const constants_1 = require("./constants");
47
48
  const generate_component_types_1 = require("./generate-component-types");
48
49
  const validation_1 = require("./validation");
49
- const compileTypeScript = (inputFilePath, outputDir) => {
50
- return new Promise((resolve, reject) => {
51
- try {
52
- const tscPath = require.resolve("typescript/bin/tsc");
53
- if (tscPath) {
54
- (0, node_child_process_1.execFile)("node", [
55
- tscPath,
56
- node_path_1.default.resolve(inputFilePath),
57
- "--outDir",
58
- outputDir,
59
- "--jsx",
60
- "react-jsx",
61
- "--target",
62
- "es2019",
63
- "--esModuleInterop",
64
- "true",
65
- "--moduleResolution",
66
- "node",
67
- "--module",
68
- "commonjs",
69
- "--strict",
70
- "true",
71
- "--skipLibCheck",
72
- "true",
73
- ], (error, stdout, stderr) => {
74
- if (error) {
75
- // Print any Typescript output to the user, such as errors
76
- console.log(stdout);
77
- reject(`Compilation failed: ${error.message}`);
78
- return;
79
- }
80
- if (stderr) {
81
- reject(`Error during compilation: ${stderr}`);
82
- return;
83
- }
84
- resolve("Compilation successful!");
85
- });
50
+ const asyncExecFile = util.promisify(node_child_process_1.execFile);
51
+ async function compileTypeScript(inputFilePath, outputDir) {
52
+ try {
53
+ const tscPath = require.resolve("typescript/bin/tsc");
54
+ if (tscPath) {
55
+ const result = await asyncExecFile("node", [
56
+ tscPath,
57
+ node_path_1.default.resolve(inputFilePath),
58
+ "--outDir",
59
+ outputDir,
60
+ "--jsx",
61
+ "react-jsx",
62
+ "--target",
63
+ "es2019",
64
+ "--esModuleInterop",
65
+ "true",
66
+ "--moduleResolution",
67
+ "node",
68
+ "--module",
69
+ "commonjs",
70
+ "--strict",
71
+ "true",
72
+ "--skipLibCheck",
73
+ "true",
74
+ ]);
75
+ if (result.stdout || result.stderr) {
76
+ return {
77
+ output: result.stdout || result.stderr,
78
+ };
86
79
  }
87
80
  }
88
- catch {
89
- reject(new Error("Could not find TypeScript, please install it globally (npm install -g typescript)"));
90
- }
91
- });
92
- };
81
+ }
82
+ catch (error) {
83
+ return {
84
+ output: error.stdout || error.stderr,
85
+ };
86
+ }
87
+ return {};
88
+ }
93
89
  async function getFolderPaths() {
94
90
  try {
95
91
  const folderPaths = await fs.readdir(constants_1.CUSTOM_COMPONENTS_PATH, {
@@ -122,35 +118,35 @@ async function getComponentConfigs(generateTypesFiles) {
122
118
  catch {
123
119
  // Noop because there is a fallback
124
120
  }
125
- try {
126
- // This is a tad complex to explain this hack but here's the context:
127
- // We want to be able to hot reload the config.ts file, but we can't import it directly
128
- // because it is going to get cached in the module cache and we dont have access to it to clear it
129
- // so instead we copy the file to a new location with a random name and import that
130
- // this is technically a memory leak, but you shouldnt really have a hot module server running forever
131
- const rawPath = `${ccpath}/config.ts`;
132
- const fileName = node_path_1.default.resolve(rawPath);
133
- const outputDirectory = node_path_1.default.resolve(`${node_os_1.default.tmpdir()}/superblocks/${ccpath}/config.timestamp-${Date.now()}-${Math.random()
134
- .toString(16)
135
- .slice(2)}`);
136
- await fs.ensureDir(outputDirectory);
137
- await compileTypeScript(fileName, outputDirectory);
138
- console.log(`Typescript compiled to ${ccpath}`);
139
- const config = (await Promise.resolve(`${outputDirectory + "/config.js"}`).then(s => __importStar(require(s)))).default;
140
- const isValid = (0, validation_1.validateCustomComponents)(config);
141
- if (!isValid.valid) {
142
- // Not throwing because we don't need a stack trace here
143
- console.log(`Invalid config.ts file found`);
144
- console.log(isValid.message);
145
- hasError = true;
121
+ // This is a tad complex to explain this hack but here's the context:
122
+ // We want to be able to hot reload the config.ts file, but we can't import it directly
123
+ // because it is going to get cached in the module cache and we dont have access to it to clear it
124
+ // so instead we copy the file to a new location with a random name and import that
125
+ // this is technically a memory leak, but you shouldnt really have a hot module server running forever
126
+ const rawPath = `${ccpath}/config.ts`;
127
+ const fileName = node_path_1.default.resolve(rawPath);
128
+ const outputDirectory = node_path_1.default.resolve(`${node_os_1.default.tmpdir()}/superblocks/${ccpath}/config.timestamp-${Date.now()}-${Math.random()
129
+ .toString(16)
130
+ .slice(2)}`);
131
+ await fs.ensureDir(outputDirectory);
132
+ const compilationResult = await compileTypeScript(fileName, outputDirectory);
133
+ if (compilationResult.output) {
134
+ hasError = true;
135
+ if (compilationResult.output.includes(`Cannot find module`)) {
136
+ throw new Error("Could not find a module in node_modules. You likely need to run 'npm install'");
146
137
  }
147
- return { ccpath, config };
138
+ throw new Error(compilationResult.output);
148
139
  }
149
- catch (e) {
150
- console.error(e);
151
- console.error(e.message);
152
- throw e;
140
+ console.log(`Typescript compiled to ${ccpath}`);
141
+ const config = (await Promise.resolve(`${outputDirectory + "/config.js"}`).then(s => __importStar(require(s)))).default;
142
+ const isValid = (0, validation_1.validateCustomComponents)(config);
143
+ if (!isValid.valid) {
144
+ // Not throwing because we don't need a stack trace here
145
+ console.log(`Invalid config.ts file found`);
146
+ console.log(isValid.message);
147
+ hasError = true;
153
148
  }
149
+ return { ccpath, config };
154
150
  });
155
151
  const configFiles = await Promise.all(promiseConfigFiles);
156
152
  if (generateTypesFiles) {
@@ -9,3 +9,9 @@ export declare class FileAccessError extends Error {
9
9
  export declare class NotFoundError extends Error {
10
10
  constructor(message: string);
11
11
  }
12
+ export declare class BadRequestError extends Error {
13
+ constructor(message: string);
14
+ }
15
+ export declare class ForbiddenError extends Error {
16
+ constructor(message: string);
17
+ }
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotFoundError = exports.FileAccessError = exports.CUSTOM_COMPONENTS_PATH = exports.ROOT_CONFIG_RELATIVE_PATH = exports.RESOURCE_CONFIG_PATH = exports.TOKEN_CONFIG_PATH = exports.SUPERBLOCKS_HOME_FOLDER_NAME = void 0;
3
+ exports.ForbiddenError = exports.BadRequestError = exports.NotFoundError = exports.FileAccessError = exports.CUSTOM_COMPONENTS_PATH = exports.ROOT_CONFIG_RELATIVE_PATH = exports.RESOURCE_CONFIG_PATH = exports.TOKEN_CONFIG_PATH = exports.SUPERBLOCKS_HOME_FOLDER_NAME = void 0;
4
4
  exports.SUPERBLOCKS_HOME_FOLDER_NAME = ".superblocks";
5
5
  exports.TOKEN_CONFIG_PATH = ".superblocks/auth.json";
6
6
  exports.RESOURCE_CONFIG_PATH = ".superblocks/superblocks.json";
@@ -33,3 +33,17 @@ class NotFoundError extends Error {
33
33
  }
34
34
  }
35
35
  exports.NotFoundError = NotFoundError;
36
+ class BadRequestError extends Error {
37
+ constructor(message) {
38
+ super(message);
39
+ this.name = "BadRequestError";
40
+ }
41
+ }
42
+ exports.BadRequestError = BadRequestError;
43
+ class ForbiddenError extends Error {
44
+ constructor(message) {
45
+ super(message);
46
+ this.name = "ForbiddenError";
47
+ }
48
+ }
49
+ exports.ForbiddenError = ForbiddenError;
package/dist/events.d.ts CHANGED
@@ -7,5 +7,6 @@ export declare enum ComponentEvent {
7
7
  COMMITS = "commits",
8
8
  PUSH = "push",
9
9
  REGISTER = "register",
10
- UPLOAD = "upload"
10
+ UPLOAD = "upload",
11
+ DEPLOY = "deploy"
11
12
  }
package/dist/events.js CHANGED
@@ -12,4 +12,5 @@ var ComponentEvent;
12
12
  ComponentEvent["PUSH"] = "push";
13
13
  ComponentEvent["REGISTER"] = "register";
14
14
  ComponentEvent["UPLOAD"] = "upload";
15
+ ComponentEvent["DEPLOY"] = "deploy";
15
16
  })(ComponentEvent || (exports.ComponentEvent = ComponentEvent = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/util",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "main": "dist/index.js",
5
5
  "homepage": "https://www.superblocks.com",
6
6
  "license": "Superblocks Community Software License",
@@ -1,63 +1,55 @@
1
1
  import { execFile } from "node:child_process";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
+ import * as util from "node:util";
4
5
  import * as fs from "fs-extra";
5
6
  import * as mimeTypes from "mime-types";
6
7
  import { CUSTOM_COMPONENTS_PATH } from "./constants";
7
8
  import { generateComponentTypesFile } from "./generate-component-types";
8
9
  import { validateCustomComponents } from "./validation";
9
10
 
10
- const compileTypeScript = (inputFilePath: string, outputDir: string) => {
11
- return new Promise((resolve, reject) => {
12
- try {
13
- const tscPath = require.resolve("typescript/bin/tsc");
14
- if (tscPath) {
15
- execFile(
16
- "node",
17
- [
18
- tscPath,
19
- path.resolve(inputFilePath),
20
- "--outDir",
21
- outputDir,
22
- "--jsx",
23
- "react-jsx",
24
- "--target",
25
- "es2019",
26
- "--esModuleInterop",
27
- "true",
28
- "--moduleResolution",
29
- "node",
30
- "--module",
31
- "commonjs",
32
- "--strict",
33
- "true",
34
- "--skipLibCheck",
35
- "true",
36
- ],
37
- (error, stdout, stderr) => {
38
- if (error) {
39
- // Print any Typescript output to the user, such as errors
40
- console.log(stdout);
41
- reject(`Compilation failed: ${error.message}`);
42
- return;
43
- }
44
- if (stderr) {
45
- reject(`Error during compilation: ${stderr}`);
46
- return;
47
- }
48
- resolve("Compilation successful!");
49
- },
50
- );
11
+ const asyncExecFile = util.promisify(execFile);
12
+
13
+ async function compileTypeScript(
14
+ inputFilePath: string,
15
+ outputDir: string,
16
+ ): Promise<{ output?: string }> {
17
+ try {
18
+ const tscPath = require.resolve("typescript/bin/tsc");
19
+ if (tscPath) {
20
+ const result = await asyncExecFile("node", [
21
+ tscPath,
22
+ path.resolve(inputFilePath),
23
+ "--outDir",
24
+ outputDir,
25
+ "--jsx",
26
+ "react-jsx",
27
+ "--target",
28
+ "es2019",
29
+ "--esModuleInterop",
30
+ "true",
31
+ "--moduleResolution",
32
+ "node",
33
+ "--module",
34
+ "commonjs",
35
+ "--strict",
36
+ "true",
37
+ "--skipLibCheck",
38
+ "true",
39
+ ]);
40
+ if (result.stdout || result.stderr) {
41
+ return {
42
+ output: result.stdout || result.stderr,
43
+ };
51
44
  }
52
- } catch {
53
- reject(
54
- new Error(
55
- "Could not find TypeScript, please install it globally (npm install -g typescript)",
56
- ),
57
- );
58
45
  }
59
- });
60
- };
46
+ } catch (error: any) {
47
+ return {
48
+ output: error.stdout || error.stderr,
49
+ };
50
+ }
51
+ return {};
52
+ }
61
53
 
62
54
  async function getFolderPaths() {
63
55
  try {
@@ -102,38 +94,43 @@ export async function getComponentConfigs(
102
94
  } catch {
103
95
  // Noop because there is a fallback
104
96
  }
105
-
106
- try {
107
- // This is a tad complex to explain this hack but here's the context:
108
- // We want to be able to hot reload the config.ts file, but we can't import it directly
109
- // because it is going to get cached in the module cache and we dont have access to it to clear it
110
- // so instead we copy the file to a new location with a random name and import that
111
- // this is technically a memory leak, but you shouldnt really have a hot module server running forever
112
- const rawPath = `${ccpath}/config.ts`;
113
- const fileName = path.resolve(rawPath);
114
- const outputDirectory = path.resolve(
115
- `${os.tmpdir()}/superblocks/${ccpath}/config.timestamp-${Date.now()}-${Math.random()
116
- .toString(16)
117
- .slice(2)}`,
118
- );
119
- await fs.ensureDir(outputDirectory);
120
- await compileTypeScript(fileName, outputDirectory);
121
- console.log(`Typescript compiled to ${ccpath}`);
122
- const config = (await import(outputDirectory + "/config.js")).default;
123
-
124
- const isValid = validateCustomComponents(config);
125
- if (!isValid.valid) {
126
- // Not throwing because we don't need a stack trace here
127
- console.log(`Invalid config.ts file found`);
128
- console.log(isValid.message);
129
- hasError = true;
97
+ // This is a tad complex to explain this hack but here's the context:
98
+ // We want to be able to hot reload the config.ts file, but we can't import it directly
99
+ // because it is going to get cached in the module cache and we dont have access to it to clear it
100
+ // so instead we copy the file to a new location with a random name and import that
101
+ // this is technically a memory leak, but you shouldnt really have a hot module server running forever
102
+ const rawPath = `${ccpath}/config.ts`;
103
+ const fileName = path.resolve(rawPath);
104
+ const outputDirectory = path.resolve(
105
+ `${os.tmpdir()}/superblocks/${ccpath}/config.timestamp-${Date.now()}-${Math.random()
106
+ .toString(16)
107
+ .slice(2)}`,
108
+ );
109
+ await fs.ensureDir(outputDirectory);
110
+ const compilationResult = await compileTypeScript(
111
+ fileName,
112
+ outputDirectory,
113
+ );
114
+ if (compilationResult.output) {
115
+ hasError = true;
116
+ if (compilationResult.output.includes(`Cannot find module`)) {
117
+ throw new Error(
118
+ "Could not find a module in node_modules. You likely need to run 'npm install'",
119
+ );
130
120
  }
131
- return { ccpath, config };
132
- } catch (e: any) {
133
- console.error(e);
134
- console.error(e.message);
135
- throw e;
121
+ throw new Error(compilationResult.output);
122
+ }
123
+ console.log(`Typescript compiled to ${ccpath}`);
124
+ const config = (await import(outputDirectory + "/config.js")).default;
125
+
126
+ const isValid = validateCustomComponents(config);
127
+ if (!isValid.valid) {
128
+ // Not throwing because we don't need a stack trace here
129
+ console.log(`Invalid config.ts file found`);
130
+ console.log(isValid.message);
131
+ hasError = true;
136
132
  }
133
+ return { ccpath, config };
137
134
  });
138
135
  const configFiles = await Promise.all(promiseConfigFiles);
139
136
  if (generateTypesFiles) {
package/src/constants.ts CHANGED
@@ -32,3 +32,17 @@ export class NotFoundError extends Error {
32
32
  this.name = "FileNotFoundError";
33
33
  }
34
34
  }
35
+
36
+ export class BadRequestError extends Error {
37
+ constructor(message: string) {
38
+ super(message);
39
+ this.name = "BadRequestError";
40
+ }
41
+ }
42
+
43
+ export class ForbiddenError extends Error {
44
+ constructor(message: string) {
45
+ super(message);
46
+ this.name = "ForbiddenError";
47
+ }
48
+ }
package/src/events.ts CHANGED
@@ -8,4 +8,5 @@ export enum ComponentEvent {
8
8
  PUSH = "push",
9
9
  REGISTER = "register",
10
10
  UPLOAD = "upload",
11
+ DEPLOY = "deploy",
11
12
  }