-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to create-svelte (#632)
* Improvements to create-svelte - add eslint setup option - add prettier setup option - introduce type checks to the project * svelte-adders * lint Co-authored-by: Simon Holthausen <simon.holthausen@accso.de>
- Loading branch information
1 parent
b06148e
commit a52cf82
Showing
15 changed files
with
307 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'create-svelte': patch | ||
--- | ||
|
||
add eslint and prettier setup options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
declare module '*.gitignore'; | ||
|
||
declare module 'gitignore-parser'; | ||
|
||
declare module 'prompts/lib/index'; | ||
|
||
declare module 'tiny-glob/sync'; | ||
|
||
declare module '*.json'; | ||
|
||
// TODO make this depend on the real types from the kit package | ||
declare module '@sveltejs/kit/filesystem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { bold, green } from 'kleur/colors'; | ||
import { | ||
copy_from_template_additions, | ||
update_package_json_dev_deps, | ||
upsert_package_json_scripts | ||
} from './utils'; | ||
|
||
/** | ||
* Add ESLint if user wants it. | ||
* | ||
* @param {string} cwd | ||
* @param {boolean} yes | ||
* @param {boolean} use_typescript | ||
*/ | ||
export default async function add_eslint(cwd, yes, use_typescript) { | ||
if (!yes) { | ||
return; | ||
} | ||
|
||
upsert_package_json_scripts(cwd, { | ||
lint: 'eslint .' | ||
}); | ||
|
||
if (use_typescript) { | ||
update_package_json_dev_deps(cwd, { | ||
'@typescript-eslint/eslint-plugin': '^4.19.0', | ||
'@typescript-eslint/parser': '^4.19.0', | ||
eslint: '^7.22.0', | ||
'eslint-plugin-svelte3': '^3.1.0' | ||
}); | ||
copy_from_template_additions(cwd, { from: ['.eslintrc.ts.cjs'], to: ['.eslintrc.cjs'] }); | ||
} else { | ||
update_package_json_dev_deps(cwd, { | ||
eslint: '^7.22.0', | ||
'eslint-plugin-svelte3': '^3.1.0' | ||
}); | ||
copy_from_template_additions(cwd, ['.eslintrc.cjs']); | ||
} | ||
|
||
console.log( | ||
bold( | ||
green( | ||
'✔ Added ESLint.\n' + | ||
'Readme for ESLint and Svelte: https://github.com/sveltejs/eslint-plugin-svelte3' | ||
) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import fs from 'fs'; | ||
import { bold, green } from 'kleur/colors'; | ||
import { join } from 'path'; | ||
import { | ||
copy_from_template_additions, | ||
update_package_json_dev_deps, | ||
upsert_package_json_scripts | ||
} from './utils'; | ||
|
||
/** | ||
* Add TypeScript if user wants it. | ||
* | ||
* @param {string} cwd | ||
* @param {boolean} yes | ||
* @param {boolean} usesEslint | ||
*/ | ||
export default async function add_prettier(cwd, yes, usesEslint) { | ||
if (!yes) { | ||
return; | ||
} | ||
|
||
update_package_json_dev_deps(cwd, { | ||
prettier: '~2.2.1', | ||
'prettier-plugin-svelte': '^2.2.0' | ||
}); | ||
copy_from_template_additions(cwd, ['.prettierrc']); | ||
copy_from_template_additions(cwd, ['.prettierignore']); | ||
|
||
if (usesEslint) { | ||
update_package_json_dev_deps(cwd, { | ||
'eslint-config-prettier': '^8.1.0' | ||
}); | ||
add_prettier_to_eslint_extends(cwd); | ||
upsert_package_json_scripts(cwd, { | ||
lint: 'prettier --check . && eslint .', | ||
format: 'prettier --write .' | ||
}); | ||
} else { | ||
upsert_package_json_scripts(cwd, { | ||
lint: 'prettier --check .', | ||
format: 'prettier --write .' | ||
}); | ||
} | ||
|
||
console.log( | ||
bold( | ||
green( | ||
'✔ Added Prettier.\n' + | ||
'General formatting options: https://prettier.io/docs/en/options.html\n' + | ||
'Svelte-specific formatting options: https://github.com/sveltejs/prettier-plugin-svelte#options' | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} cwd | ||
*/ | ||
function add_prettier_to_eslint_extends(cwd) { | ||
const file = join(cwd, '.eslintrc.cjs'); | ||
let code = fs.readFileSync(file, 'utf-8'); | ||
|
||
const insertIdx = code.indexOf(']', code.indexOf('extends: [')); | ||
code = code.substring(0, insertIdx) + ", 'prettier'" + code.substring(insertIdx); | ||
|
||
fs.writeFileSync(file, code, 'utf-8'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.