Skip to content

Commit

Permalink
create-svelte: set folder name as pkg.name
Browse files Browse the repository at this point in the history
  • Loading branch information
ambarvm committed Sep 13, 2021
1 parent d3c1fa3 commit 6bb7c67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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();

0 comments on commit 6bb7c67

Please sign in to comment.