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

add TypeScript support at project init #41

Merged
merged 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/soft-bats-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Add TypeScript support at project init
16 changes: 16 additions & 0 deletions packages/create-svelte/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mkdirp } from '@sveltejs/app-utils';
import gitignore_contents from '../template/.gitignore';
import prompts from 'prompts/lib/index';
import glob from 'tiny-glob/sync';
import add_typescript from './modifications/add_typescript.js';

async function main() {
const target = process.argv[2] || '.';
Expand Down Expand Up @@ -52,6 +53,21 @@ async function main() {
fs.writeFileSync(pkg_file, pkg_json.replace(/workspace:/g, '').replace('~TODO~', name));

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

// modifications
const modifications = [['Use TypeScript in components?', false, add_typescript]];

for (const [message, initial, fn] of modifications) {
const response = await prompts({
type: 'confirm',
name: 'value',
message,
initial
});

await fn(target, response.value);
}

console.log(`\nNext steps:`);
let i = 1;

Expand Down
25 changes: 25 additions & 0 deletions packages/create-svelte/cli/modifications/add_typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import path from 'path';
import { bold, cyan, green } from 'kleur/colors';

export default async function add_typescript(cwd, yes) {
if (yes) {
// update package.json
const pkg_file = path.join(cwd, 'package.json');
const pkg_json = fs.readFileSync(pkg_file, 'utf-8');
const pkg = JSON.parse(pkg_json);

pkg.devDependencies['typescript'] = '^4.0.0';

fs.writeFileSync(pkg_file, JSON.stringify(pkg, null, '\t'));

// update example component
const file = path.join(cwd, 'src/components/Counter.svelte');
const code = fs.readFileSync(file, 'utf-8');
fs.writeFileSync(file, code.replace('<script>', '<script lang="ts">').replace('let count = 0', 'let count: number = 0'));

console.log(bold(green(`✔ Added 'typescript' to package.json`)));
} else {
console.log(`You can add TypeScript support later with ${bold(cyan('npm install -D typescript'))}`);
}
}
3 changes: 2 additions & 1 deletion packages/create-svelte/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@sveltejs/adapter-node": "workspace:0.0.7",
"@sveltejs/kit": "workspace:0.0.17",
"@sveltejs/snowpack-config": "workspace:^0.0.3",
"svelte": "^3.29.0"
"svelte": "^3.29.0",
"svelte-preprocess": "^4.5.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
export let count = 0;
let count = 0;

const increment = () => {
count += 1;
Expand Down
8 changes: 7 additions & 1 deletion packages/create-svelte/template/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const preprocess = require('svelte-preprocess');

module.exports = {
// By default, `npm run build` will create a standard Node app.
// You can create optimized builds for different platforms by
// specifying a different adapter
adapter: '@sveltejs/adapter-node'
adapter: '@sveltejs/adapter-node',

// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess()
};