Return generate reference task for third party devDependencies

Signed-off-by: boris <boris.karastanev@progress.com>
This commit is contained in:
boris 2017-09-26 14:55:54 +03:00
Родитель 21dc8c29f1
Коммит 9c16345eba
1 изменённых файлов: 47 добавлений и 2 удалений

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

@ -184,9 +184,54 @@ module.exports = function (grunt) {
fs.renameSync(oldFileName + fileExtension, newFileName + fileExtension);
});
grunt.registerTask("test", ["transpile_additional_project", "ts:devlib"]);
grunt.registerTask("test", ["transpile_additional_project", "generate_references", "ts:devlib"]);
grunt.registerTask("generate_references", () => {
const referencesPath = path.join(__dirname, "references.d.ts");
// get all .d.ts files from nativescript-cli and mobile-cli-lib
const nodeModulesDirPath = path.join(__dirname, "node_modules");
const specialFiles = [
path.join(nodeModulesDirPath, "mobile-cli-lib", "services", "analytics-type.ts"),
path.join(nodeModulesDirPath, "mobile-cli-lib", "services", "google-analytics-data-type.ts")
];
let pathsOfDtsFiles = getReferencesFromDir(path.join(nodeModulesDirPath, "nativescript"))
.concat(getReferencesFromDir(path.join(nodeModulesDirPath, "mobile-cli-lib")))
.concat(getReferencesFromDir(path.join(nodeModulesDirPath, "ios-device-lib")));
pathsOfDtsFiles = pathsOfDtsFiles.concat(...specialFiles);
const lines = pathsOfDtsFiles.map(file => `/// <reference path="${fromWindowsRelativePathToUnix(path.relative(__dirname, file))}" />`);
fs.writeFileSync(referencesPath, lines.join(os.EOL));
});
const fromWindowsRelativePathToUnix = (windowsRelativePath) => {
return windowsRelativePath.replace(/\\/g, "/");
};
// returns paths that have to be added to reference.d.ts.
const getReferencesFromDir = (dir) => {
const currentDirContent = fs.readdirSync(dir).map(item => path.join(dir, item));
let pathsToDtsFiles = [];
_.each(currentDirContent, d => {
const stat = fs.statSync(d);
if (stat.isDirectory() && path.basename(d) !== "node_modules") {
// recursively check all dirs for .d.ts files.
pathsToDtsFiles = pathsToDtsFiles.concat(getReferencesFromDir(d));
} else if (stat.isFile() && d.endsWith(".d.ts") && path.basename(d) !== ".d.ts") {
pathsToDtsFiles.push(d);
}
});
return pathsToDtsFiles;
};
grunt.registerTask("pack", [
"clean",
"generate_references",
"ts:release_build",
"transpile_additional_project",
"tslint:build",
@ -197,5 +242,5 @@ module.exports = function (grunt) {
grunt.registerTask("lint", ["tslint:build"]);
grunt.registerTask("all", ["clean", "test", "lint"]);
grunt.registerTask("rebuild", ["clean", "ts:devlib"]);
grunt.registerTask("default", ["ts:devlib"]);
grunt.registerTask("default", ["generate_references", "ts:devlib"]);
};