Skip to content

Commit 4c4c353

Browse files
Verify that all explicitly-specified files and/or globs exist. Don't silently ignore missing files
1 parent 1019c9f commit 4c4c353

4 files changed

Lines changed: 45 additions & 17 deletions

File tree

src/get-old-version.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ async function readVersion(file: string, cwd: string): Promise<string | undefine
5353
}
5454
}
5555
catch (error) {
56+
// Ignore errors (no such file, not valid JSON, etc.)
57+
// Just try the next file instead.
5658
return undefined;
5759
}
5860
}

src/normalize-options.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
9797

9898
let files;
9999
if (Array.isArray(raw.files) && raw.files.length > 0) {
100-
files = await globby(raw.files, { cwd });
100+
files = await strictGlobMatches(raw.files, { cwd });
101101
}
102102
else {
103-
files = ["package.json", "package-lock.json"];
103+
// Try to find these files by default.
104+
// If they don't exist, then they will NOT be included in the `files` array.
105+
files = await globby(["package.json", "package-lock.json"], { cwd });
104106
}
105107

106108
let ui: Interface;
@@ -130,3 +132,41 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
130132

131133
return { release, commit, tag, push, files, cwd, interface: ui };
132134
}
135+
136+
/**
137+
* Returns all files that match the given glob patterns.
138+
* An error is thrown if any pattern matches zero files.
139+
*/
140+
async function strictGlobMatches(files: string[], options: object): Promise<string[]> {
141+
// Match all glob patterns simultaneously
142+
let matches = await Promise.all(files.map((file) => strictGlobMatch(file, options)));
143+
144+
// Get all the unique files
145+
let matchedFiles = new Set<string>();
146+
for (let match of matches) {
147+
for (let file of match) {
148+
matchedFiles.add(file);
149+
}
150+
}
151+
152+
return [...matchedFiles];
153+
}
154+
155+
/**
156+
* Returns all files that match the given glob pattern.
157+
* An error is thrown if the pattern matches zero files.
158+
*/
159+
async function strictGlobMatch(file: string, options: object): Promise<string[]> {
160+
let matches = await globby(file, options);
161+
162+
if (matches.length === 0) {
163+
if (globby.hasMagic(file)) {
164+
throw new Error(`Could not find any files matching "${file}".`);
165+
}
166+
else {
167+
throw new Error(`Could not find file: ${file}.`);
168+
}
169+
}
170+
171+
return matches;
172+
}

src/update-files.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ async function updateManifestFile(name: string, operation: Operation): Promise<b
5757
let { newVersion } = operation.state;
5858
let modified = false;
5959

60-
try {
6160
let file = await readJsonFile(name, cwd);
6261

6362
if (isManifest(file.data) && file.data.version !== newVersion) {
@@ -66,11 +65,6 @@ async function updateManifestFile(name: string, operation: Operation): Promise<b
6665
modified = true;
6766
}
6867
}
69-
catch (error) {
70-
// Ignore nonexistent files
71-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
72-
throw error;
73-
}
7468
}
7569

7670
return modified;
@@ -86,7 +80,6 @@ async function updateTextFile(name: string, operation: Operation): Promise<boole
8680
let { oldVersion, newVersion } = operation.state;
8781
let modified = false;
8882

89-
try {
9083
let file = await readTextFile(name, cwd);
9184

9285
// Only update the file if it contains at least one occurrence of the old version
@@ -104,12 +97,6 @@ async function updateTextFile(name: string, operation: Operation): Promise<boole
10497
return true;
10598
}
10699
}
107-
catch (error) {
108-
// Ignore nonexistent files
109-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
110-
throw error;
111-
}
112-
}
113100

114101
return modified;
115102
}

test/specs/api.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ describe("versionBup() API", () => {
155155
catch (error) {
156156
expect(error).to.be.an.instanceOf(Error);
157157
error.message.should.equal(
158-
"Unable to determine the current version number. " +
159-
"Checked package.json, package-lock.json."
158+
"Unable to determine the current version number. Checked package.json."
160159
);
161160

162161
// The CWD should not have changed

0 commit comments

Comments
 (0)