whet 0.0.17 → 0.0.18

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.
@@ -0,0 +1,98 @@
1
+
2
+ export type PrettyOptions = {
3
+ /**
4
+ Opens the file with the 'a' flag.
5
+ */
6
+ append?: null | boolean,
7
+ /**
8
+ If set to true, will add color information to the formatted output message.
9
+ */
10
+ colorize?: null | boolean,
11
+ /**
12
+ Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
13
+ */
14
+ crlf?: null | boolean,
15
+ /**
16
+ Provides the ability to add a custom prettify function for specific log properties.
17
+ `customPrettifiers` is an object, where keys are log properties that will be prettified
18
+ and value is the prettify function itself.
19
+ For example, if a log line contains a query property, you can specify a prettifier for it:
20
+ */
21
+ customPrettifiers?: null | {
22
+ },
23
+ /**
24
+ The file, file descriptor, or stream to write to. Defaults to 1 (stdout).
25
+ */
26
+ destination?: null | any,
27
+ /**
28
+ Define the log keys that are associated with error like objects.
29
+ */
30
+ errorLikeObjectKeys?: null | string[],
31
+ /**
32
+ When formatting an error object, display this list of properties.
33
+ The list should be a comma separated list of properties.
34
+ */
35
+ errorProps?: null | string,
36
+ /**
37
+ Hide objects from output (but not error object).
38
+ */
39
+ hideObject?: null | boolean,
40
+ /**
41
+ Ignore one or several keys.
42
+ Will be overridden by the option include if include is presented.
43
+ */
44
+ ignore?: null | string,
45
+ /**
46
+ Include one or several keys.
47
+ */
48
+ include?: null | string,
49
+ /**
50
+ If set to true, it will print the name of the log level as the first field in the log line.
51
+ */
52
+ levelFirst?: null | boolean,
53
+ /**
54
+ Define the key that contains the level of the log.
55
+ */
56
+ levelKey?: null | string,
57
+ /**
58
+ Output the log level using the specified label.
59
+ */
60
+ levelLabel?: null | string,
61
+ /**
62
+ Format output of message, e.g. {level} - {pid} will output message: INFO - 1123
63
+ */
64
+ messageFormat?: null | any,
65
+ /**
66
+ The key in the JSON object to use as the highlighted message.
67
+ */
68
+ messageKey?: null | string,
69
+ /**
70
+ The minimum log level to include in the output.
71
+ */
72
+ minimumLevel?: null | any,
73
+ /**
74
+ Ensure directory for destination file exists.
75
+ */
76
+ mkdir?: null | boolean,
77
+ /**
78
+ Print each log message on a single line (errors will still be multi-line).
79
+ */
80
+ singleLine?: null | boolean,
81
+ /**
82
+ Makes messaging synchronous.
83
+ */
84
+ sync?: null | boolean,
85
+ /**
86
+ The key in the JSON object to use for timestamp display.
87
+ */
88
+ timestampKey?: null | string,
89
+ /**
90
+ Translate the epoch time value into a human readable date and time string. This flag also can set the format
91
+ string to apply when translating the date to human readable format. For a list of available pattern letters
92
+ see the {@link https://www.npmjs.com/package/dateformat|dateformat documentation}.
93
+ - The default format is `yyyy-mm-dd HH:MM:ss.l o` in UTC.
94
+ - Requires a `SYS:` prefix to translate time to the local system's timezone. Use the shortcut `SYS:standard`
95
+ to translate time to `yyyy-mm-dd HH:MM:ss.l o` in system timezone.
96
+ */
97
+ translateTime?: null | any
98
+ }
@@ -0,0 +1,3 @@
1
+
2
+ export type MessageFormatFunc = ((log: {
3
+ }, messageKey: string, levelLabel: string) => string)
package/bin/whet/Log.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare class Log {
7
7
  static error(...args: any[]): void
8
8
  static fatal(...args: any[]): void
9
9
  static logLevel: number
10
+ static stream: IWritable
10
11
  protected static log(level: number, ...args: any[]): void
11
12
  protected static replacer(key: any, val: any): any
12
13
  }
package/bin/whet/Log.js CHANGED
@@ -50,12 +50,12 @@ class Log {
50
50
  };
51
51
  };
52
52
  };
