@nethreed/make 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +72 -1
  2. package/package.json +14 -3
  3. package/source/make.mjs +144 -23
package/README.md CHANGED
@@ -1,7 +1,78 @@
1
1
  # `MAKE`
2
2
  > NodeJS CLI Make Command Scripts
3
3
 
4
- Project Overview
4
+ ## Usage
5
+
6
+ ### Install
7
+
8
+ ```sh
9
+ npm install @nethreed/make
10
+ ```
11
+
12
+ ### Example
13
+
14
+ - `scripts.js`: file with scripts
15
+
16
+ ```mjs
17
+ import { stdout } from "node:process"
18
+ import { Make, Text, Console } from "@nethreed/make"
19
+
20
+ Make.commands({
21
+ // `usage` is built in automatically; add project-specific commands below
22
+ "hello-world": { run( ) { Make.exec( "node", ["-e", "console.log( 'hello, world' )"] ); }, },
23
+ example: {
24
+ info: "check Console and Text formatting helpers",
25
+ run( ) {
26
+ // basic styled console helpers
27
+ Console.failure( "printed to stderr in red" );
28
+ Console.success( "printed to stdout in green" );
29
+ Console.comment( "printed to stdout in grey" );
30
+ // formatted output (redirects to stderr if `f` or `w` was active during formatting):
31
+ // \ _ : default (also used for any extra arguments)
32
+ // \ c : comment (grey)
33
+ // \ i : inspect (magenta)
34
+ // \ h : heading (blue)
35
+ // \ k : keyword (cyan)
36
+ // \ s : success (green)
37
+ // \ w : warning (yellow, redirects to stderr)
38
+ // \ f : failure (red, redirects to stderr)
39
+ const failedCode = 3;
40
+ Console.format( "_cf", "status: ", "warning/error code ", failedCode, " (fallback default)" );
41
+ // use uppercase `F` / `W` to print `red` / `yellow` text to `stdout` without redirecting to `stderr`
42
+ Console.format( "_F", "stdout only: ", "red text on stdout" );
43
+ // or call Text helpers directly and override the stream (`stderr` is default for `w` and `f`)
44
+ const yellowText = Text.warning( "yellow text checked against stdout TTY", stdout );
45
+ Console.default( yellowText );
46
+ },
47
+ },
48
+ "error-status": {
49
+ info: "check executing missing system command",
50
+ run( ) { Make.exec( "this-command-does-not-exist" ); },
51
+ },
52
+ "node-failed": {
53
+ info: "check subprocess exit with custom status code",
54
+ run( ) { Make.exec( "node", ["-e", "process.exit( 42 )"] ); },
55
+ },
56
+ "node-throw": {
57
+ info: "check subprocess throw uncaught error",
58
+ run( ) { Make.exec( "node", ["-e", "throw new Error( 'boom' )"] ); },
59
+ },
60
+ });
61
+ ```
62
+
63
+ - `package.json`: add scripts to package file, and invoke them with `node --run [command]`
64
+
65
+ ```json
66
+ "scripts": {
67
+ "usage": "node scripts.js usage",
68
+ "example": "node scripts.js example",
69
+ "hello-world": "node scripts.js hello-world",
70
+ "error-status": "node scripts.js error-status",
71
+ "node-failed": "node scripts.js node-failed",
72
+ "node-throw": "node scripts.js node-throw",
73
+ "not-exist": "node scripts.js script-not-exist"
74
+ }
75
+ ```
5
76
 
6
77
  ## [CONTRIBUTING](./CONTRIBUTING.md)
7
78
  > Project Contribution Rules
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@nethreed/make",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "Apache-2.0",
5
+ "author": "Oleg'Ease'Kharchuk",
5
6
  "description": "CLI Makefile Replacement Command Scripts",
6
- "exports": "./source/make.mjs",
7
7
  "type": "module",
8
+ "exports": "./source/make.mjs",
8
9
  "files": [
10
+ "LICENSE",
9
11
  "NOTICE",
12
+ "README.md",
10
13
  "licenses",
11
14
  "source"
12
15
  ],
@@ -21,12 +24,20 @@
21
24
  "url": "git+https://github.com/nethreed/make.git"
22
25
  },
23
26
  "keywords": [
27
+ "beta",
24
28
  "cli",
25
29
  "command",
30
+ "dev",
26
31
  "make",
27
32
  "scripts"
28
33
  ],
29
34
  "scripts": {
30
- "usage": "node scripts.js usage"
35
+ "usage": "node scripts.js usage",
36
+ "example": "node scripts.js example",
37
+ "hello-world": "node scripts.js hello-world",
38
+ "error-status": "node scripts.js error-status",
39
+ "node-failed": "node scripts.js node-failed",
40
+ "node-throw": "node scripts.js node-throw",
41
+ "not-exist": "node scripts.js script-not-exist"
31
42
  }
