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

Remove empty package dirs #2831

Merged
merged 3 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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/odd-foxes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Don't create empty dirs when packaging
9 changes: 8 additions & 1 deletion packages/kit/src/packaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ export async function make_package(config, cwd = process.cwd()) {
if (!config.kit.package.files(normalized)) {
const dts_file = (svelte_ext ? file : file.slice(0, -ext.length)) + '.d.ts';
const dts_path = path.join(abs_package_dir, dts_file);
if (fs.existsSync(dts_path)) fs.unlinkSync(dts_path);
if (fs.existsSync(dts_path)) {
fs.unlinkSync(dts_path);

const dir = path.dirname(dts_path);
if (fs.readdirSync(dir).length === 0) {
fs.rmdirSync(dir);
}
}
continue;
}

Expand Down
24 changes: 13 additions & 11 deletions packages/kit/src/packaging/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { statSync, readFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';

Expand All @@ -24,8 +24,8 @@ async function test_make_package(path) {
try {
const config = await load_config({ cwd });
await make_package(config, cwd);
const expected_files = walk(ewd);
const actual_files = walk(pwd);
const expected_files = walk(ewd, true);
const actual_files = walk(pwd, true);
assert.equal(
actual_files.length,
expected_files.length,
Expand All @@ -35,14 +35,16 @@ async function test_make_package(path) {
);

for (const file of actual_files) {
assert.equal(expected_files.includes(file), true, `Did not expect ${file} in ${path}`);
const expected_content = format(file, readFileSync(join(ewd, file), 'utf-8'));
const actual_content = format(file, readFileSync(join(pwd, file), 'utf-8'));
assert.fixture(
actual_content,
expected_content,
`Expected equal file contents for ${file} in ${path}`
);
if (!statSync(join(pwd, file)).isDirectory()) {
assert.equal(expected_files.includes(file), true, `Did not expect ${file} in ${path}`);
const expected_content = format(file, readFileSync(join(ewd, file), 'utf-8'));
const actual_content = format(file, readFileSync(join(pwd, file), 'utf-8'));
assert.fixture(
actual_content,
expected_content,
`Expected equal file contents for ${file} in ${path}`
);
}
}
} finally {
rimraf(pwd);
Expand Down
8 changes: 6 additions & 2 deletions packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ export function copy(from, to, filter = () => true) {
return files;
}

/** @param {string} cwd */
export function walk(cwd) {
/**
* @param {string} cwd
* @param {boolean} [dirs]
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
*/
export function walk(cwd, dirs = false) {
/** @type {string[]} */
const all_files = [];

Expand All @@ -54,6 +57,7 @@ export function walk(cwd) {
const joined = path.join(dir, file);
const stats = fs.statSync(path.join(cwd, joined));
if (stats.isDirectory()) {
if (dirs) all_files.push(joined);
walk_dir(joined);
} else {
all_files.push(joined);
Expand Down