-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
72 lines (68 loc) · 2.44 KB
/
index.ts
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
import { existsSync } from "fs";
import { readdir, writeFile } from "fs/promises";
import { CSharp } from "./ProjectTypes/CSharp.js";
import { Go } from "./ProjectTypes/Go.js";
import { Gradle } from "./ProjectTypes/Gradle.js";
import { NodeJS } from "./ProjectTypes/NodeJS.js";
import { ProjectType } from "./ProjectType.js";
import { Python } from "./ProjectTypes/Python.js";
import { Rust } from "./ProjectTypes/Rust.js";
import { valueType } from "@merrymake/utils";
export const ProjectTypes = valueType<ProjectType>()({
nodejs: new NodeJS(false),
typescript: new NodeJS(true),
gradle: new Gradle(),
go: new Go(),
rust: new Rust(),
csharp: new CSharp(),
python: new Python(),
});
export async function detectProjectType(
folder: string
): Promise<keyof typeof ProjectTypes> {
let files = await readdir(folder);
if (files.includes(`dockerfile`))
throw "Custom dockerfiles are not supported";
if (files.includes(`tsconfig.json`)) return "typescript";
if (files.includes(`package.json`)) return "nodejs";
if (
files.includes(`gradlew`) ||
files.includes(`build.gradle`) ||
files.includes(`settings.gradle`)
)
return "gradle";
if (files.includes(`pom.xml`)) throw "Maven support is coming soon";
if (
files.includes(`requirements.txt`) ||
files.includes(`setup.py`) ||
files.includes(`Pipfile`) ||
files.includes(`pyproject.toml`) ||
files.includes(`app.py`) ||
files.includes(`main.py`)
)
return "python";
if (files.includes(`composer.json`) || files.includes(`index.php`))
throw "Php support is coming soon";
if (files.includes(`Gemfile`)) throw "Ruby support is coming soon";
if (files.includes(`go.mod`)) return "go";
if (files.includes(`project.clj`)) throw "Clojure support is coming soon";
if (files.includes(`Cargo.toml`)) return "rust";
if (files.some((x) => x.endsWith(`.csproj`))) return "csharp";
if (files.includes(`*.stb`)) throw "Scala support is coming soon";
throw `Unknown project type in ${folder}`;
}
function generateNewFileName(folder: string) {
let result: string;
do {
result = "f" + Math.random();
} while (existsSync(`${folder}/${result}`));
return result;
}
export function writeBuildScript(build: (folder: string) => Promise<string[]>) {
return async (folder: string) => {
const cmds = await build(folder);
const fileName = generateNewFileName(folder);
await writeFile(`${folder}/${fileName}`, cmds.join("\n"));
return fileName;
};
}