ts-vibe 2.1.0 → 2.4.0

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/src/npm.mjs CHANGED
@@ -10,7 +10,10 @@ export function updatePackageJSON(nodeType) {
10
10
  const config = getPackageConfig();
11
11
 
12
12
  config.scripts ??= {};
13
- config.scripts.start = "tsc && node dist/index.js";
13
+ config.scripts.dev = "tsx watch src/index.ts";
14
+ config.scripts.start = "tsx src/index.ts";
15
+ config.scripts.build = "tsc";
16
+ config.scripts.prod = "node dist/index.js";
14
17
  config.type = nodeType;
15
18
 
16
19
  json.write(PACKAGE_PATH, config);
@@ -0,0 +1,186 @@
1
+ const CORE_DEV_DEPENDENCIES = {
2
+ tsx: "^4.23.15",
3
+ typescript: "~6.0.3",
4
+ };
5
+
6
+ export const FEATURES = {
7
+ git: {
8
+ labelKey: "feature_git",
9
+ recommended: true,
10
+ files({ config }) {
11
+ return {
12
+ ".gitattributes": "* text=auto\n",
13
+ ".gitignore": `${config.ignores.git}\n`,
14
+ };
15
+ },
16
+ },
17
+ readme: {
18
+ labelKey: "feature_readme",
19
+ recommended: true,
20
+ files({ language, selectedFeatures }) {
21
+ const tips = [];
22
+ if (selectedFeatures.includes("eslint")) tips.push(language.lint_tip);
23
+ if (selectedFeatures.includes("prettier")) tips.push(language.prettier_tip);
24
+ if (selectedFeatures.includes("vitest")) tips.push(language.test_tip);
25
+
26
+ return {
27
+ "README.md": `${language.readme_content}${tips.join("")}`,
28
+ };
29
+ },
30
+ },
31
+ node: {
32
+ labelKey: "feature_node",
33
+ recommended: true,
34
+ devDependencies: {
35
+ "@types/node": "^26.6.6",
36
+ },
37
+ configureTs(config) {
38
+ config.compilerOptions.types = ["node"];
39
+ },
40
+ },
41
+ alias: {
42
+ labelKey: "feature_alias",
43
+ recommended: true,
44
+ configureTs(config) {
45
+ config.compilerOptions.baseUrl = ".";
46
+ config.compilerOptions.paths = {
47
+ "@/*": ["src/*"],
48
+ };
49
+ },
50
+ },
51
+ eslint: {
52
+ labelKey: "feature_eslint",
53
+ devDependencies: {
54
+ "@eslint/js": "^10.0.1",
55
+ eslint: "^10.15.0",
56
+ "eslint-config-prettier": "^10.1.8",
57
+ globals: "^17.14.0",
58
+ "typescript-eslint": "^8.73.0",
59
+ },
60
+ scripts: {
61
+ lint: "eslint .",
62
+ },
63
+ files({ config }) {
64
+ return {
65
+ "eslint.config.mjs": config.default_code.eslintConfig,
66
+ };
67
+ },
68
+ },
69
+ vitest: {
70
+ labelKey: "feature_vitest",
71
+ devDependencies: {
72
+ vitest: "^4.1.12",
73
+ },
74
+ scripts: {
75
+ test: "vitest",
76
+ "test:run": "vitest run",
77
+ },
78
+ files({ config }) {
79
+ return {
80
+ "src/index.test.ts": config.default_code.vitestTest,
81
+ };
82
+ },
83
+ },
84
+ prettier: {
85
+ labelKey: "feature_prettier",
86
+ devDependencies: {
87
+ prettier: "^3.10.1",
88
+ },
89
+ scripts: {
90
+ format: "prettier --write .",
91
+ },
92
+ files({ config }) {
93
+ return {
94
+ ".prettierignore": `${config.ignores.prettier}\n`,
95
+ ".prettierrc": config.default_code.prettierrc,
96
+ };
97
+ },
98
+ },
99
+ };
100
+
101
+ export const FEATURE_IDS = Object.keys(FEATURES);
102
+ export const RECOMMENDED_FEATURE_IDS = FEATURE_IDS.filter(
103
+ (id) => FEATURES[id].recommended,
104
+ );
105
+
106
+ export function getFeatureChoices(language) {
107
+ return FEATURE_IDS.map((id) => ({
108
+ label: language[FEATURES[id].labelKey],
109
+ value: id,
110
+ }));
111
+ }
112
+
113
+ function createTsConfig(nodeType) {
114
+ return {
115
+ compilerOptions: {
116
+ rootDir: "./src",
117
+ outDir: "./dist",
118
+ target: "ES2020",
119
+ lib: [nodeType === "module" ? "ESNext" : "ES2020"],
120
+ module: nodeType === "module" ? "NodeNext" : "CommonJS",
121
+ moduleResolution: nodeType === "module" ? "NodeNext" : "Node",
122
+ sourceMap: true,
123
+ strict: true,
124
+ noUncheckedIndexedAccess: true,
125
+ exactOptionalPropertyTypes: true,
126
+ resolveJsonModule: true,
127
+ forceConsistentCasingInFileNames: true,
128
+ skipLibCheck: true,
129
+ esModuleInterop: true,
130
+ allowSyntheticDefaultImports: true,
131
+ },
132
+ include: ["src/**/*.ts"],
133
+ };
134
+ }
135
+
136
+ export function createProjectPlan({
137
+ name,
138
+ nodeType,
139
+ selectedFeatures,
140
+ language,
141
+ config,
142
+ }) {
143
+ const scripts = {
144
+ dev: "tsx watch src/index.ts",
145
+ start: "tsx src/index.ts",
146
+ check: "tsc --noEmit",
147
+ build: "tsc",
148
+ prod: "node dist/index.js",
149
+ };
150
+ const devDependencies = { ...CORE_DEV_DEPENDENCIES };
151
+ const tsconfig = createTsConfig(nodeType);
152
+ const context = { config, language, selectedFeatures };
153
+ const files = {
154
+ "src/index.ts": config.default_code.indexTs,
155
+ };
156
+
157
+ for (const id of selectedFeatures) {
158
+ const feature = FEATURES[id];
159
+ if (!feature) {
160
+ throw new Error(`Unknown feature: ${id}`);
161
+ }
162
+
163
+ Object.assign(scripts, feature.scripts);
164
+ Object.assign(devDependencies, feature.devDependencies);
165
+ Object.assign(files, feature.files?.(context));
166
+ feature.configureTs?.(tsconfig, context);
167
+ }
168
+
169
+ const packageJson = {
170
+ name,
171
+ version: "1.0.0",
172
+ private: true,
173
+ type: nodeType,
174
+ scripts,
175
+ devDependencies,
176
+ };
177
+
178
+ files["package.json"] = `${JSON.stringify(packageJson, null, 2)}\n`;
179
+ files["tsconfig.json"] = `${JSON.stringify(tsconfig, null, 2)}\n`;
180
+
181
+ return {
182
+ files,
183
+ packageJson,
184
+ tsconfig,
185
+ };
186
+ }
@@ -1,141 +1,13 @@
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
1
  import { confirm } from "../utils/cmd.mjs";
2
+ import { executeTransaction } from "./transaction.mjs";
7
3
 
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
- "npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier",
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
- "npm i -D 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
- }
4
+ export async function init(project) {
5
+ const { language } = project;
73
6
 
74
- async function setupNode(language) {
75
7
  await confirm(
76
- async () => {
77
- await exec("npm i -D @types/node");
78
- ts.addType("node");
79
- },
80
- "npm i -D @types/node",
8
+ (ui) => executeTransaction(project, { onStep: ui.message }),
9
+ project.install ? language.create_and_install : language.create_without_install,
81
10
  language.success,
82
11
  language.error,
83
12
  );
84
13
  }
85
- export async function init(language, config) {
86
- const commands = [
87
- { command: "npm init -y", message: "npm init -y" },
88
- {
89
- command: "npm i -D typescript",
90
- message: "npm i -D typescript",
91
- },
92
- { command: "npx tsc --init", message: "npx tsc --init" },
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
- }
@@ -1,124 +1,197 @@
1
- import { radio, checkbox } from "../utils/cmd.mjs";
2
- import { input, ask } from "../cmd.mjs";
3
- import fs from "fs";
4
- import path from "path";
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
5
3
 
4
+ import { checkProjectName } from "../npm.mjs";
5
+ import {
6
+ ask,
7
+ cancel,
8
+ checkbox,
9
+ input,
10
+ intro,
11
+ note,
12
+ radio,
13
+ } from "../utils/cmd.mjs";
6
14
  import languageLib from "../utils/language.mjs";
7
15
  import Path from "../fs/path.mjs";
8
- import { checkProjectName } from "../npm.mjs";
9
- import Dir from "../fs/dir.mjs";
10
-
11
- export async function getName(
12
- prompt = "Enter project name: ",
13
- error = "Not valid",
14
- ) {
15
- process.stdin.setRawMode(false);
16
- process.stdin.resume();
17
-
18
- while (true) {
19
- const value = await input(prompt);
20
-
21
- // "." означает текущую папку
22
- if (value === ".") {
23
- const currentDirName = path.basename(process.cwd());
24
-
25
- if (checkProjectName(currentDirName)) {
26
- return {
27
- name: currentDirName,
28
- dir: ".",
29
- };
30
- }
31
-
32
- console.log(error);
33
- continue;
34
- }
35
-
36
- if (checkProjectName(value)) {
37
- return {
38
- name: value,
39
- dir: value,
40
- };
41
- }
42
-
43
- console.log(error);
16
+ import {
17
+ createProjectPlan,
18
+ getFeatureChoices,
19
+ RECOMMENDED_FEATURE_IDS,
20
+ } from "./features.mjs";
21
+
22
+ async function pathStat(targetPath) {
23
+ try {
24
+ return await fs.lstat(targetPath);
25
+ } catch (error) {
26
+ if (error.code === "ENOENT") return null;
27
+ throw error;
44
28
  }
45
29
  }
46
30
 
47
- export async function chooseNodeType(language) {
48
- return await radio(language.choose_node_type, ["module", "commonjs"]);
31
+ async function containsProjectFiles(targetPath) {
32
+ const entries = await fs.readdir(targetPath);
33
+ return entries.some((entry) => entry !== ".git");
49
34
  }
50
35
 
51
- export async function prepareDir(dirPath, language) {
52
- if (!Path.exists(dirPath)) {
53
- Dir.create(dirPath);
54
- }
55
-
56
- Path.cd(dirPath);
36
+ async function findGitRoot(startPath) {
37
+ let current = startPath;
57
38
 
58
- if (await Dir.hasFiles()) {
59
- const shouldClear = await ask(
60
- language.dir_is_not_empty,
61
- language.question_prompt,
62
- language.confirm_variants,
63
- );
64
-
65
- if (shouldClear) {
66
- await Dir.clear();
67
- }
39
+ while (true) {
40
+ if (await pathStat(path.join(current, ".git"))) return current;
41
+ const parent = path.dirname(current);
42
+ if (parent === current) return null;
43
+ current = parent;
68
44
  }
69
45
  }
70
46
 
71
- export async function setupProject() {
72
- const language = languageLib.init(languageLib.choose());
73
-
74
- const { name, dir } = await getName(language.enter_project_name);
75
-
76
- console.log();
77
-
78
- const configPath = Path.resolveCliPath("config.json");
79
-
80
- const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
81
-
82
- // Используем dir, а не name
83
- await prepareDir(dir, language);
47
+ export async function getName(
48
+ prompt = "Enter project name",
49
+ error = "Not valid",
50
+ ) {
51
+ const value = await input(prompt, {
52
+ placeholder: "my-project",
53
+ validate(inputValue) {
54
+ const projectName = inputValue.trim();
55
+ const name =
56
+ projectName === "." ? path.basename(process.cwd()) : projectName;
57
+
58
+ if (!checkProjectName(name)) return error;
59
+ },
60
+ });
61
+ const dir = value.trim();
84
62
 
85
- const allOptions = ["git", "eslint", "prettier", "README.md", "node"];
63
+ return {
64
+ name: dir === "." ? path.basename(process.cwd()) : dir,
65
+ dir,
66
+ };
67
+ }
86
68
 
87
- console.log();
69
+ export async function chooseNodeType(language) {
70
+ return radio(language.choose_node_type, ["module", "commonjs"], "module");
71
+ }
88
72
 
89
- const template = await radio("Выберите вариант проекта:", [
90
- language.template_min,
91
- language.template_max,
92
- language.template_custom,
73
+ async function inspectTarget(targetPath, language) {
74
+ const stat = await pathStat(targetPath);
75
+ if (!stat) return "create";
76
+ if (stat.isSymbolicLink()) throw new Error(language.target_is_symlink);
77
+ if (!stat.isDirectory()) throw new Error(language.target_is_not_directory);
78
+ if (!(await containsProjectFiles(targetPath))) return "ignore";
79
+
80
+ const action = await radio(language.dir_is_not_empty, [
81
+ { label: language.dir_action_cancel, value: "cancel" },
82
+ { label: language.dir_action_clear, value: "clear" },
83
+ { label: language.dir_action_ignore, value: "ignore" },
93
84
  ]);
94
85
 
95
- switch (template) {
96
- case language.template_max:
97
- config.options = allOptions;
98
- break;
99
-
100
- case language.template_min:
101
- config.options = ["README.md"];
102
- break;
103
-
104
- case language.template_custom:
105
- config.options = await checkbox(
106
- language.choose_project_template,
107
- allOptions,
108
- );
109
- break;
86
+ if (action === "cancel") {
87
+ cancel(language.operation_cancelled);
88
+ process.exit(0);
110
89
  }
111
90
 
112
- console.log();
91
+ return action;
92
+ }
113
93
 
114
- config.nodeType = await chooseNodeType(language);
94
+ function buildSummary(project) {
95
+ const language = project.language;
96
+ const featureLabels = project.selectedFeatures.map(
97
+ (id) => language[`feature_${id}`],
98
+ );
99
+ const policyLabels = {
100
+ create: language.summary_policy_create,
101
+ clear: language.summary_policy_clear,
102
+ ignore: language.summary_policy_ignore,
103
+ };
104
+ const gitBehavior = project.selectedFeatures.includes("git")
105
+ ? project.gitMode === "new"
106
+ ? language.summary_git_new
107
+ : language.summary_git_existing
108
+ : language.summary_git_disabled;
109
+
110
+ return [
111
+ `${language.summary_project}: ${project.name}`,
112
+ `${language.summary_target}: ${project.targetPath}`,
113
+ `${language.summary_directory}: ${policyLabels[project.directoryPolicy]}`,
114
+ `${language.summary_module}: ${project.nodeType}`,
115
+ `${language.summary_features}: ${featureLabels.join(", ") || language.none}`,
116
+ `${language.summary_install}: ${project.install ? language.yes : language.no}`,
117
+ `${language.summary_git}: ${gitBehavior}`,
118
+ "",
119
+ `${language.summary_files}:`,
120
+ ...Object.keys(project.files).map((file) => ` • ${file}`),
121
+ ].join("\n");
122
+ }
115
123
 
116
- console.log();
124
+ export async function setupProject({ skipInstall = false } = {}) {
125
+ const language = languageLib.init(languageLib.choose());
126
+ intro(language.cli_title, language.operation_cancelled);
127
+
128
+ const startDirectory = process.cwd();
129
+ const { name, dir } = await getName(
130
+ language.enter_project_name,
131
+ language.name_not_valid,
132
+ );
133
+ const targetPath = path.resolve(startDirectory, dir);
134
+ const directoryPolicy = await inspectTarget(targetPath, language);
135
+ const configPath = Path.resolveCliPath("config.json");
136
+ const config = JSON.parse(await fs.readFile(configPath, "utf8"));
137
+
138
+ const template = await radio(
139
+ language.choose_project_template,
140
+ [
141
+ { label: language.template_recommended, value: "recommended" },
142
+ { label: language.template_custom, value: "custom" },
143
+ ],
144
+ "recommended",
145
+ );
146
+
147
+ let selectedFeatures;
148
+ let nodeType = "module";
149
+
150
+ if (template === "recommended") {
151
+ selectedFeatures = [...RECOMMENDED_FEATURE_IDS];
152
+ } else {
153
+ selectedFeatures = await checkbox(
154
+ language.choose_packages,
155
+ getFeatureChoices(language),
156
+ RECOMMENDED_FEATURE_IDS,
157
+ );
158
+ nodeType = await chooseNodeType(language);
159
+ }
117
160
 
118
- return {
161
+ const gitRoot = await findGitRoot(
162
+ (await pathStat(targetPath)) ? targetPath : path.dirname(targetPath),
163
+ );
164
+ const generated = createProjectPlan({
119
165
  name,
166
+ nodeType,
167
+ selectedFeatures,
120
168
  language,
121
169
  config,
170
+ });
171
+ const project = {
172
+ ...generated,
173
+ config,
122
174
  dir,
175
+ directoryPolicy,
176
+ gitMode: gitRoot ? "existing" : "new",
177
+ install: !skipInstall,
178
+ language,
179
+ name,
180
+ nodeType,
181
+ selectedFeatures,
182
+ targetPath,
123
183
  };
184
+
185
+ note(buildSummary(project), language.summary_title);
186
+ const approved = await ask(language.confirm_create, {
187
+ initialValue: true,
188
+ active: language.yes,
189
+ inactive: language.no,
190
+ });
191
+ if (!approved) {
192
+ cancel(language.operation_cancelled);
193
+ process.exit(0);
194
+ }
195
+
196
+ return project;
124
197
  }