Improving d.ts generation to allow for external module imports, and so it can be applied to more than just taco-utils.

Fixing up install gulp task so it waits for the installations to finish.
Requiring that ResourcesManager is given a path to the resources, since referring to __dirname would be to the installation of taco-utils not the caller's module.
This commit is contained in:
Jimmy Thomson 2015-02-05 10:25:41 -08:00
Родитель cf44cd6fc3
Коммит c21d8c0bcd
5 изменённых файлов: 33 добавлений и 29 удалений

Просмотреть файл

@ -17,10 +17,20 @@ gulp.task("prep", ["install"], function (callback) {
/* install gulp in root folder */
gulp.task("install", function (callback) {
var modules = ["del", "gulp", "typescript", "ncp", "q"];
for (var i in modules){
exec("npm install " + modules[i], { cwd: ".." });
}
callback();
var asyncLoop = function(idx) {
if (idx < modules.length) {
exec("npm install" + modules[idx], { cwd: ".."}, function(error, stdout, stderr) {
if (!error) {
asyncLoop(idx + 1);
} else {
callback(error);
}
})
} else {
callback();
}
}
asyncLoop(0);
});

Просмотреть файл

@ -55,15 +55,14 @@ gulp.task("copy", function (callback: Function): void {
});
/* auto-generate taco-utils.d.ts*/
gulp.task("generate-dts", function (cb: Function): void {
gulp.task("generate-dts", function (): Q.Promise<any> {
var tacoUtils: string = "taco-utils";
dtsUtil.DefinitionServices.generateTSExportDefinition(
return dtsUtil.DefinitionServices.generateTSExportDefinition(
tacoUtils,
path.join(buildConfig.src, tacoUtils),
path.join(buildConfig.src, "typings"),
"TacoUtility",
tacoUtils);
cb();
});
/* Task to run tests */

Просмотреть файл

@ -15,11 +15,7 @@ module TacoUtility {
private static SupportedLanguages: string[] = null;
private static DefaultLanguage: string = "en";
public static init(language: string, resourcesDir?: string): void {
if (!resourcesDir) {
resourcesDir = path.join(__dirname, "..", "resources");
}
public static init(language: string, resourcesDir: string): void {
ResourcesManager.Resources = {};
ResourcesManager.SupportedLanguages = [];
fs.readdirSync(resourcesDir).forEach(function (filename: string): void {

7
src/typings/taco-utils.d.ts поставляемый
Просмотреть файл

@ -1,12 +1,13 @@
/// <reference path="../typings/node.d.ts" />
/// <reference path="../typings/elementtree.d.ts" />
/// <reference path="../typings/unorm.d.ts" />
declare module TacoUtility {
declare module "taco-utils" {
module TacoUtility {
class ResourcesManager {
private static Resources;
private static SupportedLanguages;
private static DefaultLanguage;
static init(language: string, resourcesDir?: string): void;
static init(language: string, resourcesDir: string): void;
static teardown(): void;
/** ...optionalArgs is only there for typings, function rest params */
static getString(id: string, ...optionalArgs: any[]): string;
@ -99,6 +100,6 @@ declare module TacoUtility {
localize(req: any): BuildInfo;
}
}
declare module "taco-utils"{
export = TacoUtility;
}

Просмотреть файл

@ -11,12 +11,11 @@ var ncp = require("ncp");
/*utility to generate .d.ts file*/
export module DefinitionServices {
export function generateTSExportDefinition(fileName: string, srcFolderPath: string, destFolderPath: string, moduleName: string, moduleString: string): void {
export function generateTSExportDefinition(fileName: string, srcFolderPath: string, destFolderPath: string, moduleName: string, moduleString: string): Q.Promise<any> {
var destDtsFile: string = path.join(destFolderPath, fileName + ".d.ts");
Q(compileDeclarationFile(fileName, srcFolderPath)).
return Q(compileDeclarationFile(fileName, srcFolderPath)).
then(function (): void { copyDTSTypings(fileName, srcFolderPath, destFolderPath); }).
then(function (): void { addExportsInTypings(destDtsFile, "TacoUtility", "taco-utils"); }).
done();
then(function (): void { addExportsInTypings(destDtsFile, moduleName, moduleString); });
}
/*call tsc --d, only option is to generate it in same folder as .ts*/
@ -46,19 +45,18 @@ export module DefinitionServices {
del([srcJSFilePath], { force: true });
}
/*add wrap "export = moduleName" with "declare module "moduleString"{}"*/
function addExportsInTypings(dtsPath: string, moduleName: string, moduleString: string): Q.Promise<any> {
var d = Q.defer();
/*add wrap everything except ///<reference>s with "declare module "moduleString"{}"*/
function addExportsInTypings(dtsPath: string, moduleName: string, moduleString: string): void {
console.log("---processing: " + dtsPath);
var buf: any = fs.readFileSync(dtsPath, "utf8");
var regex: string = "export.*=.*" + moduleName + ".*;";
var buf: string = fs.readFileSync(dtsPath, "utf8");
var result: string= buf.replace("declare module " + moduleName, "module " + moduleName);
var regex: string = "(module " + moduleName + ")|(import)";
var match: string[] = buf.match(regex);
if (match && match[0]) {
var foundMatch = match[0];
var result = buf.replace(foundMatch, "declare module \"" + moduleString + "\"{\n" + foundMatch + "\n}");
fs.writeFileSync(dtsPath, result, "utf8");
result = result.replace(foundMatch, "declare module \"" + moduleString + "\" {\n" + foundMatch) + "\n}\n";
}
return d.promise;
fs.writeFileSync(dtsPath, result, "utf8");
}
}
}