@cspell/cspell-tools 7.0.1-alpha.5 → 7.0.1-alpha.6

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/bin.js CHANGED
@@ -6,6 +6,8 @@ import { run } from './dist/app.js';
6
6
  run(program, process.argv).catch((e) => {
7
7
  if (!(e instanceof CommanderError)) {
8
8
  console.log(e);
9
+ } else {
10
+ console.log(e.message);
9
11
  }
10
12
  process.exitCode = 1;
11
13
  });
package/dist/app.js CHANGED
@@ -1,12 +1,12 @@
1
1
  // For large dictionaries, it is necessary to increase the memory limit.
2
- import { CommanderError } from 'commander';
2
+ import { CommanderError, Option } from 'commander';
3
3
  import { readFileSync } from 'fs';
4
4
  import { build } from './build.js';
5
5
  import { processCompileAction } from './compile.js';
6
6
  import * as compiler from './compiler/index.js';
7
7
  import { logWithTimestamp } from './compiler/logWithTimestamp.js';
8
8
  import { gzip } from './gzip/index.js';
9
- import { reportCheckChecksumFile, reportChecksumForFiles } from './shasum/shasum.js';
9
+ import { reportCheckChecksumFile, reportChecksumForFiles, updateChecksumForFiles } from './shasum/shasum.js';
10
10
  import { toError } from './util/errors.js';
11
11
  const npmPackageRaw = readFileSync(new URL('../package.json', import.meta.url), 'utf8');
12
12
  const npmPackage = JSON.parse(npmPackageRaw);
@@ -43,8 +43,10 @@ export async function run(program, argv, flags) {
43
43
  }
