Validate package.json files' "main" setting.

Don't use a filepath ending in .js, let the require implementation
resolve the js file. This makes it easier to require the correct
platform-specific file when bundling by switching the webpack
`resolve.extensions` setting.
This commit is contained in:
Hristo Deshev
2016-08-04 16:22:27 +03:00
parent 2e8458f274
commit 052f4617d5

View File

@ -439,24 +439,37 @@ module.exports = function(grunt) {
"copy:childPackageFiles" "copy:childPackageFiles"
]); ]);
grunt.registerTask("check-packagejson-boms", function() { function validatePackageJsons(fileValidator, errorFormatter) {
function hasBOM(filepath) {
var buf = grunt.file.read(filepath, { encoding: null });
return (buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF);
}
var packageDescriptors = grunt.file.expand({}, [ var packageDescriptors = grunt.file.expand({}, [
'**/package.json', 'tns-core-modules/**/package.json'
'!node_modules/**'
]); ]);
var errors = packageDescriptors.map(function(packagePath) { var errors = packageDescriptors.map(function(packagePath) {
if (hasBOM(packagePath)) { if (fileValidator(packagePath)) {
return "File " + packagePath + " contains a UTF-8 BOM."; return errorFormatter(packagePath);
} else { } else {
return null; return null;
} }
}).filter(function(errorMessage) { return !!errorMessage; }); }).filter(function(errorMessage) { return !!errorMessage; });
if (errors.length > 0) if (errors.length > 0)
grunt.fail.fatal("\n" + errors.join("\n")); grunt.fail.fatal("\n" + errors.join("\n"));
}
grunt.registerTask("check-packagejson-boms", function() {
validatePackageJsons(function (filepath) {
var buf = grunt.file.read(filepath, { encoding: null });
return (buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF);
}, function(filepath) {
return "File " + filepath + " contains a UTF-8 BOM.";
});
});
grunt.registerTask("check-packagejson-mains", function() {
validatePackageJsons(function (filepath) {
var packageData = grunt.file.readJSON(filepath);
return /\.js/i.test(packageData.main || "");
}, function(filepath) {
return "File " + filepath + " contains a broken main setting.";
});
}); });
grunt.registerTask("generate-tns-core-modules-dev-dts", generateModulesDts.bind(null, ".", localCfg.srcTnsCoreModules)); grunt.registerTask("generate-tns-core-modules-dev-dts", generateModulesDts.bind(null, ".", localCfg.srcTnsCoreModules));
@ -475,6 +488,7 @@ module.exports = function(grunt) {
"clean:build", "clean:build",
"shell:getGitSHA", "shell:getGitSHA",
"check-packagejson-boms", "check-packagejson-boms",
"check-packagejson-mains",
"compile-ts", "compile-ts",
"collect-modules-raw-files", "collect-modules-raw-files",
"copy:definitionFiles", "copy:definitionFiles",