53
- process.stdout.write(JSON.stringify(out, Log.replacer) + "\n");
53
+ Log.stream.write(JSON.stringify(out, Log.replacer) + "\n");
54
54
  };
55
55
  }
56
56
  static replacer(key, val) {
57
57
  if (((val) instanceof Error)) {
58
- return val.stack;
58
+ return {"type": val.name, "message": val.message, "stack": val.stack};
59
59
  };
60
60
  if (val != null && typeof(val.toString) == "function" && val.toString != Object.prototype.toString) {
61
61
  return val.toString();
@@ -72,6 +72,7 @@ class Log {
72
72
 
73
73
 
74
74
  Log.logLevel = 30
75
+ Log.stream = process.stdout
75
76
  export const LogLevel = Register.global("$hxClasses")["whet._Log.LogLevel"] =
76
77
  class LogLevel {
77
78
  static fromString(s) {
package/bin/whet/Stone.js CHANGED
@@ -161,6 +161,7 @@ class Stone extends Register.inherits() {
161
161
  * @param err Any error that might have happened during `generateSource`.
162
162
  */
163
163
  handleError(err) {
164
+ Log.log(50, ...["Error while generating.", {"stone": this, "err": err}]);
164
165
  return Promise.reject(err);
165
166
  }
166
167
 
@@ -2,3 +2,4 @@ import {Command} from "commander"
2
2
 
3
3
  export const program: Command
4
4
  export const main: () => void
5
+ export const executeCommand: (cmd: string[]) => Promise<Command>
package/bin/whet/Whet.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {Project} from "./Project.js"
2
2
  import {LogLevel, Log} from "./Log.js"
3
3
  import * as Url from "url"
4
+ import * as PinoPretty from "pino-pretty"
4
5
  import {Register} from "../genes/Register.js"
5
6
  import {Command, CommanderError} from "commander"
6
7
  import {Std} from "../Std.js"
@@ -10,7 +11,7 @@ const $global = Register.$global
10
11
  export const Whet_Fields_ = Register.global("$hxClasses")["whet._Whet.Whet_Fields_"] =
11
12
  class Whet_Fields_ {
12
13
  static main() {
13
- Whet_Fields_.program.enablePositionalOptions().passThroughOptions().description("Project tooling.").usage("[options] [command] [+ [command]...]").version("0.0.17", "-v, --version").allowUnknownOption(true).showSuggestionAfterError(true).option("-p, --project <file>", "project to run", "Project.mjs").option("-l, --log-level <level>", "log level, a string/number", "info").exitOverride();
14
+ Whet_Fields_.program.enablePositionalOptions().passThroughOptions().description("Project tooling.").usage("[options] [command] [+ [command]...]").version("0.0.18", "-v, --version").allowUnknownOption(true).showSuggestionAfterError(true).option("-p, --project <file>", "project to run", "Project.mjs").option("-l, --log-level <level>", "log level, a string/number", "info").option("--no-pretty", "disable pretty logging").exitOverride();
14
15
  Whet_Fields_.program.parse();
15
16
  var options = Whet_Fields_.program.opts();
16
17
  if (options.logLevel != null) {
@@ -24,6 +25,9 @@ class Whet_Fields_ {
24
25
  Log.logLevel = n;
25
26
  };
26
27
  };
28
+ if (options.pretty) {
29
+ Log.stream = PinoPretty["default"]();
30
+ };
27
31
  global.setImmediate(Whet_Fields_.init, options);
28
32
  }
29
33
  static init(options) {
@@ -41,7 +45,10 @@ class Whet_Fields_ {
41
45
  if (((e) instanceof Error)) {
42
46
  Log.log(50, ...[e.stack]);
43
47
  };
44
- return Whet_Fields_.program.help();
48
+ try {
49
+ Whet_Fields_.program.help();
50
+ }catch (_g) {
51
+ };
45
52
  });
46
53
  };
47
54
  }
@@ -98,6 +105,10 @@ class Whet_Fields_ {
98
105
  nextCommand();
99
106
  });
100
107
  }
108
+ static executeCommand(cmd) {
109
+ Log.log(10, ...["Executing command.", {"command": cmd}]);
110
+ return Whet_Fields_.program.parseAsync(cmd, {"from": "user"});
111
+ }
101
112
  static getCommands(args) {
102
113
  var commands = [];
103
114
  var from = 0;
@@ -124,3 +135,4 @@ class Whet_Fields_ {
124
135
  Whet_Fields_.program = new Command("whet")
125
136
  export const program = Whet_Fields_.program
126
137
  export const main = Whet_Fields_.main
138
+ export const executeCommand = Whet_Fields_.executeCommand
@@ -42,6 +42,7 @@ export type ServerConfig = {
42
42
  * Do not create cyclic dependencies!
43
43
  */
44
44
  dependencies?: null | MaybeArray<AnyStone>,
45
+ headers?: null | {[key: string]: string},
45
46
  /**
46
47
  Defaults to the Stone's class name.
47
48
  */
@@ -1,3 +1,4 @@
1
+ import {Whet_Fields_} from "../Whet.js"
1
2
  import {Stone} from "../Stone.js"
2
3
  import {Log} from "../Log.js"
3
4
  import * as Path from "path"
@@ -6,6 +7,7 @@ import * as Http from "http"
6
7
  import {Register} from "../../genes/Register.js"
7
8
  import {StringTools} from "../../StringTools.js"
8
9
  import {Std} from "../../Std.js"
10
+ import {Reflect as Reflect__1} from "../../Reflect.js"
9
11
  import {HxOverrides} from "../../HxOverrides.js"
10
12
 
11
13
  const $global = Register.$global
@@ -75,41 +77,62 @@ class Server extends Register.inherits(Stone) {
75
77
  s = str;
76
78
  id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
77
79
  };
78
- if (req.method == "GET") {
79
- if (HxOverrides.cca(id, id.length - 1) == 47) {
80
- if ("index.html".length > 0) {
81
- var s = id.substring(0, id.lastIndexOf("/") + 1);
80
+ switch (req.method) {
81
+ case "GET":
82
+ if (HxOverrides.cca(id, id.length - 1) == 47) {
83
+ if ("index.html".length > 0) {
84
+ var s = id.substring(0, id.lastIndexOf("/") + 1);
85
+ var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
86
+ if (str.length > 0) {
87
+ str = Path.posix.normalize(str);
88
+ str = StringTools.replace(str, "\\", "/");
89
+ };
90
+ s = str;
91
+ id = ((HxOverrides.cca(s, 0) == 47) ? s : "/" + s) + "index.html";
92
+ };
93
+ } else if (Path.posix.extname(id) == "") {
94
+ var s = "" + id + "/index.html";
82
95
  var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
83
96
  if (str.length > 0) {
84
97
  str = Path.posix.normalize(str);
85
98
  str = StringTools.replace(str, "\\", "/");
86
99
  };
87
100
  s = str;
88
- id = ((HxOverrides.cca(s, 0) == 47) ? s : "/" + s) + "index.html";
89
- };
90
- } else if (Path.posix.extname(id) == "") {
91
- var s = "" + id + "/index.html";
92
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
93
- if (str.length > 0) {
94
- str = Path.posix.normalize(str);
95
- str = StringTools.replace(str, "\\", "/");
101
+ id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
96
102
  };
97
- s = str;
98
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
99
- };
100
- this.config.router.get(id).then(function (routeResult) {
101
- return ((routeResult.length > 0) ? routeResult[0].get() : (_gthis.routeDynamic != null) ? _gthis.routeDynamic(id) : Promise.resolve(null)).then(function (source) {
102
- if (source == null) {
103
- res.writeHead(404, "File not found.");
103
+ this.config.router.get(id).then(function (routeResult) {
104
+ return ((routeResult.length > 0) ? routeResult[0].get() : (_gthis.routeDynamic != null) ? _gthis.routeDynamic(id) : Promise.resolve(null)).then(function (source) {
105
+ if (source == null) {
106
+ res.writeHead(404, "File not found.");
107
+ res.end();
108
+ return;
109
+ };
110
+ var headers = {"Content-Type": Mime.getType(Path.posix.extname(id).toLowerCase()), "Last-Modified": new Date(source.source.ctime * 1000).toUTCString(), "Content-Length": Std.string(source.data.length), "Cache-Control": "no-store, no-cache"};
111
+ if (_gthis.config.headers != null) {
112
+ var access = _gthis.config.headers;
113
+ var _g_keys = Reflect__1.fields(access);
114
+ var _g_index = 0;
115
+ while (_g_index < _g_keys.length) {
116
+ var key = _g_keys[_g_index++];
117
+ headers[key] = access[key];
118
+ };
119
+ };
120
+ res.writeHead(200, headers);
121
+ res.write(source.data, "binary");
104
122
  res.end();
105
- return;
106
- };
107
- res.writeHead(200, {"Content-Type": Mime.getType(Path.posix.extname(id).toLowerCase()), "Last-Modified": new Date(source.source.ctime * 1000).toUTCString(), "Content-Length": Std.string(source.data.length), "Cache-Control": "no-store, no-cache"});
108
- res.write(source.data, "binary");
109
- res.end();
123
+ })["catch"](function (e) {
124
+ Log.log(40, ...["Server error.", {"error": e}]);
125
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
126
+ if (((e) instanceof Error)) {
127
+ res.write(e.stack, "utf-8");
128
+ } else {
129
+ res.write(Std.string(e), "utf-8");
130
+ };
131
+ res.end();
132
+ });
110
133
  })["catch"](function (e) {
111
134
  Log.log(40, ...["Server error.", {"error": e}]);
112
- res.writeHead(500, "Error happened.");
135
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
113
136
  if (((e) instanceof Error)) {
114
137
  res.write(e.stack, "utf-8");
115
138
  } else {
@@ -117,19 +140,52 @@ class Server extends Register.inherits(Stone) {
117
140
  };
118
141
  res.end();
119
142
  });
120
- })["catch"](function (e) {
121
- Log.log(40, ...["Server error.", {"error": e}]);
122
- res.writeHead(500, "Error happened.");
123
- if (((e) instanceof Error)) {
124
- res.write(e.stack, "utf-8");
125
- } else {
126
- res.write(Std.string(e), "utf-8");
127
- };
143
+ break
144
+ case "OPTIONS":
145
+ res.writeHead(200, this.config.headers);
128
146
  res.end();
129
- });
130
- } else {
147
+ break
148
+ case "PUT":
149
+ var s = "/";
150
+ var str = ("/".length > 1 && HxOverrides.cca("/", 0) == 47) ? "/".substring(1) : "/";
151
+ if (str.length > 0) {
152
+ str = Path.posix.normalize(str);
153
+ str = StringTools.replace(str, "\\", "/");
154
+ };
155
+ s = str;
156
+ var root = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
157
+ if (id.charAt(0) != "/") {
158
+ throw new Error("Badly formed SourceId.");
159
+ };
160
+ var cmd = [Path.posix.join(".", root, ".", id)];
161
+ var body = "";
162
+ req.on("data", function (chunk) {
163
+ body += chunk;
164
+ return body;
165
+ });
166
+ req.on("end", function () {
167
+ if (body != "") {
168
+ cmd.push(body);
169
+ };
170
+ return Whet_Fields_.executeCommand(cmd).then(function (_) {
171
+ res.writeHead(200, _gthis.config.headers);
172
+ res.end();
173
+ })["catch"](function (e) {
174
+ Log.log(40, ...["Server error.", {"error": e}]);
175
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
176
+ if (((e) instanceof Error)) {
177
+ res.write(e.stack, "utf-8");
178
+ } else {
179
+ res.write(Std.string(e), "utf-8");
180
+ };
181
+ res.end();
182
+ });
183
+ });
184
+ break
185
+ default:
131
186
  res.writeHead(400, "Unsupported method.");
132
187
  res.end();
188
+
133
189
  };
134
190
  }
135
191
  static get __name__() {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "whet",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "NodeJS based assets management and project tooling library.",
5
5
  "scripts": {
6
- "devinit": "npx dts2hx commander --modular --noLibWrap",
6
+ "devinit": "npx dts2hx commander pino-pretty --modular --noLibWrap",
7
7
  "build": "npx haxe build.hxml"
8
8
  },
9
9
  "repository": {
@@ -25,7 +25,8 @@
25
25
  "dependencies": {
26
26
  "commander": "^9.0.0",
27
27
  "mime": "^3.0.0",
28
- "minimatch": "^5.1.0"
28
+ "minimatch": "^5.1.0",
29
+ "pino-pretty": "^9.1.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/minimatch": "^5.1.0",