44
44
  async function shasum(files, options) {
45
45
  const report = options.check
46
- ? await reportCheckChecksumFile(options.check, files, options.root)
47
- : await reportChecksumForFiles(files, options.root);
46
+ ? await reportCheckChecksumFile(options.check, files, options)
47
+ : options.update
48
+ ? await updateChecksumForFiles(options.update, files, options)
49
+ : await reportChecksumForFiles(files, options);
48
50
  console.log('%s', report.report);
49
51
  if (!report.passed) {
50
52
  throw new CommanderError(1, 'Failed Checksum', 'One or more files had issues.');
@@ -73,7 +75,9 @@ export async function run(program, argv, flags) {
73
75
  program
74
76
  .command('shasum [files...]')
75
77
  .description('Calculate the checksum for files.')
78
+ .option('--list-file <list-file.txt...>', 'Specify one or more files that contain paths of files to check.')
76
79
  .option('-c, --check <checksum.txt>', 'Verify the checksum of files against those stored in the checksum.txt file.')
80
+ .addOption(new Option('-u, --update <checksum.txt>', 'Update checksums found in the file.').conflicts('--check'))
77
81
  .option('-r, --root <root>', 'Specify the root to use for relative paths. The current working directory is used by default.')
78
82
  .action(shasum);
79
83
  await program.parseAsync(argv);
@@ -22,7 +22,13 @@ interface ReportResult {
22
22
  report: string;
23
23
  passed: boolean;
24
24
  }
25
- export declare function reportChecksumForFiles(files: string[], root: string | undefined): Promise<ReportResult>;
26
- export declare function reportCheckChecksumFile(filename: string, files: string[] | undefined, root: string | undefined): Promise<ReportResult>;
25
+ interface ReportOptions {
26
+ root?: string | undefined;
27
+ listFile?: string[];
28
+ }
29
+ export declare function reportChecksumForFiles(files: string[], options: ReportOptions): Promise<ReportResult>;
30
+ export declare function reportCheckChecksumFile(filename: string, files: string[] | undefined, options: ReportOptions): Promise<ReportResult>;
31
+ export declare function calcUpdateChecksumForFiles(filename: string, files: string[], options: ReportOptions): Promise<string>;
32
+ export declare function updateChecksumForFiles(filename: string, files: string[], options: ReportOptions): Promise<ReportResult>;
27
33
  export {};
28
34
  //# sourceMappingURL=shasum.d.ts.map
@@ -1,5 +1,6 @@
1
- import { readFile } from 'node:fs/promises';
1
+ import { readFile, writeFile } from 'node:fs/promises';
2
2
  import { resolve } from 'node:path';
3
+ import { toError } from '../util/errors.js';
3
4
  import { isDefined } from '../util/index.js';
4
5
  import { calcFileChecksum, checkFile } from './checksum.js';
5
6
  export async function shasumFile(filename, root) {
@@ -66,9 +67,11 @@ export function parseShasumFile(content) {
66
67
  return { checksum, filename, lineNumber };
67
68
  }
68
69
  }
69
- export async function reportChecksumForFiles(files, root) {
70
+ export async function reportChecksumForFiles(files, options) {
71
+ const root = options.root;
72
+ const filesToCheck = await resolveFileList(files, options.listFile);
70
73
  let numFailed = 0;
71
- const result = await Promise.all(files.map((file) => shasumFile(file, root).catch((e) => {
74
+ const result = await Promise.all(filesToCheck.map((file) => shasumFile(file, root).catch((e) => {
72
75
  ++numFailed;
73
76
  if (typeof e !== 'string')
74
77
  throw e;
@@ -78,8 +81,10 @@ export async function reportChecksumForFiles(files, root) {
78
81
  const passed = !numFailed;
79
82
  return { report, passed };
80
83
  }
81
- export async function reportCheckChecksumFile(filename, files, root) {
82
- const result = await checkShasumFile(filename, files, root);
84
+ export async function reportCheckChecksumFile(filename, files, options) {
85
+ const root = options.root;
86
+ const filesToCheck = await resolveFileList(files, options.listFile);
87
+ const result = await checkShasumFile(filename, filesToCheck, root);
83
88
  const lines = result.map(({ filename, passed, error }) => `${filename}: ${passed ? 'OK' : 'FAILED'} ${error ? '- ' + error.message : ''}`.trim());
84
89
  const withErrors = result.filter((a) => !a.passed);
85
90
  const passed = !withErrors.length;
@@ -88,4 +93,51 @@ export async function reportCheckChecksumFile(filename, files, root) {
88
93
  }
89
94
  return { report: lines.join('\n'), passed };
90
95
  }
96
+ async function resolveFileList(files, listFile) {
97
+ files = files || [];
98
+ listFile = listFile || [];
99
+ const setOfFiles = new Set(files);
100
+ const pending = listFile.map((filename) => readFile(filename, 'utf8'));
101
+ for await (const content of pending) {
102
+ content
103
+ .split('\n')
104
+ .map((a) => a.trim())
105
+ .filter((a) => a)
106
+ .forEach((file) => setOfFiles.add(file));
107
+ }
108
+ return [...setOfFiles];
109
+ }
110
+ export async function calcUpdateChecksumForFiles(filename, files, options) {
111
+ const root = options.root || '.';
112
+ const filesToCheck = await resolveFileList(files, options.listFile);
113
+ const currentEntries = await readAndParseShasumFile(filename).catch((err) => {
114
+ const e = toError(err);
115
+ if (e.code !== 'ENOENT')
116
+ throw e;
117
+ return [];
118
+ });
119
+ const entriesToUpdate = new Set([...filesToCheck, ...currentEntries.map((e) => e.filename)]);
120
+ const mustExist = new Set(filesToCheck);
121
+ const checksumMap = new Map(currentEntries.map(({ filename, checksum }) => [filename, checksum]));
122
+ for (const file of entriesToUpdate) {
123
+ try {
124
+ const checksum = await calcFileChecksum(resolve(root, file));
125
+ checksumMap.set(file, checksum);
126
+ }
127
+ catch (e) {
128
+ if (mustExist.has(file) || toError(e).code !== 'ENOENT')
129
+ throw e;
130
+ checksumMap.delete(file);
131
+ }
132
+ }
133
+ const updatedEntries = [...checksumMap]
134
+ .map(([filename, checksum]) => ({ filename, checksum }))
135
+ .sort((a, b) => (a.filename < b.filename ? -1 : 1));
136
+ return updatedEntries.map((e) => `${e.checksum} ${e.filename}`).join('\n') + '\n';
137
+ }
138
+ export async function updateChecksumForFiles(filename, files, options) {
139
+ const content = await calcUpdateChecksumForFiles(filename, files, options);
140
+ await writeFile(filename, content);
141
+ return { passed: true, report: content };
142
+ }
91
143
  //# sourceMappingURL=shasum.js.map
@@ -1,3 +1,6 @@
1
- export declare function toError(err: unknown): Error;
1
+ export interface NodeError extends Error {
2
+ code?: string;
3
+ }
4
+ export declare function toError(err: unknown): NodeError;
2
5
  export declare function isError(err: unknown): err is Error;
3
6
  //# sourceMappingURL=errors.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cspell/cspell-tools",
3
- "version": "7.0.1-alpha.5",
3
+ "version": "7.0.1-alpha.6",
4
4
  "description": "Tools to assist with the development of cSpell",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -50,13 +50,13 @@
50
50
  },
51
51
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
52
52
  "dependencies": {
53
- "@cspell/cspell-pipe": "7.0.1-alpha.5",
53
+ "@cspell/cspell-pipe": "7.0.1-alpha.6",
54
54
  "commander": "^11.0.0",
55
55
  "cosmiconfig": "8.0.0",
56
- "cspell-trie-lib": "7.0.1-alpha.5",
56
+ "cspell-trie-lib": "7.0.1-alpha.6",
57
57
  "gensequence": "^5.0.2",
58
58
  "glob": "^10.3.3",
59
- "hunspell-reader": "7.0.1-alpha.5",
59
+ "hunspell-reader": "7.0.1-alpha.6",
60
60
  "yaml": "^2.3.1"
61
61
  },
62
62
  "engines": {
@@ -72,5 +72,5 @@
72
72
  "ts-json-schema-generator": "^1.2.0"
73
73
  },
74
74
  "main": "bin.js",
75
- "gitHead": "5de6ec67ac1f77c2c994cb956317598a9034e923"
75
+ "gitHead": "42a31c7216a9af068ae4b7c3921f8edd327f756f"
76
76
  }