Skip to content

Commit

Permalink
Remove empty package dirs (#2831)
Browse files Browse the repository at this point in the history
* dont create empty dirs when packaging

* changeset

* explain what walk does
  • Loading branch information
Rich-Harris authored Nov 23, 2021
1 parent b9a8dd4 commit a7653c1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
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
Empty file.
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
9 changes: 7 additions & 2 deletions packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ export function copy(from, to, filter = () => true) {
return files;
}

/** @param {string} cwd */
export function walk(cwd) {
/**
* Get a list of all files in a directory
* @param {string} cwd - the directory to walk
* @param {boolean} [dirs] - whether to include directories in the result
*/
export function walk(cwd, dirs = false) {
/** @type {string[]} */
const all_files = [];

Expand All @@ -54,6 +58,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

0 comments on commit a7653c1

Please sign in to comment.