Skip to content

Commit 5eb9f2f

Browse files
committed
Autocompletion
1 parent 7b89099 commit 5eb9f2f

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+
constructor(private $fs: IFileSystem,
8+
private $childProcess: IChildProcess,
9+
private $logger: ILogger) { }
10+
11+
public disableAnalytics = true;
12+
13+
public execute(args: string[]): IFuture<void> {
14+
return (() => {
15+
var scriptsOk = true;
16+
17+
try {
18+
this.updateShellScript(".profile").wait();
19+
this.updateShellScript(".bashrc").wait();
20+
21+
this.updateBashProfile().wait();
22+
23+
// zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
24+
this.updateShellScript(".zshrc").wait();
25+
} catch(err) {
26+
this.$logger.out("Failed to update all shell start-up scripts. Auto-completion may not work. " + err);
27+
scriptsOk = false;
28+
}
29+
30+
if(scriptsOk) {
31+
this.$logger.out("Restart your shell to enable command auto-completion.");
32+
}
33+
}).future<void>()();
34+
}
35+
36+
private updateShellScript(fileName: string): IFuture<void> {
37+
return (() => {
38+
var filePath = this.getHomePath(fileName);
39+
40+
var doUpdate = true;
41+
if (this.$fs.exists(filePath).wait()) {
42+
var contents = this.$fs.readText(filePath).wait();
43+
if (contents.match(/nativescript\s+completion\s+--\s+/) || contents.match(/tns\s+completion\s+--\s+/)) {
44+
doUpdate = false;
45+
}
46+
}
47+
48+
if(doUpdate) {
49+
this.updateShellScriptCore(filePath).wait();
50+
}
51+
52+
}).future<void>()();
53+
}
54+
55+
private updateShellScriptCore(filePath: string): IFuture<void> {
56+
return (() => {
57+
this.$childProcess.exec("nativescript completion >> " + filePath).wait();
58+
this.$childProcess.exec("tns completion >> " + filePath).wait();
59+
}).future<void>()();
60+
}
61+
62+
private getHomePath(fileName: string): string {
63+
return path.join(osenv.home(), fileName);
64+
}
65+
66+
private updateBashProfile(): IFuture<void> {
67+
return (() => {
68+
var callProfileScript =
69+
"if [ -f ~/.profile ]; then\n" +
70+
" . ~/.profile\n" +
71+
"fi\n";
72+
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(callProfileScript) < 0) {
77+
this.updateShellScript(".bash_profile");
78+
}
79+
} else {
80+
this.$fs.writeFile(bashProfileFileName, callProfileScript).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)