@ng-desktop/builders 0.0.0-stage → 0.4.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.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/bin/ng-desktop.mjs +200 -0
  4. package/builders.json +15 -0
  5. package/fesm2022/application-builder.d.mts +62 -0
  6. package/fesm2022/application-builder.mjs +58 -0
  7. package/fesm2022/ast-metadata-extractor.d.mts +40 -0
  8. package/fesm2022/ast-metadata-extractor.mjs +460 -0
  9. package/fesm2022/build-bundle.d.mts +45 -0
  10. package/fesm2022/build-bundle.mjs +462 -0
  11. package/fesm2022/builder.d.ts +66 -0
  12. package/fesm2022/builder.js +123 -0
  13. package/fesm2022/dev-server-builder.d.mts +26 -0
  14. package/fesm2022/dev-server-builder.mjs +24 -0
  15. package/fesm2022/dev-server.mjs +47 -0
  16. package/fesm2022/hmr-client.d.mts +24 -0
  17. package/fesm2022/hmr-client.mjs +84 -0
  18. package/fesm2022/icon-compiler.d.mts +6 -0
  19. package/fesm2022/icon-compiler.mjs +148 -0
  20. package/fesm2022/index.d.ts +13 -0
  21. package/fesm2022/index.js +13 -0
  22. package/fesm2022/permission-analyzer.d.mts +45 -0
  23. package/fesm2022/permission-analyzer.mjs +333 -0
  24. package/fesm2022/resolve-engine-binary.d.mts +35 -0
  25. package/fesm2022/resolve-engine-binary.mjs +130 -0
  26. package/fesm2022/server-compiler.d.mts +29 -0
  27. package/fesm2022/server-compiler.mjs +236 -0
  28. package/fesm2022/style-compiler.d.mts +26 -0
  29. package/fesm2022/style-compiler.mjs +258 -0
  30. package/fesm2022/template-compiler.d.mts +53 -0
  31. package/fesm2022/template-compiler.mjs +892 -0
  32. package/fesm2022/workspace-resolver.d.mts +24 -0
  33. package/fesm2022/workspace-resolver.mjs +199 -0
  34. package/package.json +79 -4
  35. package/schema/angular-native-schema.json +52 -0
  36. package/schema/native-builder-schema.json +97 -0
  37. package/schematics/collection.json +10 -0
  38. package/schematics/ng-add/index.d.mts +20 -0
  39. package/schematics/ng-add/index.mjs +56 -0
  40. package/schematics/ng-add/schema.json +24 -0
