@kntic/kntic 0.3.0 → 0.3.1
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/package.json +1 -1
- package/src/commands/update.js +33 -0
- package/src/commands/update.test.js +58 -1
package/package.json
CHANGED
package/src/commands/update.js
CHANGED
|
@@ -109,6 +109,35 @@ function extractVersion(artifactFilename) {
|
|
|
109
109
|
return match[1];
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Update or create the KNTIC_VERSION variable in a .kntic.env file.
|
|
114
|
+
* If the file exists and contains a KNTIC_VERSION line, that line is replaced.
|
|
115
|
+
* If the file exists but has no KNTIC_VERSION, it is appended.
|
|
116
|
+
* If the file does not exist, it is created with the variable.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} version – semantic version string (e.g. "0.0.10")
|
|
119
|
+
* @param {string} envPath – path to .kntic.env (default: ".kntic.env")
|
|
120
|
+
*/
|
|
121
|
+
function updateEnvVersion(version, envPath = ".kntic.env") {
|
|
122
|
+
const line = `KNTIC_VERSION=${version}`;
|
|
123
|
+
|
|
124
|
+
if (!fs.existsSync(envPath)) {
|
|
125
|
+
fs.writeFileSync(envPath, line + "\n");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const content = fs.readFileSync(envPath, "utf8");
|
|
130
|
+
const regex = /^KNTIC_VERSION=.*$/m;
|
|
131
|
+
|
|
132
|
+
if (regex.test(content)) {
|
|
133
|
+
const updated = content.replace(regex, line);
|
|
134
|
+
fs.writeFileSync(envPath, updated);
|
|
135
|
+
} else {
|
|
136
|
+
const separator = content.endsWith("\n") ? "" : "\n";
|
|
137
|
+
fs.writeFileSync(envPath, content + separator + line + "\n");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
112
141
|
/**
|
|
113
142
|
* Recursively remove all contents of a directory (but not the directory itself).
|
|
114
143
|
*/
|
|
@@ -166,6 +195,9 @@ async function update() {
|
|
|
166
195
|
console.log("Updating .kntic/lib …");
|
|
167
196
|
extractLibOnly(tmpFile, ".");
|
|
168
197
|
|
|
198
|
+
// Update KNTIC_VERSION in .kntic.env
|
|
199
|
+
updateEnvVersion(version);
|
|
200
|
+
|
|
169
201
|
// Clean up
|
|
170
202
|
fs.unlinkSync(tmpFile);
|
|
171
203
|
|
|
@@ -177,3 +209,4 @@ module.exports = update;
|
|
|
177
209
|
module.exports.extractLibOnly = extractLibOnly;
|
|
178
210
|
module.exports.extractVersion = extractVersion;
|
|
179
211
|
module.exports.clearDirectory = clearDirectory;
|
|
212
|
+
module.exports.updateEnvVersion = updateEnvVersion;
|
|
@@ -7,7 +7,7 @@ const path = require("path");
|
|
|
7
7
|
const os = require("os");
|
|
8
8
|
const { execSync } = require("child_process");
|
|
9
9
|
|
|
10
|
-
const { extractLibOnly, extractVersion, clearDirectory } = require("./update");
|
|
10
|
+
const { extractLibOnly, extractVersion, clearDirectory, updateEnvVersion } = require("./update");
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Helper — create a tar.gz archive in `tmpDir` containing the given files.
|
|
@@ -202,6 +202,63 @@ describe("extractLibOnly", () => {
|
|
|
202
202
|
});
|
|
203
203
|
});
|
|
204
204
|
|
|
205
|
+
describe("updateEnvVersion", () => {
|
|
206
|
+
let tmpDir;
|
|
207
|
+
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "kntic-test-env-"));
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
afterEach(() => {
|
|
213
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("creates the file with KNTIC_VERSION if it does not exist", () => {
|
|
217
|
+
const envPath = path.join(tmpDir, ".kntic.env");
|
|
218
|
+
updateEnvVersion("1.2.3", envPath);
|
|
219
|
+
|
|
220
|
+
assert.equal(fs.readFileSync(envPath, "utf8"), "KNTIC_VERSION=1.2.3\n");
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("appends KNTIC_VERSION if file exists but variable is missing", () => {
|
|
224
|
+
const envPath = path.join(tmpDir, ".kntic.env");
|
|
225
|
+
fs.writeFileSync(envPath, "UID=1000\nGID=1000\n");
|
|
226
|
+
|
|
227
|
+
updateEnvVersion("0.0.10", envPath);
|
|
228
|
+
|
|
229
|
+
const content = fs.readFileSync(envPath, "utf8");
|
|
230
|
+
assert.ok(content.includes("UID=1000"), "existing content must be preserved");
|
|
231
|
+
assert.ok(content.includes("GID=1000"), "existing content must be preserved");
|
|
232
|
+
assert.ok(content.includes("KNTIC_VERSION=0.0.10"), "version must be appended");
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("replaces existing KNTIC_VERSION in-place", () => {
|
|
236
|
+
const envPath = path.join(tmpDir, ".kntic.env");
|
|
237
|
+
fs.writeFileSync(envPath, "UID=1000\nKNTIC_VERSION=0.0.1\nGID=1000\n");
|
|
238
|
+
|
|
239
|
+
updateEnvVersion("2.0.0", envPath);
|
|
240
|
+
|
|
241
|
+
const content = fs.readFileSync(envPath, "utf8");
|
|
242
|
+
assert.ok(content.includes("KNTIC_VERSION=2.0.0"), "version must be updated");
|
|
243
|
+
assert.ok(!content.includes("KNTIC_VERSION=0.0.1"), "old version must be gone");
|
|
244
|
+
assert.ok(content.includes("UID=1000"), "other vars must be preserved");
|
|
245
|
+
assert.ok(content.includes("GID=1000"), "other vars must be preserved");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("handles file without trailing newline when appending", () => {
|
|
249
|
+
const envPath = path.join(tmpDir, ".kntic.env");
|
|
250
|
+
fs.writeFileSync(envPath, "UID=1000");
|
|
251
|
+
|
|
252
|
+
updateEnvVersion("0.1.0", envPath);
|
|
253
|
+
|
|
254
|
+
const content = fs.readFileSync(envPath, "utf8");
|
|
255
|
+
assert.ok(content.includes("UID=1000"), "existing content must be preserved");
|
|
256
|
+
assert.ok(content.includes("KNTIC_VERSION=0.1.0"), "version must be appended");
|
|
257
|
+
// Ensure they're on separate lines
|
|
258
|
+
assert.ok(content.includes("UID=1000\nKNTIC_VERSION=0.1.0"), "must be on separate lines");
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
205
262
|
describe("extractVersion (update module)", () => {
|
|
206
263
|
it("parses version from artifact filename", () => {
|
|
207
264
|
assert.equal(extractVersion("kntic-bootstrap-v0.0.10.tar.gz"), "0.0.10");
|