From bdd671e5f1f71bdb2ba7fdbd86b8a6f65a2c371b Mon Sep 17 00:00:00 2001 From: dominikg Date: Thu, 21 Jul 2022 11:25:59 +0200 Subject: [PATCH 01/11] fix: use postinstall hook to run sync instead of prepare --- .changeset/soft-masks-trade.md | 6 +++++ .../templates/default/package.template.json | 3 +-- .../templates/skeleton/package.template.json | 3 +-- packages/kit/package.json | 3 ++- packages/kit/src/cli.js | 27 ++++++++++++++++--- packages/kit/test/apps/amp/package.json | 1 - packages/kit/test/apps/basics/package.json | 1 - packages/kit/test/apps/options-2/package.json | 1 - packages/kit/test/apps/options/package.json | 1 - packages/kit/test/apps/writes/package.json | 1 - .../kit/test/prerendering/basics/package.json | 1 - .../test/prerendering/fallback/package.json | 1 - .../test/prerendering/options/package.json | 1 - .../test/prerendering/paths-base/package.json | 1 - .../prerendering/trailing-slash/package.json | 1 - 15 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 .changeset/soft-masks-trade.md diff --git a/.changeset/soft-masks-trade.md b/.changeset/soft-masks-trade.md new file mode 100644 index 000000000000..36c01cfaa824 --- /dev/null +++ b/.changeset/soft-masks-trade.md @@ -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 diff --git a/packages/create-svelte/templates/default/package.template.json b/packages/create-svelte/templates/default/package.template.json index 1fb109ee082f..3e51b6a62117 100644 --- a/packages/create-svelte/templates/default/package.template.json +++ b/packages/create-svelte/templates/default/package.template.json @@ -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", diff --git a/packages/create-svelte/templates/skeleton/package.template.json b/packages/create-svelte/templates/skeleton/package.template.json index 095414961acb..6168c971b6ac 100644 --- a/packages/create-svelte/templates/skeleton/package.template.json +++ b/packages/create-svelte/templates/skeleton/package.template.json @@ -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:*", diff --git a/packages/kit/package.json b/packages/kit/package.json index 3947d99076ab..03328651e8e8 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -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", diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index 3f1a28f3764e..02ca7630fa86 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -1,9 +1,11 @@ import fs from 'fs'; +import path from 'path'; import colors from 'kleur'; import sade from 'sade'; import { load_config } from './core/config/index.js'; import { coalesce_to_error } from './utils/error.js'; + /** @param {unknown} e */ function handle_error(e) { const error = coalesce_to_error(e); @@ -39,13 +41,32 @@ prog .command('sync') .describe('Synchronise generated files') .action(async () => { - if (!fs.existsSync('svelte.config.js')) { - console.warn('Missing svelte.config.js — skipping'); + const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; + const cwd = is_postinstall ? (process.env.INIT_CWD || '') : process.cwd(); + const config_file = path.join(cwd,'svelte.config.js'); + const pkg_file = path.join(cwd,'package.json'); + let is_self = false; + if(fs.existsSync(pkg_file)) { + try { + const pkg = JSON.parse(fs.readFileSync(pkg_file,'utf-8')); + is_self = pkg.name === '@sveltejs/kit'; + if (pkg.scripts?.prepare === 'svelte-kit sync') { + const message =`script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; + console.error(colors.bold().red(message)); + } + } catch (e) { + // ignore, we can be sure that our own package.json exists and can be parsed, so it is not self + } + } + if (is_self || !fs.existsSync(config_file)) { + if (!is_postinstall) { + console.warn(`Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync`); + } return; } try { - const config = await load_config(); + const config = await load_config({cwd}); const sync = await import('./core/sync/sync.js'); sync.all(config); } catch (error) { diff --git a/packages/kit/test/apps/amp/package.json b/packages/kit/test/apps/amp/package.json index 9e3ce6607092..1ba74c43208d 100644 --- a/packages/kit/test/apps/amp/package.json +++ b/packages/kit/test/apps/amp/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", diff --git a/packages/kit/test/apps/basics/package.json b/packages/kit/test/apps/basics/package.json index eee4f24d884a..3881249ea288 100644 --- a/packages/kit/test/apps/basics/package.json +++ b/packages/kit/test/apps/basics/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "rimraf test/errors.json && cross-env DEV=true playwright test", diff --git a/packages/kit/test/apps/options-2/package.json b/packages/kit/test/apps/options-2/package.json index 1c2dc5e57fd8..54b69641da68 100644 --- a/packages/kit/test/apps/options-2/package.json +++ b/packages/kit/test/apps/options-2/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", diff --git a/packages/kit/test/apps/options/package.json b/packages/kit/test/apps/options/package.json index ccb66c4d310c..b21decb73ea2 100644 --- a/packages/kit/test/apps/options/package.json +++ b/packages/kit/test/apps/options/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build --mode custom", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", diff --git a/packages/kit/test/apps/writes/package.json b/packages/kit/test/apps/writes/package.json index 6feb75ddee38..24f3ca4e37ab 100644 --- a/packages/kit/test/apps/writes/package.json +++ b/packages/kit/test/apps/writes/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "rimraf test/errors.json && cross-env DEV=true playwright test", diff --git a/packages/kit/test/prerendering/basics/package.json b/packages/kit/test/prerendering/basics/package.json index aa2545571454..4f6f4e0156a7 100644 --- a/packages/kit/test/prerendering/basics/package.json +++ b/packages/kit/test/prerendering/basics/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run build && uvu test" }, diff --git a/packages/kit/test/prerendering/fallback/package.json b/packages/kit/test/prerendering/fallback/package.json index e8befc6b7305..0d04a9440c65 100644 --- a/packages/kit/test/prerendering/fallback/package.json +++ b/packages/kit/test/prerendering/fallback/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run build" }, diff --git a/packages/kit/test/prerendering/options/package.json b/packages/kit/test/prerendering/options/package.json index 66aae51545ab..2b2250e821f8 100644 --- a/packages/kit/test/prerendering/options/package.json +++ b/packages/kit/test/prerendering/options/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run build && uvu test" }, diff --git a/packages/kit/test/prerendering/paths-base/package.json b/packages/kit/test/prerendering/paths-base/package.json index e4cf5e4869fe..2548a401f303 100644 --- a/packages/kit/test/prerendering/paths-base/package.json +++ b/packages/kit/test/prerendering/paths-base/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", "test": "npm run build && uvu test" }, diff --git a/packages/kit/test/prerendering/trailing-slash/package.json b/packages/kit/test/prerendering/trailing-slash/package.json index cc7a15ebe36e..450d2aca4151 100644 --- a/packages/kit/test/prerendering/trailing-slash/package.json +++ b/packages/kit/test/prerendering/trailing-slash/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check" }, "devDependencies": { From 220b1a738cc38cf970b4276698e31574ac649c2d Mon Sep 17 00:00:00 2001 From: dominikg Date: Thu, 21 Jul 2022 11:27:45 +0200 Subject: [PATCH 02/11] chore: fix format --- packages/kit/src/cli.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index 02ca7630fa86..0104b804c357 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -5,7 +5,6 @@ import sade from 'sade'; import { load_config } from './core/config/index.js'; import { coalesce_to_error } from './utils/error.js'; - /** @param {unknown} e */ function handle_error(e) { const error = coalesce_to_error(e); @@ -42,31 +41,33 @@ prog .describe('Synchronise generated files') .action(async () => { const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; - const cwd = is_postinstall ? (process.env.INIT_CWD || '') : process.cwd(); - const config_file = path.join(cwd,'svelte.config.js'); - const pkg_file = path.join(cwd,'package.json'); + const cwd = is_postinstall ? process.env.INIT_CWD || '' : process.cwd(); + const config_file = path.join(cwd, 'svelte.config.js'); + const pkg_file = path.join(cwd, 'package.json'); let is_self = false; - if(fs.existsSync(pkg_file)) { + if (fs.existsSync(pkg_file)) { try { - const pkg = JSON.parse(fs.readFileSync(pkg_file,'utf-8')); + const pkg = JSON.parse(fs.readFileSync(pkg_file, 'utf-8')); is_self = pkg.name === '@sveltejs/kit'; if (pkg.scripts?.prepare === 'svelte-kit sync') { - const message =`script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; + const message = `script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; console.error(colors.bold().red(message)); } - } catch (e) { + } catch (e) { // ignore, we can be sure that our own package.json exists and can be parsed, so it is not self } } if (is_self || !fs.existsSync(config_file)) { if (!is_postinstall) { - console.warn(`Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync`); + console.warn( + `Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync` + ); } return; } try { - const config = await load_config({cwd}); + const config = await load_config({ cwd }); const sync = await import('./core/sync/sync.js'); sync.all(config); } catch (error) { From b3c34cca4dccb1339752cc5fe11bdec3ca2cff9b Mon Sep 17 00:00:00 2001 From: dominikg Date: Thu, 21 Jul 2022 15:36:31 +0200 Subject: [PATCH 03/11] fix: add detection to workspace'd kit - in a very ugly way --- packages/kit/src/cli.js | 26 ++++---------------------- packages/kit/svelte-kit.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index 0104b804c357..064634b9fbf5 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -41,28 +41,10 @@ prog .describe('Synchronise generated files') .action(async () => { const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; - const cwd = is_postinstall ? process.env.INIT_CWD || '' : process.cwd(); - const config_file = path.join(cwd, 'svelte.config.js'); - const pkg_file = path.join(cwd, 'package.json'); - let is_self = false; - if (fs.existsSync(pkg_file)) { - try { - const pkg = JSON.parse(fs.readFileSync(pkg_file, 'utf-8')); - is_self = pkg.name === '@sveltejs/kit'; - if (pkg.scripts?.prepare === 'svelte-kit sync') { - const message = `script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; - console.error(colors.bold().red(message)); - } - } catch (e) { - // ignore, we can be sure that our own package.json exists and can be parsed, so it is not self - } - } - if (is_self || !fs.existsSync(config_file)) { - if (!is_postinstall) { - console.warn( - `Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync` - ); - } + const cwd = is_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; } diff --git a/packages/kit/svelte-kit.js b/packages/kit/svelte-kit.js index d17768a371d7..a9c74aaaba8d 100755 --- a/packages/kit/svelte-kit.js +++ b/packages/kit/svelte-kit.js @@ -1,2 +1,37 @@ #!/usr/bin/env node -import './dist/cli.js'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; +const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; +const cwd = is_postinstall ? process.env.INIT_CWD ?? '' : process.cwd(); +const svelte_config_file = path.join(cwd, 'svelte.config.js'); +const pkg_file = path.join(cwd, 'package.json'); +let is_self = false; +let is_workspace = false; +if (is_postinstall && fs.existsSync(pkg_file)) { + try { + const pkg = JSON.parse(fs.readFileSync(pkg_file, 'utf-8')); + is_self = pkg.name === '@sveltejs/kit'; + is_workspace = pkg.devDependencies?.['@sveltejs/kit']?.startsWith('workspace:') ?? false; + // TODO remove for 1.0 + if (pkg.scripts?.prepare === 'svelte-kit sync') { + const message = `script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; + console.error(message); + } + } catch (e) { + // ignore, we can be sure that our own package.json exists and can be parsed, so it is not self + } +} + +if (is_self || !fs.existsSync(svelte_config_file)) { + if (!is_postinstall) { + console.warn( + `Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync` + ); + } +} else { + const cliPath = is_workspace + ? fileURLToPath(new URL('./src/cli.js', import.meta.url)) + : '@sveltejs/kit/dist/cli.js'; + await import(cliPath); +} From 2a5c4fd817fb586dab6f9c96155475819dbda30b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 29 Jul 2022 18:30:59 -0400 Subject: [PATCH 04/11] update check scripts to compensate for lack of postinstall --- packages/kit/test/apps/amp/package.json | 2 +- packages/kit/test/apps/basics/package.json | 2 +- packages/kit/test/apps/options-2/package.json | 2 +- packages/kit/test/apps/options/package.json | 2 +- packages/kit/test/apps/writes/package.json | 2 +- packages/kit/test/prerendering/basics/package.json | 2 +- packages/kit/test/prerendering/fallback/package.json | 2 +- packages/kit/test/prerendering/options/package.json | 2 +- packages/kit/test/prerendering/paths-base/package.json | 2 +- packages/kit/test/prerendering/trailing-slash/package.json | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/kit/test/apps/amp/package.json b/packages/kit/test/apps/amp/package.json index 1ba74c43208d..bdcaa43af4e7 100644 --- a/packages/kit/test/apps/amp/package.json +++ b/packages/kit/test/apps/amp/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "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" diff --git a/packages/kit/test/apps/basics/package.json b/packages/kit/test/apps/basics/package.json index 3881249ea288..06a472f7f57d 100644 --- a/packages/kit/test/apps/basics/package.json +++ b/packages/kit/test/apps/basics/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "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" diff --git a/packages/kit/test/apps/options-2/package.json b/packages/kit/test/apps/options-2/package.json index 54b69641da68..096eca6c80a7 100644 --- a/packages/kit/test/apps/options-2/package.json +++ b/packages/kit/test/apps/options-2/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "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" diff --git a/packages/kit/test/apps/options/package.json b/packages/kit/test/apps/options/package.json index e26a6daa66a7..5da7c11fc3b9 100644 --- a/packages/kit/test/apps/options/package.json +++ b/packages/kit/test/apps/options/package.json @@ -6,7 +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", - "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" diff --git a/packages/kit/test/apps/writes/package.json b/packages/kit/test/apps/writes/package.json index 24f3ca4e37ab..396e62815efe 100644 --- a/packages/kit/test/apps/writes/package.json +++ b/packages/kit/test/apps/writes/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "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" diff --git a/packages/kit/test/prerendering/basics/package.json b/packages/kit/test/prerendering/basics/package.json index 4f6f4e0156a7..3c0ad3cca129 100644 --- a/packages/kit/test/prerendering/basics/package.json +++ b/packages/kit/test/prerendering/basics/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "tsc && svelte-check", + "check": "svelte-kit sync && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/fallback/package.json b/packages/kit/test/prerendering/fallback/package.json index 0d04a9440c65..cd2f0c4c7c30 100644 --- a/packages/kit/test/prerendering/fallback/package.json +++ b/packages/kit/test/prerendering/fallback/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "tsc && svelte-check", + "check": "svelte-kit sync && tsc && svelte-check", "test": "npm run build" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/options/package.json b/packages/kit/test/prerendering/options/package.json index 2b2250e821f8..f83b597edf9a 100644 --- a/packages/kit/test/prerendering/options/package.json +++ b/packages/kit/test/prerendering/options/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "tsc && svelte-check", + "check": "svelte-kit sync && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/paths-base/package.json b/packages/kit/test/prerendering/paths-base/package.json index 2548a401f303..6ad24e692f7a 100644 --- a/packages/kit/test/prerendering/paths-base/package.json +++ b/packages/kit/test/prerendering/paths-base/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "tsc && svelte-check", + "check": "svelte-kit sync && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/trailing-slash/package.json b/packages/kit/test/prerendering/trailing-slash/package.json index a8bf773b3ee6..2fa3e9d82529 100644 --- a/packages/kit/test/prerendering/trailing-slash/package.json +++ b/packages/kit/test/prerendering/trailing-slash/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "tsc && svelte-check", + "check": "svelte-kit sync && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { From 483dcddb607928a98d099b7143295d200a99da44 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 29 Jul 2022 18:50:22 -0400 Subject: [PATCH 05/11] see if this works --- packages/kit/svelte-kit.js | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/packages/kit/svelte-kit.js b/packages/kit/svelte-kit.js index a9c74aaaba8d..d17768a371d7 100755 --- a/packages/kit/svelte-kit.js +++ b/packages/kit/svelte-kit.js @@ -1,37 +1,2 @@ #!/usr/bin/env node -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; -const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; -const cwd = is_postinstall ? process.env.INIT_CWD ?? '' : process.cwd(); -const svelte_config_file = path.join(cwd, 'svelte.config.js'); -const pkg_file = path.join(cwd, 'package.json'); -let is_self = false; -let is_workspace = false; -if (is_postinstall && fs.existsSync(pkg_file)) { - try { - const pkg = JSON.parse(fs.readFileSync(pkg_file, 'utf-8')); - is_self = pkg.name === '@sveltejs/kit'; - is_workspace = pkg.devDependencies?.['@sveltejs/kit']?.startsWith('workspace:') ?? false; - // TODO remove for 1.0 - if (pkg.scripts?.prepare === 'svelte-kit sync') { - const message = `script "prepare": "svelte-kit sync" in ${pkg_file} is no longer needed. Remove it.`; - console.error(message); - } - } catch (e) { - // ignore, we can be sure that our own package.json exists and can be parsed, so it is not self - } -} - -if (is_self || !fs.existsSync(svelte_config_file)) { - if (!is_postinstall) { - console.warn( - `Your project at ${cwd} does not have a svelte.config.js — skipping svelte-kit sync` - ); - } -} else { - const cliPath = is_workspace - ? fileURLToPath(new URL('./src/cli.js', import.meta.url)) - : '@sveltejs/kit/dist/cli.js'; - await import(cliPath); -} +import './dist/cli.js'; From 3e9ef4c225e1955e8d64483d3819ef23612a220d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 30 Jul 2022 15:46:52 -0400 Subject: [PATCH 06/11] add --ignore-scripts --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d68d443f9cf6..8ac12c421bb5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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/action-setup@v2.2.2 @@ -28,7 +28,7 @@ jobs: with: node-version: '16.x' cache: pnpm - - run: pnpm install --frozen-lockfile + - run: pnpm install --frozen-lockfile --ignore-scripts - run: cd packages/kit && pnpm build - run: pnpm turbo run lint check Tests: @@ -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 @@ -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 From 969fcd5ba48245ffb1af3712fe43f5df5a1908dc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 30 Jul 2022 15:49:28 -0400 Subject: [PATCH 07/11] here too --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8ac12c421bb5..bea2b556b5d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,7 +63,7 @@ jobs: with: node-version: ${{ matrix.node-version }} cache: pnpm - - run: pnpm install --frozen-lockfile + - run: pnpm install --frozen-lockfile --ignore-scripts - run: pnpm playwright install - run: pnpm test - name: Archive test results diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a89290259db0..4eeaacaae1d7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,7 +29,7 @@ jobs: node-version: 16.x cache: pnpm - - run: pnpm install --frozen-lockfile + - run: pnpm install --frozen-lockfile --ignore-scripts - run: pnpm turbo run build --filter="./packages/*" - name: Create Release Pull Request or Publish to npm From dc8661fa15701471c699cb3e82244980a20502fb Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 30 Jul 2022 16:08:32 -0400 Subject: [PATCH 08/11] try this --- .github/workflows/ci.yml | 4 ++-- .github/workflows/release.yml | 2 +- packages/kit/svelte-kit.js | 11 ++++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bea2b556b5d9..5691cd5e558b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: with: node-version: '16.x' cache: pnpm - - run: pnpm install --frozen-lockfile --ignore-scripts + - run: pnpm install --frozen-lockfile - run: cd packages/kit && pnpm build - run: pnpm turbo run lint check Tests: @@ -63,7 +63,7 @@ jobs: with: node-version: ${{ matrix.node-version }} cache: pnpm - - run: pnpm install --frozen-lockfile --ignore-scripts + - run: pnpm install --frozen-lockfile - run: pnpm playwright install - run: pnpm test - name: Archive test results diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4eeaacaae1d7..a89290259db0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,7 +29,7 @@ jobs: node-version: 16.x cache: pnpm - - run: pnpm install --frozen-lockfile --ignore-scripts + - run: pnpm install --frozen-lockfile - run: pnpm turbo run build --filter="./packages/*" - name: Create Release Pull Request or Publish to npm diff --git a/packages/kit/svelte-kit.js b/packages/kit/svelte-kit.js index d17768a371d7..bc94d37d31d6 100755 --- a/packages/kit/svelte-kit.js +++ b/packages/kit/svelte-kit.js @@ -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'); +} else { + console.error('Run "pnpm build" and try running this command again'); +} From ad0c80ff7b92f104296a2269138e60f98d363e63 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 30 Jul 2022 16:42:16 -0400 Subject: [PATCH 09/11] add check for prepare script --- packages/kit/src/cli.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index b84a6df8746e..c39c0752c2df 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -41,8 +41,17 @@ prog .describe('Synchronise generated files') .option('--mode', 'Specify a mode for loading environment variables', 'development') .action(async ({ mode }) => { - const is_postinstall = process.env.npm_lifecycle_event === 'postinstall'; - const cwd = is_postinstall ? process.env.INIT_CWD ?? '' : process.cwd(); + const event = process.env.npm_lifecycle_event; + + // TODO remove for 1.0 + if (event === 'prepare') { + 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`); From eef885be16b06852c481c5eac333f48941045961 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 31 Jul 2022 06:25:03 -0400 Subject: [PATCH 10/11] make it possible to use src cli to test sync changes --- packages/kit/test/apps/amp/package.json | 2 +- packages/kit/test/apps/basics/package.json | 2 +- packages/kit/test/apps/options-2/package.json | 2 +- packages/kit/test/apps/options/package.json | 2 +- packages/kit/test/apps/writes/package.json | 2 +- packages/kit/test/prerendering/basics/package.json | 2 +- packages/kit/test/prerendering/fallback/package.json | 2 +- packages/kit/test/prerendering/options/package.json | 2 +- packages/kit/test/prerendering/paths-base/package.json | 2 +- packages/kit/test/prerendering/trailing-slash/package.json | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/kit/test/apps/amp/package.json b/packages/kit/test/apps/amp/package.json index bdcaa43af4e7..ea35010815f5 100644 --- a/packages/kit/test/apps/amp/package.json +++ b/packages/kit/test/apps/amp/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", "test:build": "playwright test" diff --git a/packages/kit/test/apps/basics/package.json b/packages/kit/test/apps/basics/package.json index 06a472f7f57d..dfa4862f57ec 100644 --- a/packages/kit/test/apps/basics/package.json +++ b/packages/kit/test/apps/basics/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && 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" diff --git a/packages/kit/test/apps/options-2/package.json b/packages/kit/test/apps/options-2/package.json index 096eca6c80a7..df605763d4f0 100644 --- a/packages/kit/test/apps/options-2/package.json +++ b/packages/kit/test/apps/options-2/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", "test:build": "playwright test" diff --git a/packages/kit/test/apps/options/package.json b/packages/kit/test/apps/options/package.json index 5da7c11fc3b9..2b927be74002 100644 --- a/packages/kit/test/apps/options/package.json +++ b/packages/kit/test/apps/options/package.json @@ -6,7 +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", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", "test:build": "playwright test" diff --git a/packages/kit/test/apps/writes/package.json b/packages/kit/test/apps/writes/package.json index 396e62815efe..f07e6655f6b2 100644 --- a/packages/kit/test/apps/writes/package.json +++ b/packages/kit/test/apps/writes/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && 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" diff --git a/packages/kit/test/prerendering/basics/package.json b/packages/kit/test/prerendering/basics/package.json index 3c0ad3cca129..db8df5f35cc4 100644 --- a/packages/kit/test/prerendering/basics/package.json +++ b/packages/kit/test/prerendering/basics/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/fallback/package.json b/packages/kit/test/prerendering/fallback/package.json index cd2f0c4c7c30..ee20095cd9d8 100644 --- a/packages/kit/test/prerendering/fallback/package.json +++ b/packages/kit/test/prerendering/fallback/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run build" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/options/package.json b/packages/kit/test/prerendering/options/package.json index f83b597edf9a..71a5279c38ba 100644 --- a/packages/kit/test/prerendering/options/package.json +++ b/packages/kit/test/prerendering/options/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/paths-base/package.json b/packages/kit/test/prerendering/paths-base/package.json index 6ad24e692f7a..465028077b7b 100644 --- a/packages/kit/test/prerendering/paths-base/package.json +++ b/packages/kit/test/prerendering/paths-base/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { diff --git a/packages/kit/test/prerendering/trailing-slash/package.json b/packages/kit/test/prerendering/trailing-slash/package.json index 2fa3e9d82529..b0073089142f 100644 --- a/packages/kit/test/prerendering/trailing-slash/package.json +++ b/packages/kit/test/prerendering/trailing-slash/package.json @@ -6,7 +6,7 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "check": "svelte-kit sync && tsc && svelte-check", + "check": "node ../../cli.js && tsc && svelte-check", "test": "npm run build && uvu test" }, "devDependencies": { From 072fe86e6f97444bfe2998a3e09ded4775b981ff Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 31 Jul 2022 06:30:26 -0400 Subject: [PATCH 11/11] update wording of prepare script message --- packages/kit/src/cli.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index c39c0752c2df..e5d88164e640 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -45,7 +45,12 @@ prog // TODO remove for 1.0 if (event === 'prepare') { - const message = `\`svelte-kit sync\` now runs on "postinstall" — please remove the "prepare" script from your package.json\n`; + const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); + const message = + pkg.scripts.prepare === 'svelte-kit sync' + ? `\`svelte-kit sync\` now runs on "postinstall" — please remove the "prepare" script from your package.json\n` + : `\`svelte-kit sync\` now runs on "postinstall" — please remove it from your "prepare" script\n`; + console.error(colors.bold().red(message)); return; }