laravel-workers 0.4.0 → 0.4.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.
@@ -1,92 +1,92 @@
1
- // compiler/bin/controller.mjs — CLI for controller extraction/transpilation.
2
- //
3
- // Usage:
4
- // node controller.mjs extract --file app/Http/Controllers/PostController.php --method show [--out meta.json]
5
- // node controller.mjs emit --file app/Http/Controllers/PostController.php --method show --meta meta.json --out dist/src/controllers/posts-show.mjs
6
- //
7
- // extract prints {ok, fqcn, namespace, className, method, params, classes, views} on
8
- // stdout (or writes it with --out). emit writes the handler module atomically on
9
- // success. Both exit non-zero with a JSON error array [{code,message,line,column,hint}]
10
- // on stderr when the controller/method is outside the supported subset.
11
- import fs from 'node:fs';
12
- import path from 'node:path';
13
- import { extractController, emitController } from '../src/controller.mjs';
14
-
15
- const USAGE =
16
- 'usage: controller.mjs extract|emit --file <controller.php> --method <name> [--meta <meta.json>] [--out <file>]';
17
-
18
- function fail(errors) {
19
- process.stderr.write(JSON.stringify(errors));
20
- process.exit(1);
21
- }
22
-
23
- function usageError(message) {
24
- fail([{ code: 'LW004', message: message + ' — ' + USAGE, line: 0, column: 0, hint: '' }]);
25
- }
26
-
27
- const args = process.argv.slice(2);
28
- let cmd = null;
29
- let file = null;
30
- let method = null;
31
- let metaPath = null;
32
- let outPath = null;
33
- for (let i = 0; i < args.length; i++) {
34
- const a = args[i];
35
- if (a === 'extract' || a === 'emit') cmd = a;
36
- else if (a === '--file') file = args[++i];
37
- else if (a === '--method') method = args[++i];
38
- else if (a === '--meta') metaPath = args[++i];
39
- else if (a === '--out') outPath = args[++i];
40
- else usageError('unexpected argument ' + JSON.stringify(a));
41
- }
42
- if (!cmd) usageError('missing command (extract|emit)');
43
- if (!file) usageError('missing --file');
44
- if (!method) usageError('missing --method');
45
-
46
- const absFile = path.resolve(process.cwd(), file);
47
- if (!fs.existsSync(absFile)) usageError('file not found: ' + absFile);
48
- const source = fs.readFileSync(absFile, 'utf8');
49
-
50
- if (cmd === 'extract') {
51
- const info = extractController(source, method);
52
- if (!info.ok) fail(info.errors);
53
- const out = {
54
- ok: true,
55
- fqcn: info.fqcn,
56
- namespace: info.namespace,
57
- className: info.className,
58
- method: info.method,
59
- params: info.params,
60
- classes: info.classes,
61
- // The classes the body instantiates (`new OrderMail()`).
62
- newClasses: info.newClasses,
63
- constants: info.constants,
64
- views: info.views,
65
- // A paginator in the method + the kinds of the data the views receive.
66
- usesPaginator: info.usesPaginator,
67
- // A mail in the method: the build compiles the mailable's view.
68
- usesMail: info.usesMail,
69
- viewDataKinds: info.viewDataKinds,
70
- };
71
- const text = JSON.stringify(out);
72
- if (outPath) fs.writeFileSync(path.resolve(process.cwd(), outPath), text);
73
- else process.stdout.write(text);
74
- } else {
75
- if (!metaPath) usageError('emit requires --meta <file.json>');
76
- if (!outPath) usageError('emit requires --out <file.mjs>');
77
- const absMeta = path.resolve(process.cwd(), metaPath);
78
- if (!fs.existsSync(absMeta)) usageError('meta file not found: ' + absMeta);
79
- let meta;
80
- try {
81
- meta = JSON.parse(fs.readFileSync(absMeta, 'utf8'));
82
- } catch (e) {
83
- usageError('invalid meta JSON: ' + (e.message || String(e)));
84
- }
85
- const res = emitController(source, method, meta);
86
- if (res.errors.length) fail(res.errors);
87
- const absOut = path.resolve(process.cwd(), outPath);
88
- fs.mkdirSync(path.dirname(absOut), { recursive: true });
89
- const tmp = absOut + '.tmp-' + process.pid;
90
- fs.writeFileSync(tmp, res.code);
91
- fs.renameSync(tmp, absOut);
92
- }
1
+ // compiler/bin/controller.mjs — CLI for controller extraction/transpilation.
2
+ //
3
+ // Usage:
4
+ // node controller.mjs extract --file app/Http/Controllers/PostController.php --method show [--out meta.json]
5
+ // node controller.mjs emit --file app/Http/Controllers/PostController.php --method show --meta meta.json --out dist/src/controllers/posts-show.mjs
6
+ //
7
+ // extract prints {ok, fqcn, namespace, className, method, params, classes, views} on
8
+ // stdout (or writes it with --out). emit writes the handler module atomically on
9
+ // success. Both exit non-zero with a JSON error array [{code,message,line,column,hint}]
10
+ // on stderr when the controller/method is outside the supported subset.
11
+ import fs from 'node:fs';
12
+ import path from 'node:path';
13
+ import { extractController, emitController } from '../src/controller.mjs';
14
+
15
+ const USAGE =
16
+ 'usage: controller.mjs extract|emit --file <controller.php> --method <name> [--meta <meta.json>] [--out <file>]';
17
+
18
+ function fail(errors) {
19
+ process.stderr.write(JSON.stringify(errors));
20
+ process.exit(1);
21
+ }
22
+
23
+ function usageError(message) {
24
+ fail([{ code: 'LW004', message: message + ' — ' + USAGE, line: 0, column: 0, hint: '' }]);
25
+ }
26
+
27
+ const args = process.argv.slice(2);
28
+ let cmd = null;
29
+ let file = null;
30
+ let method = null;
31
+ let metaPath = null;
32
+ let outPath = null;
33
+ for (let i = 0; i < args.length; i++) {
34
+ const a = args[i];
35
+ if (a === 'extract' || a === 'emit') cmd = a;
36
+ else if (a === '--file') file = args[++i];
37
+ else if (a === '--method') method = args[++i];
38
+ else if (a === '--meta') metaPath = args[++i];
39
+ else if (a === '--out') outPath = args[++i];
40
+ else usageError('unexpected argument ' + JSON.stringify(a));
41
+ }
42
+ if (!cmd) usageError('missing command (extract|emit)');
43
+ if (!file) usageError('missing --file');
44
+ if (!method) usageError('missing --method');
45
+
46
+ const absFile = path.resolve(process.cwd(), file);
47
+ if (!fs.existsSync(absFile)) usageError('file not found: ' + absFile);
48
+ const source = fs.readFileSync(absFile, 'utf8');
49
+
50
+ if (cmd === 'extract') {
51
+ const info = extractController(source, method);
52
+ if (!info.ok) fail(info.errors);
53
+ const out = {
54
+ ok: true,
55
+ fqcn: info.fqcn,
56
+ namespace: info.namespace,
57
+ className: info.className,
58
+ method: info.method,
59
+ params: info.params,
60
+ classes: info.classes,
61
+ // The classes the body instantiates (`new OrderMail()`).
62
+ newClasses: info.newClasses,
63
+ constants: info.constants,
64
+ views: info.views,
65
+ // A paginator in the method + the kinds of the data the views receive.
66
+ usesPaginator: info.usesPaginator,
67
+ // A mail in the method: the build compiles the mailable's view.
68
+ usesMail: info.usesMail,
69
+ viewDataKinds: info.viewDataKinds,
70
+ };
71
+ const text = JSON.stringify(out);
72
+ if (outPath) fs.writeFileSync(path.resolve(process.cwd(), outPath), text);
73
+ else process.stdout.write(text);
74
+ } else {
75
+ if (!metaPath) usageError('emit requires --meta <file.json>');
76
+ if (!outPath) usageError('emit requires --out <file.mjs>');
77
+ const absMeta = path.resolve(process.cwd(), metaPath);
78
+ if (!fs.existsSync(absMeta)) usageError('meta file not found: ' + absMeta);
79
+ let meta;
80
+ try {
81
+ meta = JSON.parse(fs.readFileSync(absMeta, 'utf8'));
82
+ } catch (e) {
83
+ usageError('invalid meta JSON: ' + (e.message || String(e)));
84
+ }
85
+ const res = emitController(source, method, meta);
86
+ if (res.errors.length) fail(res.errors);
87
+ const absOut = path.resolve(process.cwd(), outPath);
88
+ fs.mkdirSync(path.dirname(absOut), { recursive: true });
89
+ const tmp = absOut + '.tmp-' + process.pid;
90
+ fs.writeFileSync(tmp, res.code);
91
+ fs.renameSync(tmp, absOut);
92
+ }