Skip to content

Commit

Permalink
Fix vls initialize is slow
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Nov 11, 2020
1 parent e5e9083 commit 280d7b5
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions server/src/services/dependencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,47 @@ import { performance } from 'perf_hooks';
import { logger } from '../log';

const readFileAsync = util.promisify(fs.readFile);
const existsFileAsync = util.promisify(fs.existsSync);

async function findAllPackages(workspacePath: string, moduleName: string) {
async function createNodeModulesPaths(workspacePath: string) {
const startTime = performance.now();
const packages = await fg(`**/node_modules/${moduleName}/package.json`, {
const nodeModules = await fg('**/node_modules', {
cwd: workspacePath,
absolute: true,
unique: true
}).then(filePaths =>
Promise.all(
filePaths.map(filePath =>
readFileAsync(filePath, { encoding: 'utf8' }).then(content => {
const info = JSON.parse(content) as { name: string; version: string; main: string };

return {
name: info.name,
dir: path.dirname(filePath),
version: info.version,
module: require(path.resolve(path.dirname(filePath), info.main))
};
})
)
)
);
logger.logInfo(`Try to find ${moduleName} in ${workspacePath}. - ${Math.round(performance.now() - startTime)}ms`);
unique: true,
onlyDirectories: true,
onlyFiles: false,
deep: 10
});

logger.logInfo(`Find node_modules paths in ${workspacePath} - ${Math.round(performance.now() - startTime)}ms`);
return nodeModules;
}

async function findAllPackages(nodeModulesPaths: string[], moduleName: string) {
async function getPackage(nodeModulesPath: string) {
const packageJSONPath = path.resolve(nodeModulesPath, moduleName, 'package.json');
if (!(await existsFileAsync(packageJSONPath))) {
return null;
}

const info: { name: string; version: string; main: string } = JSON.parse(
await readFileAsync(packageJSONPath, { encoding: 'utf8' })
);
return {
name: info.name,
dir: path.dirname(packageJSONPath),
version: info.version,
module: require(path.resolve(path.dirname(packageJSONPath), info.main))
};
}

const packages = (await Promise.all(nodeModulesPaths.map(path => getPackage(path)))).filter(info => info) as Array<{
name: string;
dir: string;
version: string;
module: unknown;
}>;

return packages;
}
Expand Down Expand Up @@ -83,6 +100,8 @@ export const createDependencyService = () => {
let loaded: { [K in keyof RuntimeLibrary]: Dependency<RuntimeLibrary[K]>[] };

async function init(workspacePath: string, useWorkspaceDependencies: boolean, tsSDKPath?: string) {
const nodeModulesPaths = useWorkspaceDependencies ? await createNodeModulesPaths(workspacePath) : [];

const loadTypeScript = async (): Promise<Dependency<typeof ts>[]> => {
try {
if (useWorkspaceDependencies && tsSDKPath) {
Expand All @@ -103,7 +122,7 @@ export const createDependencyService = () => {
}

if (useWorkspaceDependencies) {
const packages = await findAllPackages(workspacePath, 'typescript');
const packages = await findAllPackages(nodeModulesPaths, 'typescript');
if (packages.length === 0) {
throw new Error(`No find any packages in ${workspacePath}.`);
}
Expand Down Expand Up @@ -140,7 +159,7 @@ export const createDependencyService = () => {
const loadCommonDep = async <N extends string, BM>(name: N, bundleModule: BM): Promise<Dependency<BM>[]> => {
try {
if (useWorkspaceDependencies) {
const packages = await findAllPackages(workspacePath, name);
const packages = await findAllPackages(nodeModulesPaths, name);
if (packages.length === 0) {
throw new Error(`No find ${name} packages in ${workspacePath}.`);
}
Expand Down

0 comments on commit 280d7b5

Please sign in to comment.