Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧Create update script #25

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@
],
"attachSimplePort": 9229,
"console": "integratedTerminal"
},
{ // UPDATE SCRIPT
"request": "launch",
"name": "Update Script",
"type": "node",
"program": "${workspaceFolder}/installation/update.ts",
"cwd": "${workspaceFolder}",
"windows": {
"runtimeExecutable": "${userHome}\\.deno\\bin\\deno.exe"
},
"linux": {
"runtimeExecutable": "${userHome}/.deno/bin/deno"
},
"runtimeArgs": [
"run",
"--allow-read",
"--allow-write",
"--allow-net",
"--inspect-wait",
],
"args": [
"latest",
],
"attachSimplePort": 9229,
"console": "integratedTerminal"
}
]
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@


kd-admin is a CLI application written in [deno](https://deno.com/) to simplify dev-related work in the KinsonDigital organization.

### Installing and updating

To install the tool locally into a repository, use the following install command.

```pwsh
deno run -Ar https://raw.githubusercontent.com/KinsonDigital/kd-admin/preview/installation/install.ts latest
```

To update the currently installed tool to the latest version, use the following update command.

```pwsh
deno run -Ar https://raw.githubusercontent.com/KinsonDigital/kd-admin/preview/installation/update.ts latest
```
9 changes: 8 additions & 1 deletion installation/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,14 @@ const args = [
];

console.log(" %c⏳ Installing kd-admin ...", "color: gray");
await runAsync("deno", args);

try {
await runAsync("deno", args);
} catch (error) {
console.log(`%c${error.message}`, "color: red");
Deno.exit(1);
}

console.log("%c\nInstallation complete!", "color: green");

console.log("%cMake sure to fill out all of the setting files!", "color: khaki");
87 changes: 87 additions & 0 deletions installation/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { existsSync } from "https://deno.land/[email protected]/fs/exists.ts";
import { walkSync } from "https://deno.land/[email protected]/fs/walk.ts";
import { TagClient } from "https://deno.land/x/[email protected]/GitHubClients/TagClient.ts";

const baseDirPath = "./dev-tools/bin";
const filesToUpdate = [...walkSync(baseDirPath, { includeFiles: true, match: [/kd-admin/]})].map((f) => f.path);

const tagClient = new TagClient("KinsonDigital", "kd-admin");
const tags: string[] = (await tagClient.getAllTags()).map((t) => t.name);
const latestVersion = tags[0];

filesToUpdate.forEach(async file => {
await updateFile(file);
});

console.log(`%ckd-admin has been updated to version '${latestVersion}'.`, "color: green");

async function updateFile (filePath: string) {
const isInstalled = existsSync(filePath, { isFile: true });

if (!isInstalled) {
console.log("%ckd-admin is not installed in this directory.", "color: red");
Deno.exit(0);
}

const fileContent = Deno.readTextFileSync(filePath);
const fileLines = fileContent.split("\n");

const urlLine = fileLines.find((line) => line.includes("deno") && line.includes("https://"));

const corruptErrorMsg = `%cThe file '${filePath}' is corrupted. Please reinstall the script.`;

if (urlLine === undefined) {
console.log(corruptErrorMsg, "color: red");
Deno.exit(1);
}

const sections = urlLine.split(" ");

const url = sections.find((section) => section.includes("https://")) ?? "";

if (url === "") {
console.log(corruptErrorMsg, "color: red");
Deno.exit(1);
}

const versionRegex = /v([1-9]\d*|0)\.([1-9]\d*|0)\.([1-9]\d*|0)(-preview\.([1-9]\d*))?/gm;

const regexResult = versionRegex.exec(url);
const currentVersion = regexResult?.[0] ?? "";

if (currentVersion === "") {
console.log(corruptErrorMsg, "color: red");
Deno.exit(1);
}

if (currentVersion === latestVersion) {
console.log("%ckd-admin is up to date.", "color: green");
Deno.exit(0);
}

const newUrl = url.replace(versionRegex, latestVersion);

// Find the url in the sections array and replace it with the new url
const newSections = sections.map((section) => {
if (section.includes("https://")) {
return newUrl;
}

return section;
});


const newUrlLine = newSections.join(" ");

const newFileLines = fileLines.map((line) => {
if (line.includes("deno") && line.includes("https://")) {
return newUrlLine;
}

return line;
});

const newFileContent = newFileLines.join("\n");

Deno.writeTextFileSync(filePath, newFileContent);
};
Loading