Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postinstall sync #5760

Merged
merged 12 commits into from
Jul 31, 2022
6 changes: 6 additions & 0 deletions .changeset/soft-masks-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

Use @sveltejs/kit postinstall lifecycle hook to invoke 'svelte-kit sync' instead of prepare in projects created by create-svelte
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
env:
# not needed for linting
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'
steps:
- uses: actions/checkout@v3
- uses: pnpm/[email protected]
Expand All @@ -38,7 +38,7 @@ jobs:
fail-fast: false
matrix:
node-version: [16]
os: [ubuntu-latest , windows-latest]
os: [ubuntu-latest, windows-latest]
e2e-browser: ['chromium']
include:
- node-version: 16
Expand All @@ -53,7 +53,7 @@ jobs:
env:
TURBO_CACHE_KEY: ${{ matrix.os }}-${{ matrix.node-version }}
# Install playwright's binray under node_modules so it will be cached together
PLAYWRIGHT_BROWSERS_PATH: "0"
PLAYWRIGHT_BROWSERS_PATH: '0'
KIT_E2E_BROWSER: ${{matrix.e2e-browser}}
steps:
- run: git config --global core.autocrlf false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"dev": "vite dev",
"build": "vite build",
"package": "svelte-kit package",
"preview": "vite preview",
"prepare": "svelte-kit sync"
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/adapter-auto": "next",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"dev": "vite dev",
"build": "vite build",
"package": "svelte-kit package",
"preview": "vite preview",
"prepare": "svelte-kit sync"
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/adapter-auto": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\" -i packaging",
"test:typings": "tsc --project test/typings",
"test:packaging": "uvu src/packaging \"(spec\\.js|test[\\\\/]index\\.js)\"",
"types": "node scripts/extract-types.js"
"types": "node scripts/extract-types.js",
"postinstall": "node svelte-kit.js sync"
},
"exports": {
"./package.json": "./package.json",
Expand Down
19 changes: 16 additions & 3 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import path from 'path';
import colors from 'kleur';
import sade from 'sade';
import { load_config } from './core/config/index.js';
Expand Down Expand Up @@ -40,13 +41,25 @@ prog
.describe('Synchronise generated files')
.option('--mode', 'Specify a mode for loading environment variables', 'development')
.action(async ({ mode }) => {
if (!fs.existsSync('svelte.config.js')) {
console.warn('Missing svelte.config.js — skipping');
const event = process.env.npm_lifecycle_event;

// TODO remove for 1.0
if (event === 'prepare') {
dominikg marked this conversation as resolved.
Show resolved Hide resolved
const message = `\`svelte-kit sync\` now runs on "postinstall" — please remove the "prepare" script from your package.json\n`;
console.error(colors.bold().red(message));
return;
}

const cwd = event === 'postinstall' ? process.env.INIT_CWD ?? '' : process.cwd();

const svelte_config_file = path.join(cwd, 'svelte.config.js');
if (!fs.existsSync(svelte_config_file)) {
console.warn(`Missing ${svelte_config_file} — skipping`);
return;
}

try {
const config = await load_config();
const config = await load_config({ cwd });
const sync = await import('./core/sync/sync.js');
sync.all(config, mode);
} catch (error) {
Expand Down
11 changes: 10 additions & 1 deletion packages/kit/svelte-kit.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
#!/usr/bin/env node
import './dist/cli.js';
import fs from 'fs';
import { fileURLToPath } from 'url';

// in our own CI, and when deploying directly from this monorepo,
// the `dist` directory will not exist yet
if (fs.existsSync(fileURLToPath(new URL('./dist', import.meta.url)))) {
import('./dist/cli.js');
dominikg marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.error('Run "pnpm build" and try running this command again');
}
3 changes: 1 addition & 2 deletions packages/kit/test/apps/amp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
"test:dev": "cross-env DEV=true playwright test",
"test:build": "playwright test"
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/apps/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"check": "svelte-kit sync && tsc && svelte-check",
"check": "node ../../cli.js sync && tsc && svelte-check",

without this is always goes to dist for me.
And do we want to keep the prepare script too to work around the fact that pnpm doesn't call postinstall for workspace dependencies in a monorepo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, changed it to ../../cli.js so we can test out changes to the sync script without rebuilding.

IIRC the prepare script was running before stuff was built, which meant it didn't work (though that would have been fixed by the ../../cli.js I guess) but in any case "prepare": "svelte-kit sync" is now a no-op, so there's no reason to keep it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of prepare with the ../../cli.js call too, so that when you pull changes and do pnpm i it syncs to avoid red squiggles.

"test": "npm run test:dev && npm run test:build",
"test:dev": "rimraf test/errors.json && cross-env DEV=true playwright test",
"test:build": "rimraf test/errors.json && playwright test"
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/apps/options-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
"test:dev": "cross-env DEV=true playwright test",
"test:build": "playwright test"
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/apps/options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev -c vite.custom.config.js",
"build": "vite build -c vite.custom.config.js --mode custom",
"preview": "vite preview -c vite.custom.config.js",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
"test:dev": "cross-env DEV=true playwright test",
"test:build": "playwright test"
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/apps/writes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
"test:dev": "rimraf test/errors.json && cross-env DEV=true playwright test",
"test:build": "rimraf test/errors.json && playwright test"
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/prerendering/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run build && uvu test"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/prerendering/fallback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run build"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/prerendering/options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run build && uvu test"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/prerendering/paths-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run build && uvu test"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/test/prerendering/trailing-slash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"check": "svelte-kit sync && tsc && svelte-check",
"test": "npm run build && uvu test"
},
"devDependencies": {
Expand Down