Skip to content

Commit d2b3fbf

Browse files
author
Alan Agius
committed
Allow packages to not have a version field
NPM does now allow that, this is actually a node implementation of modules. NPM will only validate the primary package.json and fail on publish if version is missing. In the node documentation for modules it is clearly specified the the only requirement is the 'main' property Closes: microsoft#1044
1 parent 4adc7b0 commit d2b3fbf

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

apps/api-extractor/src/api/Extractor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class Extractor {
9898
/**
9999
* Returns the version number of the API Extractor NPM package.
100100
*/
101-
public static get version(): string {
101+
public static get version(): string | undefined {
102102
return Extractor._getPackageJson().version;
103103
}
104104

libraries/node-core-library/src/IPackageJson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface IPackageJson {
5959
/**
6060
* A version number conforming to the Semantic Versioning (SemVer) standard.
6161
*/
62-
version: string;
62+
version?: string;
6363

6464
/**
6565
* Indicates whether this package is allowed to be published or not.

libraries/node-core-library/src/PackageJsonLookup.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ export class PackageJsonLookup {
201201
throw new Error(`Error reading "${jsonFilename}":\n `
202202
+ 'The required field "name" was not found');
203203
}
204-
if (!loadedPackageJson.version) {
205-
throw new Error(`Error reading "${jsonFilename}":\n `
206-
+ 'The required field "version" was not found');
207-
}
208204

209205
if (this._loadExtraFields) {
210206
packageJson = loadedPackageJson;

libraries/node-core-library/src/test/PackageJsonLookup.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ describe('PackageJsonLookup', () => {
2828
}
2929
});
3030

31+
test('tryLoadPackageJsonFor() test package with no version', () => {
32+
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
33+
const sourceFilePath: string = path.join(__dirname, './test-data/example-package-no-version');
34+
const packageJson: IPackageJson | undefined = packageJsonLookup.tryLoadPackageJsonFor(sourceFilePath);
35+
expect(packageJson).toBeDefined();
36+
if (packageJson) {
37+
expect(packageJson.name).toEqual('example-package');
38+
expect(packageJson.version).not.toBeDefined();
39+
40+
// The "nonstandardField" should have been trimmed because loadExtraFields=false
41+
expect(packageJson).not.toHaveProperty('nonstandardField');
42+
}
43+
});
44+
3145
test('tryGetPackageFolderFor() test', () => {
3246
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
3347
const sourceFilePath: string = path.join(__dirname, './test-data/example-package/src/ExampleFile.txt');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "example-package",
3+
"nonstandardField": 123
4+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
/**
5+
* AEDoc for AliasClass
6+
* @public
7+
*/
8+
export class AliasClass {
9+
private readOnlyNumber: number;
10+
11+
/**
12+
* AEDoc for aliasFunc()
13+
* @internal
14+
*/
15+
public _aliasFunc(): void {
16+
console.log('this is an internal API');
17+
}
18+
19+
public aliasField: number;
20+
21+
public get shouldBeReadOnly(): number {
22+
return this.readOnlyNumber;
23+
}
24+
}
25+
26+
class PrivateAliasClass {
27+
public test(): void {
28+
}
29+
}

0 commit comments

Comments
 (0)