-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from unyt-org/fix-git-install-check
Fix #146
- Loading branch information
Showing
7 changed files
with
105 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,80 @@ | ||
import { path, updateRootPath } from "../app/args.ts"; | ||
import { ESCAPE_SEQUENCES } from "datex-core-legacy/datex_all.ts"; | ||
import { path, updatePath, updateRootPath } from "../app/args.ts"; | ||
import { isGitInstalled } from "./git.ts"; | ||
import { logger } from "./global-values.ts"; | ||
import { Path } from "datex-core-legacy/utils/path.ts"; | ||
import process from "node:process"; | ||
|
||
export async function initBaseProject() { | ||
export async function initBaseProject(name?: string) { | ||
|
||
if (!isGitInstalled()) { | ||
logger.error(`Unable to find git executable in PATH. Please install Git before retrying (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)`); | ||
Deno.exit(1); | ||
} | ||
|
||
const gitRepo = "https://github.com/unyt-org/uix-base-project.git" | ||
const rootPath = new Path(path??'./', 'file://' + Deno.cwd() + '/'); | ||
const cwd = new Path(path??'./', 'file://' + Deno.cwd() + '/'); | ||
let projectName: string|undefined = undefined; | ||
let projectPath: Path; | ||
|
||
if (rootPath.getChildPath(".git").fs_exists) { | ||
logger.error(`Git repository already exist in this location`); | ||
Deno.exit(1); | ||
} | ||
|
||
console.log("Initializing new UIX project"); | ||
|
||
const move = rootPath.getChildPath(".datex-cache").fs_exists; | ||
const tempDir = new Path('file://' + await Deno.makeTempDir() + '/'); | ||
if (move) { | ||
try { | ||
await Deno.rename(rootPath.getChildPath(".datex-cache"), tempDir.normal_pathname); | ||
if (!name) { | ||
while (!projectName) { | ||
projectName = prompt(ESCAPE_SEQUENCES.BOLD+"Enter the name of the new project:"+ESCAPE_SEQUENCES.RESET)!; | ||
} | ||
catch { | ||
await Deno.remove(rootPath.getChildPath(".datex-cache"), {recursive: true}); | ||
do { | ||
name = prompt(ESCAPE_SEQUENCES.BOLD+"Enter the name of the project directory:"+ESCAPE_SEQUENCES.RESET, projectName?.toLowerCase().replace(/[^\w]/g, "-") ?? "new-project")!; | ||
projectPath = cwd.getChildPath(name).asDir(); | ||
} | ||
while (projectPath.fs_exists && | ||
(logger.error(`The directory ${projectPath.normal_pathname} already exists. Please choose a different directory.`), true) | ||
) | ||
} | ||
else { | ||
projectName = name; | ||
projectPath = cwd.getChildPath(name).asDir(); | ||
if (projectPath.fs_exists) { | ||
logger.error(`The directory ${projectPath.normal_pathname} already exists. Please choose a different name.`); | ||
Deno.exit(1); | ||
} | ||
} | ||
|
||
logger.success(`Initializing new UIX project "${projectName}"`); | ||
|
||
const dxCacheDirExists = cwd.getChildPath(".datex-cache").fs_exists; | ||
if (dxCacheDirExists) { | ||
await Deno.remove(cwd.getChildPath(".datex-cache"), {recursive: true}); | ||
} | ||
|
||
const clone = Deno.run({ | ||
cmd: ["git", "clone", gitRepo, rootPath.normal_pathname], | ||
stdout: "null" | ||
}); | ||
const cloneResult = await clone.status(); | ||
|
||
if (!cloneResult.success) { | ||
throw new Error("Failed to clone."); | ||
try { | ||
const clone = Deno.run({ | ||
cmd: ["git", "clone", gitRepo, projectPath.normal_pathname], | ||
stdout: "null" | ||
}); | ||
if (!(await clone.status()).success) | ||
throw new Error("Git clone failed"); | ||
} catch (error) { | ||
logger.error(`Unable to clone repository. Please make sure that Git is correctly installed.`, error); | ||
Deno.exit(1); | ||
} | ||
|
||
if (move) { | ||
try { | ||
await Deno.rename(tempDir.normal_pathname, rootPath.getChildPath(".datex-cache")); | ||
} | ||
catch {} | ||
await Deno.remove(projectPath.getChildPath(".git"), {recursive: true}); | ||
|
||
// update app name in app.dx | ||
const appDxContent = await Deno.readTextFile(projectPath.getChildPath("app.dx")); | ||
const newAppDxContent = appDxContent.replace(/name:.*/, `name: "${projectName}",`); | ||
await Deno.writeTextFile(projectPath.getChildPath("app.dx"), newAppDxContent); | ||
|
||
try { | ||
await Deno.run({ | ||
cwd: projectPath.normal_pathname, | ||
cmd: ["git", "init"], | ||
stdout: "null" | ||
}).status(); | ||
} | ||
catch (error) { | ||
logger.error(`Unable to initialize git repository.`, error); | ||
} | ||
await Deno.remove(rootPath.getChildPath(".git"), {recursive: true}); | ||
|
||
updateRootPath(); | ||
updatePath(projectPath.normal_pathname); | ||
await updateRootPath(); | ||
} |