This repository was archived by the owner on May 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig-inferrer.ts
More file actions
170 lines (155 loc) · 5.95 KB
/
config-inferrer.ts
File metadata and controls
170 lines (155 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { WorkspaceConfig } from './config';
export interface Context {
config: WorkspaceConfig;
exists(path: string): Promise<boolean>;
read(path: string): Promise<string | undefined>;
}
export class ConfigInferrer {
protected contributions: ((ctx: Context) => Promise<void>)[] = [
this.checkNode.bind(this),
this.checkJava.bind(this),
this.checkMake.bind(this),
this.checkPython.bind(this),
this.checkGo.bind(this),
this.checkRust.bind(this),
this.checkNuget.bind(this),
this.checkRuby.bind(this),
this.checkDockerCompose.bind(this),
]
async getConfig(ctx: Context): Promise<WorkspaceConfig> {
for (const contrib of this.contributions) {
try {
await contrib(ctx);
} catch (e) {
console.log(e);
}
}
return ctx.config;
}
protected async checkNode(ctx: Context): Promise<void> {
const pckjsonContent = await ctx.read('package.json');
if (!pckjsonContent) {
return;
}
let command: 'yarn' | 'npm' = 'npm';
if (await ctx.exists('yarn.lock')) {
command = 'yarn';
}
this.addCommand(ctx.config, command + ' install', 'init');
try {
const pckjson = JSON.parse(pckjsonContent);
if (pckjson.scripts) {
if (pckjson.scripts.build) {
this.addCommand(ctx.config, command + ' run build', 'init');
} else if (pckjson.scripts.compile) {
this.addCommand(ctx.config, command + ' run compile', 'init');
}
if (pckjson.scripts.start) {
this.addCommand(ctx.config, command + ' run start', 'command');
} else if (pckjson.scripts.dev) {
this.addCommand(ctx.config, command + ' run dev', 'command');
} else if (pckjson.scripts.watch) {
this.addCommand(ctx.config, command + ' run watch', 'command');
}
}
} catch (e) {
console.log(e, pckjsonContent);
}
}
protected async checkJava(ctx: Context): Promise<void> {
if (await ctx.exists('build.gradle')) {
let cmd = 'gradle';
if (await ctx.exists('gradlew')) {
cmd = './gradlew';
}
this.addCommand(ctx.config, cmd + ' build', 'init');
return;
}
if (await ctx.exists('pom.xml')) {
let cmd = 'mvn';
if (await ctx.exists('mvnw')) {
cmd = './mvnw';
}
this.addCommand(ctx.config, cmd + ' install -DskipTests=false', 'init');
return;
}
}
protected async isMake(ctx: Context) {
return await ctx.exists('Makefile') || await ctx.exists('makefile');
}
protected async checkMake(ctx: Context) {
if (await ctx.exists('CMakeLists.txt')) {
this.addCommand(ctx.config, 'cmake .', 'init');
} else if (await this.isMake(ctx)) {
this.addCommand(ctx.config, 'make', 'init');
}
}
protected async checkPython(ctx: Context) {
if (await this.isMake(ctx)) {
// https://docs.python-guide.org/writing/structure/#makefile
return;
}
if (await ctx.exists('requirements.txt')) {
this.addCommand(ctx.config, 'pip install -r requirements.txt', 'init');
} else if (await ctx.exists('setup.py')) {
this.addCommand(ctx.config, 'pip install .', 'init');
}
if (await ctx.exists('main.py')) {
this.addCommand(ctx.config, 'python main.py', 'command');
} else if (await ctx.exists('app.py')) {
this.addCommand(ctx.config, 'python app.py', 'command');
} else if (await ctx.exists('runserver.py')) {
this.addCommand(ctx.config, 'python runserver.py', 'command');
}
}
protected async checkGo(ctx: Context) {
if (await ctx.exists('go.mod')) {
this.addCommand(ctx.config, 'go get', 'init');
this.addCommand(ctx.config, 'go build ./...', 'init');
this.addCommand(ctx.config, 'go test ./...', 'init');
this.addCommand(ctx.config, 'go run', 'command');
}
}
protected async checkRust(ctx: Context) {
if (await ctx.exists('Cargo.toml')) {
this.addCommand(ctx.config, 'cargo build', 'init');
this.addCommand(ctx.config, 'cargo watch -x run', 'command');
}
}
protected async checkNuget(ctx: Context) {
if (await ctx.exists('packages.config')) {
this.addCommand(ctx.config, 'nuget install', 'init');
}
}
protected async checkRuby(ctx: Context) {
if (await ctx.exists('bin/setup')) {
this.addCommand(ctx.config, 'bin/setup', 'init');
} else if (await ctx.exists('Gemfile')) {
this.addCommand(ctx.config, 'bundle install', 'init');
}
if (await ctx.exists('bin/startup')) {
this.addCommand(ctx.config, 'bin/startup', 'command');
} else if (await ctx.exists('bin/rails')) {
this.addCommand(ctx.config, 'bin/rails server', 'command');
}
}
protected async checkDockerCompose(ctx: Context) {
if (await ctx.exists('docker-compose.yml')) {
this.addCommand(ctx.config, 'docker-compose up', 'command');
}
}
protected addCommand(config: WorkspaceConfig, command: string, phase: 'before' | 'init' | 'command', unless?: string): void {
if (!config.tasks) {
config.tasks = [];
}
if (!config.tasks[0]) {
config.tasks.push({});
}
const existing = config.tasks[0][phase];
if (unless && existing && existing.indexOf(unless) !== -1) {
// skip
return;
}
config.tasks[0][phase] = (existing ? existing + ' && ' : '') + command;
}
}