Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-flowers-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Only run postinstall script if package.json exists
55 changes: 32 additions & 23 deletions packages/kit/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,44 @@ import glob from 'tiny-glob/sync.js';
import { load_config } from './src/core/config/index.js';
import * as sync from './src/core/sync/sync.js';

const cwd = process.env.INIT_CWD ?? process.cwd();
process.chdir(cwd);
try {
const cwd = process.env.INIT_CWD ?? process.cwd();
process.chdir(cwd);

const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
if (fs.existsSync('package.json')) {
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));

const directories = [];
const directories = [];

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

for (const cwd of directories) {
process.chdir(cwd);
for (const directory of packages) {
directories.push(...glob(directory, { cwd }).map((dir) => path.resolve(cwd, dir)));
}
} else {
directories.push(cwd);
}

for (const cwd of directories) {
process.chdir(cwd);

if (!fs.existsSync('package.json')) continue;
if (!fs.existsSync('svelte.config.js')) continue;
if (!fs.existsSync('package.json')) continue;
if (!fs.existsSync('svelte.config.js')) continue;

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

try {
const config = await load_config();
await sync.all(config, 'development');
} catch (e) {
console.log('Error while trying to sync SvelteKit config');
console.log(e);
try {
const config = await load_config();
await sync.all(config, 'development');
} catch (error) {
console.log('Error while trying to sync SvelteKit config');
console.log(error.stack);
}
}
}
} catch (error) {
console.error(error.stack);
}