forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.js
More file actions
1 lines (1 loc) · 3.76 KB
/
Copy pathbootstrap.js
File metadata and controls
1 lines (1 loc) · 3.76 KB
1
var tern=require("./tern"),fs=require("fs"),path=require("path"),url=require("url"),glob=require("glob"),minimatch=require("minimatch"),resolveFrom=require("resolve-from"),projectFileName=".tern-project",portFileName=".tern-port";function findProjectDir(){for(var dir=process.cwd();;){try{if(fs.statSync(path.resolve(dir,projectFileName)).isFile())return dir}catch(e){}var shorter=path.dirname(dir);if(shorter==dir)return null;dir=shorter}}var defaultConfig={libs:[],loadEagerly:!1,plugins:{doc_comment:!0},ecmaScript:!0,ecmaVersion:9,dependencyBudget:tern.defaultOptions.dependencyBudget},homeDir=process.env.HOME||process.env.USERPROFILE;function readJSON(fileName){var file=fs.readFileSync(fileName,"utf8");try{return JSON.parse(file)}catch(e){console.error("Bad JSON in "+fileName+": "+e.message),process.exit(1)}}function readProjectFile(fileName){var data=readJSON(fileName),name;for(var option in defaultConfig)if(data.hasOwnProperty(option)){if("plugins"==option)for(name in defaultConfig.plugins)Object.prototype.hasOwnProperty.call(data.plugins,name)||(data.plugins[name]=defaultConfig.plugins[name])}else data[option]=defaultConfig[option];return data}function findFile(file,projectDir,fallbackDir,options){var local=path.resolve(projectDir,file);if(!options.disableLoadingLocal&&fs.existsSync(local))return local;var shared=path.resolve(fallbackDir,file);return fs.existsSync(shared)?shared:void 0}homeDir&&fs.existsSync(path.resolve(homeDir,".tern-config"))&&(defaultConfig=readProjectFile(path.resolve(homeDir,".tern-config")));var distDir=path.resolve(__dirname,"..");function findDefs(projectDir,config,options){var defs=[],src=config.libs.slice();config.ecmaScript&&-1==src.indexOf("ecmascript")&&src.unshift("ecmascript");for(var i=0;i<src.length;++i){var file=src[i];/\.json$/.test(file)||(file+=".json");var found=findFile(file,projectDir,path.resolve(distDir,"defs"),options)||resolveFrom(projectDir,"tern-"+src[i]);if(!found)try{found=require.resolve("tern-"+src[i])}catch(e){process.stderr.write("Failed to find library "+src[i]+".\n");continue}found&&defs.push(readJSON(found))}return defs}function loadPlugins(projectDir,config,options){var plugins=config.plugins,options={};for(var plugin in plugins){var val=plugins[plugin];if(val){var found=findFile(plugin+".js",projectDir,path.resolve(distDir,"plugin"),options)||resolveFrom(projectDir,"tern-"+plugin);if(!found)try{found=require.resolve("tern-"+plugin)}catch(e){process.stderr.write("Failed to find plugin "+plugin+".\n");continue}var mod=require(found);mod.hasOwnProperty("initialize")&&mod.initialize(distDir),options[path.basename(plugin)]=val}}return options}function startServer(dir,config,options){var defs,plugins,ternConfig={getFile:function(name,c){config.dontLoad&&config.dontLoad.some(function(pat){return minimatch(name,pat)})?c(null,""):fs.readFile(path.resolve(dir,name),"utf8",c)},normalizeFilename:function(name){var pt=path.resolve(dir,name);try{pt=fs.realPathSync(path.resolve(dir,name),!0)}catch(e){}return path.relative(dir,pt)},async:!0,defs:findDefs(dir,config,options),plugins:loadPlugins(dir,config,options),projectDir:dir,ecmaVersion:config.ecmaVersion,dependencyBudget:config.dependencyBudget};if(options.tern)for(var option in options.tern)options.tern.hasOwnProperty(option)&&(ternConfig[option]=options.tern[option]);var server=new tern.Server(ternConfig);return config.loadEagerly&&config.loadEagerly.forEach(function(pat){glob.sync(pat,{cwd:dir}).forEach(function(file){server.addFile(file)})}),server.flush(function(){}),server}module.exports=function bootstrapServer(options){var projectDir=options.projectDir;if(projectDir||(projectDir=findProjectDir()),projectDir)var config=readProjectFile(path.resolve(projectDir,projectFileName));else{projectDir=process.cwd();var config=defaultConfig}return startServer(projectDir,config,options)};