Skip to content

Commit 84ad168

Browse files
only run postinstall if package.json exists (#6998)
* only run postinstall if package.json exists - closes #6987 * Update packages/kit/postinstall.js Co-authored-by: Simon H <[email protected]> * belt and braces * accommodate yarn's... idiosyncracies Co-authored-by: Simon H <[email protected]>
1 parent ec0f943 commit 84ad168

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

.changeset/hungry-flowers-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Only run postinstall script if package.json exists

packages/kit/postinstall.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,44 @@ import glob from 'tiny-glob/sync.js';
44
import { load_config } from './src/core/config/index.js';
55
import * as sync from './src/core/sync/sync.js';
66

7-
const cwd = process.env.INIT_CWD ?? process.cwd();
8-
process.chdir(cwd);
7+
try {
8+
const cwd = process.env.INIT_CWD ?? process.cwd();
9+
process.chdir(cwd);
910

10-
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
11+
if (fs.existsSync('package.json')) {
12+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
1113

12-
const directories = [];
14+
const directories = [];
1315

14-
if (pkg.workspaces) {
15-
for (const directory of pkg.workspaces) {
16-
directories.push(...glob(directory, { cwd }).map((dir) => path.resolve(cwd, dir)));
17-
}
18-
} else {
19-
directories.push(cwd);
20-
}
16+
if (pkg.workspaces) {
17+
// we have to do this because of https://classic.yarnpkg.com/blog/2018/02/15/nohoist/
18+
const packages = Array.isArray(pkg.workspaces) ? pkg.workspaces : pkg.workspaces.packages;
2119

22-
for (const cwd of directories) {
23-
process.chdir(cwd);
20+
for (const directory of packages) {
21+
directories.push(...glob(directory, { cwd }).map((dir) => path.resolve(cwd, dir)));
22+
}
23+
} else {
24+
directories.push(cwd);
25+
}
26+
27+
for (const cwd of directories) {
28+
process.chdir(cwd);
2429

25-
if (!fs.existsSync('package.json')) continue;
26-
if (!fs.existsSync('svelte.config.js')) continue;
30+
if (!fs.existsSync('package.json')) continue;
31+
if (!fs.existsSync('svelte.config.js')) continue;
2732

28-
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
29-
if (!pkg.dependencies?.['@sveltejs/kit'] && !pkg.devDependencies?.['@sveltejs/kit']) continue;
33+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
34+
if (!pkg.dependencies?.['@sveltejs/kit'] && !pkg.devDependencies?.['@sveltejs/kit']) continue;
3035

31-
try {
32-
const config = await load_config();
33-
await sync.all(config, 'development');
34-
} catch (e) {
35-
console.log('Error while trying to sync SvelteKit config');
36-
console.log(e);
36+
try {
37+
const config = await load_config();
38+
await sync.all(config, 'development');
39+
} catch (error) {
40+
console.log('Error while trying to sync SvelteKit config');
41+
console.log(error.stack);
42+
}
43+
}
3744
}
45+
} catch (error) {
46+
console.error(error.stack);
3847
}

0 commit comments

Comments
 (0)