Skip to content

Commit

Permalink
various create-svelte improvements (#3472)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 20, 2022
1 parent 7b79522 commit c926080
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-camels-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Respect Ctrl-C when running create-svelte
5 changes: 5 additions & 0 deletions .changeset/popular-hounds-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Make project name an explicit option'
5 changes: 5 additions & 0 deletions .changeset/unlucky-otters-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Prompt for directory when running create-svelte without argument
101 changes: 62 additions & 39 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ async function main() {
console.log(gray(`\ncreate-svelte version ${version}`));
console.log(disclaimer);

const cwd = process.argv[2] || '.';
let cwd = process.argv[2] || '.';

if (cwd === '.') {
const opts = await prompts([
{
type: 'text',
name: 'dir',
message: 'Where should we create your project?\n (leave blank to use current directory)'
}
]);

if (opts.dir) {
cwd = opts.dir;
}
}

if (fs.existsSync(cwd)) {
if (fs.readdirSync(cwd).length > 0) {
Expand All @@ -39,48 +53,57 @@ async function main() {
}

const options = /** @type {import('./types/internal').Options} */ (
await prompts([
{
type: 'select',
name: 'template',
message: 'Which Svelte app template?',
choices: fs.readdirSync(dist('templates')).map((dir) => {
const meta_file = dist(`templates/${dir}/meta.json`);
const meta = JSON.parse(fs.readFileSync(meta_file, 'utf8'));

return {
title: meta.description,
value: dir
};
})
},
{
type: 'toggle',
name: 'typescript',
message: 'Use TypeScript?',
initial: false,
active: 'Yes',
inactive: 'No'
},
await prompts(
[
{
type: 'select',
name: 'template',
message: 'Which Svelte app template?',
choices: fs.readdirSync(dist('templates')).map((dir) => {
const meta_file = dist(`templates/${dir}/meta.json`);
const meta = JSON.parse(fs.readFileSync(meta_file, 'utf8'));

return {
title: meta.description,
value: dir
};
})
},
{
type: 'toggle',
name: 'typescript',
message: 'Use TypeScript?',
initial: false,
active: 'Yes',
inactive: 'No'
},
{
type: 'toggle',
name: 'eslint',
message: 'Add ESLint for code linting?',
initial: false,
active: 'Yes',
inactive: 'No'
},
{
type: 'toggle',
name: 'prettier',
message: 'Add Prettier for code formatting?',
initial: false,
active: 'Yes',
inactive: 'No'
}
],
{
type: 'toggle',
name: 'eslint',
message: 'Add ESLint for code linting?',
initial: false,
active: 'Yes',
inactive: 'No'
},
{
type: 'toggle',
name: 'prettier',
message: 'Add Prettier for code formatting?',
initial: false,
active: 'Yes',
inactive: 'No'
onCancel: () => {
process.exit(1);
}
}
])
)
);

options.name = path.basename(path.resolve(cwd));

await create(cwd, options);

console.log(bold(green('\nYour project is ready!')));
Expand Down
6 changes: 2 additions & 4 deletions packages/create-svelte/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import { mkdirp, copy, dist } from './utils.js';
export async function create(cwd, options) {
mkdirp(cwd);

const name = path.basename(path.resolve(cwd));

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fs.readdirSync(repo).forEach((file) => {
});

await create(repo, {
name: 'kit-template-default',
template: 'default',
eslint: false,
typescript: false,
Expand Down
1 change: 1 addition & 0 deletions packages/create-svelte/types/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Options = {
name: string;
template: 'default' | 'skeleton';
typescript: boolean;
prettier: boolean;
Expand Down

0 comments on commit c926080

Please sign in to comment.