Skip to content

Commit

Permalink
Merge master into ts-migration branch
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrsKondratjevs committed Sep 7, 2022
2 parents 7e770c0 + 678141e commit aea6e5c
Show file tree
Hide file tree
Showing 14 changed files with 1,489 additions and 1,522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"homepage": "https://github.com/scandipwa/scandipwa/tree/master/build-packages/scandipwa-development-toolkit-vscode",
"scripts": {
"vscode:prepublish": "npm ci && npm run compile",
"compile": "rm -rf out && tsc --sourceMap false -p ./",
"compile": "rm -rf out && tsc --sourceMap false -p ./tsconfig.json",
"lint": "eslint src --ext ts",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"test": "node ./out/test/runTest.js"
},
"engines": {
"vscode": "^1.63.1"
"vscode": "^1.45.0"
},
"categories": [
"Other"
Expand Down Expand Up @@ -121,7 +121,7 @@
"@types/glob": "^7.1.1",
"@types/mocha": "^7.0.2",
"@types/node": "^14.14.35",
"@types/vscode": "^1.63.1",
"@types/vscode": "^1.45.0",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^6.8.0",
Expand Down
4 changes: 2 additions & 2 deletions build-packages/ts-server-plugin/build-config/before.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ module.exports = () => {
...readJsonConfig(pathToConfig),
// vvv Enables custom TS server
'typescript.tsdk': getTsSdkPath(),
'typescript.enablePromptUseWorkspaceTsdk': true,
'typescript.enablePromptUseWorkspaceTsdk': true
// vvv Required for inlineHints to work
...INLINE_HINTS_CONFIG
// ...INLINE_HINTS_CONFIG
};

fs.writeFileSync(
Expand Down
2 changes: 1 addition & 1 deletion build-packages/ts-server-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scandipwa/ts-server-plugin",
"version": "0.0.0",
"version": "0.0.2",
"license": "OSL-3.0",
"main": "./out",
"publishConfig": {
Expand Down
56 changes: 16 additions & 40 deletions build-packages/ts-server-plugin/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ type FileToNamespaceMap = Record<string, Array<NamespaceReference | NamespaceDec
export class Cache {
ctx: Ctx;

mosaicSourceFiles: ts.SourceFile[] | undefined;

mosaicProgram: ts.Program | undefined;

hasCachedAll = false;

declarationMap: DeclarationCacheMap = {};
Expand All @@ -40,10 +36,13 @@ export class Cache {

getProgramSourceFiles(): readonly ts.SourceFile[] {
const program = this.ctx.info.project.getLanguageService().getProgram();

if (!program) {
return [];
}

const sourceFiles = program.getSourceFiles();

if (!sourceFiles) {
return [];
}
Expand All @@ -66,47 +65,18 @@ export class Cache {
return sourceFile;
}

this.ctx.info.project.projectService.logger.info(
`File missing in program: ${fileName}`
);

// vvv Add file as new source file
this.ctx.info.project.addMissingFileRoot(fileName as ts.server.NormalizedPath);

return this.getSourceFileByPath(fileName, tryCount + 1);
}

getMosaicSourceFiles(): ts.SourceFile[] {
if (this.mosaicSourceFiles) {
return this.mosaicSourceFiles;
}

const program = this.ctx.info.project.getLanguageService().getProgram();
if (!program) {
return [];
}

const themeFiles = getAllThemeFiles(program.getCurrentDirectory()).sort(
(a, b) => (a.length - b.length)
// ^^^ Sort by length to put index files first
);

const moreSourceFiles = themeFiles.reduce((acc, fileName) => {
const sourceFile = this.getSourceFileByPath(fileName);

if (!sourceFile) {
return acc;
}

return [...acc, sourceFile];
}, [] as ts.SourceFile[]);

this.mosaicSourceFiles = moreSourceFiles;

return this.mosaicSourceFiles;
}

getAllSourceFiles(): ts.SourceFile[] {
return [
...this.getMosaicSourceFiles(),
...this.getProgramSourceFiles()
];
return Array.from(this.getProgramSourceFiles());
}

cacheAllFiles(): void {
Expand Down Expand Up @@ -215,8 +185,14 @@ export class Cache {
}

refreshFileCache(fileName: string): void {
const sourceFiles = this.getAllSourceFiles();
const sourceFile = sourceFiles.find((s) => s.fileName === fileName);
const program = this.ctx.info.project.getLanguageService().getProgram();

if (!program) {
return;
}

const sourceFile = program.getSourceFile(fileName);

if (!sourceFile) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions build-packages/ts-server-plugin/src/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export const handleReferenceCompletions = (
// ^^^ Get one before
);

// TODO: make these work better (completion does not always work)

if (
!completionNode
|| !completionNode.parent
Expand Down
22 changes: 20 additions & 2 deletions build-packages/ts-server-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
pluginNodeReferenceEntries
} from './reference';
import { Ctx } from './util/context';
import { getAllExtensionsFiles } from './util/extension';
import { getAllThemeFiles } from './util/parent-theme';

function init() {
function init(): ts.server.PluginModule {
function create(info: ts.server.PluginCreateInfo) {
const ctx = new Ctx(info);
const cache = new Cache(ctx);
Expand Down Expand Up @@ -116,7 +118,23 @@ function init() {
return proxy;
}

return { create };
function getExternalFiles(project: ts.server.Project): string[] {
const themeFiles = getAllThemeFiles(project.getCurrentDirectory()).sort(
(a, b) => (a.length - b.length)
// ^^^ Sort by length to put index files first
);

const extensionFiles = getAllExtensionsFiles(project.getCurrentDirectory());

const allFiles = [
...themeFiles,
...extensionFiles
];

return allFiles;
}

return { create, getExternalFiles };
}

export = init;
26 changes: 26 additions & 0 deletions build-packages/ts-server-plugin/src/util/all-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as fs from 'fs';
import * as path from 'path';

export const getAllFilesFromPath = (pathname: string): string[] => {
const files = fs.readdirSync(pathname);

return files.reduce((acc, file) => {
if (/node_modules/gm.test(file)) {
// ^^^ ignore node_modules
return acc;
}

const filePath = path.join(pathname, file);

if (!fs.statSync(filePath).isDirectory()) {
if (!(/\.[tj]sx?$/gm.test(file))) {
// ^^^ Ignore non-js files
return acc;
}

return [...acc, filePath];
}

return [...acc, ...getAllFilesFromPath(filePath)];
}, [] as string[]);
};
51 changes: 51 additions & 0 deletions build-packages/ts-server-plugin/src/util/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getMosaicConfig } from '@tilework/mosaic-dev-utils/mosaic-config';
import { getPackageJson } from '@tilework/mosaic-dev-utils/package-json';

import { getAllFilesFromPath } from './all-files';
import { getPackagePath } from './package-path';

let visitedDeps: string[] = [];

export const getAllExtensions = (pathname: string = process.cwd(), _isFirst = true): string[] => {
if (_isFirst) {
visitedDeps = [];
}

if (visitedDeps.indexOf(pathname) !== -1) {
return [];
}

visitedDeps.push(pathname);

try {
const packageJson = getPackageJson(pathname);
const { dependencies = {} } = packageJson;
const { extensions = {} } = getMosaicConfig(pathname);
const extensionFolders = Object.keys(extensions).map(
(packageName) => getPackagePath(packageName)
);

return Array.from(
Object.keys(dependencies).reduce(
(acc, packageName: string) => {
const packagePath = getPackagePath(packageName);
const childExtensions = getAllExtensions(packagePath, false);
childExtensions.forEach((ext) => acc.add(ext));
return acc;
},
new Set(extensionFolders)
)
);
} catch (e) {
return [];
}
};

export const getAllExtensionsFiles = (sourcePath: string): string[] => {
const extensions = getAllExtensions(sourcePath);

return extensions.reduce((acc, extension) => ([
...acc,
...getAllFilesFromPath(extension)
]), [] as string[]);
};
22 changes: 22 additions & 0 deletions build-packages/ts-server-plugin/src/util/package-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as fs from 'fs';
import * as path from 'path';

export const getPackagePath = (packageName: string, context: string = process.cwd()): string => {
const possibleRelativePath = path.join(
process.cwd(),
packageName,
'package.json'
);

const isPathReference = fs.existsSync(possibleRelativePath);

if (isPathReference) {
return path.join(possibleRelativePath, '..');
}

// This is not a local package, path based extension -> try loading it as a package
return path.join(
require.resolve(`${ packageName }/package.json`, { paths: [context] }),
'..'
);
};
49 changes: 3 additions & 46 deletions build-packages/ts-server-plugin/src/util/parent-theme.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import { getParentTheme } from '@tilework/mosaic-dev-utils/parent-theme';
import * as fs from 'fs';
import * as path from 'path';

const getPackagePath = (packageName: string, context: string = process.cwd()): string => {
const possibleRelativePath = path.join(
process.cwd(),
packageName,
'package.json'
);
import { getAllFilesFromPath } from './all-files';
import { getPackagePath } from './package-path';

const isPathReference = fs.existsSync(possibleRelativePath);

if (isPathReference) {
return path.join(possibleRelativePath, '..');
}

// This is not a local package, path based extension -> try loading it as a package
return path.join(
require.resolve(`${ packageName }/package.json`, { paths: [context] }),
'..'
);
};

const getParentThemes = (pathname: string = process.cwd(), rootTheme: string = pathname): string[] => {
export const getParentThemes = (pathname: string = process.cwd(), rootTheme: string = pathname): string[] => {
const parentThemePackage = getParentTheme(pathname);

if (!parentThemePackage) {
Expand All @@ -37,30 +18,6 @@ const getParentThemes = (pathname: string = process.cwd(), rootTheme: string = p
];
};

const getAllFilesFromPath = (pathname: string): string[] => {
const files = fs.readdirSync(pathname);

return files.reduce((acc, file) => {
if (/node_modules/gm.test(file)) {
// ^^^ ignore node_modules
return acc;
}

const filePath = path.join(pathname, file);

if (!fs.statSync(filePath).isDirectory()) {
if (!(/\.[tj]sx?$/gm.test(file))) {
// ^^^ Ignore non-js files
return acc;
}

return [...acc, filePath];
}

return [...acc, ...getAllFilesFromPath(filePath)];
}, [] as string[]);
};

export const getAllThemeFiles = (sourcePath: string): string[] => {
const themes = [
sourcePath,
Expand Down
2 changes: 1 addition & 1 deletion packages/scandipwa/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@scandipwa/scandipwa",
"description": "ScandiPWA source code used by CSA.",
"version": "5.3.3",
"version": "5.3.4",
"scripts": {
"start": "scandipwa-scripts start",
"start:magento": "BUILD_MODE=magento scandipwa-scripts start",
Expand Down
4 changes: 3 additions & 1 deletion packages/scandipwa/src/util/Request/QueryDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export abstract class QueryDispatcher<Options, Data, Error = NetworkError | Netw
}
} catch (err) {
if (!(err as NetworkError).message.includes('abort')) {
this.onError(err as NetworkError, dispatch, options);
if (!(err as NetworkError).message.includes('abort')) {
this.onError(err as NetworkError, dispatch, options);
}
}
}
const broadcast = await listenForBroadCast<Data>(name);
Expand Down
Loading

0 comments on commit aea6e5c

Please sign in to comment.