Skip to content

Commit

Permalink
feat(core): add nx.json, workspace.json, and project.json JSON schemas
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #8574, #2299
  • Loading branch information
nartc committed May 10, 2022
1 parent 6025ce5 commit 70224cd
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 3 deletions.
127 changes: 127 additions & 0 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://nx.dev/core-concepts/configuration#nxjson",
"title": "JSON schema for Nx configuration",
"type": "object",
"properties": {
"implicitDependencies": {
"type": "object",
"description": "Map of files to projects that implicitly depend on them."
},
"affected": {
"type": "object",
"description": "Default options for `nx affected`.",
"properties": {
"defaultBase": {
"type": "string",
"description": "Default based branch used by affected commands."
}
},
"additionalProperties": false
},
"npmScope": {
"type": "string",
"description": "NPM Scope that the workspace uses."
},
"tasksRunnerOptions": {
"additionalProperties": {
"$ref": "#/definitions/tasksRunnerOptions"
}
},
"targetDependencies": {
"type": "object",
"description": "Dependencies between different target names across all projects.",
"additionalProperties": {
"$ref": "#/definitions/targetDependencyConfig"
}
},
"workspaceLayout": {
"type": "object",
"description": "Where new apps + libs should be placed.",
"properties": {
"libsDir": {
"type": "string",
"description": "Default folder name for libs."
},
"appsDir": {
"type": "string",
"description": "Default folder name for apps."
}
},
"additionalProperties": false
},
"cli": {
"$ref": "#/definitions/cliOptions"
},
"generators": {
"$ref": "#/definitions/generatorOptions"
},
"plugins": {
"type": "array",
"description": "Plugins for extending the project graph.",
"items": {
"type": "string"
}
},
"defaultProject": {
"type": "string",
"description": "Default project. When project isn't provided, the default project will be used."
}
},
"definitions": {
"cliOptions": {
"type": "object",
"description": "Default generator collection.",
"properties": {
"packageManager": {
"type": "string",
"description": "The default package manager to use.",
"enum": ["yarn", "pnpm", "npm"]
},
"defaultCollection": {
"type": "string",
"description": "The default schematics collection to use."
}
},
"additionalProperties": false
},
"generatorOptions": {
"type": "object",
"description": "List of default values used by generators."
},
"tasksRunnerOptions": {
"type": "object",
"description": "Available Task Runners.",
"properties": {
"runner": {
"type": "string",
"description": "Path to resolve the runner."
},
"options": {
"type": "object",
"description": "Default options for the runner."
}
},
"additionalProperties": false
},
"targetDependencyConfig": {
"type": "array",
"description": "Target dependency.",
"items": {
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"target": {
"type": "string",
"description": "The name of the target."
}
},
"additionalProperties": false
}
}
}
}
66 changes: 66 additions & 0 deletions packages/nx/schemas/project-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://nx.dev/project-schema",
"title": "JSON schema for Nx projects",
"type": "object",
"properties": {
"targets": {
"description": "Configures all the targets which define what tasks you can run against the project",
"additionalProperties": {
"type": "object",
"properties": {
"executor": {
"description": "The function that Nx will invoke when you run this target",
"type": "string"
},
"options": {
"type": "object"
},
"outputs": {
"type": "array",
"items": {
"type": "string"
}
},
"configurations": {
"description": "provides extra sets of values that will be merged into the options map",
"additionalProperties": {
"type": "object"
}
},
"dependsOn": {
"type": "array",
"description": "Target dependency.",
"items": {
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"target": {
"type": "string",
"description": "The name of the target."
}
},
"additionalProperties": false
}
}
}
}
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"implicitDependencies": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
136 changes: 136 additions & 0 deletions packages/nx/schemas/workspace-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://nx.dev",
"title": "JSON schema for Nx workspaces",
"type": "object",
"properties": {
"version": {
"type": "number",
"enum": [1, 2]
}
},
"allOf": [
{
"if": {
"properties": { "version": { "const": 1 } },
"required": ["version"]
},
"then": {
"properties": {
"projects": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"architect": {
"description": "Configures all the targets which define what tasks you can run against the project",
"additionalProperties": {
"type": "object",
"properties": {
"builder": {
"description": "The function that Nx will invoke when you run this architect",
"type": "string"
},
"options": {
"type": "object"
},
"configurations": {
"description": "provides extra sets of values that will be merged into the options map",
"additionalProperties": {
"type": "object"
}
}
}
}
}
}
}
}
}
}
},
{
"if": {
"properties": { "version": { "const": 2 } },
"required": ["version"]
},
"then": {
"properties": {
"projects": {
"type": "object",
"additionalProperties": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"targets": {
"description": "Configures all the targets which define what tasks you can run against the project",
"additionalProperties": {
"type": "object",
"properties": {
"executor": {
"description": "The function that Nx will invoke when you run this target",
"type": "string"
},
"options": {
"type": "object"
},
"outputs": {
"type": "array",
"items": {
"type": "string"
}
},
"configurations": {
"description": "provides extra sets of values that will be merged into the options map",
"additionalProperties": {
"type": "object"
}
},
"dependsOn": {
"type": "array",
"description": "Target dependency.",
"items": {
"type": "object",
"properties": {
"projects": {
"type": "string",
"description": "The projects that the targets belong to.",
"enum": ["self", "dependencies"]
},
"target": {
"type": "string",
"description": "The name of the target."
}
},
"additionalProperties": false
}
}
}
}
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"implicitDependencies": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
]
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ describe('project configuration', () => {
expect(configurations.get('test')).toEqual(baseTestProjectConfigV2);
expect(configurations.get('test2')).toEqual(baseTestProjectConfigV2);
});

it('should have JSON $schema in project configuration for standalone projects', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfigV2, true);
const projectJson = readJson(tree, 'libs/test/project.json');
expect(projectJson['$schema']).toBeTruthy();
expect(projectJson['$schema']).toEqual(
'../../node_modules/nx/schemas/project-schema.json'
);
});
});

describe('updateWorkspaceConfiguration', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
reformattedWorkspaceJsonOrNull,
toNewFormat,
} from '../../config/workspaces';
import { basename, dirname, relative } from 'path';
import { basename, dirname, join, relative } from 'path';

import { readJson, updateJson, writeJson } from './json';

Expand Down Expand Up @@ -352,8 +352,17 @@ function addProjectToWorkspaceJson(
if (workspaceConfigPath && mode === 'create') {
workspaceJson.projects[projectName] = project.root;
}

// update the project.json file
writeJson(tree, configFile, { ...project, root: undefined });
const relativeJsonSchemaPath = relative(
join(tree.root, project.root),
join(tree.root, 'node_modules/nx/schemas/project-schema.json')
);
writeJson(tree, configFile, {
$schema: relativeJsonSchemaPath,
...project,
root: undefined,
});
}
} else if (mode === 'delete') {
delete workspaceJson.projects[projectName];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "./node_modules/nx/schemas/workspace-schema.json",
"version": 2,
"projects": {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"npmScope": "<%= npmScope %>",
"affected": {
"defaultBase": "<%= defaultBase %>"
Expand Down

0 comments on commit 70224cd

Please sign in to comment.