ramm 0.0.15 → 0.0.18

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/dist/ramm.js CHANGED
@@ -239,7 +239,7 @@ EOF`);
239
239
  };
240
240
  // src/files.ts
241
241
  import { appendFile, exists } from "fs/promises";
242
- var {write } = globalThis.Bun;
242
+ var {file, write } = globalThis.Bun;
243
243
 
244
244
  // src/path.ts
245
245
  import { homedir } from "os";
@@ -253,7 +253,10 @@ function normalizePath(rawFilePath) {
253
253
  }
254
254
 
255
255
  // src/files.ts
256
- var finalizeWithNewline = (str) => {
256
+ var normalizeFileContent = (str) => {
257
+ if (str === "") {
258
+ return str;
259
+ }
257
260
  if (str.at(-1) !== `
258
261
  `) {
259
262
  return str + `
@@ -270,11 +273,11 @@ var createDir = async (str) => {
270
273
  await execCommand(`mkdir -p ${dirname}`);
271
274
  };
272
275
  var checkStrInFile = async (filePath, str) => {
273
- const file = Bun.file(filePath);
274
- if (!await file.exists()) {
276
+ const file2 = Bun.file(filePath);
277
+ if (!await file2.exists()) {
275
278
  return false;
276
279
  }
277
- const fileText = await file.text();
280
+ const fileText = await file2.text();
278
281
  if (fileText.includes(str)) {
279
282
  return true;
280
283
  }
@@ -293,13 +296,22 @@ var writeIfNew = async (rawFilePath, str) => {
293
296
  if (await checkStrInFile(filePath, str)) {
294
297
  return;
295
298
  }
296
- await appendFile(filePath, finalizeWithNewline(str));
299
+ await appendFile(filePath, normalizeFileContent(str));
297
300
  };
298
301
  var writeFile = async (pathToFile, str) => {
299
302
  const normalizedPath = normalizePath(pathToFile);
300
303
  await createFileIfNeed(normalizedPath);
301
304
  await write(normalizedPath, str);
302
305
  };
306
+ var writeFileIfNotMatch = async (pathToFile, str) => {
307
+ const normalizedPath = normalizePath(pathToFile);
308
+ await createFileIfNeed(normalizedPath);
309
+ const fileText = await file(normalizedPath).text();
310
+ if (fileText === str) {
311
+ return;
312
+ }
313
+ await write(normalizedPath, str);
314
+ };
303
315
  // src/ssh.ts
304
316
  import { debug } from "console";
305
317
  var addKeyToHostConfig = async (pathToHost, address, pathToKey) => {
@@ -344,30 +356,35 @@ var createCron = async ({
344
356
  time,
345
357
  pathToFile
346
358
  }) => {
347
- const constructedLine = `${time} ${pathToFile}`;
359
+ const pathToFileNorm = normalizePath(pathToFile);
360
+ const constructedLine = `${time} ${pathToFileNorm}`;
348
361
  const tempFile = `/tmp/cron_${new Date().getTime().toString()}`;
349
362
  const { output: cronConfig } = await execCommand("crontab -l");
350
363
  let newCronConfig = cronConfig;
351
364
  if (cronConfig.includes(constructedLine)) {
352
365
  return;
353
366
  }
354
- if (cronConfig.includes(pathToFile)) {
367
+ if (cronConfig.includes(pathToFileNorm)) {
355
368
  newCronConfig = cronConfig.split(`
356
- `).filter((str) => !str.includes(pathToFile)).join(`
369
+ `).filter((str) => !str.includes(pathToFileNorm)).join(`
357
370
  `);
358
371
  }
372
+ newCronConfig = normalizeFileContent(normalizeFileContent(cronConfig) + constructedLine);
359
373
  await writeFile(tempFile, newCronConfig);
374
+ await execCommand(`cat ${tempFile}`);
360
375
  await execCommand(`crontab ${tempFile}`);
361
376
  await execCommand(`rm ${tempFile}`);
362
377
  };
363
378
  export {
364
379
  writeIfNew,
380
+ writeFileIfNotMatch,
365
381
  writeFile,
366
382
  setupNftable,
367
383
  runPodmanContainerService,
368
384
  runPodmanContainer,
369
385
  restartSystemdService,
370
386
  normalizePath,
387
+ normalizeFileContent,
371
388
  installSystemPackage,
372
389
  installPodman,
373
390
  installBun,
@@ -1,2 +1,4 @@
1
+ export declare const normalizeFileContent: (str: string) => string;
1
2
  export declare const writeIfNew: (rawFilePath: string, str: string) => Promise<void>;
2
3
  export declare const writeFile: (pathToFile: string, str: string) => Promise<void>;
4
+ export declare const writeFileIfNotMatch: (pathToFile: string, str: string) => Promise<void>;
@@ -7,7 +7,7 @@ export { restartSystemdService } from "./systemd.ts";
7
7
  export { installSystemPackage } from "./packages.ts";
8
8
  export { debugBlock } from "./debug.ts";
9
9
  export { setupNftable } from "./nft.ts";
10
- export { writeIfNew, writeFile } from "./files.ts";
10
+ export { writeIfNew, writeFile, writeFileIfNotMatch, normalizeFileContent, } from "./files.ts";
11
11
  export { createAndAddSshKey, getServerFingerprintBySsh, addKeyToHostConfig, } from "./ssh.ts";
12
12
  export { normalizePath } from "./path.ts";
13
13
  export { createCron } from "./cron.ts";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ramm",
3
3
  "type": "module",
4
- "version": "0.0.15",
4
+ "version": "0.0.18",
5
5
  "scripts": {
6
6
  "build": "bun build ./src/ramm.ts --target bun --outdir ./dist && cp ./src/bun.sh ./dist/bun.sh && bun run types",
7
7
  "types": "tsc --project tsconfig.types.json"