Skip to content

Commit 568d272

Browse files
add modules
1 parent da6b05e commit 568d272

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

src/modules/directory.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { existsSync, mkdirSync, opendirSync } from "fs";
2+
import path from "path"
3+
4+
export const createDirectory = async (directoryName: string) => {
5+
if (existsSync(directoryName)) {
6+
const dir = opendirSync(directoryName);
7+
const entry = await dir.read();
8+
dir.closeSync();
9+
if (!!entry) throw `Error: Directory ${directoryName} is not empty`;
10+
} else {
11+
mkdirSync(directoryName);
12+
}
13+
14+
return path.resolve(directoryName);
15+
}

src/modules/git.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { simpleGit } from 'simple-git';
2+
3+
export const createRepo = async (path: string) => {
4+
simpleGit(path).init();
5+
}

src/modules/input.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#! /usr/bin/env node
2+
3+
import inquirer from "inquirer"
4+
5+
export interface Input {
6+
unityVersion: string,
7+
packageName: string,
8+
description: string,
9+
displayName: string,
10+
author: {
11+
name: string,
12+
email: string,
13+
},
14+
namespace: string,
15+
}
16+
17+
export default async (availableVersions: string[]) => inquirer
18+
.prompt<Input>([
19+
{
20+
type: "list",
21+
message: "Unity Version",
22+
name: "unityVersion",
23+
choices: availableVersions
24+
},
25+
{
26+
type: "input",
27+
message: "Package Name (com.company.my-system)",
28+
name: "packageName",
29+
},
30+
{
31+
type: "input",
32+
message: "Display Name (My System)",
33+
name: "displayName",
34+
},
35+
{
36+
type: "input",
37+
message: "Description",
38+
name: "description",
39+
},
40+
{
41+
type: "input",
42+
message: "Author (Rick Morty)",
43+
name: "author.name",
44+
},
45+
{
46+
type: "input",
47+
message: "Email (mail@example.com)",
48+
name: "author.email",
49+
},
50+
{
51+
type: "input",
52+
message: "Scripts Namespace (Company.MySystem)",
53+
name: "namespace",
54+
},
55+
{
56+
type: "confirm",
57+
message: "Enable Github Actions + Semantic Release",
58+
name: "enableCi",
59+
}
60+
]);

src/modules/render.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ts-ignore
2+
import template from "template-directory";
3+
import { dirname, join, resolve } from 'path';
4+
import { fileURLToPath } from 'url';
5+
import { renameSync } from "fs";
6+
import fsExtra from "fs-extra"
7+
8+
// @ts-ignore
9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
const base = resolve(__dirname, "../templates/base")
11+
const ci = resolve(__dirname, "../templates/ci")
12+
13+
export const render = async (data: Record<string, any>, projectPath: string) => {
14+
await template(base, projectPath, data);
15+
16+
if (data.enableCi) fsExtra.copySync(ci, projectPath, { overwrite: true });
17+
18+
const oldRuntime = join(projectPath, "Assets/Package/Runtime", "HGS.Template.Runtime.asmdef");
19+
const newRuntime = join(projectPath, "Assets/Package/Runtime", `${data.namespace}.Runtime.asmdef`);
20+
21+
renameSync(oldRuntime, newRuntime);
22+
}

src/modules/unity.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { execa } from 'execa';
2+
import path from 'path';
3+
import process from 'process';
4+
5+
export type UnityInstallations = Record<string, string>;
6+
7+
const programFiles = process.env.ProgramFiles || path.resolve("C:/Program Files/");
8+
const unityHubPath = process.env.UnityHub || path.join(programFiles, "Unity Hub", "Unity Hub.exe")
9+
10+
export const getUnityInstallations = async (): Promise<UnityInstallations> => {
11+
const { stdout } = await execa(unityHubPath, "-- --headless editors -i".split(" "), { reject: false });
12+
13+
const isSuccess = stdout.includes(", installed at");
14+
if (!isSuccess) throw `Failed to execute command ${unityHubPath}. Consider create UnityHub env var`;
15+
16+
const lines = stdout.split(/\r\n|\n/);
17+
const installations: UnityInstallations = {};
18+
19+
lines.forEach(line => {
20+
const [version, path] = line
21+
.split(", installed at")
22+
.map(entry => entry.trim());
23+
24+
if (!!version || !!path) installations[version] = path;
25+
});
26+
27+
if (Object.keys(installations).length == 0) throw `No unity installations found at ${unityHubPath}.`;
28+
29+
return installations;
30+
}
31+
32+
export const createUnityProject = async (unityPath: string, projectPath: string) => {
33+
await execa(unityPath, ["-quit", "-batchmode", "-createProject", projectPath]);
34+
}
35+
36+
export const openUnityProject = async (unityPath: string, projectPath: string) => {
37+
await execa(unityPath, ["-projectPath", projectPath], { stdio: "ignore" });
38+
}

0 commit comments

Comments
 (0)