Skip to content

Commit

Permalink
add cli for creating new presentify project (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestTeluk authored Mar 10, 2023
1 parent 00b9a31 commit f8fa14b
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
Expand Down
6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const settings = require('eslint-config-vazco/default.json');

module.exports = settings.rules['prettier/prettier'][1];
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
},
"lint-staged": {
"*.{ts,tsx}": [
"prettier --config .prettierrc --write",
"prettier --config .prettierrc.js --write",
"eslint --fix"
]
},
"dependencies": {
"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"
}
}
84 changes: 84 additions & 0 deletions packages/create-presentify/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/create-presentify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
112 changes: 111 additions & 1 deletion packages/create-presentify/src/index.ts
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit f8fa14b

Please sign in to comment.