Skip to content

Commit 2aa7276

Browse files
committed
fix(version): fix bug from using app-root-path inside a node_module
1 parent 472694e commit 2aa7276

4 files changed

Lines changed: 28 additions & 34 deletions

File tree

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,28 @@ Session will be kept alive for 7 days
190190

191191

192192
## Specifying the version
193-
By default classy-commander will report the version stored in your the `package.json`, or you can optionally specify your own.
193+
There are two ways to specify the version of your CLI:
194+
195+
Using the version in your `package.json`
196+
197+
```typescript
198+
import * as commander from 'classy-commander';
199+
200+
...
201+
202+
commander
203+
.versionFromPackage(__dirname)
204+
.execute();
205+
```
206+
207+
Or manually
194208

195209
```typescript
196210
import * as commander from 'classy-commander';
197211

198212
...
199213

200214
commander
201-
.version('1.0.1')
215+
.version('1.3.1')
202216
.execute();
203217
```

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"@commitlint/cli": "^7.1.2",
4242
"@commitlint/config-conventional": "^7.1.2",
4343
"@commitlint/prompt-cli": "^7.1.2",
44-
"@types/app-root-path": "^1.2.4",
4544
"@types/fs-extra": "^5.0.4",
4645
"@types/jest": "^23.3.2",
4746
"jest": "^23.6.0",
@@ -54,7 +53,6 @@
5453
"typescript": "^3.0.3"
5554
},
5655
"dependencies": {
57-
"app-root-path": "^2.1.0",
5856
"chalk": "^2.4.1",
5957
"commander": "^2.18.0",
6058
"fs-extra": "^7.0.0",

src/classy.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// tslint:disable:no-console
2-
import * as root from 'app-root-path';
32
import * as cli from 'commander';
43
import * as fs from 'fs-extra';
4+
import * as path from 'path';
55
import { setIocContainer } from './commander';
66
import { IocContainer } from './types';
77

@@ -18,8 +18,15 @@ export class Commander {
1818
return this;
1919
}
2020

21+
public versionFromPackage(dirName: string): this {
22+
this._version = this.getPackageVersion(path.resolve(dirName, './package.json'));
23+
return this;
24+
}
25+
2126
public async execute(argv?: string[]): Promise<void> {
22-
await this.setVersion();
27+
if (this._version) {
28+
cli.version(this._version);
29+
}
2330

2431
const args = argv || process.argv;
2532

@@ -30,25 +37,11 @@ export class Commander {
3037
cli.parse(args);
3138
}
3239

33-
private async getPackageVersion(): Promise<string | undefined> {
34-
const packageFileName = root.resolve('package.json');
35-
if (!await fs.pathExists(packageFileName)) {
40+
private getPackageVersion(packageFileName: string): string | undefined {
41+
if (!fs.pathExistsSync(packageFileName)) {
3642
return undefined;
3743
}
38-
39-
const packageObject = await fs.readJSON(packageFileName);
40-
44+
const packageObject = fs.readJSONSync(packageFileName);
4145
return packageObject && packageObject.version;
4246
}
43-
44-
private async setVersion() {
45-
const version =
46-
this._version !== undefined ?
47-
this._version :
48-
await this.getPackageVersion();
49-
50-
if (version) {
51-
cli.version(version);
52-
}
53-
}
5447
}

0 commit comments

Comments
 (0)