Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

Commit

Permalink
requests: #3 and #4 implementeds
Browse files Browse the repository at this point in the history
* 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
Diullei committed Jan 24, 2013
1 parent 958a82c commit 4e79b18
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 220 deletions.
312 changes: 201 additions & 111 deletions deploy/tsd.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion deploy/version.txt

This file was deleted.

41 changes: 41 additions & 0 deletions src/Command/CreateLocalConfigCommand.ts
@@ -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;
}
}
}
46 changes: 36 additions & 10 deletions src/Command/InstallCommand.ts
Expand Up @@ -2,6 +2,8 @@
///<reference path='../System/Web/WebRequest.ts'/> ///<reference path='../System/Web/WebRequest.ts'/>
///<reference path='../System/IO/FileManager.ts'/> ///<reference path='../System/IO/FileManager.ts'/>
///<reference path='../System/IO/DirectoryManager.ts'/> ///<reference path='../System/IO/DirectoryManager.ts'/>
///<reference path='../System/Console.ts'/>
///<reference path='../System/Uri.ts'/>


module Command { module Command {


Expand Down Expand Up @@ -43,15 +45,28 @@ module Command {
sw.close(); sw.close();
} }


private save(name: string, version: string, key: string, content: string): void { private normalizeGithubUrl(uri: UriParsedObject) {
if(!System.IO.DirectoryManager.handle.directoryExists(this.cfg.localPath)) { if (uri.host == 'github.com') {
System.IO.DirectoryManager.handle.createDirectory(this.cfg.localPath); 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); 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); this.saveFile(fileNameWithoutExtension + ".d.key", key);
System.Console.writeLine(" └── " + key + ".key"); System.Console.writeLine(" └── " + key + ".key");
Expand Down Expand Up @@ -86,7 +101,7 @@ module Command {
var version = targetLib.versions[0]; var version = targetLib.versions[0];


System.Web.WebHandler.request.getUrl(version.url, (body) => { 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); this._cache.push(targetLib.name + '@' + version.version);
var deps = (<DataSource.LibDep[]>targetLib.versions[0].dependencies) || []; var deps = (<DataSource.LibDep[]>targetLib.versions[0].dependencies) || [];
for (var i = 0; i < deps.length; i++) { for (var i = 0; i < deps.length; i++) {
Expand All @@ -98,14 +113,25 @@ module Command {
} }


public exec(args: Array): void { public exec(args: Array): void {
this.dataSource.all((libs) => { var targetLib: DataSource.Lib;
System.Console.writeLine("");
var targetLib: DataSource.Lib = this.find(args[3], libs);


if(targetLib) var tryInstall = (libs, lib: string) => {
targetLib = this.find(lib, libs);

if (targetLib)
this.install(targetLib, targetLib.versions[0].version, libs); this.install(targetLib, targetLib.versions[0].version, libs);
else else
System.Console.writeLine("Lib not found."); 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];
}
}); });
} }


Expand Down
19 changes: 13 additions & 6 deletions src/Command/UpdateCommand.ts
@@ -1,5 +1,6 @@
///<reference path='ICommand.ts'/> ///<reference path='ICommand.ts'/>
///<reference path='../System/IO/DirectoryManager.ts'/> ///<reference path='../System/IO/DirectoryManager.ts'/>
///<reference path='../System/Console.ts'/>


module Command { module Command {


Expand All @@ -24,13 +25,19 @@ module Command {
public exec(args: Array): void { public exec(args: Array): void {
this.dataSource.all((libs) => { this.dataSource.all((libs) => {


var libList: Lib[] = []; var libList: Lib[] = [];
var files = System.IO.DirectoryManager.handle.getAllFiles(this.cfg.localPath); 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++) { for (var i = 0; i < files.length; i++) {


var file = files[i].substr(this.cfg.localPath.length + 1); 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 name = file.substr(0, file.lastIndexOf('-'));
var version = file.substr(name.length + 1, file.length - name.length - 7); var version = file.substr(name.length + 1, file.length - name.length - 7);
var key = System.IO.FileManager.handle.readFile(files[i]); var key = System.IO.FileManager.handle.readFile(files[i]);
Expand All @@ -44,7 +51,7 @@ module Command {
if (version == lib.versions[0].version) { if (version == lib.versions[0].version) {
if (key != lib.versions[0].key) { if (key != lib.versions[0].key) {
System.Console.writeLine( 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' : '') ); + (System.Environment.isNode() ? '\033[33m' : '') + 'A new version is available!' + (System.Environment.isNode() ? '\033[0m' : '') );
flg = true; flg = true;
} }
Expand All @@ -54,10 +61,10 @@ module Command {


if (!flg) { if (!flg) {
System.Console.writeLine( 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.'); + 'Is the latest version.');
} }
} //}
} }


}); });
Expand Down
2 changes: 2 additions & 0 deletions src/CommandLineProcessor.ts
Expand Up @@ -5,6 +5,7 @@
///<reference path='Command\SearchCommand.ts'/> ///<reference path='Command\SearchCommand.ts'/>
///<reference path='Command\InstallCommand.ts'/> ///<reference path='Command\InstallCommand.ts'/>
///<reference path='Command\UpdateCommand.ts'/> ///<reference path='Command\UpdateCommand.ts'/>
///<reference path='Command\CreateLocalConfigCommand.ts'/>


class CommandLineProcessor { class CommandLineProcessor {


Expand All @@ -17,6 +18,7 @@ class CommandLineProcessor {
this.commands.push(new Command.SearchCommand(this.dataSource)); this.commands.push(new Command.SearchCommand(this.dataSource));
this.commands.push(new Command.InstallCommand(this.dataSource, this.cfg)); 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.UpdateCommand(this.dataSource, this.cfg));
this.commands.push(new Command.CreateLocalConfigCommand());
} }


public printUsage() { public printUsage() {
Expand Down
29 changes: 21 additions & 8 deletions src/Config.ts
@@ -1,23 +1,36 @@
///<reference path='System/IO/FileManager.ts'/> ///<reference path='System/IO/FileManager.ts'/>


declare var unescape;

enum RepositoryTypeEnum { enum RepositoryTypeEnum {
FileSystem, FileSystem,
Web Web
} }


class Config { class Config {
public static FILE_NAME = 'tsd-config.json';

public repositoryType: RepositoryTypeEnum; public repositoryType: RepositoryTypeEnum;
public uri: string; public uri: string;
public localPath: 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() { public load() {
try{ var cfg = Config.tryGetConfigFile();
var cfgStr = System.IO.FileManager.handle.readFile('tsd-config.json'); this.localPath = Config.isNull(cfg, 'localPath', 'd.ts');
var cfg = JSON.parse(cfgStr); this.repositoryType = Config.isNull(cfg, 'repositoryType', RepositoryTypeEnum.Web);
this.localPath = cfg.localPath; this.uri = Config.isNull(cfg, 'uri', "https://github.com/Diullei/tsd/raw/master/deploy/repository.json");
}catch(e){
console.log(e);
this.localPath = "d.ts";
}
} }
} }
7 changes: 5 additions & 2 deletions src/DataSource/FileSystemDataSource.ts
Expand Up @@ -11,8 +11,11 @@ module DataSource {
constructor (public repositoryPath: string) { } constructor (public repositoryPath: string) { }


public all(callback: (data: DataSource.Lib[]) => void ): void { public all(callback: (data: DataSource.Lib[]) => void ): void {
this._fs.readFile(this.repositoryPath, function (err, data) { this._fs.readFile(this.repositoryPath, (err, data) => {
if (err) throw err; if (err) {
throw new Error("Error reading file repository file: " + err.message);
//throw err;
}
callback(JSON.parse(data)); callback(JSON.parse(data));
}); });
} }
Expand Down
4 changes: 2 additions & 2 deletions src/NodeJs/DirectoryHandle.ts
Expand Up @@ -18,7 +18,7 @@ module NodeJs {
var dpath = ''; var dpath = '';
for (var i = 0; i < parts.length; i++) { for (var i = 0; i < parts.length; i++) {
dpath += parts[i] + '/'; dpath += parts[i] + '/';
if (!this.directoryExists(path)) { if (!this.directoryExists(dpath)) {
this._fs.mkdirSync(dpath); this._fs.mkdirSync(dpath);
} }
} }
Expand All @@ -29,7 +29,7 @@ module NodeJs {
return this._path.dirname(path); return this._path.dirname(path);
} }


public getAllFiles(path, spec?, options?): string[] { public getAllFiles(path, spec?, options?): string[] {
options = options || <{ recursive?: bool; }>{}; options = options || <{ recursive?: bool; }>{};


var filesInFolder = (folder: string): string[] => { var filesInFolder = (folder: string): string[] => {
Expand Down
8 changes: 6 additions & 2 deletions src/NodeJs/WebRequest.ts
@@ -1,15 +1,19 @@
///<reference path='../System/Web/IWebRequest.ts'/> ///<reference path='../System/Web/IWebRequest.ts'/>
///<reference path='../System/Console.ts'/> ///<reference path='../System/Console.ts'/>


module NodeJs { module NodeJs {

export class WebRequest implements System.Web.IWebRequest { export class WebRequest implements System.Web.IWebRequest {
private _request: any = require('request'); private _request: any = require('request');


public getUrl(url: string, callback: (data: string) => void ): void { public getUrl(url: string, callback: (data: string) => void ): void {


System.Console.writeLine("tsd \033[32mhttp \033[35mGET\033[0m " + url); System.Console.writeLine("tsd \033[32mhttp \033[35mGET\033[0m " + url);

this._request(url, (error, response, body) => { 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); System.Console.writeLine("tsd \033[32mhttp \033[35m" + response.statusCode + "\033[0m " + url);
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
callback(body); callback(body);
Expand Down
55 changes: 55 additions & 0 deletions src/System/Uri.ts
@@ -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 };
})();
2 changes: 1 addition & 1 deletion src/Wsh/DirectoryHandle.ts
Expand Up @@ -25,7 +25,7 @@ module Wsh {
var dpath = ''; var dpath = '';
for (var i = 0; i < parts.length; i++) { for (var i = 0; i < parts.length; i++) {
dpath += parts[i] + '/'; dpath += parts[i] + '/';
if (!this.directoryExists(path)) { if (!this.directoryExists(dpath)) {
this._fso.CreateFolder(dpath); this._fso.CreateFolder(dpath);
} }
} }
Expand Down

0 comments on commit 4e79b18

Please sign in to comment.