feat: Add basic project creation
All checks were successful
Release / Release (push) Successful in 32s
All checks were successful
Release / Release (push) Successful in 32s
This commit is contained in:
parent
85f3baef47
commit
fb23f6fd8b
2 changed files with 90 additions and 1 deletions
|
@ -4,8 +4,36 @@ import * as toml from "@std/toml";
|
||||||
|
|
||||||
export const configPath = os.homedir() + "/.config/project-cli/";
|
export const configPath = os.homedir() + "/.config/project-cli/";
|
||||||
|
|
||||||
|
export interface Project {
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Configuration {
|
export interface Configuration {
|
||||||
projectsDirectory: string;
|
projectsDirectory: string;
|
||||||
|
cloningCommand: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getProjects(): Project[] {
|
||||||
|
const projectsString = fs
|
||||||
|
.readFileSync(configPath + "projects.json")
|
||||||
|
.toString();
|
||||||
|
return JSON.parse(projectsString) as Project[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getConfiguration(): Configuration {
|
||||||
|
return toml.parse(
|
||||||
|
fs.readFileSync(configPath + "config.toml").toString(),
|
||||||
|
) as unknown as Configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function addProject(project: Project) {
|
||||||
|
let projects = getProjects();
|
||||||
|
if (projects.length == undefined) {
|
||||||
|
saveProjects([project]);
|
||||||
|
} else {
|
||||||
|
projects.push(project);
|
||||||
|
saveProjects(projects);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateDefaultConfig() {
|
export function generateDefaultConfig() {
|
||||||
|
@ -14,10 +42,19 @@ export function generateDefaultConfig() {
|
||||||
|
|
||||||
const defaultConfig: Configuration = {
|
const defaultConfig: Configuration = {
|
||||||
projectsDirectory: os.homedir() + "/projects",
|
projectsDirectory: os.homedir() + "/projects",
|
||||||
|
cloningCommand: "git clone %s %n",
|
||||||
};
|
};
|
||||||
|
|
||||||
const configString = toml.stringify(
|
const configString = toml.stringify(
|
||||||
defaultConfig as unknown as Record<string, unknown>,
|
defaultConfig as unknown as Record<string, unknown>,
|
||||||
);
|
);
|
||||||
fs.writeFileSync(configPath + "config.toml", configString);
|
fs.writeFileSync(configPath + "config.toml", configString);
|
||||||
|
|
||||||
|
const projects: Project[] = [];
|
||||||
|
saveProjects(projects);
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveProjects(projects: Project[]) {
|
||||||
|
const projectsString = JSON.stringify(projects);
|
||||||
|
fs.writeFileSync(configPath + "projects.json", projectsString);
|
||||||
}
|
}
|
||||||
|
|
54
index.ts
54
index.ts
|
@ -1,10 +1,15 @@
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { confirm } from "@inquirer/prompts";
|
import { confirm, select, input } from "@inquirer/prompts";
|
||||||
import { exit } from "process";
|
import { exit } from "process";
|
||||||
import {
|
import {
|
||||||
|
addProject,
|
||||||
configPath,
|
configPath,
|
||||||
generateDefaultConfig,
|
generateDefaultConfig,
|
||||||
|
getConfiguration,
|
||||||
|
getProjects,
|
||||||
|
type Project,
|
||||||
} from "./configuration/configuration";
|
} from "./configuration/configuration";
|
||||||
|
import { execSync } from "child_process";
|
||||||
|
|
||||||
if (!fs.existsSync(configPath + "config.toml")) {
|
if (!fs.existsSync(configPath + "config.toml")) {
|
||||||
const createConfig = await confirm({
|
const createConfig = await confirm({
|
||||||
|
@ -21,3 +26,50 @@ if (!fs.existsSync(configPath + "config.toml")) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!getProjects() ||
|
||||||
|
getProjects().length == 0 ||
|
||||||
|
getProjects().length == undefined
|
||||||
|
) {
|
||||||
|
const anwser = await select({
|
||||||
|
message: "Create a new Project",
|
||||||
|
choices: ["Clone a project with git", "Create a new empty Project"],
|
||||||
|
});
|
||||||
|
if (anwser == "Clone a project with git") {
|
||||||
|
const repoUrl = await input({ message: "What is the url of the repo?" });
|
||||||
|
const wantsCustomName = await confirm({
|
||||||
|
message: "Would you like to give this project a custom name?",
|
||||||
|
default: false,
|
||||||
|
});
|
||||||
|
let customName = "";
|
||||||
|
if (wantsCustomName) {
|
||||||
|
customName = await input({
|
||||||
|
message: "What would you like the custom name to be?",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
execSync(
|
||||||
|
"cd " +
|
||||||
|
getConfiguration().projectsDirectory +
|
||||||
|
" && " +
|
||||||
|
getConfiguration()
|
||||||
|
.cloningCommand.replace("%s", repoUrl)
|
||||||
|
.replace("%n", customName),
|
||||||
|
);
|
||||||
|
|
||||||
|
const newProject: Project = {
|
||||||
|
name: customName,
|
||||||
|
};
|
||||||
|
addProject(newProject);
|
||||||
|
} else if (anwser == "Create a new empty Project") {
|
||||||
|
const name = await input({
|
||||||
|
message: "What would you like to call the project?",
|
||||||
|
});
|
||||||
|
fs.mkdirSync(getConfiguration().projectsDirectory + "/" + name);
|
||||||
|
const newProject: Project = {
|
||||||
|
name: name,
|
||||||
|
};
|
||||||
|
addProject(newProject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(getProjects());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue