Skip to content

Commit 0af5951

Browse files
authored
Merge branch 'main' into prettier-and-eslint-fixes
2 parents 372a78a + bfbc40c commit 0af5951

File tree

7 files changed

+162
-16
lines changed

7 files changed

+162
-16
lines changed

config/nativephp-internal.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'storage/hot',
9090

9191
// Only needed for local testing
92+
'vendor/nativephp/desktop/.git',
9293
'vendor/nativephp/desktop/resources',
9394
'vendor/nativephp/desktop/vendor',
9495
'vendor/nativephp/php-bin',

resources/electron/electron-builder.mjs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const deepLinkProtocol = process.env.NATIVEPHP_DEEPLINK_SCHEME;
1313
const updaterEnabled = process.env.NATIVEPHP_UPDATER_ENABLED === 'true';
1414

1515
// Azure signing configuration
16-
const azurePublisherName = process.env.NATIVEPHP_AZURE_PUBLISHER_NAME;
1716
const azureEndpoint = process.env.NATIVEPHP_AZURE_ENDPOINT;
1817
const azureCertificateProfileName = process.env.NATIVEPHP_AZURE_CERTIFICATE_PROFILE_NAME;
1918
const azureCodeSigningAccountName = process.env.NATIVEPHP_AZURE_CODE_SIGNING_ACCOUNT_NAME;
@@ -83,15 +82,14 @@ export default {
8382
afterSign: 'build/notarize.js',
8483
win: {
8584
executableName: fileName,
86-
...(azurePublisherName && azureEndpoint && azureCertificateProfileName && azureCodeSigningAccountName
85+
...(azureEndpoint && azureCertificateProfileName && azureCodeSigningAccountName
8786
? {
88-
azureSignOptions: {
89-
publisherName: azurePublisherName,
90-
endpoint: azureEndpoint,
91-
certificateProfileName: azureCertificateProfileName,
92-
codeSigningAccountName: azureCodeSigningAccountName,
93-
},
94-
}
87+
azureSignOptions: {
88+
endpoint: azureEndpoint,
89+
certificateProfileName: azureCertificateProfileName,
90+
codeSigningAccountName: azureCodeSigningAccountName
91+
},
92+
}
9593
: {}),
9694
},
9795
nsis: {

resources/electron/electron-plugin/dist/server/php.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
77
step((generator = generator.apply(thisArg, _arguments || [])).next());
88
});
99
};
10-
import { mkdirSync, statSync, writeFileSync, existsSync } from 'fs';
10+
import { mkdirSync, statSync, writeFileSync, existsSync, readFileSync } from 'fs';
1111
import fs_extra from 'fs-extra';
1212
const { copySync, mkdirpSync } = fs_extra;
1313
import Store from 'electron-store';
@@ -40,6 +40,53 @@ function shouldMigrateDatabase(store) {
4040
function shouldOptimize(store) {
4141
return process.env.NODE_ENV !== 'development';
4242
}
43+
function hasNightwatchInstalled(appPath) {
44+
const candidateRoots = [
45+
appPath,
46+
join(appPath, "build", "__nativephp_app_bundle")
47+
];
48+
for (const root of candidateRoots) {
49+
if (existsSync(join(root, "vendor", "laravel", "nightwatch"))) {
50+
return true;
51+
}
52+
const composerLock = join(root, "composer.lock");
53+
if (!existsSync(composerLock)) {
54+
continue;
55+
}
56+
try {
57+
if (readFileSync(composerLock, "utf8").includes("\"name\": \"laravel/nightwatch\"")) {
58+
return true;
59+
}
60+
}
61+
catch (_a) {
62+
}
63+
}
64+
return false;
65+
}
66+
function getNightwatchToken(appPath) {
67+
if (process.env.NIGHTWATCH_TOKEN) {
68+
return process.env.NIGHTWATCH_TOKEN;
69+
}
70+
const candidateRoots = [
71+
appPath,
72+
join(appPath, "build", "__nativephp_app_bundle")
73+
];
74+
for (const root of candidateRoots) {
75+
const envPath = join(root, ".env");
76+
if (!existsSync(envPath)) {
77+
continue;
78+
}
79+
try {
80+
const content = readFileSync(envPath, "utf8");
81+
const match = content.match(/^NIGHTWATCH_TOKEN=(.+)$/m);
82+
if (match && match[1]) {
83+
return match[1].replace(/^['"]|['"]$/g, "");
84+
}
85+
}
86+
catch (_a) {
87+
}
88+
}
89+
}
4390
function getPhpPort() {
4491
return __awaiter(this, void 0, void 0, function* () {
4592
const suggestedPort = yield getPort({
@@ -229,13 +276,28 @@ function serveApp(secret, apiPort, phpIniSettings) {
229276
ensureAppFoldersAreAvailable();
230277
console.log('Making sure app folders are available');
231278
const env = getDefaultEnvironmentVariables(secret, apiPort);
279+
const nightwatchToken = getNightwatchToken(appPath);
280+
let phpNightWatchPort;
281+
if (nightwatchToken && hasNightwatchInstalled(appPath)) {
282+
phpNightWatchPort = yield getPhpPort();
283+
env.NIGHTWATCH_TOKEN = nightwatchToken;
284+
env.NIGHTWATCH_INGEST_URI = `127.0.0.1:${phpNightWatchPort}`;
285+
}
286+
else if (nightwatchToken) {
287+
console.log("Skipping Nightwatch: package not installed.");
288+
}
232289
const phpOptions = {
233290
cwd: appPath,
234291
env
235292
};
236293
const store = new Store({
237294
name: 'nativephp',
238295
});
296+
if (env.NIGHTWATCH_INGEST_URI && phpNightWatchPort) {
297+
console.log('Starting Nightwatch server...');
298+
callPhp(['artisan', 'nightwatch:agent', `--listen-on=${env.NIGHTWATCH_INGEST_URI}`], phpOptions, phpIniSettings);
299+
console.log('Nightwatch server started on port:', phpNightWatchPort);
300+
}
239301
if (shouldOptimize(store)) {
240302
console.log('Caching view and routes...');
241303
let result = callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings);

resources/electron/electron-plugin/src/server/php.ts

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, mkdirSync, statSync, writeFileSync } from 'fs';
1+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'fs';
22
import fs_extra from 'fs-extra';
33

44
const { copySync, mkdirpSync } = fs_extra;
@@ -46,6 +46,65 @@ function shouldOptimize() {
4646
// return runningSecureBuild();
4747
}
4848

49+
function hasNightwatchInstalled(appPath: string) {
50+
const candidateRoots = [
51+
appPath,
52+
join(appPath, "build", "__nativephp_app_bundle")
53+
];
54+
55+
for (const root of candidateRoots) {
56+
if (existsSync(join(root, "vendor", "laravel", "nightwatch"))) {
57+
return true;
58+
}
59+
60+
const composerLock = join(root, "composer.lock");
61+
62+
if (!existsSync(composerLock)) {
63+
continue;
64+
}
65+
66+
try {
67+
if (readFileSync(composerLock, "utf8").includes("\"name\": \"laravel/nightwatch\"")) {
68+
return true;
69+
}
70+
} catch {
71+
// ignore and keep looking
72+
}
73+
}
74+
75+
return false;
76+
}
77+
78+
function getNightwatchToken(appPath: string) {
79+
if (process.env.NIGHTWATCH_TOKEN) {
80+
return process.env.NIGHTWATCH_TOKEN;
81+
}
82+
83+
const candidateRoots = [
84+
appPath,
85+
join(appPath, "build", "__nativephp_app_bundle")
86+
];
87+
88+
for (const root of candidateRoots) {
89+
const envPath = join(root, ".env");
90+
91+
if (!existsSync(envPath)) {
92+
continue;
93+
}
94+
95+
try {
96+
const content = readFileSync(envPath, "utf8");
97+
const match = content.match(/^NIGHTWATCH_TOKEN=(.+)$/m);
98+
99+
if (match && match[1]) {
100+
return match[1].replace(/^['"]|['"]$/g, "");
101+
}
102+
} catch {
103+
// ignore and keep looking
104+
}
105+
}
106+
}
107+
49108
async function getPhpPort() {
50109
// Try get-port first (fast path)
51110
const suggestedPort = await getPort({
@@ -270,7 +329,7 @@ interface EnvironmentVariables {
270329
APP_ROUTES_CACHE?: string;
271330
APP_EVENTS_CACHE?: string;
272331
VIEW_COMPILED_PATH?: string;
273-
332+
NIGHTWATCH_TOKEN?: string;
274333
NIGHTWATCH_INGEST_URI?: string;
275334
}
276335

@@ -336,15 +395,19 @@ async function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult>
336395

337396
const env = getDefaultEnvironmentVariables(secret, apiPort);
338397

398+
const nightwatchToken = getNightwatchToken(appPath);
339399
let phpNightWatchPort: number | undefined;
340-
if (process.env.NIGHTWATCH_TOKEN) {
400+
if (nightwatchToken && hasNightwatchInstalled(appPath)) {
341401
phpNightWatchPort = await getPhpPort();
402+
env.NIGHTWATCH_TOKEN = nightwatchToken;
342403
env.NIGHTWATCH_INGEST_URI = `127.0.0.1:${phpNightWatchPort}`;
404+
} else if (nightwatchToken) {
405+
console.log("Skipping Nightwatch: package not installed.");
343406
}
344407

345408
const phpOptions = {
346409
cwd: appPath,
347-
env,
410+
env
348411
};
349412

350413
const store = new Store({

routes/api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
use Native\Desktop\Http\Controllers\CreateSecurityCookieController;
66
use Native\Desktop\Http\Controllers\DispatchEventFromAppController;
77
use Native\Desktop\Http\Controllers\NativeAppBootedController;
8+
use Native\Desktop\Http\Middleware\OptionalNightwatchNever;
89
use Native\Desktop\Http\Middleware\PreventRegularBrowserAccess;
910

10-
Route::group(['middleware' => PreventRegularBrowserAccess::class], function () {
11+
Route::group(['middleware' => [OptionalNightwatchNever::class, PreventRegularBrowserAccess::class]], function () {
1112
Route::post('_native/api/booted', NativeAppBootedController::class);
1213
Route::post('_native/api/events', DispatchEventFromAppController::class);
1314
})->withoutMiddleware(VerifyCsrfToken::class);
1415

15-
Route::get('_native/api/cookie', CreateSecurityCookieController::class);
16+
Route::get('_native/api/cookie', CreateSecurityCookieController::class)->middleware(OptionalNightwatchNever::class);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Native\Desktop\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class OptionalNightwatchNever
9+
{
10+
public function handle(Request $request, Closure $next): mixed
11+
{
12+
if (class_exists(\Laravel\Nightwatch\Http\Middleware\Sample::class)) {
13+
$middleware = app(\Laravel\Nightwatch\Http\Middleware\Sample::class);
14+
15+
return $middleware->handle($request, $next, 0.0);
16+
}
17+
18+
return $next($request);
19+
}
20+
}

src/QueueWorker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function up(string|QueueConfig $config): void
3636
"--memory={$config->memoryLimit}",
3737
"--timeout={$config->timeout}",
3838
"--sleep={$config->sleep}",
39+
'--quiet',
3940
],
4041
'queue_'.$config->alias,
4142
persistent: true,

0 commit comments

Comments
 (0)