diff --git a/.eslintrc.json b/.eslintrc.json index f8e2992..91d75ee 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,13 @@ "vazco/typescript" ], "root": true, - "rules": {}, + "rules": { + "@typescript-eslint/no-unsafe-argument": 0, + "@typescript-eslint/no-unsafe-call": 0, + "@typescript-eslint/no-unsafe-member-access": 0, + "@typescript-eslint/no-unused-vars": 0, + "@typescript-eslint/no-unsafe-assignment": 0 + }, "settings": { "import/resolver": { "typescript": {} diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 13c593e..0000000 --- a/.prettierrc +++ /dev/null @@ -1,6 +0,0 @@ -{ - "singleQuote": true, - "trailingComma": "all", - "endOfLine": "lf", - "semi": true -} diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..9fa19b4 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,3 @@ +const settings = require('eslint-config-vazco/default.json'); + +module.exports = settings.rules['prettier/prettier'][1]; diff --git a/package-lock.json b/package-lock.json index e75beb9..b6d3042 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,9 @@ "husky": "8.0.0", "lerna": "6.0.1", "lint-staged": "13.2.0" + }, + "devDependencies": { + "kolorist": "^1.7.0" } }, "node_modules/@ampproject/remapping": { @@ -6599,6 +6602,12 @@ "node": ">=0.10.0" } }, + "node_modules/kolorist": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/kolorist/-/kolorist-1.7.0.tgz", + "integrity": "sha512-ymToLHqL02udwVdbkowNpzjFd6UzozMtshPQKVi5k1EjKRqKqBrOnE9QbLEb0/pV76SAiIT13hdL8R6suc+f3g==", + "dev": true + }, "node_modules/language-subtag-registry": { "version": "0.3.22", "resolved": "https://registry.npmjs.org/language-subtag-registry/-/language-subtag-registry-0.3.22.tgz", diff --git a/package.json b/package.json index bdff6d5..85e6d0b 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "lint-staged": { "*.{ts,tsx}": [ - "prettier --config .prettierrc --write", + "prettier --config .prettierrc.js --write", "eslint --fix" ] }, @@ -16,8 +16,8 @@ "eslint-config-vazco": "7.1.0", "eslint-import-resolver-alias": "1.1.2", "eslint-import-resolver-typescript": "3.5.3", + "husky": "8.0.0", "lerna": "6.0.1", - "lint-staged": "13.2.0", - "husky": "8.0.0" + "lint-staged": "13.2.0" } } diff --git a/packages/create-presentify/package-lock.json b/packages/create-presentify/package-lock.json new file mode 100644 index 0000000..596e2a7 --- /dev/null +++ b/packages/create-presentify/package-lock.json @@ -0,0 +1,84 @@ +{ + "name": "create-presentify", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "create-presentify", + "version": "0.0.1", + "license": "MIT", + "bin": { + "create-presentify": "index.js" + }, + "devDependencies": { + "@types/prompts": "^2.4.2", + "kolorist": "1.7.0", + "minimist": "^1.2.8", + "prompts": "2.4.2" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@types/node": { + "version": "18.15.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.0.tgz", + "integrity": "sha512-z6nr0TTEOBGkzLGmbypWOGnpSpSIBorEhC4L+4HeQ2iezKCi4f77kyslRwvHeNitymGQ+oFyIWGP96l/DPSV9w==", + "dev": true + }, + "node_modules/@types/prompts": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/@types/prompts/-/prompts-2.4.2.tgz", + "integrity": "sha512-TwNx7qsjvRIUv/BCx583tqF5IINEVjCNqg9ofKHRlSoUHE62WBHrem4B1HGXcIrG511v29d1kJ9a/t2Esz7MIg==", + "dev": true, + "dependencies": { + "@types/node": "*", + "kleur": "^3.0.3" + } + }, + "node_modules/kleur": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", + "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/kolorist": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/kolorist/-/kolorist-1.7.0.tgz", + "integrity": "sha512-ymToLHqL02udwVdbkowNpzjFd6UzozMtshPQKVi5k1EjKRqKqBrOnE9QbLEb0/pV76SAiIT13hdL8R6suc+f3g==", + "dev": true + }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/prompts": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", + "integrity": "sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==", + "dev": true, + "dependencies": { + "kleur": "^3.0.3", + "sisteransi": "^1.0.5" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/sisteransi": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", + "integrity": "sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==", + "dev": true + } + } +} diff --git a/packages/create-presentify/package.json b/packages/create-presentify/package.json index ea48881..121cf1c 100644 --- a/packages/create-presentify/package.json +++ b/packages/create-presentify/package.json @@ -10,5 +10,11 @@ }, "bin": { "create-presentify": "index.js" + }, + "devDependencies": { + "@types/prompts": "^2.4.2", + "kolorist": "1.7.0", + "minimist": "^1.2.8", + "prompts": "2.4.2" } } diff --git a/packages/create-presentify/src/index.ts b/packages/create-presentify/src/index.ts index a2248ef..97678ec 100644 --- a/packages/create-presentify/src/index.ts +++ b/packages/create-presentify/src/index.ts @@ -1 +1,111 @@ -console.log('test'); +import { blue, bold, red, yellow } from 'kolorist'; +import minimist from 'minimist'; +import { existsSync, readdirSync } from 'node:fs'; +import prompts from 'prompts'; + +const getTargetDir = (targetDir: string | undefined) => { + return targetDir?.trim().replace(/\/+$/g, ''); +}; + +const isDirEmpty = (path: string) => { + const files = readdirSync(path); + return files.length === 0 || (files.length === 1 && files[0] === '.git'); +}; + +const argValue = minimist<{ + t?: string; + template?: string; +}>(process.argv.slice(2), { string: ['_'] }); + +const defaultTargetDir = 'presentify-project'; + +const variants = [ + { + name: 'js', + display: 'JavaScript', + color: yellow, + }, + { + name: 'ts', + display: 'TypeScript', + color: blue, + }, + { + name: 'js-swc', + display: 'JavaScript + SWC', + color: yellow, + }, + { + name: 'ts-swc', + display: 'TypeScript + SWC', + color: blue, + }, +]; + +const init = async () => { + const argTargetDir = getTargetDir(argValue._[0]); + + let targetDir = argTargetDir || defaultTargetDir; + + let result: prompts.Answers<'projectName' | 'overwrite'>; + + try { + result = await prompts( + [ + { + type: 'text', + name: 'projectName', + message: bold('Project name:'), + initial: defaultTargetDir, + onState: (state: { value: string }) => { + targetDir = getTargetDir(state.value) || defaultTargetDir; + }, + }, + { + type: () => + !existsSync(targetDir) || isDirEmpty(targetDir) ? null : 'confirm', + name: 'overwrite', + message: () => + (targetDir === '.' + ? 'Current directory' + : `Target directory "${targetDir}"`) + + ' is not empty. Remove existing files and continue?', + }, + { + type: (_, { overwrite }: { overwrite?: boolean }) => { + if (overwrite === false) { + throw new Error(red('✖') + ' Operation cancelled'); + } + return null; + }, + name: 'overwriteChecker', + }, + { + type: 'select', + name: 'variant', + message: bold('Select a variant:'), + choices: () => + variants.map(variant => { + const variantColor = variant.color; + return { + title: variantColor(variant.display || variant.name), + value: variant.name, + }; + }), + }, + ], + { + onCancel: () => { + throw new Error(red('✖') + ' Operation cancelled'); + }, + }, + ); + } catch (error: any) { + // eslint-disable-next-line no-console -- this console.log is needed to log errors + console.log(error); + return; + } +}; + +// eslint-disable-next-line no-console -- this console.log is needed to log errors +init().catch(e => console.log(e));