Skip to content

Commit

Permalink
add lock system
Browse files Browse the repository at this point in the history
comply anca
update npm packages
  • Loading branch information
TimurRin committed Jul 22, 2024
1 parent 5921af0 commit fe957c7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ jobs:
name: "Install dependencies"
- run: npm test
name: "Run tests"
- run: npm run build
name: "Build distribution bundle"
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
name: "Publish to registry"
name: "Build and publish to registry"
build-executables:
permissions: write-all
runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pids
*.pid
*.seed
*.pid.lock
*.lock

# Temp folder
tmp
Expand Down
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
"fix": "prettier . --write && eslint --fix .",
"format": "prettier . --write",
"lint": "eslint --fix .",
"prepack": "npm run build",
"test": "prettier . -c && eslint --max-warnings 0 . && tsc && mocha './build/dev/test'"
},
"dependencies": {
"clivo": "0.4.0"
"clivo": "0.5.1"
},
"devDependencies": {
"@cinnabar-forge/eslint-plugin": "0.6.1",
Expand Down
8 changes: 8 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ export async function askCommitType(): Promise<string> {
])
).name;
}

/**
* Ask the user to enter yes or no
* @param text
*/
export async function askYesOrNo(text: string): Promise<string> {
return await promptText(`${text} (yes/no)`);
}
51 changes: 51 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { listenClivoEvent } from "clivo";
import fs from "fs";
import path from "path";

import { getTheLastCommitHash } from "./git.js";
import { CinnabarMeta, CinnabarMetaFile, CinnabarMetaRepo } from "./types.js";

const ANCA_JSON_PATH = "anca.json";
Expand Down Expand Up @@ -150,3 +152,52 @@ export function detectVersionsFromFiles(): string[] {

return versions;
}

/**
* Locks the file to prevent multiple instances of Cinnabar running or errors
*/
export function lockCinnabar() {
const lockFilePath = "cinnabar.lock";
if (fs.existsSync(lockFilePath)) {
return fs.readFileSync(lockFilePath, "utf8");
}
fs.writeFileSync(lockFilePath, getTheLastCommitHash() || "");
return true;
}

/**
* Unlocks the file
*/
export function unlockCinnabar() {
const lockFilePath = "cinnabar.lock";
if (fs.existsSync(lockFilePath)) {
fs.unlinkSync(lockFilePath);
}
}

/**
* Locks the PID file to prevent multiple instances of Cinnabar running or errors
*/
export function lockCinnabarPid() {
const pidLockFilePath = "cinnabar.pid.lock";
if (fs.existsSync(pidLockFilePath)) {
throw new Error("Cinnabar is already running");
}
fs.writeFileSync(pidLockFilePath, process.pid.toString());
return true;
}

/**
* Unlocks the PID file
*/
export function unlockCinnabarPid() {
const pidLockFilePath = "cinnabar.pid.lock";
if (fs.existsSync(pidLockFilePath)) {
fs.unlinkSync(pidLockFilePath);
}
}

listenClivoEvent("clivoCancel", () => {
unlockCinnabar();
unlockCinnabarPid();
});
31 changes: 28 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ export function getMostRecentGitTag(): string {
export function commitChanges(version: string, push: boolean): boolean {
try {
execSync("git add -A");
execSync(`git commit -m "release version ${version}" -n`);
execSync(`git commit -m "release version ${version}"`);
execSync(`git tag "v${version}"`);
if (push) {
execSync("git push");
execSync("git push --tags");
}
return true;
} catch (error) {
console.error("Error committing changes:", error);
return false;
throw new Error("Error committing changes to git repository: " + error);
}
}

Expand All @@ -91,3 +90,29 @@ export function checkGithubRepo(text?: string) {
text.split("/")[1].length > 0
: false;
}

/**
* Get the last commit hash
*/
export function getTheLastCommitHash() {
try {
return execSync("git rev-parse HEAD").toString().trim();
} catch (error) {
console.error("Error fetching the last commit hash:", error);
return false;
}
}

/**
* Reset to a specific commit
* @param commit
*/
export function resetToCommit(commit: string) {
try {
execSync(`git reset --hard ${commit}`);
return true;
} catch (error) {
console.error("Error resetting to commit:", error);
return false;
}
}
36 changes: 32 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import {
askGithubRepo,
askPrereleaseTag,
askUpdateType,
askYesOrNo,
setupCli,
} from "./cli.js";
import {
detectVersionsFromFiles,
getMetaDataFromFiles,
lockCinnabar,
lockCinnabarPid,
unlockCinnabar,
unlockCinnabarPid,
updateMetaDataFiles,
} from "./files.js";
import { checkGithubRepo, commitChanges } from "./git.js";
import { checkGithubRepo, commitChanges, resetToCommit } from "./git.js";
import {
checkVersion,
markBuild,
Expand All @@ -25,13 +30,33 @@ import {
* Main function
*/
async function main() {
lockCinnabarPid();
const printIntro = () => {
const design1 = "=".repeat(4);
const text = `${design1} Cinnabar Meta v${CINNABAR_PROJECT_VERSION} ${design1}`;
const design2 = "=".repeat(text.length);
console.log(`\n${design2}\n${text}\n${design2}\n`);
};
printIntro();
const bye = () => {
unlockCinnabar();
console.log("\nBye!\n");
unlockCinnabarPid();
};

const success = lockCinnabar();
if (success !== true) {
console.log("[WARNING] Reverting to the last stable commit", success);
const askToRevert = await askYesOrNo(
"Do you want to revert to the last stable commit?",
);
if (askToRevert === "yes") {
resetToCommit(success);
} else {
bye();
return;
}
}

const options = setupCli();

Expand Down Expand Up @@ -68,9 +93,10 @@ async function main() {
value: await askGithubRepo(),
};
} else {
throw new Error(
console.log(
"No GitHub repository found in metadata. Use --interactive option to set it.",
);
bye();
}
}

Expand Down Expand Up @@ -126,7 +152,7 @@ async function main() {
console.log(
"No update type specified. Pass --interactive option to choose one, or use specific update type with --update option.",
);
return;
bye();
}

// console.log(
Expand All @@ -146,6 +172,8 @@ async function main() {
} else if (build != null) {
newVersion = markBuild(parsedVersion);
} else {
console.log("No update.");
bye();
return;
}

Expand Down Expand Up @@ -180,7 +208,7 @@ async function main() {
}
}

console.log("\nBye!\n");
bye();
}

main();

0 comments on commit fe957c7

Please sign in to comment.