@@ -0,0 +1,333 @@
1
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
2
+ import { relative, resolve } from 'node:path';
3
+ import { parseSync } from 'oxc-parser';
4
+
5
+ function createEmptyCapabilities() {
6
+ return {
7
+ network: false,
8
+ sqlite: false,
9
+ filesystem: {
10
+ xdg: false,
11
+ dialogs: false,
12
+ drop: false,
13
+ },
14
+ clipboard: false,
15
+ notifications: false,
16
+ computeThreads: false,
17
+ fullSystemAccess: false,
18
+ };
19
+ }
20
+
21
+ function normalizeToken(token) {
22
+ return String(token ?? '')
23
+ .trim()
24
+ .toLowerCase()
25
+ .replaceAll('_', '-');
26
+ }
27
+
28
+ function applyCapabilityToken(capabilities, token, enabled) {
29
+ switch (normalizeToken(token)) {
30
+ case 'network':
31
+ case 'http':
32
+ capabilities.network = enabled;
33
+ break;
34
+ case 'sqlite':
35
+ case 'database':
36
+ capabilities.sqlite = enabled;
37
+ break;
38
+ case 'filesystem':
39
+ case 'fs':
40
+ capabilities.filesystem.xdg = enabled;
41
+ capabilities.filesystem.dialogs = enabled;
42
+ capabilities.filesystem.drop = enabled;
43
+ break;
44
+ case 'filesystem:xdg':
45
+ case 'filesystem-xdg':
46
+ case 'filesystemxdg':
47
+ case 'xdg':
48
+ capabilities.filesystem.xdg = enabled;
49
+ break;
50
+ case 'filesystem:dialogs':
51
+ case 'filesystem-dialogs':
52
+ case 'filesystemdialogs':
53
+ case 'dialogs':
54
+ capabilities.filesystem.dialogs = enabled;
55
+ break;
56
+ case 'filesystem:drop':
57
+ case 'filesystem-drop':
58
+ case 'filesystemdrop':
59
+ case 'drop':
60
+ capabilities.filesystem.drop = enabled;
61
+ break;
62
+ case 'clipboard':
63
+ capabilities.clipboard = enabled;
64
+ break;
65
+ case 'notifications':
66
+ case 'notification':
67
+ capabilities.notifications = enabled;
68
+ break;
69
+ case 'compute-threads':
70
+ case 'computethreads':
71
+ case 'compute':
72
+ case 'threads':
73
+ capabilities.computeThreads = enabled;
74
+ break;
75
+ case 'full-system-access':
76
+ case 'fullsystemaccess':
77
+ capabilities.fullSystemAccess = enabled;
78
+ break;
79
+ default:
80
+ break;
81
+ }
82
+ }
83
+
84
+ function isCapabilityEnabled(capabilities, cap) {
85
+ switch (cap) {
86
+ case 'network':
87
+ return capabilities.network;
88
+ case 'sqlite':
89
+ return capabilities.sqlite;
90
+ case 'filesystem:xdg':
91
+ return capabilities.filesystem.xdg;
92
+ case 'filesystem:dialogs':
93
+ return capabilities.filesystem.dialogs;
94
+ case 'filesystem:drop':
95
+ return capabilities.filesystem.drop;
96
+ case 'clipboard':
97
+ return capabilities.clipboard;
98
+ case 'notifications':
99
+ return capabilities.notifications;
100
+ case 'compute-threads':
101
+ return capabilities.computeThreads;
102
+ default:
103
+ return false;
104
+ }
105
+ }
106
+
107
+ const IMPORT_SPECIFIER_CAPS = new Map([
108
+ ['HttpClient', 'network'],
109
+ ['provideNativeHttp', 'network'],
110
+ ['provideHttpClient', 'network'],
111
+ ['NativeSqliteService', 'sqlite'],
112
+ ['NativeFsService', 'filesystem:xdg'],
113
+ ['NativeComputeService', 'compute-threads'],
114
+ ]);
115
+
116
+ const CALL_NAME_CAPS = new Map([
117
+ ['fetch', 'network'],
118
+ ['httpRequest', 'network'],
119
+ ['httpBatch', 'network'],
120
+ ['downloadFiles', 'network'],
121
+ ['httpResource', 'network'],
122
+ ['configureHttp', 'network'],
123
+ ['sqliteExec', 'sqlite'],
124
+ ['sqliteQuery', 'sqlite'],
125
+ ['openSqlite', 'sqlite'],
126
+ ['resolveXdgPath', 'filesystem:xdg'],
127
+ ['fsResolvePath', 'filesystem:xdg'],
128
+ ['fsReadText', 'filesystem:xdg'],
129
+ ['fsWriteText', 'filesystem:xdg'],
130
+ ['fsWatchFile', 'filesystem:xdg'],
131
+ ['openFileDialog', 'filesystem:dialogs'],
132
+ ['saveFileDialog', 'filesystem:dialogs'],
133
+ ['getClipboard', 'clipboard'],
134
+ ['setClipboard', 'clipboard'],
135
+ ['readClipboard', 'clipboard'],
136
+ ['writeClipboard', 'clipboard'],
137
+ ['sendNotification', 'notifications'],
138
+ ['computeFilter', 'compute-threads'],
139
+ ['computeHash', 'compute-threads'],
140
+ ]);
141
+
142
+ const SERVER_CONTEXT_CAPS = new Map([
143
+ ['db', 'sqlite'],
144
+ ['fs', 'filesystem:xdg'],
145
+ ['compute', 'compute-threads'],
146
+ ['http', 'network'],
147
+ ]);
148
+
149
+ function visitAst(node, visitor) {
150
+ if (!node || typeof node !== 'object') return;
151
+ if (Array.isArray(node)) {
152
+ for (const item of node) {
153
+ visitAst(item, visitor);
154
+ }
155
+ return;
156
+ }
157
+ if (typeof node.type === 'string') {
158
+ visitor(node);
159
+ }
160
+ for (const key of Object.keys(node)) {
161
+ if (key === 'loc' || key === 'span' || key === 'start' || key === 'end') continue;
162
+ const val = node[key];
163
+ if (val && typeof val === 'object') {
164
+ visitAst(val, visitor);
165
+ }
166
+ }
167
+ }
168
+
169
+ function inspectTypeScriptAst(filename, code, addEvidence) {
170
+ const parsed = parseSync(filename, code);
171
+ visitAst(parsed.program, (node) => {
172
+ if (node.type === 'ImportDeclaration') {
173
+ const modName = node.source?.value;
174
+ if (
175
+ modName === '@ng-desktop/http' ||
176
+ modName === '@ng-native/http' ||
177
+ modName === '@angular/common/http'
178
+ ) {
179
+ addEvidence('network', filename, `import from '${modName}'`);
180
+ }
181
+ for (const spec of node.specifiers || []) {
182
+ const imported = spec.imported?.name || spec.local?.name;
183
+ const cap = IMPORT_SPECIFIER_CAPS.get(imported);
184
+ if (cap) {
185
+ addEvidence(cap, filename, `import '${imported}'`);
186
+ }
187
+ }
188
+ return;
189
+ }
190
+
191
+ if (node.type === 'CallExpression') {
192
+ if (node.callee?.type === 'Identifier') {
193
+ const cap = CALL_NAME_CAPS.get(node.callee.name);
194
+ if (cap) {
195
+ addEvidence(cap, filename, `call '${node.callee.name}()'`);
196
+ }
197
+ } else if (
198
+ node.callee?.type === 'MemberExpression' &&
199
+ node.callee.property?.type === 'Identifier'
200
+ ) {
201
+ const method = node.callee.property.name;
202
+ const cap = method === 'notify' ? 'notifications' : CALL_NAME_CAPS.get(method);
203
+ if (cap) {
204
+ addEvidence(cap, filename, `call '.${method}()'`);
205
+ }
206
+ }
207
+ return;
208
+ }
209
+
210
+ if (
211
+ node.type === 'NewExpression' &&
212
+ node.callee?.type === 'Identifier' &&
213
+ node.callee.name === 'WebSocket'
214
+ ) {
215
+ addEvidence('network', filename, `new WebSocket()`);
216
+ return;
217
+ }
218
+
219
+ if (node.type === 'MemberExpression' && node.property?.type === 'Identifier') {
220
+ const cap = SERVER_CONTEXT_CAPS.get(node.property.name);
221
+ const isContextObj =
222
+ (node.object?.type === 'Identifier' && node.object.name === 'context') ||
223
+ (node.object?.type === 'MemberExpression' &&
224
+ node.object.property?.type === 'Identifier' &&
225
+ node.object.property.name === 'context');
226
+ if (cap && isContextObj) {
227
+ addEvidence(cap, filename, `context.${node.property.name}`);
228
+ }
229
+ return;
230
+ }
231
+
232
+ if (node.type === 'Literal' && typeof node.value === 'string') {
233
+ if (node.value === 'file-drop' || node.value === 'ng-drop-target') {
234
+ addEvidence('filesystem:drop', filename, `literal '${node.value}'`);
235
+ }
236
+ }
237
+ });
238
+ }
239
+
240
+ function collectProjectSourceFiles(rootDir, currentDir = rootDir, out = []) {
241
+ if (!existsSync(currentDir)) return out;
242
+ for (const entry of readdirSync(currentDir)) {
243
+ const fullPath = resolve(currentDir, entry);
244
+ const stat = statSync(fullPath);
245
+ if (stat.isDirectory()) {
246
+ collectProjectSourceFiles(rootDir, fullPath, out);
247
+ } else if (
248
+ (entry.endsWith('.ts') && !entry.endsWith('.spec.ts') && !entry.endsWith('.d.ts')) ||
249
+ entry.endsWith('.html')
250
+ ) {
251
+ out.push({
252
+ file: relative(rootDir, fullPath),
253
+ content: readFileSync(fullPath, 'utf8'),
254
+ });
255
+ }
256
+ }
257
+ return out;
258
+ }
259
+
260
+ /**
261
+ * Statically analyzes source files using `oxc-parser` AST traversal and template attribute checks,
262
+ * applying optional `permissions` policy (`mode: 'auto' | 'strict'`, `allow`, `deny`).
263
+ */
264
+ export function analyzeSourcePermissions(files, permissionsConfig = {}) {
265
+ const mode = permissionsConfig?.mode === 'strict' ? 'strict' : 'auto';
266
+ const allowList = Array.isArray(permissionsConfig?.allow) ? permissionsConfig.allow : [];
267
+ const denyList = Array.isArray(permissionsConfig?.deny) ? permissionsConfig.deny : [];
268
+
269
+ const evidence = [];
270
+ const seenKeys = new Set();
271
+ const addEvidence = (capability, file, reason) => {
272
+ const key = `${capability}::${file}::${reason}`;
273
+ if (seenKeys.has(key)) return;
274
+ seenKeys.add(key);
275
+ evidence.push({ capability, file, reason });
276
+ };
277
+
278
+ for (const item of files) {
279
+ if (item.file.endsWith('.ts') || item.file.endsWith('.mts') || item.file.endsWith('.js')) {
280
+ inspectTypeScriptAst(item.file, item.content, addEvidence);
281
+ } else if (item.file.endsWith('.html')) {
282
+ if (
283
+ item.content.includes('(file-drop)') ||
284
+ item.content.includes('dropTarget') ||
285
+ item.content.includes('ng-drop-target')
286
+ ) {
287
+ addEvidence('filesystem:drop', item.file, 'template drop-target binding');
288
+ }
289
+ }
290
+ }
291
+
292
+ const capabilities = createEmptyCapabilities();
293
+ if (mode === 'auto') {
294
+ for (const item of evidence) {
295
+ applyCapabilityToken(capabilities, item.capability, true);
296
+ }
297
+ }
298
+
299
+ for (const token of allowList) {
300
+ applyCapabilityToken(capabilities, token, true);
301
+ }
302
+ for (const token of denyList) {
303
+ applyCapabilityToken(capabilities, token, false);
304
+ }
305
+
306
+ const warnings = [];
307
+ if (mode === 'strict') {
308
+ for (const item of evidence) {
309
+ if (!isCapabilityEnabled(capabilities, item.capability) && !capabilities.fullSystemAccess) {
310
+ warnings.push(
311
+ `Detected '${item.capability}' usage in '${item.file}' (${item.reason}), but strict mode omits it unless listed in allow.`,
312
+ );
313
+ }
314
+ }
315
+ }
316
+
317
+ return {
318
+ schemaVersion: 1,
319
+ mode,
320
+ capabilities,
321
+ evidence,
322
+ warnings,
323
+ };
324
+ }
325
+
326
+ /**
327
+ * Recursively collects application TypeScript/HTML sources under `sourceRoot` and computes the
328
+ * build-time least-privilege capability manifest.
329
+ */
330
+ export function analyzeProjectPermissions(sourceRoot, permissionsConfig = {}) {
331
+ const files = collectProjectSourceFiles(sourceRoot);
332
+ return analyzeSourcePermissions(files, permissionsConfig);
333
+ }
@@ -0,0 +1,35 @@
1
+ export interface EngineBinaryPackageMeta {
2
+ readonly pkgName: string;
3
+ readonly dirName: string;
4
+ readonly os: 'linux' | 'win32' | 'darwin';
5
+ readonly cpu: 'x64' | 'arm64';
6
+ readonly libc?: 'glibc';
7
+ readonly binaryName: string;
8
+ }
9
+
10
+ export const ENGINE_BINARY_PACKAGES: Readonly<Record<string, EngineBinaryPackageMeta>>;
11
+
12
+ export type EngineBinarySource = 'env' | 'optional-package' | 'cargo-target' | 'cargo-fallback';
13
+
14
+ export interface ResolveEngineBinaryOptions {
15
+ env?: Record<string, string | undefined>;
16
+ platform?: NodeJS.Platform;
17
+ arch?: string;
18
+ cwd?: string;
19
+ workspaceRoot?: string;
20
+ resolvePkgPath?: (pkgName: string) => string | null | undefined;
21
+ fileExists?: (path: string) => boolean;
22
+ }
23
+
24
+ export interface EngineBinaryResolution {
25
+ mode: 'binary' | 'cargo';
26
+ command: string;
27
+ args: string[];
28
+ source: EngineBinarySource;
29
+ packageName: string | null;
30
+ targetKey: string;
31
+ }
32
+
33
+ export function resolveEngineBinaryPath(
34
+ options?: ResolveEngineBinaryOptions,
35
+ ): EngineBinaryResolution;
@@ -0,0 +1,130 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, resolve } from 'node:path';
4
+
5
+ export const ENGINE_BINARY_PACKAGES = {
6
+ 'linux-x64': {
7
+ pkgName: '@ng-desktop/engine-linux-x64-gnu',
8
+ dirName: 'engine-linux-x64-gnu',
9
+ os: 'linux',
10
+ cpu: 'x64',
11
+ libc: 'glibc',
12
+ binaryName: 'ng-native-engine',
13
+ },
14
+ 'linux-arm64': {
15
+ pkgName: '@ng-desktop/engine-linux-arm64-gnu',
16
+ dirName: 'engine-linux-arm64-gnu',
17
+ os: 'linux',
18
+ cpu: 'arm64',
19
+ libc: 'glibc',
20
+ binaryName: 'ng-native-engine',
21
+ },
22
+ 'win32-x64': {
23
+ pkgName: '@ng-desktop/engine-win32-x64-msvc',
24
+ dirName: 'engine-win32-x64-msvc',
25
+ os: 'win32',
26
+ cpu: 'x64',
27
+ binaryName: 'ng-native-engine.exe',
28
+ },
29
+ 'darwin-arm64': {
30
+ pkgName: '@ng-desktop/engine-darwin-arm64',
31
+ dirName: 'engine-darwin-arm64',
32
+ os: 'darwin',
33
+ cpu: 'arm64',
34
+ binaryName: 'ng-native-engine',
35
+ },
36
+ 'darwin-x64': {
37
+ pkgName: '@ng-desktop/engine-darwin-x64',
38
+ dirName: 'engine-darwin-x64',
39
+ os: 'darwin',
40
+ cpu: 'x64',
41
+ binaryName: 'ng-native-engine',
42
+ },
43
+ };
44
+
45
+ function defaultResolvePkgJsonPath(pkgName, cwd) {
46
+ try {
47
+ const req = createRequire(resolve(cwd, 'package.json'));
48
+ return req.resolve(`${pkgName}/package.json`);
49
+ } catch {
50
+ return null;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Resolves the prebuilt `ng-native-engine` host executable in priority order:
56
+ * 1. Explicit `NG_DESKTOP_ENGINE_BIN` / `NG_NATIVE_ENGINE_BIN` environment override
57
+ * 2. Installed platform-specific `@ng-desktop/engine-*` npm `optionalDependencies` package
58
+ * 3. Local Cargo build artifact (`target/release` then `target/debug`)
59
+ * 4. Fallback to `cargo run --` for source-checkout development
60
+ */
61
+ export function resolveEngineBinaryPath({
62
+ env = process.env,
63
+ platform = process.platform,
64
+ arch = process.arch,
65
+ cwd = process.cwd(),
66
+ workspaceRoot,
67
+ resolvePkgPath,
68
+ fileExists = existsSync,
69
+ } = {}) {
70
+ const targetKey = `${platform}-${arch}`;
71
+ const entry = ENGINE_BINARY_PACKAGES[targetKey] ?? null;
72
+ const fallbackBinName = platform === 'win32' ? 'ng-native-engine.exe' : 'ng-native-engine';
73
+ const binName = entry?.binaryName ?? fallbackBinName;
74
+
75
+ const envOverride =
76
+ env?.['NG_DESKTOP_ENGINE_BIN']?.trim() || env?.['NG_NATIVE_ENGINE_BIN']?.trim();
77
+ if (envOverride) {
78
+ return {
79
+ mode: 'binary',
80
+ command: envOverride,
81
+ args: [],
82
+ source: 'env',
83
+ packageName: entry?.pkgName ?? null,
84
+ targetKey,
85
+ };
86
+ }
87
+
88
+ if (entry) {
89
+ const pkgJsonPath = resolvePkgPath
90
+ ? resolvePkgPath(entry.pkgName)
91
+ : defaultResolvePkgJsonPath(entry.pkgName, cwd);
92
+ if (pkgJsonPath) {
93
+ const candidateBin = resolve(dirname(pkgJsonPath), 'bin', entry.binaryName);
94
+ if (fileExists(candidateBin)) {
95
+ return {
96
+ mode: 'binary',
97
+ command: candidateBin,
98
+ args: [],
99
+ source: 'optional-package',
100
+ packageName: entry.pkgName,
101
+ targetKey,
102
+ };
103
+ }
104
+ }
105
+ }
106
+
107
+ const searchRoot = workspaceRoot ?? cwd;
108
+ for (const profile of ['release', 'debug']) {
109
+ const localBin = resolve(searchRoot, 'target', profile, binName);
110
+ if (fileExists(localBin)) {
111
+ return {
112
+ mode: 'binary',
113
+ command: localBin,
114
+ args: [],
115
+ source: 'cargo-target',
116
+ packageName: entry?.pkgName ?? null,
117
+ targetKey,
118
+ };
119
+ }
120
+ }
121
+
122
+ return {
123
+ mode: 'cargo',
124
+ command: 'cargo',
125
+ args: ['run', '--'],
126
+ source: 'cargo-fallback',
127
+ packageName: entry?.pkgName ?? null,
128
+ targetKey,
129
+ };
130
+ }
@@ -0,0 +1,29 @@
1
+ export interface DiscoveredServerRoute {
2
+ method: string;
3
+ pattern: string;
4
+ filePath: string;
5
+ }
6
+
7
+ export interface CompileServerBundleOptions {
8
+ workspaceRoot: string;
9
+ sourceRootAbs: string;
10
+ outDir: string;
11
+ projectName: string;
12
+ }
13
+
14
+ export interface EsbuildBuildOptions {
15
+ workspaceRoot: string;
16
+ resolveDir?: string;
17
+ entrySource: string;
18
+ loader?: 'ts' | 'js';
19
+ alias?: Record<string, string>;
20
+ define?: Record<string, string>;
21
+ minifySyntax?: boolean;
22
+ banner?: { js?: string };
23
+ }
24
+
25
+ export function discoverServerRoutes(sourceRootAbs: string): DiscoveredServerRoute[];
26
+
27
+ export function runEsbuildBuild(options: EsbuildBuildOptions): string;
28
+
29
+ export function compileServerBundle(options: CompileServerBundleOptions): string | undefined;