32
43
  }
package/source/make.mjs CHANGED
@@ -1,36 +1,124 @@
1
- import { execFileSync } from "node:child_process"
2
- import { argv, exit } from "node:process"
3
- import { styleText } from "node:util"
1
+ import { execFileSync } from "node:child_process"
2
+ import { argv, exit, stderr, stdout } from "node:process"
3
+ import { styleText } from "node:util"
4
4
 
5
5
  const [, , target = "usage", ...extraArgs] = argv;
6
-
6
+ // magic values
7
+ // -- numbers
8
+ const Zero = 0;
7
9
  const Exit_Success = 0;
8
10
  const Exit_Failure = 1;
11
+ // -- color strings
12
+ const Color_Comment = "grey";
13
+ const Color_Failure = "red";
14
+ const Color_Success = "green";
15
+ const Color_Warning = "yellow";
16
+ const Color_Keyword = "cyan";
17
+ const Color_Heading = "blue";
18
+ const Color_Inspect = "magenta";
19
+
20
+ const Max_Usage_Padding_Length = 16;
21
+ // export
22
+ export function text_default( text ) { return String( text ); }
23
+ export function text_comment( text, stream = stdout ) { return styleText( [Color_Comment], String( text ), { stream }); }
24
+ export function text_heading( text, stream = stdout ) { return styleText( [Color_Heading], String( text ), { stream }); }
25
+ export function text_inspect( text, stream = stdout ) { return styleText( [Color_Inspect], String( text ), { stream }); }
26
+ export function text_keyword( text, stream = stdout ) { return styleText( [Color_Keyword], String( text ), { stream }); }
27
+ export function text_success( text, stream = stdout ) { return styleText( [Color_Success], String( text ), { stream }); }
28
+ export function text_warning( text, stream = stderr ) { return styleText( [Color_Warning], String( text ), { stream }); }
29
+ export function text_failure( text, stream = stderr ) { return styleText( [Color_Failure], String( text ), { stream }); }
30
+
31
+ export function console_default( text ) { console.log( text_default( text )); }
32
+ export function console_comment( text ) { console.log( text_comment( text )); }
33
+ export function console_inspect( text ) { console.log( text_inspect( text )); }
34
+ export function console_heading( text ) { console.log( text_heading( text )); }
35
+ export function console_keyword( text ) { console.log( text_keyword( text )); }
36
+ export function console_success( text ) { console.log( text_success( text )); }
37
+ export function console_warning( text ) { console.warn( text_warning( text )); }
38
+ export function console_failure( text ) { console.error( text_failure( text )); }
39
+
40
+ const Text_Format_Map = Object.freeze({
41
+ _: text_default,
42
+ c: text_comment,
43
+ i: text_inspect,
44
+ h: text_heading,
45
+ k: text_keyword,
46
+ s: text_success,
47
+ w: text_warning,
48
+ f: text_failure,
49
+ // NOTE: does not redirect formatting to `stderr` by default
50
+ W: text_warning, // `console.warn`
51
+ F: text_failure, // `console.error`
52
+ });
53
+
54
+ const OutputMode_ = Object.freeze({
55
+ Default: Symbol( "Default" ),
56
+ Warning: Symbol( "Warning" ),
57
+ Failure: Symbol( "Failure" ),
58
+ });
59
+
60
+ function impl_Output_Mode( formatString ) {
61
+ if ( formatString.includes( 'f' )) { return OutputMode_.Failure; }
62
+ if ( formatString.includes( 'w' )) { return OutputMode_.Warning; }
63
+ return OutputMode_.Default;
64
+ }
9
65
 
10
- export function ne3_console_log( text ) { console.log( text ); }
11
- export function ne3_console_comment( text ) { console.log( styleText( ["grey"], text )); }
12
- export function ne3_console_error( text ) { console.error( styleText( ["red"], text )); }
66
+ function impl_Text_Format( formatString, textStrings ) {
67
+ const minCount = Math.min( formatString.length, textStrings.length );
68
+ const mode = impl_Output_Mode( formatString.slice( Zero, minCount ));
69
+ const stream = ( mode === OutputMode_.Default ) ? stdout : stderr;
70
+ let text = "";
71
+
72
+ for ( let i = Zero; i < minCount; ++i ) {
73
+ const formatFunc = Text_Format_Map[formatString[i]] ?? text_default;
74
+ text += formatFunc( String( textStrings[i] ), stream );
75
+ }
76
+
77
+ for ( let i = minCount; i < textStrings.length; ++i ) {
78
+ text += text_default( String( textStrings[i] ));
79
+ }
80
+
81
+ return { mode, text };
82
+ }
83
+
84
+ export function text_format( formatString = "", ...textStrings ) {
85
+ return impl_Text_Format( formatString, textStrings ).text;
86
+ }
13
87
 
