-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide json schema for workspace.json (#1077)
* feat: create json schema for workspace file
- Loading branch information
Showing
18 changed files
with
424 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"parserOptions": { | ||
"project": ["libs/vscode/json-schema/tsconfig.*?.json"] | ||
}, | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# vscode-json-schema | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test vscode-json-schema` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
displayName: 'vscode-json-schema', | ||
preset: '../../../jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
transform: { | ||
'^.+\\.[tj]sx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../../coverage/libs/vscode/json-schema', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "@nx-console/vscode/json-schema", | ||
"version": "0.0.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/workspace-json-schema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { clearJsonCache, readAndCacheJsonFile } from '@nx-console/server'; | ||
import { dirname, join } from 'path'; | ||
|
||
export interface ExecutorInfo { | ||
name: string; | ||
path: string; | ||
} | ||
|
||
export function getAllExecutors( | ||
workspaceJsonPath: string, | ||
clearPackageJsonCache: boolean | ||
): ExecutorInfo[] { | ||
return readExecutorCollectionsFromNodeModules( | ||
workspaceJsonPath, | ||
clearPackageJsonCache | ||
); | ||
} | ||
|
||
function readExecutorCollectionsFromNodeModules( | ||
workspaceJsonPath: string, | ||
clearPackageJsonCache: boolean | ||
): ExecutorInfo[] { | ||
const basedir = join(workspaceJsonPath, '..'); | ||
const nodeModulesDir = join(basedir, 'node_modules'); | ||
|
||
if (clearPackageJsonCache) { | ||
clearJsonCache('package.json', basedir); | ||
} | ||
|
||
const packages: { [packageName: string]: string } = | ||
readAndCacheJsonFile('package.json', basedir).json.devDependencies || {}; | ||
const executorCollections = Object.keys(packages).filter((p) => { | ||
try { | ||
const packageJson = readAndCacheJsonFile( | ||
join(p, 'package.json'), | ||
nodeModulesDir | ||
).json; | ||
// TODO: to add support for schematics, we can change this to include schematics/generators | ||
return !!(packageJson.builders || packageJson.executors); | ||
} catch (e) { | ||
if ( | ||
e.message && | ||
(e.message.indexOf('no such file') > -1 || | ||
e.message.indexOf('not a directory') > -1) | ||
) { | ||
return false; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}); | ||
|
||
return executorCollections | ||
.map((c) => readCollections(nodeModulesDir, c)) | ||
.flat() | ||
.filter((c): c is ExecutorInfo => Boolean(c)); | ||
} | ||
|
||
function readCollections( | ||
basedir: string, | ||
collectionName: string | ||
): ExecutorInfo[] | null { | ||
try { | ||
const packageJson = readAndCacheJsonFile( | ||
join(collectionName, 'package.json'), | ||
basedir | ||
); | ||
|
||
const collection = readAndCacheJsonFile( | ||
packageJson.json.builders || packageJson.json.executors, | ||
dirname(packageJson.path) | ||
); | ||
|
||
return getBuilderPaths(collectionName, collection.path, collection.json); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
function getBuilderPaths( | ||
collectionName: string, | ||
path: string, | ||
json: any | ||
): ExecutorInfo[] { | ||
const baseDir = dirname(path); | ||
|
||
const builders: ExecutorInfo[] = []; | ||
for (const [key, value] of Object.entries<any>( | ||
json.builders || json.executors | ||
)) { | ||
builders.push({ | ||
name: `${collectionName}:${key}`, | ||
path: join(baseDir, value.schema), | ||
}); | ||
} | ||
|
||
return builders; | ||
} |
Oops, something went wrong.