@blotch/angular-lynx-native 22.2.21 → 22.2.23

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 (44) hide show
  1. package/README.md +4 -0
  2. package/dist/android/build.d.ts +6 -1
  3. package/dist/android/build.d.ts.map +1 -1
  4. package/dist/android/build.js +14 -3
  5. package/dist/android/run.d.ts +10 -1
  6. package/dist/android/run.d.ts.map +1 -1
  7. package/dist/android/run.js +46 -1
  8. package/dist/dev-build.d.ts +16 -0
  9. package/dist/dev-build.d.ts.map +1 -0
  10. package/dist/dev-build.js +44 -0
  11. package/dist/generate.d.ts +24 -5
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +14 -6
  14. package/dist/index.d.ts +12 -3
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +65 -9
  17. package/dist/ios/build.d.ts +14 -1
  18. package/dist/ios/build.d.ts.map +1 -1
  19. package/dist/ios/build.js +15 -6
  20. package/dist/ios/iphone.d.ts +1 -1
  21. package/dist/ios/iphone.d.ts.map +1 -1
  22. package/dist/ios/iphone.js +21 -4
  23. package/dist/ios/project.d.ts +8 -1
  24. package/dist/ios/project.d.ts.map +1 -1
  25. package/dist/ios/project.js +17 -3
  26. package/dist/ios/run.d.ts +1 -1
  27. package/dist/ios/run.d.ts.map +1 -1
  28. package/dist/ios/run.js +4 -1
  29. package/dist/run.d.ts +42 -2
  30. package/dist/run.d.ts.map +1 -1
  31. package/dist/run.js +16 -1
  32. package/package.json +1 -1
  33. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +159 -0
  34. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServerModules.kt +163 -0
  35. package/templates/android/app/src/debug/res/xml/network_security_config.xml +13 -0
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +22 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +8 -1
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/ResourceFetchers.kt +12 -6
  39. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +24 -0
  40. package/templates/ios/App/AssetsModule.swift +21 -3
  41. package/templates/ios/App/BundleResourceFetcher.swift +46 -2
  42. package/templates/ios/App/DevServer.swift +190 -0
  43. package/templates/ios/App/DevServerModules.swift +223 -0
  44. package/templates/ios/App/LynxViewController.swift +9 -1
package/dist/ios/build.js CHANGED
@@ -57,7 +57,8 @@ export const resolveIosSigning = (app, env = process.env) => {
57
57
  * would be thrown away, and archiving the `.ipa` takes minutes a Personal
58
58
  * Team's run would spend failing. Returns the artifacts' paths.
59
59
  */
60
- export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDir, logFile, log, signing, device, }) => {
60
+ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDir, logFile, log, signing, device, development = false, simulator, }) => {
61
+ const configuration = development ? 'Debug' : 'Release';
61
62
  linkFrameworks(project.dir, frameworksDir);
62
63
  const xcodebuild = (args) => run('xcodebuild', args, { cwd: project.dir, logFile });
63
64
  const common = ['-project', 'App.xcodeproj', '-scheme', 'App'];
@@ -89,7 +90,7 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
89
90
  'build',
90
91
  ...common,
91
92
  '-configuration',
92
- 'Release',
93
+ configuration,
93
94
  '-sdk',
94
95
  'iphoneos',
95
96
  // The phone itself rather than generic/platform=iOS, so automatic
@@ -107,7 +108,7 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
107
108
  });
108
109
  const deviceApp = path.join(outputDir, `${app.productName}-device.app`);
109
110
  rmSync(deviceApp, { recursive: true, force: true });
110
- cpSync(path.join(buildDir, 'Build/Products/Release-iphoneos', `${app.productName}.app`), deviceApp, { recursive: true, verbatimSymlinks: true });
111
+ cpSync(path.join(buildDir, `Build/Products/${configuration}-iphoneos`, `${app.productName}.app`), deviceApp, { recursive: true, verbatimSymlinks: true });
111
112
  return [deviceApp];
112
113
  }
113
114
  const artifacts = [];
