Skip to content

Commit 0dc855d

Browse files
committed
Autocompletion
1 parent fee3115 commit 0dc855d

9 files changed

+98
-3
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
@@ -19,6 +19,7 @@ $injector.requireCommand("platform|remove", "./commands/remove-platform");
1919
$injector.requireCommand("run", "./commands/run");
2020
$injector.requireCommand("prepare", "./commands/prepare");
2121
$injector.requireCommand("build", "./commands/build");
22+
$injector.requireCommand("dev-post-install", "./commands/post-install");
2223

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

lib/commands/post-install.ts

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

lib/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import path = require("path");
44

55
export class StaticConfig implements IStaticConfig {
66
public PROJECT_FILE_NAME = ".tnsproject";
7-
public CLIENT_NAME = "tns";
7+
public CLIENT_NAME = "nativescript";
8+
public CLIENT_NAME_ALIAS = "tns";
89
public ANALYTICS_API_KEY = "";
910

1011
public version = require("../package.json").version;

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

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var child_process = require("child_process");
3+
4+
child_process.spawn('node', ['tns', 'dev-post-install']);

0 commit comments

Comments
 (0)