From 3f78be25b977df0792383fcbca8e670449300071 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Tue, 27 Jul 2021 13:41:23 +0800 Subject: [PATCH] style: format files --- .prettierignore | 6 + .prettierrc | 7 + README.md | 1 + index.js | 144 ++++++++++-------- package.json | 1 + playground/default-ts-with-tests/package.json | 2 +- .../src/components/HelloWorld.vue | 2 +- .../default-ts-with-tests/tsconfig.json | 15 +- .../default-ts-with-tests/vite.config.ts | 4 +- playground/default-ts/package.json | 2 +- .../default-ts/src/components/HelloWorld.vue | 2 +- playground/default-ts/tsconfig.json | 15 +- playground/default-ts/vite.config.ts | 4 +- playground/default-with-tests/jsconfig.json | 15 +- playground/default-with-tests/package.json | 2 +- playground/default-with-tests/vite.config.js | 4 +- playground/default/jsconfig.json | 15 +- playground/default/package.json | 2 +- playground/default/vite.config.js | 4 +- playground/spa-ts-with-tests/package.json | 2 +- playground/spa-ts-with-tests/src/App.vue | 2 +- .../src/components/HelloWorld.vue | 2 +- .../spa-ts-with-tests/src/store/index.ts | 12 +- playground/spa-ts-with-tests/tsconfig.json | 15 +- playground/spa-ts-with-tests/vite.config.ts | 4 +- playground/spa-ts/package.json | 2 +- playground/spa-ts/src/App.vue | 2 +- .../spa-ts/src/components/HelloWorld.vue | 2 +- playground/spa-ts/src/store/index.ts | 12 +- playground/spa-ts/tsconfig.json | 15 +- playground/spa-ts/vite.config.ts | 4 +- playground/spa-with-tests/jsconfig.json | 15 +- playground/spa-with-tests/package.json | 2 +- playground/spa-with-tests/src/App.vue | 2 +- playground/spa-with-tests/src/store/index.js | 12 +- playground/spa-with-tests/vite.config.js | 4 +- playground/spa/jsconfig.json | 15 +- playground/spa/package.json | 2 +- playground/spa/src/App.vue | 2 +- playground/spa/src/store/index.js | 12 +- playground/spa/vite.config.js | 4 +- snapshot.js | 20 ++- template/code/spa/src/App.vue | 2 +- template/code/spa/src/store/index.js | 12 +- .../src/components/HelloWorld.vue | 2 +- template/code/typescript-spa/src/App.vue | 2 +- .../src/components/HelloWorld.vue | 2 +- .../code/typescript-spa/src/store/index.ts | 12 +- template/config/base/jsconfig.json | 15 +- template/config/base/vite.config.js | 4 +- utils/deepMerge.js | 2 +- utils/directoryTraverse.js | 10 +- utils/renderTemplate.js | 7 +- utils/templateList.js | 4 +- 54 files changed, 203 insertions(+), 282 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..1b9dac1d --- /dev/null +++ b/.prettierignore @@ -0,0 +1,6 @@ +pnpm-lock.yaml + +# prettier doesn't respect newlines between chained methods +# https://github.com/prettier/prettier/issues/7884 +**/*.spec.js +**/*.spec.ts diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..dbd96373 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": false, + "tabWidth": 2, + "singleQuote": true, + "printWidth": 80, + "trailingComma": "none" +} diff --git a/README.md b/README.md index e6df97ae..3b3d1d93 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # create-vue + An easy way to start a Vue project diff --git a/index.js b/index.js index c762c60b..6bca421c 100755 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ function canSafelyOverwrite(dir) { return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0 } -function emptyDir (dir) { +function emptyDir(dir) { postOrderDirectoryTraverse( dir, (dir) => fs.rmdirSync(dir), @@ -45,7 +45,7 @@ function emptyDir (dir) { async function init() { const cwd = process.cwd() const argv = minimist(process.argv.slice(2)) - + let targetDir = argv._[0] const defaultProjectName = !targetDir ? 'vue-project' : targetDir @@ -62,71 +62,77 @@ async function init() { // - Project language: JavaScript / TypeScript // - Install Vue Router & Vuex for SPA development? // - Add Cypress for testing? - result = await prompts([ - { - name: 'projectName', - type: targetDir ? null : 'text', - message: 'Project name:', - initial: defaultProjectName, - onState: (state) => - (targetDir = String(state.value).trim() || defaultProjectName) - }, - { - name: 'shouldOverwrite', - type: () => canSafelyOverwrite(targetDir) ? null : 'confirm', - message: () => { - const dirForPrompt = targetDir === '.' - ? 'Current directory' - : `Target directory "${targetDir}"` - - return `${dirForPrompt} is not empty. Remove existing files and continue?` - } - }, - { - name: 'overwriteChecker', - type: (prev, values = {}) => { - if (values.shouldOverwrite === false) { - throw new Error(red('✖') + ' Operation cancelled') + result = await prompts( + [ + { + name: 'projectName', + type: targetDir ? null : 'text', + message: 'Project name:', + initial: defaultProjectName, + onState: (state) => + (targetDir = String(state.value).trim() || defaultProjectName) + }, + { + name: 'shouldOverwrite', + type: () => (canSafelyOverwrite(targetDir) ? null : 'confirm'), + message: () => { + const dirForPrompt = + targetDir === '.' + ? 'Current directory' + : `Target directory "${targetDir}"` + + return `${dirForPrompt} is not empty. Remove existing files and continue?` } - return null + }, + { + name: 'overwriteChecker', + type: (prev, values = {}) => { + if (values.shouldOverwrite === false) { + throw new Error(red('✖') + ' Operation cancelled') + } + return null + } + }, + { + name: 'packageName', + type: () => (isValidPackageName(targetDir) ? null : 'text'), + message: 'Package name:', + initial: () => toValidPackageName(targetDir), + validate: (dir) => + isValidPackageName(dir) || 'Invalid package.json name' + }, + { + name: 'shouldUseTypeScript', + type: () => (isValidTemplate ? null : 'toggle'), + message: 'Add TypeScript?', + initial: false, + active: 'Yes', + inactive: 'No' + }, + { + name: 'isSPA', + type: () => (isValidTemplate ? null : 'toggle'), + message: + 'Add Vue Router & Vuex for Single Page Application development?', + initial: false, + active: 'Yes', + inactive: 'No' + }, + { + name: 'shouldAddCypress', + type: () => (isValidTemplate ? null : 'toggle'), + message: 'Add Cypress for testing?', + initial: false, + active: 'Yes', + inactive: 'No' } - }, + ], { - name: 'packageName', - type: () => (isValidPackageName(targetDir) ? null : 'text'), - message: 'Package name:', - initial: () => toValidPackageName(targetDir), - validate: (dir) => isValidPackageName(dir) || 'Invalid package.json name' - }, - { - name: 'shouldUseTypeScript', - type: () => isValidTemplate ? null : 'toggle', - message: 'Add TypeScript?', - initial: false, - active: 'Yes', - inactive: 'No' - }, - { - name: 'isSPA', - type: () => isValidTemplate ? null : 'toggle', - message: 'Add Vue Router & Vuex for Single Page Application development?', - initial: false, - active: 'Yes', - inactive: 'No' - }, - { - name: 'shouldAddCypress', - type: () => isValidTemplate ? null : 'toggle', - message: 'Add Cypress for testing?', - initial: false, - active: 'Yes', - inactive: 'No' - } - ], { - onCancel: () => { - throw new Error(red('✖') + ' Operation cancelled') + onCancel: () => { + throw new Error(red('✖') + ' Operation cancelled') + } } - }) + ) } catch (cancelled) { console.log(cancelled.message) process.exit(1) @@ -180,13 +186,17 @@ async function init() { if (filepath.endsWith('.js')) { fs.renameSync(filepath, filepath.replace(/\.js$/, '.ts')) } else if (path.basename(filepath) === 'jsconfig.json') { - fs.renameSync(filepath, filepath.replace(/jsconfig\.json$/, 'tsconfig.json')) + fs.renameSync( + filepath, + filepath.replace(/jsconfig\.json$/, 'tsconfig.json') + ) } } ) } // Render code template. + // prettier-ignore const codeTemplate = (shouldUseTypeScript ? 'typescript-' : '') + (isSPA ? 'spa' : 'default') @@ -219,9 +229,9 @@ async function init() { const packageManager = /pnpm/.test(process.env.npm_execpath) ? 'pnpm' : /yarn/.test(process.env.npm_execpath) - ?'yarn' - : 'npm' - + ? 'yarn' + : 'npm' + const commandsMap = { install: { pnpm: 'pnpm install', diff --git a/package.json b/package.json index 17567eb9..054f73da 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "node": "^12.13.0 || ^14.0.0 || >= 16.0.0" }, "scripts": { + "format": "prettier --write .", "test": "echo \"Error: no test specified\" && exit 1", "prepublishOnly": "node snapshot.js" }, diff --git a/playground/default-ts-with-tests/package.json b/playground/default-ts-with-tests/package.json index b1e12f16..fee85fe2 100644 --- a/playground/default-ts-with-tests/package.json +++ b/playground/default-ts-with-tests/package.json @@ -27,4 +27,4 @@ "typescript": "~4.3.5", "vue-tsc": "^0.2.2" } -} \ No newline at end of file +} diff --git a/playground/default-ts-with-tests/src/components/HelloWorld.vue b/playground/default-ts-with-tests/src/components/HelloWorld.vue index 2dac5234..97b3d3da 100644 --- a/playground/default-ts-with-tests/src/components/HelloWorld.vue +++ b/playground/default-ts-with-tests/src/components/HelloWorld.vue @@ -29,7 +29,7 @@ import { ref } from 'vue' defineProps<{ - msg: String, + msg: String }>() const count = ref(0) diff --git a/playground/default-ts-with-tests/tsconfig.json b/playground/default-ts-with-tests/tsconfig.json index d3adf1bf..9c2cfe6b 100644 --- a/playground/default-ts-with-tests/tsconfig.json +++ b/playground/default-ts-with-tests/tsconfig.json @@ -11,20 +11,11 @@ "resolveJsonModule": true, "esModuleInterop": true, "paths": { - "@/*": [ - "src/*" - ] + "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"], "skipLibCheck": true }, - "include": [ - "vite.config.*", - - "src/**/*", - "src/**/*.vue" - ], - "exclude": [ - "src/**/__tests__/**" - ] + "include": ["vite.config.*", "src/**/*", "src/**/*.vue"], + "exclude": ["src/**/__tests__/**"] } diff --git a/playground/default-ts-with-tests/vite.config.ts b/playground/default-ts-with-tests/vite.config.ts index 8ae6fd07..032e163c 100644 --- a/playground/default-ts-with-tests/vite.config.ts +++ b/playground/default-ts-with-tests/vite.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ plugins: [vue(), vueJsx()], resolve: { alias: { - '@/': new URL('./src/', import.meta.url).pathname, - }, + '@/': new URL('./src/', import.meta.url).pathname + } } }) diff --git a/playground/default-ts/package.json b/playground/default-ts/package.json index c12f476c..875e356b 100644 --- a/playground/default-ts/package.json +++ b/playground/default-ts/package.json @@ -19,4 +19,4 @@ "typescript": "~4.3.5", "vue-tsc": "^0.2.2" } -} \ No newline at end of file +} diff --git a/playground/default-ts/src/components/HelloWorld.vue b/playground/default-ts/src/components/HelloWorld.vue index 2dac5234..97b3d3da 100644 --- a/playground/default-ts/src/components/HelloWorld.vue +++ b/playground/default-ts/src/components/HelloWorld.vue @@ -29,7 +29,7 @@ import { ref } from 'vue' defineProps<{ - msg: String, + msg: String }>() const count = ref(0) diff --git a/playground/default-ts/tsconfig.json b/playground/default-ts/tsconfig.json index d3adf1bf..9c2cfe6b 100644 --- a/playground/default-ts/tsconfig.json +++ b/playground/default-ts/tsconfig.json @@ -11,20 +11,11 @@ "resolveJsonModule": true, "esModuleInterop": true, "paths": { - "@/*": [ - "src/*" - ] + "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"], "skipLibCheck": true }, - "include": [ - "vite.config.*", - - "src/**/*", - "src/**/*.vue" - ], - "exclude": [ - "src/**/__tests__/**" - ] + "include": ["vite.config.*", "src/**/*", "src/**/*.vue"], + "exclude": ["src/**/__tests__/**"] } diff --git a/playground/default-ts/vite.config.ts b/playground/default-ts/vite.config.ts index 8ae6fd07..032e163c 100644 --- a/playground/default-ts/vite.config.ts +++ b/playground/default-ts/vite.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ plugins: [vue(), vueJsx()], resolve: { alias: { - '@/': new URL('./src/', import.meta.url).pathname, - }, + '@/': new URL('./src/', import.meta.url).pathname + } } }) diff --git a/playground/default-with-tests/jsconfig.json b/playground/default-with-tests/jsconfig.json index d3adf1bf..9c2cfe6b 100644 --- a/playground/default-with-tests/jsconfig.json +++ b/playground/default-with-tests/jsconfig.json @@ -11,20 +11,11 @@ "resolveJsonModule": true, "esModuleInterop": true, "paths": { - "@/*": [ - "src/*" - ] + "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"], "skipLibCheck": true }, - "include": [ - "vite.config.*", - - "src/**/*", - "src/**/*.vue" - ], - "exclude": [ - "src/**/__tests__/**" - ] + "include": ["vite.config.*", "src/**/*", "src/**/*.vue"], + "exclude": ["src/**/__tests__/**"] } diff --git a/playground/default-with-tests/package.json b/playground/default-with-tests/package.json index 44a3acf9..c95e46c7 100644 --- a/playground/default-with-tests/package.json +++ b/playground/default-with-tests/package.json @@ -24,4 +24,4 @@ "cypress": "^8.0.0", "start-server-and-test": "^1.12.6" } -} \ No newline at end of file +} diff --git a/playground/default-with-tests/vite.config.js b/playground/default-with-tests/vite.config.js index 8ae6fd07..032e163c 100644 --- a/playground/default-with-tests/vite.config.js +++ b/playground/default-with-tests/vite.config.js @@ -7,7 +7,7 @@ export default defineConfig({ plugins: [vue(), vueJsx()], resolve: { alias: { - '@/': new URL('./src/', import.meta.url).pathname, - }, + '@/': new URL('./src/', import.meta.url).pathname + } } }) diff --git a/playground/default/jsconfig.json b/playground/default/jsconfig.json index d3adf1bf..9c2cfe6b 100644 --- a/playground/default/jsconfig.json +++ b/playground/default/jsconfig.json @@ -11,20 +11,11 @@ "resolveJsonModule": true, "esModuleInterop": true, "paths": { - "@/*": [ - "src/*" - ] + "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"], "skipLibCheck": true }, - "include": [ - "vite.config.*", - - "src/**/*", - "src/**/*.vue" - ], - "exclude": [ - "src/**/__tests__/**" - ] + "include": ["vite.config.*", "src/**/*", "src/**/*.vue"], + "exclude": ["src/**/__tests__/**"] } diff --git a/playground/default/package.json b/playground/default/package.json index 0ff042cd..b9abc773 100644 --- a/playground/default/package.json +++ b/playground/default/package.json @@ -16,4 +16,4 @@ "@vue/compiler-sfc": "^3.1.5", "vite": "^2.4.3" } -} \ No newline at end of file +} diff --git a/playground/default/vite.config.js b/playground/default/vite.config.js index 8ae6fd07..032e163c 100644 --- a/playground/default/vite.config.js +++ b/playground/default/vite.config.js @@ -7,7 +7,7 @@ export default defineConfig({ plugins: [vue(), vueJsx()], resolve: { alias: { - '@/': new URL('./src/', import.meta.url).pathname, - }, + '@/': new URL('./src/', import.meta.url).pathname + } } }) diff --git a/playground/spa-ts-with-tests/package.json b/playground/spa-ts-with-tests/package.json index 45ac7225..c55fef80 100644 --- a/playground/spa-ts-with-tests/package.json +++ b/playground/spa-ts-with-tests/package.json @@ -29,4 +29,4 @@ "typescript": "~4.3.5", "vue-tsc": "^0.2.2" } -} \ No newline at end of file +} diff --git a/playground/spa-ts-with-tests/src/App.vue b/playground/spa-ts-with-tests/src/App.vue index b9643551..6a65bcc1 100644 --- a/playground/spa-ts-with-tests/src/App.vue +++ b/playground/spa-ts-with-tests/src/App.vue @@ -3,7 +3,7 @@ Home | About - +