@@ -116,18 +117,21 @@ export const buildIos = async ({ project, app, frameworksDir, buildDir, outputDi
116
117
  'build',
117
118
  ...common,
118
119
  '-configuration',
119
- 'Release',
120
+ configuration,
120
121
  '-sdk',
121
122
  'iphonesimulator',
122
123
  '-destination',
123
- 'generic/platform=iOS Simulator',
124
+ simulator ? `id=${simulator.udid}` : 'generic/platform=iOS Simulator',
124
125
  '-derivedDataPath',
125
126
  buildDir,
126
127
  ]);
127
128
  const simulatorApp = path.join(outputDir, `${app.productName}-simulator.app`);
128
129
  rmSync(simulatorApp, { recursive: true, force: true });
129
- cpSync(path.join(buildDir, 'Build/Products/Release-iphonesimulator', `${app.productName}.app`), simulatorApp, { recursive: true, verbatimSymlinks: true });
130
+ cpSync(path.join(buildDir, `Build/Products/${configuration}-iphonesimulator`, `${app.productName}.app`), simulatorApp, { recursive: true, verbatimSymlinks: true });
130
131
  artifacts.push(simulatorApp);
132
+ if (development) {
133
+ return artifacts;
134
+ }
131
135
  if (!signing?.export) {
132
136
  log(signing
133
137
  ? 'Skipping the .ipa: set ios.signing (an App Store Connect API key) or ios.export in angular.json to build one.'
@@ -267,6 +271,11 @@ const explainSigningFailure = (error, teamId) => {
267
271
  if (/No Accounts?\b|No account for team/i.test(error.message)) {
268
272
  return new NativeBuildError(`Xcode has no signed-in account for team ${teamId}. Sign in to Xcode with the Apple ID of that team (Xcode → Settings → Accounts), or set ios.signing to an App Store Connect API key, then build again.\n\n${error.message}`);
269
273
  }
274
+ // Apple refuses to register one more device, and xcodebuild then also
275
+ // reports "No profiles for", which alone would point at the wrong fix.
276
+ if (/maximum number of registered \w+ devices/i.test(error.message)) {
277
+ return new NativeBuildError(`Team ${teamId} can't register this device: it has as many as Apple allows. A paid team can remove the devices it no longer uses at the start of its membership year (developer.apple.com → Certificates, IDs & Profiles → Devices). A free Personal Team can't remove them, so set ios.teamId to another team, such as the Personal Team of another Apple ID signed in to Xcode.\n\n${error.message}`);
278
+ }
270
279
  if (/requires a development team|No profiles for/i.test(error.message)) {
271
280
  return new NativeBuildError(`Xcode couldn't make a provisioning profile for team ${teamId}. Check that ios.teamId is a team your Apple ID in Xcode → Settings → Accounts belongs to, and for an .ipa that the team is in the paid Apple Developer Program.\n\n${error.message}`);
272
281
  }
@@ -1,4 +1,4 @@
1
- import type { RunTarget } from '../run.ts';
1
+ import { type RunTarget } from '../run.ts';
2
2
  /**
3
3
  * A device in `xcrun devicectl list devices --json-output -`, as far as it's
4
4
  * read here. Xcode 27's devicectl moved everything into `properties` and
@@ -1 +1 @@
1
- {"version":3,"file":"iphone.d.ts","sourceRoot":"","sources":["../../src/ios/iphone.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE;QACpB,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAClB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;YACvB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;YAC3B,iEAAiE;YACjE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;YAC1B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;SACjC,CAAC;QACF,QAAQ,CAAC,UAAU,CAAC,EAAE;YAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;QACzD,QAAQ,CAAC,KAAK,CAAC,EAAE;YACf,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;YACvB,2CAA2C;YAC3C,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;SACxC,CAAC;KACH,CAAC;IACF,QAAQ,CAAC,kBAAkB,CAAC,EAAE;QAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CA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b,MAAM,GAAG,SAoBX,CAAC;AAKF;;;GAGG;AACH,eAAO,MAAM,WAAW,QAAa,OAAO,CAAC,MAAM,EAAE,CAiBpD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,0BAIzB;IACD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC,KAAG,SAkCH,CAAC;AAkEF,2DAA2D;AAC3D,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,8EAA8E;IAC9E,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,KAAG,eAAe,EAsB7D,CAAC;AAKF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,GAClC,SAAS,SAAS,eAAe,EAAE,EACnC,MAAM,MAAM,EACZ,QAAQ,MAAM,EACd,SAAS,MAAM,KACd,MA2BF,CAAC"}
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readFileSync, rmSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { NativeBuildError, capture, run } from "../process.js";
4
+ import { iosDevServerArguments } from "../run.js";
4
5
  /**
5
6
  * The real iOS devices in devicectl's list. devicectl lists the simulators
6
7
  * too, which `simctl` already covers, so only physical ones are kept.
@@ -108,7 +109,7 @@ export const startIPhone = ({ phone, logFile, log, }) => {
108
109
  log(`Running the app on ${phone.name}${phone.model ? ` (${phone.model})` : ''}`);
109
110
  return {
110
111
  iPhone: { udid: phone.udid, name: phone.name },
111
- launch: async (binary, app) => {
112
+ launch: async (binary, app, devServer) => {
112
113
  log(`Installing the app on ${phone.name}`);
113
114
  await devicectl(['device', 'install', 'app', '--device', phone.udid, binary], { phone, action: 'install the app', logFile });
114
115
  // Replaces the copy a previous `--run` left open, which would otherwise
@@ -121,7 +122,12 @@ export const startIPhone = ({ phone, logFile, log, }) => {
121
122
  phone.udid,
122
123
  '--terminate-existing',
123
124
  app.id,
124
- ], { phone, action: 'open the app', logFile });
125
+ ], {
126
+ phone,
127
+ action: 'open the app',
128
+ logFile,
129
+ appArguments: iosDevServerArguments(devServer),
130
+ });
125
131
  log(`Opened ${app.name} on ${phone.name}`);
126
132
  },
127
133
  };
@@ -130,12 +136,23 @@ export const startIPhone = ({ phone, logFile, log, }) => {
130
136
  * Runs a devicectl command with its result written as JSON, so a failure is
131
137
  * read from the structured error rather than its indented text, and
132
138
  * explained when it's something the developer fixes on the phone.
139
+ *
140
+ * `appArguments` go to the app `process launch` opens, after a `--`.
141
+ * devicectl reads its own options anywhere before that, so without it,
142
+ * `-AngularLynxDevServer` failed as "Unknown option '-A'", and a
143
+ * `--json-output` after the arguments would have been the app's.
133
144
  */
134
- const devicectl = async (args, { phone, action, logFile, }) => {
145
+ const devicectl = async (args, { phone, action, logFile, appArguments = [], }) => {
135
146
  const json = path.join(path.dirname(logFile), 'devicectl.json');
136
147
  rmSync(json, { force: true });
137
148
  try {
138
- await run('xcrun', ['devicectl', ...args, '--json-output', json], {
149
+ await run('xcrun', [
150
+ 'devicectl',
151
+ ...args,
152
+ '--json-output',
153
+ json,
154
+ ...(appArguments.length > 0 ? ['--', ...appArguments] : []),
155
+ ], {
139
156
  cwd: process.cwd(),
140
157
  logFile,
141
158
  });
@@ -9,5 +9,12 @@ export declare const iosGenerator: PlatformGenerator;
9
9
  export declare const xcconfig: (app: ResolvedApp) => string;
10
10
  /** What iOS shows the first time someone taps Save Image, without `savePhotos`. */
11
11
  export declare const DEFAULT_SAVE_PHOTOS_REASON = "Saves the images you choose to your photo library.";
12
- export declare const infoPlistEntries: (app: ResolvedApp) => string;
12
+ /**
13
+ * What iOS shows the first time the development app connects to the dev
14
+ * server, on a real iPhone. The simulator never asks.
15
+ */
16
+ export declare const LOCAL_NETWORK_REASON = "Loads the app from ng serve on your computer while you develop it.";
17
+ export declare const infoPlistEntries: (app: ResolvedApp, { development }?: {
18
+ readonly development?: boolean;
19
+ }) => string;
13
20
  //# sourceMappingURL=project.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/ios/project.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAe,WAAW,EAAE,MAAM,eAAe,CAAC;AAiB9D;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,iBA6B1B,CAAC;AAQF,eAAO,MAAM,QAAQ,GAAI,KAAK,WAAW,KAAG,MAY9B,CAAC;AAEf,mFAAmF;AACnF,eAAO,MAAM,0BAA0B,uDACe,CAAC;AAEvD,eAAO,MAAM,gBAAgB,GAAI,KAAK,WAAW,KAAG,MAsDnD,CAAC"}
1
+ {"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/ios/project.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAe,WAAW,EAAE,MAAM,eAAe,CAAC;AAiB9D;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,iBA6B1B,CAAC;AAQF,eAAO,MAAM,QAAQ,GAAI,KAAK,WAAW,KAAG,MAY9B,CAAC;AAEf,mFAAmF;AACnF,eAAO,MAAM,0BAA0B,uDACe,CAAC;AAEvD;;;GAGG;AACH,eAAO,MAAM,oBAAoB,uEACqC,CAAC;AAEvE,eAAO,MAAM,gBAAgB,GAC3B,KAAK,WAAW,EAChB,kBAAyB;IAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAO,KAC/D,MAmEF,CAAC"}
@@ -21,10 +21,10 @@ const ORIENTATIONS = {
21
21
  * between the template's `angular-lynx:info` markers.
22
22
  */
23
23
  export const iosGenerator = {
24
- configure: (dir, app, projectRoot) => {
24
+ configure: (dir, app, projectRoot, development = false) => {
25
25
  writeFileSync(path.join(dir, 'Config/App.xcconfig'), xcconfig(app));
26
26
  const plistPath = path.join(dir, 'App/Info.plist');
27
- writeFileSync(plistPath, replaceMarker(readFileSync(plistPath, 'utf8'), 'info', infoPlistEntries(app)));
27
+ writeFileSync(plistPath, replaceMarker(readFileSync(plistPath, 'utf8'), 'info', infoPlistEntries(app, { development })));
28
28
  if (app.icon) {
29
29
  const icon = path.resolve(projectRoot, app.icon);
30
30
  const size = readPngSize(icon);
@@ -54,7 +54,12 @@ export const xcconfig = (app) => [
54
54
  ].join('\n');
55
55
  /** What iOS shows the first time someone taps Save Image, without `savePhotos`. */
56
56
  export const DEFAULT_SAVE_PHOTOS_REASON = 'Saves the images you choose to your photo library.';
57
- export const infoPlistEntries = (app) => {
57
+ /**
58
+ * What iOS shows the first time the development app connects to the dev
59
+ * server, on a real iPhone. The simulator never asks.
60
+ */
61
+ export const LOCAL_NETWORK_REASON = 'Loads the app from ng serve on your computer while you develop it.';
62
+ export const infoPlistEntries = (app, { development = false } = {}) => {
58
63
  const lines = [];
59
64
  const array = (key, values) => {
60
65
  lines.push(`<key>${key}</key>`, '<array>');
@@ -78,6 +83,15 @@ export const infoPlistEntries = (app) => {
78
83
  lines.push(`<key>${key}</key>`, `<string>${escapeXml(reason)}</string>`);
79
84
  }
80
85
  }
86
+ // `ng serve --platform ios`'s development app loads from the dev server at
87
+ // the Mac's address on the local network, which iOS (14 and later) lets an
88
+ // app reach only once the person allows it. iOS asks the first time, with
89
+ // this reason. Only the development app has it: the app for the stores
90
+ // loads its own bundle, and App Review asks about any reason it doesn't
91
+ // need.
92
+ if (development) {
93
+ lines.push('<key>NSLocalNetworkUsageDescription</key>', `<string>${escapeXml(LOCAL_NETWORK_REASON)}</string>`);
94
+ }
81
95
  // The schemes LinkingModule's canOpenURL can ask about: iOS answers false
82
96
  // for any other. UIApplication.open needs none of them.
83
97
  array('LSApplicationQueriesSchemes', app.canOpenURLSchemes);
package/dist/ios/run.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RunTarget } from '../run.ts';
1
+ import { type RunTarget } from '../run.ts';
2
2
  /** What `xcrun simctl list --json devices runtimes` prints, as far as it's read here. */
3
3
  export type SimctlList = {
4
4
  readonly devices: Readonly<Record<string, readonly SimctlDevice[]>>;
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/ios/run.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAG3C,yFAAyF;AACzF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,CAAC,CAAC,CAAC;IACpE,QAAQ,CAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;CAC7C,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0DAA0D;IAC1D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,KAAK,aAAa,GAAG;IACnB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,uBAAuB;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,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 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { NativeBuildError, capture, run } from "../process.js";
4
+ import { iosDevServerArguments } from "../run.js";
4
5
  import { listIPhones, pickIPhone, startIPhone } from "./iphone.js";
5
6
  /**
6
7
  * Chooses the simulator `ng build --platform ios --run` opens the app on.
@@ -149,7 +150,8 @@ export const startIosSimulator = async ({ device, logFile, log, iPhones, }) => {
149
150
  const booted = xcrun(['simctl', 'bootstatus', udid, '-b']);
150
151
  booted.catch(() => { });
151
152
  return {
152
- launch: async (binary, app) => {
153
+ simulator: { udid },
154
+ launch: async (binary, app, devServer) => {
153
155
  await booted;
154
156
  await showSimulator(udid, logFile, log);
155
157
  log(`Installing the app on ${simulator.name}`);
@@ -162,6 +164,7 @@ export const startIosSimulator = async ({ device, logFile, log, iPhones, }) => {
162
164
  '--terminate-running-process',
163
165
  udid,
164
166
  app.id,
167
+ ...iosDevServerArguments(devServer),
165
168
  ]);
166
169
  log(`Opened ${app.name} on ${simulator.name}`);
167
170
  },
package/dist/run.d.ts CHANGED
@@ -9,6 +9,20 @@ export type RunOptions = {
9
9
  */
10
10
  readonly device?: string;
11
11
  };
12
+ /**
13
+ * `ng serve --platform <platform>`'s dev server, which the development app
14
+ * loads its bundle from instead of the copy inside it.
15
+ */
16
+ export type DevServerOptions = {
17
+ /**
18
+ * The Lynx bundle's URL on the dev server, as the bundle itself names the
19
+ * server: rspeedy builds it with this host in its asset URLs and its live
20
+ * reload socket (`dev.assetPrefix`), and the QR code for Lynx Explorer
21
+ * carries it. Usually the Mac's address on the local network, so a phone
22
+ * on the same Wi-Fi reaches it.
23
+ */
24
+ readonly bundleUrl: string;
25
+ };
12
26
  /**
13
27
  * A simulator, emulator or phone picked for `--run`. Picking it and starting
14
28
  * it come before the native build, so a device that doesn't exist fails in
@@ -23,7 +37,33 @@ export type RunTarget = {
23
37
  readonly udid: string;
24
38
  readonly name: string;
25
39
  };
26
- /** Waits for the device to finish starting, then installs and opens the app. */
27
- readonly launch: (binary: string, app: ResolvedApp) => Promise<void>;
40
+ /**
41
+ * The simulator the app runs on, which a development build is compiled for
42
+ * alone, rather than for every simulator.
43
+ */
44
+ readonly simulator?: {
45
+ readonly udid: string;
46
+ };
47
+ /**
48
+ * Waits for the device to finish starting, then installs and opens the
49
+ * app. With `devServer`, the app is told to load its bundle from there.
50
+ */
51
+ readonly launch: (binary: string, app: ResolvedApp, devServer?: DevServerOptions) => Promise<void>;
28
52
  };
53
+ /**
54
+ * The launch argument (iOS) and intent extra (Android) that hand the
55
+ * development app its dev server, and the one that names the host the bundle
56
+ * was built with, when the app reaches the server under another (Android's
57
+ * `localhost`, through `adb reverse`). The shells' DevServer reads them.
58
+ */
59
+ export declare const DEV_SERVER_ARGUMENT = "AngularLynxDevServer";
60
+ export declare const DEV_SERVER_ORIGIN_ARGUMENT = "AngularLynxDevServerOrigin";
61
+ /**
62
+ * The launch arguments that point the development app at the dev server.
63
+ * iOS puts `-Name value` arguments in the app's UserDefaults (its argument
64
+ * domain), where DevServer.swift reads them. A simulator shares the Mac's
65
+ * network, and an iPhone on the same Wi-Fi reaches the Mac's address, so
66
+ * both load the bundle from the URL it was built with.
67
+ */
68
+ export declare const iosDevServerArguments: (devServer: DevServerOptions | undefined) => string[];
29
69
  //# sourceMappingURL=run.d.ts.map
package/dist/run.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,wDAAwD;AACxD,MAAM,MAAM,UAAU,GAAG;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;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AC"}
package/dist/run.js CHANGED
@@ -1 +1,16 @@
1
- export {};
1
+ /**
2
+ * The launch argument (iOS) and intent extra (Android) that hand the
3
+ * development app its dev server, and the one that names the host the bundle
4
+ * was built with, when the app reaches the server under another (Android's
5
+ * `localhost`, through `adb reverse`). The shells' DevServer reads them.
6
+ */
7
+ export const DEV_SERVER_ARGUMENT = 'AngularLynxDevServer';
8
+ export const DEV_SERVER_ORIGIN_ARGUMENT = 'AngularLynxDevServerOrigin';
9
+ /**
10
+ * The launch arguments that point the development app at the dev server.
11
+ * iOS puts `-Name value` arguments in the app's UserDefaults (its argument
12
+ * domain), where DevServer.swift reads them. A simulator shares the Mac's
13
+ * network, and an iPhone on the same Wi-Fi reaches the Mac's address, so
14
+ * both load the bundle from the URL it was built with.
15
+ */
16
+ export const iosDevServerArguments = (devServer) => devServer ? [`-${DEV_SERVER_ARGUMENT}`, devServer.bundleUrl] : [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotch/angular-lynx-native",
3
- "version": "22.2.21",
3
+ "version": "22.2.23",
4
4
  "description": "Builds AngularLynx apps into iOS and Android binaries",
5
5
  "homepage": "https://github.com/Blotch-Smart-Frames/AngularLynx#readme",
6
6
  "bugs": {
@@ -0,0 +1,159 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.net.Uri
6
+ import android.os.Handler
7
+ import android.os.Looper
8
+ import android.util.TypedValue
9
+ import android.view.Gravity
10
+ import android.view.View
11
+ import android.widget.FrameLayout
12
+ import android.widget.TextView
13
+ import com.lynx.tasm.LynxViewBuilder
14
+
15
+ /**
16
+ * `ng serve --platform android`: the development app loads its bundle from
17
+ * the dev server rather than from its assets (where it has none), and
18
+ * reloads it on every edit, as Lynx Explorer does.
19
+ *
20
+ * This is the debug build type's copy, the one `ng serve --platform android`
21
+ * builds; the release build's (src/release) does nothing.
22
+ *
23
+ * The builder opens the activity with two extras: `AngularLynxDevServer`, the
24
+ * bundle at `http://localhost:<port>/`, which `adb reverse` forwards to the
25
+ * computer whatever network the device is on, and
26
+ * `AngularLynxDevServerOrigin`, the address the bundle was built with (the
27
+ * computer's on the local network), whose URLs are sent to localhost too.
28
+ * They're kept, so opening the app from the launcher later loads from the
29
+ * same server.
30
+ */
31
+ object DevServer {
32
+ private const val EXTRA_URL = "AngularLynxDevServer"
33
+ private const val EXTRA_ORIGIN = "AngularLynxDevServerOrigin"
34
+ private const val PREFERENCES = "angular-lynx-dev-server"
35
+
36
+ private val main = Handler(Looper.getMainLooper())
37
+
38
+ /** The bundle on the dev server, e.g. `http://localhost:3000/main.lynx.bundle`. */
39
+ private var bundle: String? = null
40
+
41
+ /** The folder it's in, which every relative URL the bundle asks for resolves against. */
42
+ private var base: Uri? = null
43
+
44
+ /** The address the bundle was built with, e.g. `http://192.168.1.20:3000`. */
45
+ private var origin: Uri? = null
46
+
47
+ private var reload: (() -> Unit)? = null
48
+ private var status: TextView? = null
49
+ private val retry = Runnable { reload?.invoke() }
50
+
51
+ /** The URL MainActivity renders, or null when it was never given one. */
52
+ fun bundleUrl(activity: Activity): String? {
53
+ val preferences = activity.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE)
54
+ val launched = activity.intent?.getStringExtra(EXTRA_URL)
55
+ if (launched != null) {
56
+ preferences.edit()
57
+ .putString(EXTRA_URL, launched)
58
+ .putString(EXTRA_ORIGIN, activity.intent.getStringExtra(EXTRA_ORIGIN))
59
+ .apply()
60
+ }
61
+ bundle = preferences.getString(EXTRA_URL, null)
62
+ base = bundle?.let { Uri.parse(it.substring(0, it.lastIndexOf('/') + 1)) }
63
+ origin = preferences.getString(EXTRA_ORIGIN, null)?.let(Uri::parse)
64
+ return bundle
65
+ }
66
+
67
+ /**
68
+ * Adds the dev server's modules: the WebSocket rspeedy's live reload
69
+ * listens on, and the reload it asks for (DevServerModules.kt).
70
+ */
71
+ fun register(builder: LynxViewBuilder, reload: () -> Unit) {
72
+ if (bundle == null) return
73
+ this.reload = reload
74
+ builder.registerModule(DevWebSocketModule.NAME, DevWebSocketModule::class.java, null)
75
+ builder.registerModule(DevReloadModule.NAME, DevReloadModule::class.java, null)
76
+ }
77
+
78
+ /** Adds the dev server's status, hidden, over the page. */
79
+ fun attach(container: FrameLayout) {
80
+ val context = container.context
81
+ val view = TextView(context).apply {
82
+ gravity = Gravity.CENTER
83
+ setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
84
+ val padding = (24 * resources.displayMetrics.density).toInt()
85
+ setPadding(padding, padding, padding, padding)
86
+ setBackgroundColor(context.getColor(R.color.window_background))
87
+ visibility = View.GONE
88
+ }
89
+ container.addView(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
90
+ status = view
91
+ if (bundle == null) {
92
+ show(
93
+ "This is the development build of the app, which loads the app from `ng serve`.\n\n" +
94
+ "Run `ng serve --platform android` to open it.",
95
+ )
96
+ }
97
+ }
98
+
99
+ /** Reloads the bundle from the dev server, from any thread. */
100
+ fun reloadBundle() {
101
+ main.post { reload?.invoke() }
102
+ }
103
+
104
+ /**
105
+ * Where a URL the bundle asks for is on the dev server, or null when the
106
+ * app isn't loading from one, or the URL is somewhere else (an API, a
107
+ * CDN, a data: or content: URL).
108
+ */
109
+ fun remoteUrl(url: String): String? {
110
+ val base = base ?: return null
111
+ val uri = Uri.parse(url)
112
+ val scheme = uri.scheme
113
+ if (scheme == "http" || scheme == "https" || scheme == "ws" || scheme == "wss") {
114
+ // The address the bundle was built with: the same server, which
115
+ // this device reaches at localhost.
116
+ if (origin?.encodedAuthority == uri.encodedAuthority) {
117
+ return uri.buildUpon().encodedAuthority(base.encodedAuthority).build().toString()
118
+ }
119
+ return if (uri.encodedAuthority == base.encodedAuthority) url else null
120
+ }
121
+ val path = when {
122
+ url.startsWith("asset:///") -> url.removePrefix("asset:///")
123
+ scheme == null -> url.removePrefix("./").trimStart('/')
124
+ else -> return null
125
+ }
126
+ return base.toString() + path
127
+ }
128
+
129
+ /**
130
+ * Called with the outcome of each load. When the bundle itself didn't
131
+ * load (the server is stopped, or `adb reverse` is gone after the device
132
+ * reconnected), the app says so rather than show nothing, and tries again
133
+ * every two seconds.
134
+ */
135
+ fun loaded(url: String, error: Throwable?) {
136
+ val bundle = bundle ?: return
137
+ if (url != bundle) return
138
+ main.post {
139
+ main.removeCallbacks(retry)
140
+ if (error == null) {
141
+ status?.visibility = View.GONE
142
+ return@post
143
+ }
144
+ show(
145
+ "Can't reach the dev server at ${base?.encodedAuthority}.\n\n${error.message}\n\n" +
146
+ "Is `ng serve --platform android` running? It forwards the port to this device again " +
147
+ "when it starts. Trying again…",
148
+ )
149
+ main.postDelayed(retry, 2000)
150
+ }
151
+ }
152
+
153
+ private fun show(message: String) {
154
+ val status = status ?: return
155
+ status.text = message
156
+ status.visibility = View.VISIBLE
157
+ status.bringToFront()
158
+ }
159
+ }