@digicroz/node-server-kit 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -162,7 +162,7 @@ npx @digicroz/node-server-kit git-acp-all-repos
162
162
  npx @digicroz/node-server-kit git-push-all-repos
163
163
 
164
164
  # Transfer project files to shared backend repositories
165
- npx @digicroz/node-server-kit transfer-2-shared
165
+ npx @digicroz/node-server-kit transfer-2-shared [branch]
166
166
 
167
167
  # Create a shell script to initialize workspaces
168
168
  npx @digicroz/node-server-kit create-init-workspace-shell-file
package/dist/bin/nsk.js CHANGED
@@ -56,8 +56,9 @@ program
56
56
  program
57
57
  .command("transfer-2-shared")
58
58
  .description("Transfer project files to shared backend repositories")
59
- .action(async () => {
60
- await transfer2Shared();
59
+ .argument("[branch]", "Shared backend branch name", "main")
60
+ .action(async (branch) => {
61
+ await transfer2Shared(branch);
61
62
  });
62
63
  program
63
64
  .command("create-init-workspace-shell-file")
@@ -1 +1 @@
1
- export declare const transfer2Shared: () => Promise<void>;
1
+ export declare const transfer2Shared: (branchName?: string) => Promise<void>;
@@ -6,7 +6,7 @@ import { simpleGit } from "simple-git";
6
6
  import { deleteAndCopy } from "./deleteAndCopy.js";
7
7
  import { loadConfig } from "../utils/loadConfig.js";
8
8
  import { createMultiProgress, failProgress, startProgress, successProgress, } from "../utils/progress.js";
9
- export const transfer2Shared = async () => {
9
+ export const transfer2Shared = async (branchName = "main") => {
10
10
  // Load projects from the configuration file
11
11
  const config = loadConfig();
12
12
  // Export projects for use in CLI tools
@@ -32,6 +32,7 @@ export const transfer2Shared = async () => {
32
32
  mainSpinner.text = "Pulling latest changes from main repository";
33
33
  await myGit.pull();
34
34
  successProgress(mainSpinner, "Main repository checked successfully");
35
+ console.log(`Using shared backend branch: ${branchName}`);
35
36
  const repositoryPackageJsonPath = join(clientRootDirPath, "package.json");
36
37
  if (!fs.existsSync(repositoryPackageJsonPath)) {
37
38
  console.log("repositoryPackageJsonPath does not exist");
@@ -88,63 +89,91 @@ export const transfer2Shared = async () => {
88
89
  projectSpinner.text = `Checking shared backend repository status for project: ${project.projectName}`;
89
90
  const sharedBackendGit = simpleGit(options);
90
91
  let sharedBackendGitStatus = await sharedBackendGit.status();
91
- // Save the current branch name
92
- const currentBranch = sharedBackendGitStatus.current;
93
- // console.log(currentBranch);
94
- if (currentBranch !== "main") {
95
- failProgress(projectSpinner, `Shared backend branch is ${currentBranch}, but it should be main.`);
96
- projectProgress.incrementFailed(`Failed: ${project.projectName}`);
97
- throw new Error(` Shared backend branch is ${currentBranch}, but it should be main.`);
98
- }
99
- projectSpinner.text = `Pulling latest changes from shared backend repository for project: ${project.projectName}`;
100
- await sharedBackendGit.pull();
101
- const sharedProjectSrcPath = join(sharedBackendPath, "src", project.projectBaseDirPath);
102
- // console.log("sharedProjectSrcPath", sharedProjectSrcPath);
103
- if (!fs.existsSync(sharedProjectSrcPath)) {
104
- failProgress(projectSpinner, "sharedProjectSrcPath does not exist");
105
- console.log({ sharedProjectSrcPath });
106
- projectProgress.incrementFailed(`Failed: ${project.projectName}`);
107
- continue;
108
- }
109
- projectSpinner.text = `Copying files for project: ${project.projectName}`;
110
- await deleteAndCopy(projectSrcPath, sharedProjectSrcPath);
111
- projectSpinner.text = `Checking for changes in shared backend repository for project: ${project.projectName}`;
112
- sharedBackendGitStatus = await sharedBackendGit.status();
113
- const sharedBackendChanges = sharedBackendGitStatus.files.some((file) => file.path.startsWith(`src/${project.projectBaseDirPath}/`));
114
- if (!sharedBackendChanges) {
115
- successProgress(projectSpinner, `No changes detected in the sharedBackend directory for project: ${project.projectName}`);
116
- projectProgress.incrementCompleted(`Completed: ${project.projectName} - No changes`);
117
- continue;
118
- }
119
- const repositoryCommitJsonPath = join(sharedBackendPath, "commit.json");
120
- if (!fs.existsSync(repositoryCommitJsonPath)) {
121
- failProgress(projectSpinner, "repositoryCommitJsonPath does not exist");
122
- console.log({ sharedBackendPath });
123
- projectProgress.incrementFailed(`Failed: ${project.projectName}`);
124
- return;
92
+ const originalSharedBranch = sharedBackendGitStatus.current;
93
+ let branchSwitched = false;
94
+ try {
95
+ if (branchName !== originalSharedBranch) {
96
+ projectSpinner.text = `Checking out branch ${branchName} in shared backend repository for project: ${project.projectName}`;
97
+ await sharedBackendGit.fetch();
98
+ const branchSummary = await sharedBackendGit.branch(["-a"]);
99
+ const branchExistsLocally = Boolean(branchSummary.branches[branchName]);
100
+ const branchExistsRemotely = branchSummary.all.includes(`remotes/origin/${branchName}`);
101
+ if (branchExistsLocally) {
102
+ await sharedBackendGit.checkout(branchName);
103
+ branchSwitched = true;
104
+ }
105
+ else if (branchExistsRemotely) {
106
+ await sharedBackendGit.checkoutBranch(branchName, `origin/${branchName}`);
107
+ branchSwitched = true;
108
+ }
109
+ else if (branchName === "main") {
110
+ await sharedBackendGit.checkout("main");
111
+ branchSwitched = originalSharedBranch !== "main";
112
+ }
113
+ else {
114
+ failProgress(projectSpinner, `Shared backend branch ${branchName} does not exist locally or on origin.`);
115
+ projectProgress.incrementFailed(`Failed: ${project.projectName}`);
116
+ continue;
117
+ }
118
+ }
119
+ projectSpinner.text = `Pulling latest changes from shared backend repository for project: ${project.projectName}`;
120
+ await sharedBackendGit.pull("origin", branchName);
121
+ const sharedProjectSrcPath = join(sharedBackendPath, "src", project.projectBaseDirPath);
122
+ // console.log("sharedProjectSrcPath", sharedProjectSrcPath);
123
+ if (!fs.existsSync(sharedProjectSrcPath)) {
124
+ failProgress(projectSpinner, "sharedProjectSrcPath does not exist");
125
+ console.log({ sharedProjectSrcPath });
126
+ projectProgress.incrementFailed(`Failed: ${project.projectName}`);
127
+ continue;
128
+ }
129
+ projectSpinner.text = `Copying files for project: ${project.projectName}`;
130
+ await deleteAndCopy(projectSrcPath, sharedProjectSrcPath);
131
+ projectSpinner.text = `Checking for changes in shared backend repository for project: ${project.projectName}`;
132
+ sharedBackendGitStatus = await sharedBackendGit.status();
133
+ const sharedBackendChanges = sharedBackendGitStatus.files.some((file) => file.path.startsWith(`src/${project.projectBaseDirPath}/`));
134
+ if (!sharedBackendChanges) {
135
+ successProgress(projectSpinner, `No changes detected in the sharedBackend directory for project: ${project.projectName}`);
136
+ projectProgress.incrementCompleted(`Completed: ${project.projectName} - No changes`);
137
+ continue;
138
+ }
139
+ const repositoryCommitJsonPath = join(sharedBackendPath, "commit.json");
140
+ if (!fs.existsSync(repositoryCommitJsonPath)) {
141
+ failProgress(projectSpinner, "repositoryCommitJsonPath does not exist");
142
+ console.log({ sharedBackendPath });
143
+ projectProgress.incrementFailed(`Failed: ${project.projectName}`);
144
+ return;
145
+ }
146
+ const repositoryCommitJson = JSON.parse(fs.readFileSync(repositoryCommitJsonPath, "utf8"));
147
+ if (!repositoryCommitJson.last_commit) {
148
+ console.log("repositoryCommitJson.last_commit does not exist");
149
+ console.log({ clientRootDirPath });
150
+ }
151
+ const currentDate = new Date().toLocaleString();
152
+ // Update last_commit
153
+ repositoryCommitJson.last_commit = currentDate;
154
+ // Write the updated JSON back to the file
155
+ fs.writeFileSync(repositoryCommitJsonPath, JSON.stringify(repositoryCommitJson, null, 4), "utf8");
156
+ projectSpinner.text = `Committing changes to shared backend repository for project: ${project.projectName}`;
157
+ await sharedBackendGit.add(`commit.json`);
158
+ // Add only the b2fPortal folder
159
+ await sharedBackendGit.add(`src/${project.projectBaseDirPath}/*`);
160
+ // Commit with the current date and time
161
+ const commitMessage = `${currentDate} transfer2Shared from ${repositoryPackageJson.name}`;
162
+ await sharedBackendGit.commit(commitMessage);
163
+ // Push the commit to the branch
164
+ projectSpinner.text = `Pushing changes to shared backend repository for project: ${project.projectName}`;
165
+ await sharedBackendGit.push("origin", branchName);
166
+ successProgress(projectSpinner, `Changes committed and pushed successfully for project: ${project.projectName}`);
167
+ projectProgress.incrementCompleted(`Completed: ${project.projectName}`);
125
168
  }
126
- const repositoryCommitJson = JSON.parse(fs.readFileSync(repositoryCommitJsonPath, "utf8"));
127
- if (!repositoryCommitJson.last_commit) {
128
- console.log("repositoryCommitJson.last_commit does not exist");
129
- console.log({ clientRootDirPath });
169
+ finally {
170
+ if (branchSwitched &&
171
+ originalSharedBranch &&
172
+ originalSharedBranch !== branchName) {
173
+ projectSpinner.text = `Restoring shared backend repository branch to ${originalSharedBranch}`;
174
+ await sharedBackendGit.checkout(originalSharedBranch);
175
+ }
130
176
  }
131
- const currentDate = new Date().toLocaleString();
132
- // Update last_commit
133
- repositoryCommitJson.last_commit = currentDate;
134
- // Write the updated JSON back to the file
135
- fs.writeFileSync(repositoryCommitJsonPath, JSON.stringify(repositoryCommitJson, null, 4), "utf8");
136
- projectSpinner.text = `Committing changes to shared backend repository for project: ${project.projectName}`;
137
- await sharedBackendGit.add(`commit.json`);
138
- // Add only the b2fPortal folder
139
- await sharedBackendGit.add(`src/${project.projectBaseDirPath}/*`);
140
- // Commit with the current date and time
141
- const commitMessage = `${currentDate} transfer2Shared from ${repositoryPackageJson.name}`;
142
- await sharedBackendGit.commit(commitMessage);
143
- // Push the commit to the main branch
144
- projectSpinner.text = `Pushing changes to shared backend repository for project: ${project.projectName}`;
145
- await sharedBackendGit.push("origin", "main");
146
- successProgress(projectSpinner, `Changes committed and pushed successfully for project: ${project.projectName}`);
147
- projectProgress.incrementCompleted(`Completed: ${project.projectName}`);
148
177
  }
149
178
  else {
150
179
  failProgress(projectSpinner, "sharedBackendPath is not defined");
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@digicroz/node-server-kit",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "NodeJS Backend Kit - A powerful TypeScript utility for managing backend projects with features like B2F Portal integration, cross-project validation, and Next.js support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "scripts": {
9
- "prepare": "husky install",
10
9
  "dev": "npm run clean && tsc -w",
11
10
  "clean": "rimraf dist && tsc",
12
11
  "release:patch": "npm version patch",
@@ -15,7 +14,8 @@
15
14
  "test": "vitest run",
16
15
  "test:watch": "vitest",
17
16
  "lint": "tsc --noEmit",
18
- "prepublishOnly": "npm run build"
17
+ "prepublishOnly": "npm run build",
18
+ "prepare": "husky"
19
19
  },
20
20
  "files": [
21
21
  "dist",