-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathclient.utils.ts
More file actions
136 lines (107 loc) · 3.46 KB
/
client.utils.ts
File metadata and controls
136 lines (107 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Buffer } from "buffer";
import type { ShellCommandOptions } from "@codesandbox/nodebox/build/modules/shell";
import { invariant } from "outvariant";
import type { SandpackBundlerFiles } from "../..";
import { createError } from "../..";
import { tokenize, TokenType } from "./taskManager";
let counter = 0;
export function generateRandomId() {
const now = Date.now();
const randomNumber = Math.round(Math.random() * 10000);
const count = (counter += 1);
return (+`${now}${randomNumber}${count}`).toString(16);
}
export const writeBuffer = (
content: string | Uint8Array,
encoding: BufferEncoding = "utf8"
): Uint8Array => {
if (typeof content === "string") {
return Buffer.from(content, encoding);
} else {
return content;
}
};
export const readBuffer = (content: string | Uint8Array): string => {
if (typeof content === "string") return content;
return Buffer.from(content).toString("utf-8");
};
export const fromBundlerFilesToFS = (
files: SandpackBundlerFiles
): Record<string, Uint8Array> => {
return Object.entries(files).reduce<Record<string, Uint8Array>>(
(acc, [key, value]) => {
acc[key] = writeBuffer(value.code);
return acc;
},
{}
);
};
type Command = [string, string[], ShellCommandOptions];
/**
* Figure out which script it must run to start a server
*/
export const findStartScriptPackageJson = (packageJson: string): Command[] => {
let scripts: Record<string, string> = {};
// TODO: support postinstall
const possibleKeys = ["dev", "start"];
try {
scripts = JSON.parse(packageJson).scripts;
} catch (e) {
throw createError(
"Could not parse package.json file: " + (e as Error).message
);
}
invariant(
scripts,
"Failed to start. Please provide a `start` or `dev` script on the package.json"
);
for (let index = 0; index < possibleKeys.length; index++) {
if (possibleKeys[index] in scripts) {
const script = possibleKeys[index];
const candidate = scripts[script];
const listOfCommands: Command[] = [];
const commandTokens = tokenize(candidate);
let env = {};
let command = "";
let args: string[] = [];
commandTokens.forEach((token, tokenIndex) => {
const commandNotFoundYet = command === "";
if (token.type === TokenType.EnvVar) {
env = token.value;
}
if (token.type === TokenType.Command && commandNotFoundYet) {
command = token.value;
}
if (
token.type === TokenType.Argument ||
(!commandNotFoundYet && token.type === TokenType.Command)
) {
args.push(token.value);
}
if (
token.type === TokenType.AND ||
tokenIndex === commandTokens.length - 1
) {
const nodeboxCommand: Command = [command, args, { env }];
listOfCommands.push(nodeboxCommand);
command = "";
args = [];
}
// TODO: support TokenType.OR, TokenType.PIPE
});
return listOfCommands;
}
}
throw createError(
"Failed to start. Please provide a `start` or `dev` script on the package.json"
);
};
export const getMessageFromError = (error: Error | string): string => {
if (typeof error === "string") return error;
if (typeof error === "object" && "message" in error) {
return error.message;
}
return createError(
"The server could not be reached. Make sure that the node script is running and that a port has been started."
);
};