Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: take a single local file back out of dist after a dev run

youxi7988ab576fae128
diff --git a/bin/dev.ts b/bin/dev.ts
index 02ec81b3..b8b931b1 100644
--- a/bin/dev.ts
+++ b/bin/dev.ts
@@ -3,6 +3,7 @@ import { PakeCliOptions } from './types';
import handleInputOptions from './options/index';
import BuilderProvider from './builders/BuilderProvider';
import { getCliProgram } from './helpers/cli-program';
+import { restoreLocalTree } from './helpers/merge';
const program = getCliProgram();
@@ -16,12 +17,30 @@ program.action(async (url: string, options: PakeCliOptions) => {
log.setDefaultLevel('debug');
- const appOptions = await handleInputOptions(options, url);
- log.debug('PakeAppOptions', appOptions);
+ // The dev entry stages local input into the checkout's own dist/, which
+ // Tauri embeds in every later build: heal a stranded run first and put the
+ // original back when this one ends, fails or is interrupted.
+ restoreLocalTree();
+ for (const [signal, code] of [
+ ['SIGINT', 130],
+ ['SIGTERM', 143],
+ ] as const) {
+ process.once(signal, () => {
+ restoreLocalTree();
+ process.exit(code);
+ });
+ }
- const builder = BuilderProvider.create(appOptions);
- await builder.prepare();
- await builder.start(url);
+ try {
+ const appOptions = await handleInputOptions(options, url);
+ log.debug('PakeAppOptions', appOptions);
+
+ const builder = BuilderProvider.create(appOptions);
+ await builder.prepare();
+ await builder.start(url);
+ } finally {
+ restoreLocalTree();
+ }
});
program.parse();
diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts
index b81d0516..acf49ff7 100644
--- a/bin/helpers/merge.ts
+++ b/bin/helpers/merge.ts
@@ -235,8 +235,13 @@ export async function handleLocalFile(
const distDir = path.join(npmDirectory, 'dist');
if (!useLocalFile) {
- const urlPath = path.join(distDir, fileName);
- await fsExtra.copy(url, urlPath);
+ // Back up the original dist first, as stageLocalTree does, so
+ // restoreLocalTree can take this file out again after the run.
+ const distBakDir = path.join(npmDirectory, 'dist_bak');
+ if (!(await fsExtra.pathExists(distBakDir))) {
+ await fsExtra.copy(distDir, distBakDir);
+ }
+ await fsExtra.copy(url, path.join(distDir, fileName));
} else {
await stageLocalTree(path.dirname(url));
}
diff --git a/dist/cli.js b/dist/cli.js
index 1e06aeec..6ee17fd4 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -1088,8 +1088,13 @@ async function handleLocalFile(url, useLocalFile, tauriConf) {
const fileName = path.basename(url);
const distDir = path.join(npmDirectory, 'dist');
if (!useLocalFile) {
- const urlPath = path.join(distDir, fileName);
- await fsExtra.copy(url, urlPath);
+ // Back up the original dist first, as stageLocalTree does, so
+ // restoreLocalTree can take this file out again after the run.
+ const distBakDir = path.join(npmDirectory, 'dist_bak');
+ if (!(await fsExtra.pathExists(distBakDir))) {
+ await fsExtra.copy(distDir, distBakDir);
+ }
+ await fsExtra.copy(url, path.join(distDir, fileName));
}
else {
await stageLocalTree(path.dirname(url));
diff --git a/tests/unit/local-staging.test.ts b/tests/unit/local-staging.test.ts
index 5d67c3c5..0da7ab1e 100644
--- a/tests/unit/local-staging.test.ts
+++ b/tests/unit/local-staging.test.ts
@@ -95,6 +95,28 @@ describe('stageLocalTree via handleLocalFile', () => {
);
});
+ it('restoreLocalTree also removes a single local file copied into dist', async () => {
+ const userDir = await makeUserDir('single');
+ const conf = makeTauriConf();
+ await handleLocalFile(path.join(userDir, 'index.html'), false, conf);
+
+ const dist = path.join(fakePkg, 'dist');
+ expect(await fsExtra.pathExists(path.join(dist, 'index.html'))).toBe(true);
+ expect(conf.pake.windows[0].url).toBe('index.html');
+
+ restoreLocalTree();
+
+ // Without a backup the copied file stayed in dist and leaked into the
+ // next app built from the same checkout.
+ expect(await fsExtra.pathExists(path.join(dist, 'index.html'))).toBe(false);
+ expect(await fsExtra.readFile(path.join(dist, 'dev.js'), 'utf8')).toBe(
+ 'dev',
+ );
+ expect(await fsExtra.pathExists(path.join(fakePkg, 'dist_bak'))).toBe(
+ false,
+ );
+ });
+
it('restores the original dist when staging fails midway', async () => {
const userDir = await makeUserDir('broken');
// Dangling symlink: copySync with dereference follows it and throws.