Permalink
Browse files

requests: #3 and #4 implementeds

* TSD allow multple definition installations by using: `tsd install lib
[lib...]`
* You can define repository url and other configuration in a config file
* Repository directory structure is mirrored in local folder.
* some bug fix
  • Loading branch information...
1 parent 958a82c commit 4e79b18c27780192b23b7de6a11ea891cc248cb5 @Diullei Diullei committed Jan 24, 2013
View
View
@@ -1 +0,0 @@
-0.2.3
@@ -0,0 +1,41 @@
+///<reference path='ICommand.ts'/>
+///<reference path='../Config.ts'/>
+///<reference path='../System/IO/FileManager.ts'/>
+
+module Command {
+
+ export class CreateLocalConfigCommand implements ICommand {
+
+ public shortcut: string = "ncfg";
+ public usage: string = "Create a local config file.";
+ private args: Array;
+
+ public accept(args: Array): bool {
+ return args[2] == this.shortcut;
+ }
+
+ private saveConfigFile(): void {
+ var sw = System.IO.FileManager.handle.createFile(Config.FILE_NAME);
+ sw.write('{\n'
+ + ' "localPath": "d.ts",\n'
+ + ' "repositoryType": "1",\n'
+ + ' "uri": "https://github.com/Diullei/tsd/raw/master/deploy/repository.json"\n'
+ + '}');
+ sw.flush();
+ sw.close();
+ }
+
+ public exec(args: Array): void {
+ if (System.IO.FileManager.handle.fileExists(Config.FILE_NAME)) {
+ throw new Error("There is already a configuration file in this folder.");
+ } else {
+ this.saveConfigFile();
+ }
+ System.Console.writeLine("configuration file created successfully.");
+ }
+
+ public toString(): string {
+ return this.shortcut + " " + this.usage;
+ }
+ }
+}
@@ -2,6 +2,8 @@
///<reference path='../System/Web/WebRequest.ts'/>
///<reference path='../System/IO/FileManager.ts'/>
///<reference path='../System/IO/DirectoryManager.ts'/>
+///<reference path='../System/Console.ts'/>
+///<reference path='../System/Uri.ts'/>
module Command {
@@ -43,15 +45,28 @@ module Command {
sw.close();
}
- private save(name: string, version: string, key: string, content: string): void {
- if(!System.IO.DirectoryManager.handle.directoryExists(this.cfg.localPath)) {
- System.IO.DirectoryManager.handle.createDirectory(this.cfg.localPath);
+ private normalizeGithubUrl(uri: UriParsedObject) {
+ if (uri.host == 'github.com') {
+ var parts = uri.directory.split('/');
+ var repo = /*parts[1] + '_' +*/ parts[2];
+ var ignore = '/' + parts[1] + '/' + parts[2] + '/' + parts[3] + '/' + parts[4];
+ uri.directory = '/' + repo + uri.directory.substr(ignore.length);
}
+ }
+
+ private save(url: string, name: string, version: string, key: string, content: string): void {
+ var uri = Uri.parseUri(url);
+ this.normalizeGithubUrl(uri);
- var fileNameWithoutExtension = this.cfg.localPath + "/" + name + "-" + version;
+ if(!System.IO.DirectoryManager.handle.directoryExists(this.cfg.localPath + uri.directory)) {
+ System.IO.DirectoryManager.handle.createDirectory(this.cfg.localPath + uri.directory);
+ }
+ var fileNameWithoutExtension = this.cfg.localPath + uri.directory + name + "-" + version;
+
+ System.Console.writeLine("");
this.saveFile(fileNameWithoutExtension + ".d.ts", content);
- System.Console.writeLine("└── " + name + "@" + version + " instaled.");
+ System.Console.writeLine("└── " + name + "@" + version + " -> " + this.cfg.localPath + uri.directory);
this.saveFile(fileNameWithoutExtension + ".d.key", key);
System.Console.writeLine(" └── " + key + ".key");
@@ -86,7 +101,7 @@ module Command {
var version = targetLib.versions[0];
System.Web.WebHandler.request.getUrl(version.url, (body) => {
- this.save(targetLib.name, version.version, version.key, body);
+ this.save(version.url, targetLib.name, version.version, version.key, body);
this._cache.push(targetLib.name + '@' + version.version);
var deps = (<DataSource.LibDep[]>targetLib.versions[0].dependencies) || [];
for (var i = 0; i < deps.length; i++) {
@@ -98,14 +113,25 @@ module Command {
}
public exec(args: Array): void {
- this.dataSource.all((libs) => {
- System.Console.writeLine("");
- var targetLib: DataSource.Lib = this.find(args[3], libs);
+ var targetLib: DataSource.Lib;
- if(targetLib)
+ var tryInstall = (libs, lib: string) => {
+ targetLib = this.find(lib, libs);
+
+ if (targetLib)
this.install(targetLib, targetLib.versions[0].version, libs);
else
System.Console.writeLine("Lib not found.");
+ };
+
+ this.dataSource.all((libs) => {
+ var index = 3;
+ var lib = args[index];
+ while (lib) {
+ tryInstall(libs, lib);
+ index++;
+ lib = args[index];
+ }
});
}
@@ -1,5 +1,6 @@
///<reference path='ICommand.ts'/>
///<reference path='../System/IO/DirectoryManager.ts'/>
+///<reference path='../System/Console.ts'/>
module Command {
@@ -24,13 +25,19 @@ module Command {
public exec(args: Array): void {
this.dataSource.all((libs) => {
- var libList: Lib[] = [];
- var files = System.IO.DirectoryManager.handle.getAllFiles(this.cfg.localPath);
+ var libList: Lib[] = [];
+ var files = [];
+
+ try {
+ files = System.IO.DirectoryManager.handle.getAllFiles(this.cfg.localPath, /.d\.key$/g, { recursive: true });
+ } catch (e) {
+ System.Console.writeLine('Empty directory.');
+ }
for (var i = 0; i < files.length; i++) {
var file = files[i].substr(this.cfg.localPath.length + 1);
- if (file.substr(file.length - 5) == 'd.key') {
+ //if (file.substr(file.length - 5) == 'd.key') {
var name = file.substr(0, file.lastIndexOf('-'));
var version = file.substr(name.length + 1, file.length - name.length - 7);
var key = System.IO.FileManager.handle.readFile(files[i]);
@@ -44,7 +51,7 @@ module Command {
if (version == lib.versions[0].version) {
if (key != lib.versions[0].key) {
System.Console.writeLine(
- ' ' + (System.Environment.isNode() ? '\033[36m' : '') + name + (System.Environment.isNode() ? '\033[0m' : '') + ' - '
+ ' ' + (System.Environment.isNode() ? '\033[36m' : '') + '> ' + name + ' d.ts' + (System.Environment.isNode() ? '\033[0m' : '') + ' - '
+ (System.Environment.isNode() ? '\033[33m' : '') + 'A new version is available!' + (System.Environment.isNode() ? '\033[0m' : '') );
flg = true;
}
@@ -54,10 +61,10 @@ module Command {
if (!flg) {
System.Console.writeLine(
- ' ' + (System.Environment.isNode() ? '\033[36m' : '') + name + (System.Environment.isNode() ? '\033[0m' : '') + ' - '
+ ' ' + (System.Environment.isNode() ? '\033[36m' : '') + '> ' + name + ' d.ts' + (System.Environment.isNode() ? '\033[0m' : '') + ' - '
+ 'Is the latest version.');
}
- }
+ //}
}
});
@@ -5,6 +5,7 @@
///<reference path='Command\SearchCommand.ts'/>
///<reference path='Command\InstallCommand.ts'/>
///<reference path='Command\UpdateCommand.ts'/>
+///<reference path='Command\CreateLocalConfigCommand.ts'/>
class CommandLineProcessor {
@@ -17,6 +18,7 @@ class CommandLineProcessor {
this.commands.push(new Command.SearchCommand(this.dataSource));
this.commands.push(new Command.InstallCommand(this.dataSource, this.cfg));
this.commands.push(new Command.UpdateCommand(this.dataSource, this.cfg));
+ this.commands.push(new Command.CreateLocalConfigCommand());
}
public printUsage() {
View
@@ -1,23 +1,36 @@
///<reference path='System/IO/FileManager.ts'/>
+declare var unescape;
+
enum RepositoryTypeEnum {
FileSystem,
Web
}
class Config {
+ public static FILE_NAME = 'tsd-config.json';
+
public repositoryType: RepositoryTypeEnum;
public uri: string;
public localPath: string;
+ private static isNull(cfg: Object, key: string, alternativeValue: any): any {
+ return cfg[key] ? cfg[key] : alternativeValue;
+ }
+
+ private static tryGetConfigFile() {
+ var cfg = {};
+ try {
+ cfg = JSON.parse(System.IO.FileManager.handle.readFile(Config.FILE_NAME));
+ } catch (e) {
+ }
+ return cfg;
+ }
+
public load() {
- try{
- var cfgStr = System.IO.FileManager.handle.readFile('tsd-config.json');
- var cfg = JSON.parse(cfgStr);
- this.localPath = cfg.localPath;
- }catch(e){
- console.log(e);
- this.localPath = "d.ts";
- }
+ var cfg = Config.tryGetConfigFile();
+ this.localPath = Config.isNull(cfg, 'localPath', 'd.ts');
+ this.repositoryType = Config.isNull(cfg, 'repositoryType', RepositoryTypeEnum.Web);
+ this.uri = Config.isNull(cfg, 'uri', "https://github.com/Diullei/tsd/raw/master/deploy/repository.json");
}
}
@@ -11,8 +11,11 @@ module DataSource {
constructor (public repositoryPath: string) { }
public all(callback: (data: DataSource.Lib[]) => void ): void {
- this._fs.readFile(this.repositoryPath, function (err, data) {
- if (err) throw err;
+ this._fs.readFile(this.repositoryPath, (err, data) => {
+ if (err) {
+ throw new Error("Error reading file repository file: " + err.message);
+ //throw err;
+ }
callback(JSON.parse(data));
});
}
@@ -18,7 +18,7 @@ module NodeJs {
var dpath = '';
for (var i = 0; i < parts.length; i++) {
dpath += parts[i] + '/';
- if (!this.directoryExists(path)) {
+ if (!this.directoryExists(dpath)) {
this._fs.mkdirSync(dpath);
}
}
@@ -29,7 +29,7 @@ module NodeJs {
return this._path.dirname(path);
}
- public getAllFiles(path, spec?, options?): string[] {
+ public getAllFiles(path, spec?, options?): string[] {
options = options || <{ recursive?: bool; }>{};
var filesInFolder = (folder: string): string[] => {
@@ -1,15 +1,19 @@
///<reference path='../System/Web/IWebRequest.ts'/>
///<reference path='../System/Console.ts'/>
-module NodeJs {
+module NodeJs {
+
export class WebRequest implements System.Web.IWebRequest {
private _request: any = require('request');
public getUrl(url: string, callback: (data: string) => void ): void {
System.Console.writeLine("tsd \033[32mhttp \033[35mGET\033[0m " + url);
-
+
this._request(url, (error, response, body) => {
+ if (error)
+ System.Console.writeLine("tsd \033[31mERR!\033[0m \033[35mGET\033[0m " + url);
+
System.Console.writeLine("tsd \033[32mhttp \033[35m" + response.statusCode + "\033[0m " + url);
if (!error && response.statusCode == 200) {
callback(body);
View
@@ -0,0 +1,55 @@
+// parseUri 1.2.2
+// (c) Steven Levithan <stevenlevithan.com>
+// MIT License
+
+interface UriParsedObject {
+ anchor: string;
+ query: string;
+ file: string;
+ directory: string;
+ path: string;
+ relative: string;
+ port: string;
+ host: string;
+ password: string;
+ user: string;
+ iserInfo: string;
+ authority: string;
+ protocol: string;
+ source: string;
+ queryKey: Object;
+}
+
+var Uri = <{ parseUri: (str: string) => UriParsedObject; }><any>(function () {
+
+ var options = {
+ strictMode: false,
+ key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
+ q: {
+ name: "queryKey",
+ parser: /(?:^|&)([^&=]*)=?([^&]*)/g
+ },
+ parser: {
+ strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
+ loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
+ }
+ };
+
+ function parseUri(str) {
+ var o = options,
+ m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
+ uri = {},
+ i = 14;
+
+ while (i--) uri[o.key[i]] = m[i] || "";
+
+ uri[o.q.name] = {};
+ uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
+ if ($1) uri[o.q.name][$1] = $2;
+ });
+
+ return uri;
+ };
+
+ return { parseUri: parseUri };
+})();
@@ -25,7 +25,7 @@ module Wsh {
var dpath = '';
for (var i = 0; i < parts.length; i++) {
dpath += parts[i] + '/';
- if (!this.directoryExists(path)) {
+ if (!this.directoryExists(dpath)) {
this._fso.CreateFolder(dpath);
}
}
Oops, something went wrong. Retry.

0 comments on commit 4e79b18

Please sign in to comment.