ts-vibe 1.4.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -63
- package/config.json +12 -0
- package/index.js +15 -21
- package/languages/en.json +22 -0
- package/languages/ru.json +22 -0
- package/package.json +25 -11
- package/src/cmd.mjs +37 -0
- package/src/fs/dir.mjs +34 -0
- package/src/fs/file.mjs +14 -0
- package/src/fs/json/index.mjs +23 -0
- package/src/fs/json/json5.mjs +20 -0
- package/src/fs/path.mjs +46 -0
- package/src/npm.mjs +33 -0
- package/src/project/index.mjs +141 -0
- package/src/project/setup.mjs +86 -0
- package/src/ts.mjs +42 -0
- package/src/utils/cmd.mjs +43 -0
- package/src/utils/language.mjs +29 -0
- package/bin/release/ts-vibe-linux +0 -0
- package/bin/release/ts-vibe-mac +0 -0
- package/bin/release/ts-vibe-windows.exe +0 -0
package/README.md
CHANGED
|
@@ -9,66 +9,5 @@ CLI tool for bootstrapping TypeScript projects
|
|
|
9
9
|
- Create TypeScript project structure
|
|
10
10
|
- Optional Git initialization
|
|
11
11
|
- Automatic README generation
|
|
12
|
-
- Path validation
|
|
13
|
-
-
|
|
14
|
-
- Execution time tracking for operations
|
|
15
|
-
|
|
16
|
-
---
|
|
17
|
-
|
|
18
|
-
## ⚙️ Usage
|
|
19
|
-
|
|
20
|
-
### Create a new project
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
npx ts-vibe --init
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
With Git:
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npx ts-vibe --init --git
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
Skip README creation:
|
|
33
|
-
|
|
34
|
-
```bash
|
|
35
|
-
npx ts-vibe --init --noReadme
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
---
|
|
39
|
-
|
|
40
|
-
### Run project
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
npm start
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
---
|
|
47
|
-
|
|
48
|
-
## 🧠 Flags
|
|
49
|
-
|
|
50
|
-
| Flag | Alias | Description |
|
|
51
|
-
| ------------ | ----- | ------------------------- |
|
|
52
|
-
| `--init` | `-i` | Create a new project |
|
|
53
|
-
| `--clear` | `-c` | Clear current directory |
|
|
54
|
-
| `--git` | `-g` | Initialize git repository |
|
|
55
|
-
| `--lang` | `-l` | Choose cli lang en/ru |
|
|
56
|
-
| `--noReadme` | - | Skip README creation |
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## ⚠️ Requirements
|
|
61
|
-
|
|
62
|
-
- Go 1.20+
|
|
63
|
-
- Node.js (for running TypeScript projects)
|
|
64
|
-
- Git (optional)
|
|
65
|
-
|
|
66
|
-
---
|
|
67
|
-
|
|
68
|
-
## 🛠 Example Workflow
|
|
69
|
-
|
|
70
|
-
```bash
|
|
71
|
-
npx ts-vibe --init --git --noReadme --lang=ru
|
|
72
|
-
cd my-project
|
|
73
|
-
npm start
|
|
74
|
-
```
|
|
12
|
+
- Path validation
|
|
13
|
+
- 🇷🇺 Ru / 🇺🇲 En languages support
|
package/config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"default_code": {
|
|
3
|
+
"indexTs": "let a: string = \"Hello, Typescript!\";\nconsole.log(a);",
|
|
4
|
+
"eslintConfig": "import js from \"@eslint/js\";\nimport tseslint from \"typescript-eslint\";\nimport prettier from \"eslint-config-prettier\";\n\nexport default [\n js.configs.recommended,\n ...tseslint.configs.recommended,\n prettier,\n {\n rules: {\n \"no-unused-vars\": \"warn\"\n }\n }\n];",
|
|
5
|
+
"prettierrc": "{\n \"semi\": true,\n \"singleQuote\": true,\n \"printWidth\": 80\n}"
|
|
6
|
+
},
|
|
7
|
+
"ignores": {
|
|
8
|
+
"git": "node_modules\ndist\n.env\n.vscode\n.idea\n*.log\ncoverage",
|
|
9
|
+
"eslint": "node_modules\ndist",
|
|
10
|
+
"prettier": "node_modules\ndist"
|
|
11
|
+
}
|
|
12
|
+
}
|
package/index.js
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { init } from "./src/project/index.mjs";
|
|
4
|
+
import { setupProject } from "./src/project/setup.mjs";
|
|
5
|
+
import { closeInput } from "./src/cmd.mjs";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
binary = "ts-vibe-linux";
|
|
13
|
-
} else if (platform === "win32") {
|
|
14
|
-
binary = "ts-vibe-windows.exe";
|
|
15
|
-
} else if (platform === "darwin") {
|
|
16
|
-
binary = "ts-vibe-mac";
|
|
17
|
-
} else {
|
|
18
|
-
console.error("Unsupported OS");
|
|
19
|
-
process.exit(1);
|
|
7
|
+
function logInstructions(name) {
|
|
8
|
+
console.log("\n--------------");
|
|
9
|
+
console.log(`\ncd ${name}`);
|
|
10
|
+
console.log(`npm start\n`);
|
|
11
|
+
console.log("--------------");
|
|
20
12
|
}
|
|
21
13
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
async function main() {
|
|
15
|
+
const { name, language, config } = await setupProject();
|
|
16
|
+
closeInput();
|
|
17
|
+
await init(language, config);
|
|
18
|
+
logInstructions(name);
|
|
19
|
+
}
|
|
20
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"success": "Success",
|
|
3
|
+
"error": "Error",
|
|
4
|
+
"choose_node_type": "Choose Node type",
|
|
5
|
+
"update_ts_config": "Update tsconfig.json",
|
|
6
|
+
"update_package": "Update package.json",
|
|
7
|
+
"create_readme": "Create README.md",
|
|
8
|
+
"question_prompt": "[y/n]",
|
|
9
|
+
"confirm_variants": ["y", "yes"],
|
|
10
|
+
"enter_project_name": "Enter project name: ",
|
|
11
|
+
"name_not_valid": "Not valid",
|
|
12
|
+
"dir_is_not_empty": "Dir isn't empty. Clear? ",
|
|
13
|
+
"lint_tip": "- To lint the project: `npm lint`\n",
|
|
14
|
+
"prettier_tip": "- To format the code: `npm prettier`\n",
|
|
15
|
+
"readme_content": "# ts-vibe\n[Source code](https://github.com/yar1kpr0grammer/ts-vibe)\n\n***\n**Main file** is `src/index.ts`\n\n- To run the project: `npm start`\n",
|
|
16
|
+
"use_git": "Use git?",
|
|
17
|
+
"choose_project_template": "Choose project template:",
|
|
18
|
+
"template_min": "Minimal",
|
|
19
|
+
"template_max": "Maximal",
|
|
20
|
+
"template_custom": "Custom",
|
|
21
|
+
"choose_packages": "Choose options:"
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"success": "Успех",
|
|
3
|
+
"error": "Ошибка",
|
|
4
|
+
"choose_node_type": "Выберите тип Nodejs",
|
|
5
|
+
"update_ts_config": "Обновление tsconfig.json",
|
|
6
|
+
"update_package": "Обновление package.json",
|
|
7
|
+
"create_readme": "Создание README.md",
|
|
8
|
+
"question_prompt": "[д/н]",
|
|
9
|
+
"confirm_variants": ["y", "yes", "д", "да"],
|
|
10
|
+
"enter_project_name": "Введите название проекта: ",
|
|
11
|
+
"name_not_valid": "Не правильное",
|
|
12
|
+
"dir_is_not_empty": "Папка не пуста. Очистить? ",
|
|
13
|
+
"lint_tip": "- Чтобы запустить проверку ошибок: `npm lint`\n",
|
|
14
|
+
"prettier_tip": "- Чтобы отформатировать код: `npm prettier`\n",
|
|
15
|
+
"readme_content": "# ts-vibe\n[Исходный код](https://github.com/yar1kpr0grammer/ts-bootstrap)\n\n***\n**Главный файл:** `src/index.ts`\n\n- Чтобы запустить проект: `npm start`\n",
|
|
16
|
+
"use_git": "Использовать git?",
|
|
17
|
+
"choose_project_template": "Выберите вариант проекта:",
|
|
18
|
+
"template_min": "Минимальный",
|
|
19
|
+
"template_max": "Максимальный",
|
|
20
|
+
"template_custom": "Настраиваемый",
|
|
21
|
+
"choose_packages": "Выберете что добавить в проект:"
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
"name": "ts-vibe",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ts-vibe": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node index.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"files": [
|
|
14
|
+
"languages",
|
|
15
|
+
"src",
|
|
16
|
+
"config.json",
|
|
17
|
+
"index.js"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@inquirer/checkbox": "^5.1.4",
|
|
23
|
+
"@inquirer/prompts": "^8.4.2",
|
|
24
|
+
"chalk": "^5.6.2",
|
|
25
|
+
"json5": "^2.2.3"
|
|
26
|
+
}
|
|
13
27
|
}
|
package/src/cmd.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { spawnSync } from "child_process";
|
|
3
|
+
import readline from "readline";
|
|
4
|
+
|
|
5
|
+
export function exec(command) {
|
|
6
|
+
const result = spawnSync(command, {
|
|
7
|
+
shell: true,
|
|
8
|
+
encoding: "utf-8",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
if (result.status !== 0) {
|
|
12
|
+
throw new Error(result.stderr || result.stdout);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return result.stdout;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const rl = readline.createInterface({
|
|
19
|
+
input: process.stdin,
|
|
20
|
+
output: process.stdout,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const input = (prompt) =>
|
|
24
|
+
new Promise((resolve) => rl.question(chalk.bold(prompt), resolve));
|
|
25
|
+
|
|
26
|
+
export async function ask(
|
|
27
|
+
prompt,
|
|
28
|
+
questionPrompt = " [y/n] ",
|
|
29
|
+
confirmVariants = ["y", "yes"],
|
|
30
|
+
) {
|
|
31
|
+
const ans = await input(chalk.bold(prompt + questionPrompt));
|
|
32
|
+
return confirmVariants.includes(ans.toLowerCase());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function closeInput() {
|
|
36
|
+
rl.close();
|
|
37
|
+
}
|
package/src/fs/dir.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fsSync from "fs";
|
|
2
|
+
import fsPromises from "fs/promises";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
async function clear(folderPath = ".") {
|
|
6
|
+
const items = await fsPromises.readdir(folderPath);
|
|
7
|
+
|
|
8
|
+
for (const item of items) {
|
|
9
|
+
const fullPath = path.join(folderPath, item);
|
|
10
|
+
await fsPromises.rm(fullPath, {
|
|
11
|
+
recursive: true,
|
|
12
|
+
force: true,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function create(path) {
|
|
18
|
+
fsSync.mkdirSync(path, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function hasFiles(dirPath = ".") {
|
|
22
|
+
const items = await fsPromises.readdir(dirPath);
|
|
23
|
+
|
|
24
|
+
for (const item of items) {
|
|
25
|
+
const fullPath = path.join(dirPath, item);
|
|
26
|
+
const stat = await fsPromises.stat(fullPath);
|
|
27
|
+
if (stat.isFile()) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default { clear, create, hasFiles };
|
package/src/fs/file.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import fsSync from "fs";
|
|
2
|
+
import Path from "./path.mjs";
|
|
3
|
+
|
|
4
|
+
function create(filePath, content) {
|
|
5
|
+
const fullPath = Path.resolveProjectPath(filePath);
|
|
6
|
+
fsSync.writeFileSync(fullPath, content);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function append(filePath, content) {
|
|
10
|
+
const fullPath = Path.resolveProjectPath(filePath);
|
|
11
|
+
fsSync.appendFileSync(fullPath, content);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default { create, append };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import Path from "../path.mjs";
|
|
3
|
+
|
|
4
|
+
export function read(filePath, type = "project") {
|
|
5
|
+
const fullPath =
|
|
6
|
+
type === "cli"
|
|
7
|
+
? Path.resolveCliPath(filePath)
|
|
8
|
+
: Path.resolveProjectPath(filePath);
|
|
9
|
+
|
|
10
|
+
const raw = fs.readFileSync(fullPath, "utf-8");
|
|
11
|
+
return JSON.parse(raw);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function write(filePath, data, type = "project") {
|
|
15
|
+
const fullPath =
|
|
16
|
+
type === "cli"
|
|
17
|
+
? Path.resolveCliPath(filePath)
|
|
18
|
+
: Path.resolveProjectPath(filePath);
|
|
19
|
+
|
|
20
|
+
fs.writeFileSync(fullPath, JSON.stringify(data, null, 2), "utf8");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default { read, write };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import JSON5 from "json5";
|
|
3
|
+
import Path from "../path.mjs";
|
|
4
|
+
|
|
5
|
+
export function read(filePath) {
|
|
6
|
+
const fullPath = Path.resolveProjectPath(filePath);
|
|
7
|
+
const raw = fs.readFileSync(fullPath, "utf-8");
|
|
8
|
+
|
|
9
|
+
return JSON5.parse(raw);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function write(filePath, data) {
|
|
13
|
+
const fullPath = Path.resolveProjectPath(filePath);
|
|
14
|
+
|
|
15
|
+
const content = JSON5.stringify(data, null, 2);
|
|
16
|
+
|
|
17
|
+
fs.writeFileSync(fullPath, content, "utf-8");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default { read, write };
|
package/src/fs/path.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import fsSync from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
function cd(name) {
|
|
9
|
+
try {
|
|
10
|
+
process.chdir(name);
|
|
11
|
+
return {
|
|
12
|
+
success: true,
|
|
13
|
+
path: process.cwd(),
|
|
14
|
+
};
|
|
15
|
+
} catch (err) {
|
|
16
|
+
throw new Error(err.message);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// путь до самого cli пакета
|
|
21
|
+
export function resolveCliPath(p) {
|
|
22
|
+
if (path.isAbsolute(p)) return p;
|
|
23
|
+
return path.join(__dirname, "../../", p);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// путь до текущего проекта
|
|
27
|
+
export function resolveProjectPath(p) {
|
|
28
|
+
if (path.isAbsolute(p)) return p;
|
|
29
|
+
return path.join(process.cwd(), p);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function exists(dirPath) {
|
|
33
|
+
try {
|
|
34
|
+
fsSync.accessSync(dirPath, fsSync.constants.F_OK);
|
|
35
|
+
return true;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default {
|
|
42
|
+
cd,
|
|
43
|
+
exists,
|
|
44
|
+
resolveCliPath,
|
|
45
|
+
resolveProjectPath,
|
|
46
|
+
};
|
package/src/npm.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import json from "./fs/json/index.mjs";
|
|
2
|
+
|
|
3
|
+
const PACKAGE_PATH = "package.json";
|
|
4
|
+
|
|
5
|
+
function getPackageConfig() {
|
|
6
|
+
return json.read(PACKAGE_PATH);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function updatePackageJSON(nodeType) {
|
|
10
|
+
const config = getPackageConfig();
|
|
11
|
+
|
|
12
|
+
config.scripts ??= {};
|
|
13
|
+
config.scripts.start = "tsc && node dist/index.js";
|
|
14
|
+
config.type = nodeType;
|
|
15
|
+
|
|
16
|
+
json.write(PACKAGE_PATH, config);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function addScript(name, command) {
|
|
20
|
+
const config = getPackageConfig();
|
|
21
|
+
|
|
22
|
+
config.scripts ??= {};
|
|
23
|
+
config.scripts[name] = command;
|
|
24
|
+
|
|
25
|
+
json.write(PACKAGE_PATH, config);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const projectNamePattern = "^[a-z0-9]+([\\-_.][a-z0-9]+)*$";
|
|
29
|
+
const projectNameReg = new RegExp(projectNamePattern, "i");
|
|
30
|
+
|
|
31
|
+
export function checkProjectName(name) {
|
|
32
|
+
return projectNameReg.test(name);
|
|
33
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { exec } from "../cmd.mjs";
|
|
2
|
+
import { updatePackageJSON, addScript } from "../npm.mjs";
|
|
3
|
+
import File from "../fs/file.mjs";
|
|
4
|
+
import Dir from "../fs/dir.mjs";
|
|
5
|
+
import ts from "../ts.mjs";
|
|
6
|
+
import { confirm } from "../utils/cmd.mjs";
|
|
7
|
+
|
|
8
|
+
async function setupGit(language, config) {
|
|
9
|
+
File.create(".gitignore", config.ignores.git);
|
|
10
|
+
File.create(".gitattributes", "* text=auto\n");
|
|
11
|
+
const commands = [
|
|
12
|
+
{ command: "git init", message: "git init" },
|
|
13
|
+
{
|
|
14
|
+
command: "git add .",
|
|
15
|
+
message: "git add .",
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
command: 'git commit -m "init project with ts-vibe"',
|
|
19
|
+
message: "git commit",
|
|
20
|
+
},
|
|
21
|
+
];
|
|
22
|
+
for (let cmd of commands) {
|
|
23
|
+
await confirm(
|
|
24
|
+
() => exec(cmd.command),
|
|
25
|
+
cmd.message,
|
|
26
|
+
language.success,
|
|
27
|
+
language.error,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function setupLint(language, config) {
|
|
33
|
+
addScript("lint", "eslint .");
|
|
34
|
+
File.create("eslint.config.js", config.default_code.eslintConfig);
|
|
35
|
+
File.create(".eslintignore", config.ignores.eslint);
|
|
36
|
+
await confirm(
|
|
37
|
+
() =>
|
|
38
|
+
exec(
|
|
39
|
+
"npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier",
|
|
40
|
+
),
|
|
41
|
+
"Install eslint",
|
|
42
|
+
language.success,
|
|
43
|
+
language.error,
|
|
44
|
+
);
|
|
45
|
+
if (config.options.includes("README.md")) {
|
|
46
|
+
File.append("README.md", language.lint_tip);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function setupPrettier(language, config) {
|
|
51
|
+
addScript("format", "prettier --write .");
|
|
52
|
+
File.create(".prettierrc", config.default_code.prettierrc);
|
|
53
|
+
File.create(".prettierignore", config.ignores.prettier);
|
|
54
|
+
await confirm(
|
|
55
|
+
() => exec("npm i -D prettier "),
|
|
56
|
+
"Install prettier",
|
|
57
|
+
language.success,
|
|
58
|
+
language.error,
|
|
59
|
+
);
|
|
60
|
+
if (config.options.includes("README.md")) {
|
|
61
|
+
File.append("README.md", language.prettier_tip);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function setupReadme(language) {
|
|
66
|
+
await confirm(
|
|
67
|
+
() => File.create("README.md", language.readme_content),
|
|
68
|
+
language.create_readme,
|
|
69
|
+
language.success,
|
|
70
|
+
language.error,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function setupNode(language) {
|
|
75
|
+
await confirm(
|
|
76
|
+
async () => {
|
|
77
|
+
await exec("npm i -D @types/node");
|
|
78
|
+
ts.addType("node");
|
|
79
|
+
},
|
|
80
|
+
"Add node",
|
|
81
|
+
language.success,
|
|
82
|
+
language.error,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
export async function init(language, config) {
|
|
86
|
+
const commands = [
|
|
87
|
+
{ command: "npm init -y", message: "npm project init" },
|
|
88
|
+
{
|
|
89
|
+
command: "npm i -D typescript",
|
|
90
|
+
message: "install typescript",
|
|
91
|
+
},
|
|
92
|
+
{ command: "npx tsc --init", message: "init typescript" },
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
for (const cmd of commands) {
|
|
96
|
+
await confirm(
|
|
97
|
+
() => exec(cmd.command),
|
|
98
|
+
cmd.message,
|
|
99
|
+
language.success,
|
|
100
|
+
language.error,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
ts.readConfig();
|
|
105
|
+
|
|
106
|
+
await confirm(
|
|
107
|
+
ts.updateTsConfig,
|
|
108
|
+
language.update_ts_config,
|
|
109
|
+
language.success,
|
|
110
|
+
language.error,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
await confirm(
|
|
114
|
+
() => updatePackageJSON(config.nodeType),
|
|
115
|
+
language.update_package,
|
|
116
|
+
language.success,
|
|
117
|
+
language.error,
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
Dir.create("src");
|
|
121
|
+
File.create("src/index.ts", config.default_code.indexTs);
|
|
122
|
+
|
|
123
|
+
if (config.options.includes("node")) {
|
|
124
|
+
await setupNode(language);
|
|
125
|
+
}
|
|
126
|
+
if (config.options.includes("README.md")) {
|
|
127
|
+
await setupReadme(language);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (config.options.includes("eslint")) {
|
|
131
|
+
await setupLint(language, config);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (config.options.includes("prettier")) {
|
|
135
|
+
await setupPrettier(language, config);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (config.options.includes("git")) {
|
|
139
|
+
await setupGit(language, config);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { radio, checkbox } from "../utils/cmd.mjs";
|
|
2
|
+
import { input, ask } from "../cmd.mjs";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import languageLib from "../utils/language.mjs";
|
|
5
|
+
import Path from "../fs/path.mjs";
|
|
6
|
+
import { checkProjectName } from "../npm.mjs";
|
|
7
|
+
import Dir from "../fs/dir.mjs";
|
|
8
|
+
|
|
9
|
+
export async function getName(
|
|
10
|
+
prompt = "Enter project name: ",
|
|
11
|
+
error = "Not valid",
|
|
12
|
+
) {
|
|
13
|
+
process.stdin.setRawMode(false);
|
|
14
|
+
process.stdin.resume();
|
|
15
|
+
let name;
|
|
16
|
+
while (true) {
|
|
17
|
+
name = await input(prompt);
|
|
18
|
+
if (checkProjectName(name)) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
console.log(error);
|
|
22
|
+
}
|
|
23
|
+
return name;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function chooseNodeType(language) {
|
|
27
|
+
return await radio(language.choose_node_type, ["commonjs", "module"]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function prepareDir(path, language) {
|
|
31
|
+
if (!Path.exists(path)) {
|
|
32
|
+
Dir.create(path);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
Path.cd(path);
|
|
36
|
+
|
|
37
|
+
if (await Dir.hasFiles()) {
|
|
38
|
+
if (
|
|
39
|
+
await ask(
|
|
40
|
+
language.dir_is_not_empty,
|
|
41
|
+
language.question_prompt,
|
|
42
|
+
language.confirm_variants,
|
|
43
|
+
)
|
|
44
|
+
) {
|
|
45
|
+
await Dir.clear();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function setupProject() {
|
|
51
|
+
const language = languageLib.init(languageLib.choose());
|
|
52
|
+
const name = await getName(language.enter_project_name);
|
|
53
|
+
console.log();
|
|
54
|
+
|
|
55
|
+
const configPath = Path.resolveCliPath("config.json");
|
|
56
|
+
const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
57
|
+
await prepareDir(name, language);
|
|
58
|
+
|
|
59
|
+
const allOptions = ["git", "eslint", "prettier", "README.md", "node"];
|
|
60
|
+
console.log();
|
|
61
|
+
const res = await radio("Выберите вариант проекта:", [
|
|
62
|
+
language.template_min,
|
|
63
|
+
language.template_max,
|
|
64
|
+
language.template_custom,
|
|
65
|
+
]);
|
|
66
|
+
switch (res) {
|
|
67
|
+
case language.template_max:
|
|
68
|
+
config.options = allOptions;
|
|
69
|
+
break;
|
|
70
|
+
case language.template_min:
|
|
71
|
+
config.options = [];
|
|
72
|
+
break;
|
|
73
|
+
case language.template_custom:
|
|
74
|
+
config.options = await checkbox(
|
|
75
|
+
language.choose_project_template,
|
|
76
|
+
allOptions,
|
|
77
|
+
);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
console.log();
|
|
81
|
+
|
|
82
|
+
config.nodeType = await chooseNodeType(language);
|
|
83
|
+
console.log();
|
|
84
|
+
|
|
85
|
+
return { name, language, config };
|
|
86
|
+
}
|
package/src/ts.mjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { read } from "./fs/json/json5.mjs";
|
|
2
|
+
import { write } from "./fs/json/index.mjs";
|
|
3
|
+
|
|
4
|
+
let config = null;
|
|
5
|
+
|
|
6
|
+
export function readConfig() {
|
|
7
|
+
config = read("tsconfig.json");
|
|
8
|
+
return config;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function updateTsConfig() {
|
|
12
|
+
if (!config) {
|
|
13
|
+
readConfig(); // автоматически читаем, если еще не прочитан
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!config) {
|
|
17
|
+
throw new Error("tsconfig.json not found");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
config.compilerOptions.rootDir = "./src";
|
|
21
|
+
config.compilerOptions.outDir = "./dist";
|
|
22
|
+
write("tsconfig.json", config);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function addType(typeName) {
|
|
26
|
+
if (!config) {
|
|
27
|
+
readConfig(); // автоматически читаем, если еще не прочитан
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!config) {
|
|
31
|
+
throw new Error("tsconfig.json not found");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!config.compilerOptions.types) {
|
|
35
|
+
config.compilerOptions.types = [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
config.compilerOptions.types.push(typeName);
|
|
39
|
+
write("tsconfig.json", config);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default { readConfig, updateTsConfig, addType };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { select } from "@inquirer/prompts";
|
|
3
|
+
import checkboxPrompt from "@inquirer/checkbox";
|
|
4
|
+
|
|
5
|
+
export async function confirm(
|
|
6
|
+
fn,
|
|
7
|
+
message,
|
|
8
|
+
success = "Success",
|
|
9
|
+
error = "Error",
|
|
10
|
+
) {
|
|
11
|
+
try {
|
|
12
|
+
const res = await fn();
|
|
13
|
+
|
|
14
|
+
console.log(`${chalk.bold(chalk.green(success))}: ${message}`);
|
|
15
|
+
return res;
|
|
16
|
+
} catch (e) {
|
|
17
|
+
console.log(`${chalk.bold(chalk.red(error))}: ${message}`);
|
|
18
|
+
console.error(e.message || e);
|
|
19
|
+
return e;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function radio(prompt, options = []) {
|
|
24
|
+
if (!options.length) {
|
|
25
|
+
throw new Error("radio: options is empty");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return await select({
|
|
29
|
+
message: prompt,
|
|
30
|
+
choices: options.map((o) => ({ name: o, value: o })),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function checkbox(prompt, options = []) {
|
|
35
|
+
if (!options.length) {
|
|
36
|
+
throw new Error("checkbox: options is empty");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return await checkboxPrompt({
|
|
40
|
+
message: prompt,
|
|
41
|
+
choices: options.map((o) => ({ name: o, value: o })),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { read } from "../fs/json/index.mjs";
|
|
2
|
+
|
|
3
|
+
function getSystemLocale() {
|
|
4
|
+
return (
|
|
5
|
+
Intl.DateTimeFormat().resolvedOptions().locale ||
|
|
6
|
+
process.env.LC_ALL ||
|
|
7
|
+
process.env.LANG ||
|
|
8
|
+
"en-US"
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function choose() {
|
|
13
|
+
const locale = getSystemLocale();
|
|
14
|
+
return locale.split("-")[0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function init(language = "en") {
|
|
18
|
+
try {
|
|
19
|
+
return read(`languages/${language}.json`, "cli");
|
|
20
|
+
} catch {
|
|
21
|
+
// fallback если языка нет
|
|
22
|
+
return read("languages/en.json", "cli");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default {
|
|
27
|
+
choose,
|
|
28
|
+
init,
|
|
29
|
+
};
|
|
Binary file
|
package/bin/release/ts-vibe-mac
DELETED
|
Binary file
|
|
Binary file
|