Skip to content

Commit

Permalink
fix(core): disable js code analysis when typescript is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 22, 2023
1 parent fa6e8ea commit 3f34a67
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ export function buildExplicitTypescriptAndPackageJsonDependencies(
filesToProcess: ProjectFileMap
) {
let res: ExplicitDependency[] = [];

let typescriptExists = false;

try {
require.resolve('typescript');
typescriptExists = true;
} catch {}
if (
jsPluginConfig.analyzeSourceFiles === undefined ||
jsPluginConfig.analyzeSourceFiles === true
typescriptExists &&
(jsPluginConfig.analyzeSourceFiles === undefined ||
jsPluginConfig.analyzeSourceFiles === true)
) {
res = res.concat(
buildExplicitTypeScriptDependencies(projectGraph, filesToProcess)
Expand Down
22 changes: 13 additions & 9 deletions packages/vite/src/utils/vite-config-edit-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { applyChangesToString, ChangeType, Tree } from '@nrwl/devkit';
import { findNodes } from 'nx/src/utils/typescript';
import ts = require('typescript');
import { tsquery } from '@phenomnomnominal/tsquery';
import { TargetFlags } from './generator-utils';
import type { Node, ReturnStatement } from 'typescript';

export function ensureViteConfigIsCorrect(
tree: Tree,
Expand Down Expand Up @@ -66,6 +65,7 @@ function handleBuildOrTestNode(
configContentObject: {},
name: 'build' | 'test'
): string | undefined {
const { tsquery } = require('@phenomnomnominal/tsquery');
const buildNode = tsquery.query(
updatedFileContent,
`PropertyAssignment:has(Identifier[name="${name}"])`
Expand All @@ -75,7 +75,7 @@ function handleBuildOrTestNode(
return tsquery.replace(
updatedFileContent,
`PropertyAssignment:has(Identifier[name="${name}"])`,
(node: ts.Node) => {
(node: Node) => {
const found = tsquery.query(node, 'ObjectLiteralExpression');
return `${name}: {
...${found?.[0].getText()},
Expand Down Expand Up @@ -159,13 +159,14 @@ function handleBuildOrTestNode(

function transformCurrentBuildObject(
index: number,
returnStatements: ts.ReturnStatement[],
returnStatements: ReturnStatement[],
appFileContent: string,
buildConfigObject: {}
): string | undefined {
if (!returnStatements?.[index]) {
return undefined;
}
const { tsquery } = require('@phenomnomnominal/tsquery');
const currentBuildObject = tsquery
.query(returnStatements[index], 'ObjectLiteralExpression')?.[0]
.getText();
Expand All @@ -176,7 +177,7 @@ function transformCurrentBuildObject(
const newReturnObject = tsquery.replace(
returnStatements[index].getText(),
'ObjectLiteralExpression',
(_node: ts.Node) => {
(_node: Node) => {
return `{
...${currentBuildObject},
...${JSON.stringify(buildConfigObject)}
Expand All @@ -201,10 +202,11 @@ function transformCurrentBuildObject(
}

function transformConditionalConfig(
conditionalConfig: ts.Node[],
conditionalConfig: Node[],
appFileContent: string,
buildConfigObject: {}
): string | undefined {
const { tsquery } = require('@phenomnomnominal/tsquery');
const functionBlock = tsquery.query(conditionalConfig[0], 'Block');

const ifStatement = tsquery.query(functionBlock?.[0], 'IfStatement');
Expand All @@ -225,9 +227,9 @@ function transformConditionalConfig(

const elseKeywordExists = findNodes(
ifStatement?.[0],
ts.SyntaxKind.ElseKeyword
require('typescript').SyntaxKind.ElseKeyword
);
const returnStatements: ts.ReturnStatement[] = tsquery.query(
const returnStatements: ReturnStatement[] = tsquery.query(
ifStatement[0],
'ReturnStatement'
);
Expand Down Expand Up @@ -282,6 +284,7 @@ function handlePluginNode(
dtsImportLine: string,
pluginOption: string
): string | undefined {
const { tsquery } = require('@phenomnomnominal/tsquery');
const file = tsquery.ast(appFileContent);
const pluginsNode = tsquery.query(
file,
Expand All @@ -294,7 +297,7 @@ function handlePluginNode(
appFileContent = tsquery.replace(
file.getText(),
'PropertyAssignment:has(Identifier[name="plugins"])',
(node: ts.Node) => {
(node: Node) => {
const found = tsquery.query(node, 'ArrayLiteralExpression');
return `plugins: [
...${found?.[0].getText()},
Expand Down Expand Up @@ -382,6 +385,7 @@ function handlePluginNode(
}

function handleCacheDirNode(appFileContent: string, cacheDir: string): string {
const { tsquery } = require('@phenomnomnominal/tsquery');
const file = tsquery.ast(appFileContent);
const cacheDirNode = tsquery.query(
file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Object {
"@nrwl/workspace": "0.0.1",
"nx": "0.0.1",
"prettier": "^2.6.2",
"typescript": "~4.9.5",
},
"license": "MIT",
"name": "my-workspace",
Expand All @@ -26,7 +25,6 @@ Object {
"@nrwl/workspace": "0.0.1",
"nx": "0.0.1",
"prettier": "^2.6.2",
"typescript": "~4.9.5",
},
"license": "MIT",
"name": "my-workspace",
Expand All @@ -44,7 +42,6 @@ Object {
"@nrwl/workspace": "0.0.1",
"nx": "0.0.1",
"prettier": "^2.6.2",
"typescript": "~4.9.5",
},
"license": "MIT",
"name": "my-workspace",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"devDependencies": {
"nx": "<%= nxVersion %>",
"@nrwl/workspace": "<%= nxVersion %>",
"typescript": "<%= typescriptVersion %>",
"prettier": "<%= prettierVersion %>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"devDependencies": {
"@nrwl/workspace": "<%= nxVersion %>",
"nx": "<%= nxVersion %>",
"prettier": "<%= prettierVersion %>",
"typescript": "<%= typescriptVersion %>"
"prettier": "<%= prettierVersion %>"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
angularCliVersion,
nxVersion,
prettierVersion,
typescriptVersion,
} from '../../utils/versions';
import { join, join as pathJoin } from 'path';
import { Preset } from '../utils/presets';
Expand Down Expand Up @@ -147,7 +146,6 @@ function createFiles(tree: Tree, options: NormalizedSchema) {
tmpl: '',
cliCommand: options.preset === Preset.AngularMonorepo ? 'ng' : 'nx',
nxCli: false,
typescriptVersion,
prettierVersion,
// angular cli is used only when workspace schematics is added to angular cli
angularCliVersion,
Expand Down

0 comments on commit 3f34a67

Please sign in to comment.