14
- export function ne3_exec( command, args = [], extra = extraArgs ) { try {
15
- execFileSync( command, [...args, ...extra ], {
88
+ export function console_format( formatString = "", ...textStrings ) {
89
+ const { mode, text } = impl_Text_Format( formatString, textStrings );
90
+
91
+ switch ( mode ) {
92
+ case OutputMode_.Warning: { console.warn( text ); } break;
93
+ case OutputMode_.Failure: { console.error( text ); } break;
94
+ default: {
95
+ console.log( text );
96
+ } break;
97
+ }
98
+ }
99
+
100
+ export function exec( command, args = [], extra = extraArgs ) { try {
101
+ execFileSync( command, [...args, ...extra], {
16
102
  stdio: "inherit",
17
103
  });
18
104
  } catch ( error ) {
19
- ne3_console_error( error.message );
20
- exit( error.status ?? Exit_Failure );
105
+ const errorStatus = error.status ?? Exit_Failure;
106
+ console_format( "f_W_f", "failure command run ", "'", command, `'\n${error.message} error status `, errorStatus );
107
+ exit( errorStatus );
21
108
  }}
22
109
 
23
- export async function ne3_commands( extraCommands ) {
110
+ export async function commands( extraCommands ) {
24
111
  const commands = {
25
112
  usage: {
26
113
  info: "show available project commands",
27
114
  run( ) {
28
- ne3_console_log( "usage: node --run [command]:" );
115
+ console_format( "_kcWc", "usage: ", "node --run ", "[", "command", "]" );
29
116
  const entries = Object.entries( commands );
30
- const maxLength = Math.max( 0, ...entries.map(( [name] ) => Math.min( name.length, 16 )));
117
+ const maxLength = Math.max( Zero, ...entries.map(( [name] ) => Math.min( name.length, Max_Usage_Padding_Length )));
31
118
 
32
119
  for ( const [name, command] of entries ) {
33
- ne3_console_log( `\t${name.padEnd( maxLength )}: ${command?.info ?? ""}` );
120
+ if ( typeof command?.run !== "function" ) { continue; }
121
+ console_format( "_W_c", "\t", name.padEnd( maxLength ), ": ", command?.info ?? "" );
34
122
  }
35
123
  },
36
124
  },
@@ -40,22 +128,55 @@ export async function ne3_commands( extraCommands ) {
40
128
  const command = Object.hasOwn( commands, target ) ? commands[target] : undefined;
41
129
 
42
130
  if ( !command ) {
43
- ne3_console_error( `unknown command: ${target}` );
131
+ console_format( "f_i", "unknown command ", "'", target, "'" );
132
+ commands.usage.run( );
133
+ exit( Exit_Failure );
134
+ }
135
+
136
+ if ( typeof command.run !== "function" ) {
137
+ console_format( "f_w", "not runnable command ", "'", target, "'" );
44
138
  commands.usage.run( );
45
139
  exit( Exit_Failure );
46
140
  }
47
141
 
48
- ne3_console_comment( command.info );
142
+ if ( command.info ) { console_comment( command.info ); }
143
+
49
144
  await command.run( extraArgs );
145
+
146
+ if ( target !== "usage" ) {
147
+ console_format( "s_W", "success command run ", "'", target, "'" );
148
+ }
149
+
50
150
  exit( Exit_Success );
51
151
  }
52
152
 
53
- export const Ne3Make = Object.freeze({
54
- console_log : ne3_console_log,
55
- console_error : ne3_console_error,
56
- console_comment : ne3_console_comment,
57
- exec : ne3_exec,
58
- commands : ne3_commands,
153
+ export const Text = Object.freeze({
154
+ format : text_format,
155
+ default : text_default,
156
+ comment : text_comment,
157
+ inspect : text_inspect,
158
+ heading : text_heading,
159
+ keyword : text_keyword,
160
+ success : text_success,
161
+ warning : text_warning,
162
+ failure : text_failure,
163
+ });
164
+
165
+ export const Console = Object.freeze({
166
+ format : console_format,
167
+ default : console_default,
168
+ comment : console_comment,
169
+ inspect : console_inspect,
170
+ heading : console_heading,
171
+ keyword : console_keyword,
172
+ success : console_success,
173
+ warning : console_warning,
174
+ failure : console_failure,
175
+ });
176
+
177
+ export const Make = Object.freeze({
178
+ commands : commands,
179
+ exec : exec,
59
180
  });
60
181
 
61
182
  // Apache License 2.0