Skip to content

Commit 48a8393

Browse files
committed
Merge pull request #61 from NativeScript/fatme/autocompletion
Autocompletion
2 parents 7b89099 + 91fb9ce commit 48a8393

9 files changed

+97
-2
lines changed

bin/nativescript.cmd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@node %~dp0\nativescript.js %*

bin/nativescript.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/usr/bin/env node
22

3+
"use strict";
34
require("../lib/nativescript-cli.js");

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ $injector.requireCommand("run", "./commands/run");
2323
$injector.requireCommand("prepare", "./commands/prepare");
2424
$injector.requireCommand("build", "./commands/build");
2525
$injector.requireCommand("deploy", "./commands/deploy");
26+
$injector.requireCommand("dev-post-install", "./commands/post-install");
2627

2728
$injector.require("npm", "./node-package-manager");
2829
$injector.require("config", "./config");

lib/commands/post-install.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
///<reference path="../.d.ts"/>
2+
"use strict";
3+
import osenv = require("osenv");
4+
import path = require("path");
5+
6+
export class PostInstallCommand implements ICommand {
7+
private static CALL_PROFILE_SCRIPT =
8+
"if [ -f ~/.profile ]; then\n" +
9+
" . ~/.profile\n" +
10+
"fi\n";
11+
12+
constructor(private $fs: IFileSystem,
13+
private $childProcess: IChildProcess,
14+
private $logger: ILogger) { }
15+
16+
public disableAnalytics = true;
17+
18+
public execute(args: string[]): IFuture<void> {
19+
return (() => {
20+
var scriptsOk = true;
21+
22+
try {
23+
this.updateShellScript(".profile").wait();
24+
this.updateShellScript(".bashrc").wait();
25+
26+
this.updateBashProfile().wait();
27+
28+
// zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
29+
this.updateShellScript(".zshrc").wait();
30+
} catch(err) {
31+
this.$logger.out("Failed to update all shell start-up scripts. Auto-completion may not work. " + err);
32+
scriptsOk = false;
33+
}
34+
35+
if(scriptsOk) {
36+
this.$logger.out("Restart your shell to enable command auto-completion.");
37+
}
38+
}).future<void>()();
39+
}
40+
41+
private updateShellScript(fileName: string): IFuture<void> {
42+
return (() => {
43+
var filePath = this.getHomePath(fileName);
44+
45+
var doUpdate = true;
46+
if (this.$fs.exists(filePath).wait()) {
47+
var contents = this.$fs.readText(filePath).wait();
48+
if (contents.match(/nativescript\s+completion\s+--\s+/) || contents.match(/tns\s+completion\s+--\s+/)) {
49+
doUpdate = false;
50+
}
51+
}
52+
53+
if(doUpdate) {
54+
this.updateShellScriptCore(filePath).wait();
55+
}
56+
57+
}).future<void>()();
58+
}
59+
60+
private updateShellScriptCore(filePath: string): IFuture<void> {
61+
return (() => {
62+
this.$childProcess.exec("nativescript completion >> " + filePath).wait();
63+
this.$childProcess.exec("tns completion >> " + filePath).wait();
64+
}).future<void>()();
65+
}
66+
67+
private getHomePath(fileName: string): string {
68+
return path.join(osenv.home(), fileName);
69+
}
70+
71+
private updateBashProfile(): IFuture<void> {
72+
return (() => {
73+
var bashProfileFileName = this.getHomePath(".bash_profile");
74+
if (this.$fs.exists(bashProfileFileName).wait()) {
75+
var contens = this.$fs.readText(bashProfileFileName).wait();
76+
if (contens.indexOf(PostInstallCommand.CALL_PROFILE_SCRIPT) < 0) {
77+
this.updateShellScript(".bash_profile");
78+
}
79+
} else {
80+
this.$fs.writeFile(bashProfileFileName, PostInstallCommand.CALL_PROFILE_SCRIPT).wait();
81+
this.updateShellScriptCore(bashProfileFileName).wait();
82+
}
83+
}).future<void>()();
84+
}
85+
}
86+
$injector.registerCommand("dev-post-install", PostInstallCommand);

lib/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import util = require("util");
66
export class StaticConfig implements IStaticConfig {
77
public PROJECT_FILE_NAME = ".tnsproject";
88
public CLIENT_NAME = "nativescript";
9+
public CLIENT_NAME_ALIAS = "tns";
910
public ANALYTICS_API_KEY = "5752dabccfc54c4ab82aea9626b7338e";
1011
public TRACK_FEATURE_USAGE_SETTING_NAME = "TrackFeatureUsage";
1112
public ANALYTICS_INSTALLATION_ID_SETTING_NAME = "AnalyticsInstallationID";

lib/nativescript-cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ $injector.register("config", {
2121

2222
var dispatcher = $injector.resolve("dispatcher");
2323
dispatcher.runMainFiber();
24+

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"main": "./lib/nativescript-cli.js",
1212
"scripts": {
13-
"test": "node_modules\\.bin\\_mocha --ui mocha-fibers --recursive --reporter spec --require test/test-bootstrap.js --timeout 15000 test/"
13+
"test": "node_modules\\.bin\\_mocha --ui mocha-fibers --recursive --reporter spec --require test/test-bootstrap.js --timeout 15000 test/",
14+
"postinstall": "node postinstall.js"
1415
},
1516
"repository": {
1617
"type": "git",

postinstall.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
var child_process = require("child_process");
3+
child_process.exec('node bin/nativescript.js dev-post-install');

0 commit comments

Comments
 (0)