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

create-svelte: set folder name as pkg.name #2415

Merged
merged 1 commit into from
Sep 13, 2021
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/cyan-ads-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Use the name of folder as name in package.json
16 changes: 14 additions & 2 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function main() {
const name = path.basename(path.resolve(cwd));

write_template_files(options.template, options.typescript, name, cwd);
write_common_files(cwd, options);
write_common_files(cwd, options, name);

console.log(bold(green('✔ Copied project files')));

Expand Down Expand Up @@ -173,8 +173,9 @@ function write_template_files(template, typescript, name, cwd) {
*
* @param {string} cwd
* @param {import('./types/internal').Options} options
* @param {string} name
*/
function write_common_files(cwd, options) {
function write_common_files(cwd, options, name) {
const shared = dist('shared.json');
const { files } = /** @type {import('./types/internal').Common} */ (JSON.parse(
fs.readFileSync(shared, 'utf-8')
Expand All @@ -201,6 +202,7 @@ function write_common_files(cwd, options) {

pkg.dependencies = sort_keys(pkg.dependencies);
pkg.devDependencies = sort_keys(pkg.devDependencies);
pkg.name = toValidPackageName(name);

fs.writeFileSync(pkg_file, JSON.stringify(pkg, null, ' '));
}
Expand Down Expand Up @@ -260,4 +262,14 @@ function dist(path) {
return fileURLToPath(new URL(`./dist/${path}`, import.meta.url).href);
}

/** @param {string} name */
function toValidPackageName(name) {
return name
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/^[._]/, '')
.replace(/[^a-z0-9~.-]+/g, '-');
}

main();