@jakubmazanec/carson 0.1.0-11dd4a3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +661 -0
- package/README.md +237 -0
- package/bin/carson.js +3 -0
- package/dist/commands/create-project/CreateProject.d.ts +8 -0
- package/dist/commands/create-project/CreateProject.js +138 -0
- package/dist/commands/create-project/CreateProject.js.map +1 -0
- package/dist/commands/create-project/CreateProjectInput.d.ts +20 -0
- package/dist/commands/create-project/CreateProjectInput.js +31 -0
- package/dist/commands/create-project/CreateProjectInput.js.map +1 -0
- package/dist/commands/create-project/createProjectCommand.d.ts +12 -0
- package/dist/commands/create-project/createProjectCommand.js +31 -0
- package/dist/commands/create-project/createProjectCommand.js.map +1 -0
- package/dist/commands/create-project/runCreateProject.d.ts +13 -0
- package/dist/commands/create-project/runCreateProject.js +34 -0
- package/dist/commands/create-project/runCreateProject.js.map +1 -0
- package/dist/commands/create-project.d.ts +4 -0
- package/dist/commands/create-project.js +6 -0
- package/dist/commands/create-project.js.map +1 -0
- package/dist/commands/create-workspace/CreateWorkspace.d.ts +7 -0
- package/dist/commands/create-workspace/CreateWorkspace.js +102 -0
- package/dist/commands/create-workspace/CreateWorkspace.js.map +1 -0
- package/dist/commands/create-workspace/CreateWorkspaceInput.d.ts +11 -0
- package/dist/commands/create-workspace/CreateWorkspaceInput.js +17 -0
- package/dist/commands/create-workspace/CreateWorkspaceInput.js.map +1 -0
- package/dist/commands/create-workspace/createWorkspaceCommand.d.ts +9 -0
- package/dist/commands/create-workspace/createWorkspaceCommand.js +30 -0
- package/dist/commands/create-workspace/createWorkspaceCommand.js.map +1 -0
- package/dist/commands/create-workspace/runCreateWorkspace.d.ts +10 -0
- package/dist/commands/create-workspace/runCreateWorkspace.js +33 -0
- package/dist/commands/create-workspace/runCreateWorkspace.js.map +1 -0
- package/dist/commands/create-workspace.d.ts +4 -0
- package/dist/commands/create-workspace.js +6 -0
- package/dist/commands/create-workspace.js.map +1 -0
- package/dist/commands/internals/packageNameToDirectory.d.ts +1 -0
- package/dist/commands/internals/packageNameToDirectory.js +12 -0
- package/dist/commands/internals/packageNameToDirectory.js.map +1 -0
- package/dist/commands/internals.d.ts +1 -0
- package/dist/commands/internals.js +3 -0
- package/dist/commands/internals.js.map +1 -0
- package/dist/commands/update-workspace/UpdateWorkspace.d.ts +8 -0
- package/dist/commands/update-workspace/UpdateWorkspace.js +71 -0
- package/dist/commands/update-workspace/UpdateWorkspace.js.map +1 -0
- package/dist/commands/update-workspace/runUpdateWorkspace.d.ts +10 -0
- package/dist/commands/update-workspace/runUpdateWorkspace.js +25 -0
- package/dist/commands/update-workspace/runUpdateWorkspace.js.map +1 -0
- package/dist/commands/update-workspace/updateWorkspaceCommand.d.ts +6 -0
- package/dist/commands/update-workspace/updateWorkspaceCommand.js +25 -0
- package/dist/commands/update-workspace/updateWorkspaceCommand.js.map +1 -0
- package/dist/commands/update-workspace.d.ts +3 -0
- package/dist/commands/update-workspace.js +5 -0
- package/dist/commands/update-workspace.js.map +1 -0
- package/dist/commands.d.ts +3 -0
- package/dist/commands.js +5 -0
- package/dist/commands.js.map +1 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.js +16 -0
- package/dist/constants.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.js +5 -0
- package/dist/main.js.map +1 -0
- package/dist/modules.d.js +2 -0
- package/dist/modules.d.js.map +1 -0
- package/dist/run.d.ts +1 -0
- package/dist/run.js +18 -0
- package/dist/run.js.map +1 -0
- package/dist/template/CarsonTemplate.d.ts +9 -0
- package/dist/template/CarsonTemplate.js +3 -0
- package/dist/template/CarsonTemplate.js.map +1 -0
- package/dist/template/CarsonTemplateConfig.d.ts +12 -0
- package/dist/template/CarsonTemplateConfig.js +7 -0
- package/dist/template/CarsonTemplateConfig.js.map +1 -0
- package/dist/template/TemplateAttributes.d.ts +9 -0
- package/dist/template/TemplateAttributes.js +16 -0
- package/dist/template/TemplateAttributes.js.map +1 -0
- package/dist/template/TemplateData.d.ts +35 -0
- package/dist/template/TemplateData.js +13 -0
- package/dist/template/TemplateData.js.map +1 -0
- package/dist/template/TemplateRender.d.ts +4 -0
- package/dist/template/TemplateRender.js +3 -0
- package/dist/template/TemplateRender.js.map +1 -0
- package/dist/template/TemplateRenderSnapshot.d.ts +27 -0
- package/dist/template/TemplateRenderSnapshot.js +11 -0
- package/dist/template/TemplateRenderSnapshot.js.map +1 -0
- package/dist/template/TemplateRenderSnapshots.d.ts +27 -0
- package/dist/template/TemplateRenderSnapshots.js +4 -0
- package/dist/template/TemplateRenderSnapshots.js.map +1 -0
- package/dist/template/TemplateRenders.d.ts +2 -0
- package/dist/template/TemplateRenders.js +3 -0
- package/dist/template/TemplateRenders.js.map +1 -0
- package/dist/template/applyTemplateRenders.d.ts +8 -0
- package/dist/template/applyTemplateRenders.js +60 -0
- package/dist/template/applyTemplateRenders.js.map +1 -0
- package/dist/template/createFileUsingTemplateRender.d.ts +2 -0
- package/dist/template/createFileUsingTemplateRender.js +12 -0
- package/dist/template/createFileUsingTemplateRender.js.map +1 -0
- package/dist/template/deleteFileUsingTemplateRender.d.ts +2 -0
- package/dist/template/deleteFileUsingTemplateRender.js +10 -0
- package/dist/template/deleteFileUsingTemplateRender.js.map +1 -0
- package/dist/template/importCarsonTemplate.d.ts +3 -0
- package/dist/template/importCarsonTemplate.js +8 -0
- package/dist/template/importCarsonTemplate.js.map +1 -0
- package/dist/template/insertUsingTemplateRender.d.ts +2 -0
- package/dist/template/insertUsingTemplateRender.js +29 -0
- package/dist/template/insertUsingTemplateRender.js.map +1 -0
- package/dist/template/internals/merge.d.ts +4 -0
- package/dist/template/internals/merge.js +30 -0
- package/dist/template/internals/merge.js.map +1 -0
- package/dist/template/internals/prettify.d.ts +1 -0
- package/dist/template/internals/prettify.js +25 -0
- package/dist/template/internals/prettify.js.map +1 -0
- package/dist/template/internals/unmerge.d.ts +4 -0
- package/dist/template/internals/unmerge.js +31 -0
- package/dist/template/internals/unmerge.js.map +1 -0
- package/dist/template/internals.d.ts +3 -0
- package/dist/template/internals.js +5 -0
- package/dist/template/internals.js.map +1 -0
- package/dist/template/loadTemplateRenderSnapshots.d.ts +7 -0
- package/dist/template/loadTemplateRenderSnapshots.js +14 -0
- package/dist/template/loadTemplateRenderSnapshots.js.map +1 -0
- package/dist/template/mergeFileUsingTemplateRender.d.ts +2 -0
- package/dist/template/mergeFileUsingTemplateRender.js +39 -0
- package/dist/template/mergeFileUsingTemplateRender.js.map +1 -0
- package/dist/template/overwriteFileUsingTemplateRender.d.ts +2 -0
- package/dist/template/overwriteFileUsingTemplateRender.js +12 -0
- package/dist/template/overwriteFileUsingTemplateRender.js.map +1 -0
- package/dist/template/parseTemplateId.d.ts +1 -0
- package/dist/template/parseTemplateId.js +12 -0
- package/dist/template/parseTemplateId.js.map +1 -0
- package/dist/template/readCarsonTemplate.d.ts +2 -0
- package/dist/template/readCarsonTemplate.js +39 -0
- package/dist/template/readCarsonTemplate.js.map +1 -0
- package/dist/template/renderTemplate.d.ts +7 -0
- package/dist/template/renderTemplate.js +19 -0
- package/dist/template/renderTemplate.js.map +1 -0
- package/dist/template/resolveTemplateModule.d.ts +1 -0
- package/dist/template/resolveTemplateModule.js +9 -0
- package/dist/template/resolveTemplateModule.js.map +1 -0
- package/dist/template/saveTemplateRenderSnapshots.d.ts +2 -0
- package/dist/template/saveTemplateRenderSnapshots.js +23 -0
- package/dist/template/saveTemplateRenderSnapshots.js.map +1 -0
- package/dist/template/uninsertUsingTemplateRender.d.ts +2 -0
- package/dist/template/uninsertUsingTemplateRender.js +29 -0
- package/dist/template/uninsertUsingTemplateRender.js.map +1 -0
- package/dist/template/unmergeFileUsingTemplateRender.d.ts +2 -0
- package/dist/template/unmergeFileUsingTemplateRender.js +39 -0
- package/dist/template/unmergeFileUsingTemplateRender.js.map +1 -0
- package/dist/template.d.ts +20 -0
- package/dist/template.js +22 -0
- package/dist/template.js.map +1 -0
- package/dist/workspace/Project.d.ts +33 -0
- package/dist/workspace/Project.js +184 -0
- package/dist/workspace/Project.js.map +1 -0
- package/dist/workspace/ProjectConfig.d.ts +9 -0
- package/dist/workspace/ProjectConfig.js +6 -0
- package/dist/workspace/ProjectConfig.js.map +1 -0
- package/dist/workspace/ProjectCreateOptions.d.ts +8 -0
- package/dist/workspace/ProjectCreateOptions.js +3 -0
- package/dist/workspace/ProjectCreateOptions.js.map +1 -0
- package/dist/workspace/ProjectError.d.ts +8 -0
- package/dist/workspace/ProjectError.js +11 -0
- package/dist/workspace/ProjectError.js.map +1 -0
- package/dist/workspace/ProjectOptions.d.ts +15 -0
- package/dist/workspace/ProjectOptions.js +3 -0
- package/dist/workspace/ProjectOptions.js.map +1 -0
- package/dist/workspace/ProjectUpdateOptions.d.ts +3 -0
- package/dist/workspace/ProjectUpdateOptions.js +3 -0
- package/dist/workspace/ProjectUpdateOptions.js.map +1 -0
- package/dist/workspace/Workspace.d.ts +68 -0
- package/dist/workspace/Workspace.js +426 -0
- package/dist/workspace/Workspace.js.map +1 -0
- package/dist/workspace/WorkspaceConfig.d.ts +9 -0
- package/dist/workspace/WorkspaceConfig.js +6 -0
- package/dist/workspace/WorkspaceConfig.js.map +1 -0
- package/dist/workspace/WorkspaceCreateOptions.d.ts +5 -0
- package/dist/workspace/WorkspaceCreateOptions.js +3 -0
- package/dist/workspace/WorkspaceCreateOptions.js.map +1 -0
- package/dist/workspace/WorkspaceCreateProjectOptions.d.ts +6 -0
- package/dist/workspace/WorkspaceCreateProjectOptions.js +3 -0
- package/dist/workspace/WorkspaceCreateProjectOptions.js.map +1 -0
- package/dist/workspace/WorkspaceDependencies.d.ts +30 -0
- package/dist/workspace/WorkspaceDependencies.js +126 -0
- package/dist/workspace/WorkspaceDependencies.js.map +1 -0
- package/dist/workspace/WorkspaceError.d.ts +8 -0
- package/dist/workspace/WorkspaceError.js +13 -0
- package/dist/workspace/WorkspaceError.js.map +1 -0
- package/dist/workspace/WorkspaceOptions.d.ts +25 -0
- package/dist/workspace/WorkspaceOptions.js +3 -0
- package/dist/workspace/WorkspaceOptions.js.map +1 -0
- package/dist/workspace/WorkspaceOptionsProjectOptions.d.ts +7 -0
- package/dist/workspace/WorkspaceOptionsProjectOptions.js +3 -0
- package/dist/workspace/WorkspaceOptionsProjectOptions.js.map +1 -0
- package/dist/workspace/WorkspacePackageJson.d.ts +2 -0
- package/dist/workspace/WorkspacePackageJson.js +3 -0
- package/dist/workspace/WorkspacePackageJson.js.map +1 -0
- package/dist/workspace/WorkspaceProjectGlobs.d.ts +1 -0
- package/dist/workspace/WorkspaceProjectGlobs.js +3 -0
- package/dist/workspace/WorkspaceProjectGlobs.js.map +1 -0
- package/dist/workspace/WorkspaceProjects.d.ts +2 -0
- package/dist/workspace/WorkspaceProjects.js +3 -0
- package/dist/workspace/WorkspaceProjects.js.map +1 -0
- package/dist/workspace/WorkspaceRepository.d.ts +4 -0
- package/dist/workspace/WorkspaceRepository.js +3 -0
- package/dist/workspace/WorkspaceRepository.js.map +1 -0
- package/dist/workspace/WorkspaceUpdateOptions.d.ts +3 -0
- package/dist/workspace/WorkspaceUpdateOptions.js +3 -0
- package/dist/workspace/WorkspaceUpdateOptions.js.map +1 -0
- package/dist/workspace/internals/compareSemver.d.ts +8 -0
- package/dist/workspace/internals/compareSemver.js +27 -0
- package/dist/workspace/internals/compareSemver.js.map +1 -0
- package/dist/workspace/internals/getPackageJsonWorkspaces.d.ts +2 -0
- package/dist/workspace/internals/getPackageJsonWorkspaces.js +11 -0
- package/dist/workspace/internals/getPackageJsonWorkspaces.js.map +1 -0
- package/dist/workspace/internals/getRepositoryBranches.d.ts +1 -0
- package/dist/workspace/internals/getRepositoryBranches.js +21 -0
- package/dist/workspace/internals/getRepositoryBranches.js.map +1 -0
- package/dist/workspace/internals/getRepositoryUrl.d.ts +1 -0
- package/dist/workspace/internals/getRepositoryUrl.js +21 -0
- package/dist/workspace/internals/getRepositoryUrl.js.map +1 -0
- package/dist/workspace/internals.d.ts +4 -0
- package/dist/workspace/internals.js +6 -0
- package/dist/workspace/internals.js.map +1 -0
- package/dist/workspace/saveProjectConfig.d.ts +6 -0
- package/dist/workspace/saveProjectConfig.js +13 -0
- package/dist/workspace/saveProjectConfig.js.map +1 -0
- package/dist/workspace/saveWorkspaceConfig.d.ts +6 -0
- package/dist/workspace/saveWorkspaceConfig.js +13 -0
- package/dist/workspace/saveWorkspaceConfig.js.map +1 -0
- package/dist/workspace.d.ts +10 -0
- package/dist/workspace.js +12 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +84 -0
- package/src/commands/create-project/CreateProject.tsx +130 -0
- package/src/commands/create-project/CreateProjectInput.ts +37 -0
- package/src/commands/create-project/createProjectCommand.tsx +33 -0
- package/src/commands/create-project/runCreateProject.ts +59 -0
- package/src/commands/create-project.ts +4 -0
- package/src/commands/create-workspace/CreateWorkspace.tsx +90 -0
- package/src/commands/create-workspace/CreateWorkspaceInput.ts +21 -0
- package/src/commands/create-workspace/createWorkspaceCommand.tsx +34 -0
- package/src/commands/create-workspace/runCreateWorkspace.ts +49 -0
- package/src/commands/create-workspace.ts +4 -0
- package/src/commands/internals/packageNameToDirectory.ts +13 -0
- package/src/commands/internals.ts +1 -0
- package/src/commands/update-workspace/UpdateWorkspace.tsx +59 -0
- package/src/commands/update-workspace/runUpdateWorkspace.ts +36 -0
- package/src/commands/update-workspace/updateWorkspaceCommand.tsx +27 -0
- package/src/commands/update-workspace.ts +3 -0
- package/src/commands.ts +3 -0
- package/src/constants.ts +21 -0
- package/src/main.ts +3 -0
- package/src/modules.d.ts +1 -0
- package/src/run.ts +21 -0
- package/src/template/CarsonTemplate.ts +11 -0
- package/src/template/CarsonTemplateConfig.ts +12 -0
- package/src/template/TemplateAttributes.ts +16 -0
- package/src/template/TemplateData.ts +17 -0
- package/src/template/TemplateRender.ts +9 -0
- package/src/template/TemplateRenderSnapshot.ts +13 -0
- package/src/template/TemplateRenderSnapshots.ts +7 -0
- package/src/template/TemplateRenders.ts +3 -0
- package/src/template/applyTemplateRenders.ts +89 -0
- package/src/template/createFileUsingTemplateRender.ts +12 -0
- package/src/template/deleteFileUsingTemplateRender.ts +15 -0
- package/src/template/importCarsonTemplate.ts +13 -0
- package/src/template/insertUsingTemplateRender.ts +33 -0
- package/src/template/internals/merge.ts +44 -0
- package/src/template/internals/prettify.ts +24 -0
- package/src/template/internals/unmerge.ts +48 -0
- package/src/template/internals.ts +3 -0
- package/src/template/loadTemplateRenderSnapshots.ts +18 -0
- package/src/template/mergeFileUsingTemplateRender.ts +39 -0
- package/src/template/overwriteFileUsingTemplateRender.ts +12 -0
- package/src/template/parseTemplateId.ts +9 -0
- package/src/template/readCarsonTemplate.ts +46 -0
- package/src/template/renderTemplate.ts +30 -0
- package/src/template/resolveTemplateModule.ts +9 -0
- package/src/template/saveTemplateRenderSnapshots.ts +34 -0
- package/src/template/uninsertUsingTemplateRender.ts +36 -0
- package/src/template/unmergeFileUsingTemplateRender.ts +42 -0
- package/src/template.ts +20 -0
- package/src/workspace/Project.ts +246 -0
- package/src/workspace/ProjectConfig.ts +9 -0
- package/src/workspace/ProjectCreateOptions.ts +9 -0
- package/src/workspace/ProjectError.ts +15 -0
- package/src/workspace/ProjectOptions.ts +17 -0
- package/src/workspace/ProjectUpdateOptions.ts +3 -0
- package/src/workspace/Workspace.ts +553 -0
- package/src/workspace/WorkspaceConfig.ts +9 -0
- package/src/workspace/WorkspaceCreateOptions.ts +5 -0
- package/src/workspace/WorkspaceCreateProjectOptions.ts +6 -0
- package/src/workspace/WorkspaceDependencies.ts +196 -0
- package/src/workspace/WorkspaceError.ts +18 -0
- package/src/workspace/WorkspaceOptions.ts +29 -0
- package/src/workspace/WorkspaceOptionsProjectOptions.ts +9 -0
- package/src/workspace/WorkspacePackageJson.ts +3 -0
- package/src/workspace/WorkspaceProjectGlobs.ts +1 -0
- package/src/workspace/WorkspaceProjects.ts +3 -0
- package/src/workspace/WorkspaceRepository.ts +4 -0
- package/src/workspace/WorkspaceUpdateOptions.ts +3 -0
- package/src/workspace/internals/compareSemver.ts +35 -0
- package/src/workspace/internals/getPackageJsonWorkspaces.ts +11 -0
- package/src/workspace/internals/getRepositoryBranches.ts +25 -0
- package/src/workspace/internals/getRepositoryUrl.ts +23 -0
- package/src/workspace/internals.ts +4 -0
- package/src/workspace/saveProjectConfig.ts +23 -0
- package/src/workspace/saveWorkspaceConfig.ts +30 -0
- package/src/workspace.ts +10 -0
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import {ensureEmptyDirectory, findExistingDirectory, isRootPath} from '@jakubmazanec/fs-utils';
|
|
2
|
+
import {readFile, packageJsonSchema, type PackageJson} from '@jakubmazanec/zod-utils';
|
|
3
|
+
import glob from 'fast-glob';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import json5 from 'json5';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import {z} from 'zod';
|
|
8
|
+
|
|
9
|
+
import {getPackageJsonWorkspaces, getRepositoryBranches, getRepositoryUrl} from './internals.js';
|
|
10
|
+
import {Project} from './Project.js';
|
|
11
|
+
import {saveWorkspaceConfig} from './saveWorkspaceConfig.js';
|
|
12
|
+
import {workspaceConfigSchema} from './WorkspaceConfig.js';
|
|
13
|
+
import {type WorkspaceConfig} from './WorkspaceConfig.js';
|
|
14
|
+
import {type WorkspaceCreateOptions} from './WorkspaceCreateOptions.js';
|
|
15
|
+
import {type WorkspaceCreateProjectOptions} from './WorkspaceCreateProjectOptions.js';
|
|
16
|
+
import {WorkspaceDependencies} from './WorkspaceDependencies.js';
|
|
17
|
+
import {WorkspaceError} from './WorkspaceError.js';
|
|
18
|
+
import type {WorkspaceOptions} from './WorkspaceOptions.js';
|
|
19
|
+
import {type WorkspacePackageJson} from './WorkspacePackageJson.js';
|
|
20
|
+
import type {WorkspaceProjectGlobs} from './WorkspaceProjectGlobs.js';
|
|
21
|
+
import {type WorkspaceProjects} from './WorkspaceProjects.js';
|
|
22
|
+
import {type WorkspaceRepository} from './WorkspaceRepository.js';
|
|
23
|
+
import {type WorkspaceUpdateOptions} from './WorkspaceUpdateOptions.js';
|
|
24
|
+
import {CARSON_CONFIG_DIRECTORY, WORKSPACE_CONFIG_FILENAME} from '../constants.js';
|
|
25
|
+
import {applyTemplateRenders} from '../template/applyTemplateRenders.js';
|
|
26
|
+
import {renderTemplate} from '../template/renderTemplate.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Workspace represent a directory, usually a git repository, that contains {@linkcode Project} instances.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam M If true, the workspace is multi-project workspace (so-called "monorepo").
|
|
32
|
+
*/
|
|
33
|
+
export class Workspace<M extends boolean = true> {
|
|
34
|
+
/** Workspace path. */
|
|
35
|
+
readonly path: string;
|
|
36
|
+
|
|
37
|
+
/** Does the workspace contain a multiple projects? */
|
|
38
|
+
readonly isMultiProject: M;
|
|
39
|
+
|
|
40
|
+
/** Globs used to find projects. */
|
|
41
|
+
projectGlobs: WorkspaceProjectGlobs<M>;
|
|
42
|
+
|
|
43
|
+
/** Workspace projects. */
|
|
44
|
+
projects: WorkspaceProjects<M>;
|
|
45
|
+
|
|
46
|
+
/** Workspace configuration; if it's not found, the default is an empty object. */
|
|
47
|
+
config: WorkspaceConfig;
|
|
48
|
+
|
|
49
|
+
/** Parsed workspace `package.json` file (if the workspace contains multiple projects); if it's not found, the default is an empty object. */
|
|
50
|
+
packageJson: WorkspacePackageJson<M>;
|
|
51
|
+
|
|
52
|
+
/** Workspace git repository. */
|
|
53
|
+
repository?: WorkspaceRepository;
|
|
54
|
+
|
|
55
|
+
/** Dependencies of all projects. */
|
|
56
|
+
dependencies: WorkspaceDependencies<M> = [] as unknown as WorkspaceDependencies<M>;
|
|
57
|
+
|
|
58
|
+
/** Development dependencies of all projects. */
|
|
59
|
+
devDependencies: WorkspaceDependencies<M> = [] as unknown as WorkspaceDependencies<M>;
|
|
60
|
+
|
|
61
|
+
/** All dependencies of all projects and workspace dependencies. */
|
|
62
|
+
allDependencies: WorkspaceDependencies<M> = [] as unknown as WorkspaceDependencies<M>;
|
|
63
|
+
|
|
64
|
+
/** Versions of each dependency. */
|
|
65
|
+
dependencyVersions: Record<string, string[]> = {};
|
|
66
|
+
|
|
67
|
+
/** Errors found during reading from the file system. */
|
|
68
|
+
errors: Array<InstanceType<typeof WorkspaceError>> = [];
|
|
69
|
+
|
|
70
|
+
constructor(options: WorkspaceOptions<M>) {
|
|
71
|
+
this.path = options.path;
|
|
72
|
+
|
|
73
|
+
if (typeof options.isMultiProject === 'undefined' || options.isMultiProject) {
|
|
74
|
+
this.isMultiProject = true as M;
|
|
75
|
+
|
|
76
|
+
let projectGlobOption = (options as WorkspaceOptions<true>).projectGlobs;
|
|
77
|
+
|
|
78
|
+
if (projectGlobOption) {
|
|
79
|
+
this.projectGlobs = projectGlobOption as WorkspaceProjectGlobs<M>;
|
|
80
|
+
} else {
|
|
81
|
+
this.projectGlobs = [] as string[] as WorkspaceProjectGlobs<M>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let {projects} = options as WorkspaceOptions<true>;
|
|
85
|
+
|
|
86
|
+
if (projects) {
|
|
87
|
+
this.projects = projects.map(
|
|
88
|
+
(project) => new Project({...project, workspace: this}),
|
|
89
|
+
) as WorkspaceProjects<M>;
|
|
90
|
+
} else {
|
|
91
|
+
this.projects = [] as Array<Project<M>> as WorkspaceProjects<M>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let packageJsonOption = (options as WorkspaceOptions<true>).packageJson;
|
|
95
|
+
|
|
96
|
+
if (packageJsonOption) {
|
|
97
|
+
this.packageJson = packageJsonOption as M extends true ? PackageJson : null;
|
|
98
|
+
} else {
|
|
99
|
+
let emptyPackageJson: PackageJson = {};
|
|
100
|
+
|
|
101
|
+
this.packageJson = emptyPackageJson as M extends true ? PackageJson : null;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
this.isMultiProject = false as M;
|
|
105
|
+
this.projectGlobs = null as M extends true ? string[] : null;
|
|
106
|
+
this.packageJson = null as M extends true ? PackageJson : null;
|
|
107
|
+
|
|
108
|
+
let {projects} = options as WorkspaceOptions<false>;
|
|
109
|
+
|
|
110
|
+
if (projects) {
|
|
111
|
+
this.projects = projects.map(
|
|
112
|
+
(project) => new Project({...project, workspace: this}),
|
|
113
|
+
) as WorkspaceProjects<M>;
|
|
114
|
+
|
|
115
|
+
if ((this.projects[0] as Project<false>).path !== this.path) {
|
|
116
|
+
throw new WorkspaceError('PATH_MISMATCH');
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
this.projects = [] as Array<Project<M>> as WorkspaceProjects<M>;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let configOption = options.config;
|
|
124
|
+
|
|
125
|
+
if (configOption) {
|
|
126
|
+
this.config = configOption;
|
|
127
|
+
} else {
|
|
128
|
+
let emptyWorkspaceConfig: WorkspaceConfig = {};
|
|
129
|
+
|
|
130
|
+
this.config = emptyWorkspaceConfig;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (options.repository) {
|
|
134
|
+
this.repository = options.repository;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (options.errors) {
|
|
138
|
+
this.errors = options.errors;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// add missing project globs
|
|
142
|
+
if (this.isMultiProject && !(this.projectGlobs as string[]).length) {
|
|
143
|
+
let packageJsonWorkspaces = getPackageJsonWorkspaces(this.packageJson as PackageJson);
|
|
144
|
+
|
|
145
|
+
if (packageJsonWorkspaces) {
|
|
146
|
+
this.projectGlobs = [...packageJsonWorkspaces] as M extends true ? string[] : null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this.updateDependencies();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Creates new workspace within the file system.
|
|
155
|
+
*
|
|
156
|
+
* @param options Options.
|
|
157
|
+
* @returns The new workspace
|
|
158
|
+
*/
|
|
159
|
+
static async create({args, path: workspacePath, templateId}: WorkspaceCreateOptions) {
|
|
160
|
+
// TODO: test that workspace returned by this method has the same value of isMultiProject when the same workspace is after its creation re-read with .read()
|
|
161
|
+
let workspace = new this({
|
|
162
|
+
path: workspacePath,
|
|
163
|
+
config: {
|
|
164
|
+
template: templateId,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
await ensureEmptyDirectory(workspace.path);
|
|
169
|
+
await saveWorkspaceConfig({
|
|
170
|
+
workspacePath: workspace.path,
|
|
171
|
+
workspaceConfig: {template: templateId},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
let templateRenders = await renderTemplate({
|
|
175
|
+
templateId,
|
|
176
|
+
templateData: {
|
|
177
|
+
workspace,
|
|
178
|
+
args,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
await applyTemplateRenders({
|
|
183
|
+
templateRenders,
|
|
184
|
+
path: workspace.path,
|
|
185
|
+
ignoreCreateStrategies: false,
|
|
186
|
+
ignoreCheckStrategies: true,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return Workspace.read(workspace.path);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static async read(workspacePath: string): Promise<Workspace | Workspace<false>> {
|
|
193
|
+
let resolvedWorkspacePath = path.resolve(workspacePath);
|
|
194
|
+
|
|
195
|
+
if (!(await fs.pathExists(resolvedWorkspacePath))) {
|
|
196
|
+
throw new WorkspaceError('NO_SUCH_WORKSPACE_PATH', {
|
|
197
|
+
messageParameters: [resolvedWorkspacePath],
|
|
198
|
+
data: {
|
|
199
|
+
path: resolvedWorkspacePath,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// load root package.json
|
|
205
|
+
let rootPackageJsonPath = path.join(resolvedWorkspacePath, 'package.json');
|
|
206
|
+
let doesRootPackageJsonExist = await fs.pathExists(rootPackageJsonPath);
|
|
207
|
+
let rootPackageJson: PackageJson | z.ZodError;
|
|
208
|
+
|
|
209
|
+
if (doesRootPackageJsonExist) {
|
|
210
|
+
rootPackageJson = await readFile(rootPackageJsonPath, packageJsonSchema, {
|
|
211
|
+
parser: (rawString: string) => json5.parse<unknown>(rawString),
|
|
212
|
+
});
|
|
213
|
+
} else {
|
|
214
|
+
rootPackageJson = {};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (rootPackageJson instanceof z.ZodError) {
|
|
218
|
+
rootPackageJson = {};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// add projects
|
|
222
|
+
let projectGlobs = getPackageJsonWorkspaces(rootPackageJson);
|
|
223
|
+
|
|
224
|
+
// we're dealing with single-project workspace
|
|
225
|
+
if (!projectGlobs) {
|
|
226
|
+
let workspace = new this({
|
|
227
|
+
path: resolvedWorkspacePath,
|
|
228
|
+
isMultiProject: false,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
await workspace.read();
|
|
232
|
+
await workspace.readProjects();
|
|
233
|
+
|
|
234
|
+
return workspace;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// assemble workspace options
|
|
238
|
+
let workspace = new this({
|
|
239
|
+
path: resolvedWorkspacePath,
|
|
240
|
+
isMultiProject: true,
|
|
241
|
+
projectGlobs,
|
|
242
|
+
packageJson: rootPackageJson,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await workspace.read();
|
|
246
|
+
await workspace.readProjects();
|
|
247
|
+
|
|
248
|
+
return workspace;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Traverses file system up from the search path and tries to find a directory that is a root of a workspace.
|
|
253
|
+
*/
|
|
254
|
+
static async find(searchPath: string): Promise<string | undefined> {
|
|
255
|
+
let startingPath = await findExistingDirectory(path.resolve(searchPath));
|
|
256
|
+
let currentPath = startingPath;
|
|
257
|
+
let singlePackageWorkspacePath: string | undefined;
|
|
258
|
+
let multiPackageWorkspacePath: string | undefined;
|
|
259
|
+
|
|
260
|
+
while (
|
|
261
|
+
!(isRootPath(currentPath) || (singlePackageWorkspacePath && multiPackageWorkspacePath))
|
|
262
|
+
) {
|
|
263
|
+
let packageJsonPath = path.join(currentPath, 'package.json');
|
|
264
|
+
let doesPackageJsonExist = await fs.pathExists(packageJsonPath);
|
|
265
|
+
|
|
266
|
+
if (doesPackageJsonExist) {
|
|
267
|
+
let packageJson = await readFile(packageJsonPath, packageJsonSchema, {
|
|
268
|
+
parser: (rawString: string) => json5.parse<unknown>(rawString),
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
if (!(packageJson instanceof Error)) {
|
|
272
|
+
let projectGlobs = getPackageJsonWorkspaces(packageJson);
|
|
273
|
+
|
|
274
|
+
if (projectGlobs?.length) {
|
|
275
|
+
multiPackageWorkspacePath ??= currentPath;
|
|
276
|
+
} else {
|
|
277
|
+
singlePackageWorkspacePath ??= currentPath;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
currentPath = path.dirname(currentPath);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (multiPackageWorkspacePath) {
|
|
286
|
+
return multiPackageWorkspacePath;
|
|
287
|
+
} else if (singlePackageWorkspacePath) {
|
|
288
|
+
return singlePackageWorkspacePath;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Traverses file system up from the search path and tries to find and read a workspace.
|
|
296
|
+
*/
|
|
297
|
+
static async findAndRead(searchPath: string): Promise<Workspace | Workspace<false> | undefined> {
|
|
298
|
+
let workspacePath = await Workspace.find(searchPath);
|
|
299
|
+
|
|
300
|
+
if (workspacePath) {
|
|
301
|
+
return Workspace.read(workspacePath);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async read(): Promise<this> {
|
|
308
|
+
if (!(await fs.pathExists(this.path))) {
|
|
309
|
+
throw new WorkspaceError('NO_SUCH_WORKSPACE_PATH', {
|
|
310
|
+
messageParameters: [this.path],
|
|
311
|
+
data: {
|
|
312
|
+
path: this.path,
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let errors: Array<InstanceType<typeof WorkspaceError>> = [];
|
|
318
|
+
let repositoryUrl = await getRepositoryUrl(this.path);
|
|
319
|
+
let repositoryBranches = await getRepositoryBranches(this.path);
|
|
320
|
+
|
|
321
|
+
// load workspace config
|
|
322
|
+
let workspaceConfigPath = path.join(
|
|
323
|
+
this.path,
|
|
324
|
+
CARSON_CONFIG_DIRECTORY,
|
|
325
|
+
WORKSPACE_CONFIG_FILENAME,
|
|
326
|
+
);
|
|
327
|
+
let doesWorkspaceConfigExist = await fs.pathExists(workspaceConfigPath);
|
|
328
|
+
let workspaceConfig: WorkspaceConfig | z.ZodError;
|
|
329
|
+
|
|
330
|
+
if (doesWorkspaceConfigExist) {
|
|
331
|
+
workspaceConfig = await readFile(workspaceConfigPath, workspaceConfigSchema, {
|
|
332
|
+
parser: (rawString: string) => json5.parse<unknown>(rawString),
|
|
333
|
+
});
|
|
334
|
+
} else {
|
|
335
|
+
workspaceConfig = {};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (workspaceConfig instanceof z.ZodError) {
|
|
339
|
+
let zodError = workspaceConfig;
|
|
340
|
+
let workspaceError = new WorkspaceError('INVALID_WORKSPACE_CONFIG', {
|
|
341
|
+
messageParameters: [workspaceConfigPath],
|
|
342
|
+
data: {
|
|
343
|
+
path: workspaceConfigPath,
|
|
344
|
+
zodIssues: zodError.issues,
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
errors.push(workspaceError);
|
|
349
|
+
|
|
350
|
+
workspaceConfig = {};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (this.isMultiProject) {
|
|
354
|
+
// load root package.json
|
|
355
|
+
let rootPackageJsonPath = path.join(this.path, 'package.json');
|
|
356
|
+
let doesRootPackageJsonExist = await fs.pathExists(rootPackageJsonPath);
|
|
357
|
+
let rootPackageJson: PackageJson | z.ZodError;
|
|
358
|
+
|
|
359
|
+
if (doesRootPackageJsonExist) {
|
|
360
|
+
rootPackageJson = await readFile(rootPackageJsonPath, packageJsonSchema, {
|
|
361
|
+
parser: (rawString: string) => json5.parse<unknown>(rawString),
|
|
362
|
+
});
|
|
363
|
+
} else {
|
|
364
|
+
rootPackageJson = {};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (rootPackageJson instanceof z.ZodError) {
|
|
368
|
+
let zodError = rootPackageJson;
|
|
369
|
+
let workspaceError = new WorkspaceError('INVALID_PACKAGE_JSON', {
|
|
370
|
+
messageParameters: [rootPackageJsonPath],
|
|
371
|
+
data: {
|
|
372
|
+
path: rootPackageJsonPath,
|
|
373
|
+
zodIssues: zodError.issues,
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
errors.push(workspaceError);
|
|
378
|
+
|
|
379
|
+
rootPackageJson = {};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
let projectGlobs = getPackageJsonWorkspaces(rootPackageJson);
|
|
383
|
+
|
|
384
|
+
this.projectGlobs = (projectGlobs ?? []) as WorkspaceProjectGlobs<M>;
|
|
385
|
+
this.packageJson = rootPackageJson as WorkspacePackageJson<M>;
|
|
386
|
+
this.config = workspaceConfig;
|
|
387
|
+
this.errors = errors;
|
|
388
|
+
|
|
389
|
+
if (repositoryUrl) {
|
|
390
|
+
this.repository = {
|
|
391
|
+
url: repositoryUrl,
|
|
392
|
+
branches: repositoryBranches ?? [],
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
await this.readProjects();
|
|
397
|
+
|
|
398
|
+
return this;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// we're dealing with single-project workspace
|
|
402
|
+
this.config = workspaceConfig;
|
|
403
|
+
this.errors = errors;
|
|
404
|
+
|
|
405
|
+
if (repositoryUrl) {
|
|
406
|
+
this.repository = {
|
|
407
|
+
url: repositoryUrl,
|
|
408
|
+
branches: repositoryBranches ?? [],
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
await this.readProjects();
|
|
413
|
+
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async update({args}: WorkspaceUpdateOptions): Promise<this> {
|
|
418
|
+
if (!this.config.template) {
|
|
419
|
+
throw new Error('Templat ID is missing in the workspace config!');
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
let templateRenders = await renderTemplate({
|
|
423
|
+
templateId: this.config.template,
|
|
424
|
+
templateData: {
|
|
425
|
+
workspace: this,
|
|
426
|
+
args,
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
await applyTemplateRenders({
|
|
431
|
+
templateRenders,
|
|
432
|
+
path: this.path,
|
|
433
|
+
ignoreCreateStrategies: true,
|
|
434
|
+
ignoreCheckStrategies: false,
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
await this.read();
|
|
438
|
+
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
getProject(projectName: Project<M> | string): Project<M> | undefined {
|
|
443
|
+
for (let project of this.projects) {
|
|
444
|
+
if (project.name === projectName || project === projectName) {
|
|
445
|
+
return project;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async createProject({
|
|
453
|
+
templateId,
|
|
454
|
+
projectPath,
|
|
455
|
+
projectName,
|
|
456
|
+
args,
|
|
457
|
+
}: WorkspaceCreateProjectOptions): Promise<Project<M>> {
|
|
458
|
+
if (this.projects.some((project) => projectPath === project.path)) {
|
|
459
|
+
throw new Error('Project with same path already exists!');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (this.projects.some((project) => projectName === project.name)) {
|
|
463
|
+
throw new Error('Project with same name already exists!');
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (!this.isMultiProject && this.projects.length >= 1) {
|
|
467
|
+
throw new Error('Only multi-project Workspace can have more than 1 project!');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
let newProject = await Project.create({
|
|
471
|
+
templateId,
|
|
472
|
+
projectPath,
|
|
473
|
+
projectName,
|
|
474
|
+
args,
|
|
475
|
+
workspace: this,
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
this.addProject(newProject);
|
|
479
|
+
|
|
480
|
+
return newProject;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
addProject(newProject: Project<M>): Project<M> {
|
|
484
|
+
if (this.projects.some((project) => newProject.path === project.path)) {
|
|
485
|
+
throw new Error('Project with same path already exists!');
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (this.projects.some((project) => newProject.name === project.name)) {
|
|
489
|
+
throw new Error('Project with same name already exists!');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (!this.isMultiProject && this.projects.length >= 1) {
|
|
493
|
+
throw new Error('Only multi-project Workspace can have more than 1 project!');
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
this.projects.push(newProject);
|
|
497
|
+
this.sortProjects();
|
|
498
|
+
this.updateDependencies();
|
|
499
|
+
|
|
500
|
+
return newProject;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async readProjects(): Promise<Array<Project<M>>> {
|
|
504
|
+
if (this.projectGlobs) {
|
|
505
|
+
let projects: Array<Project<M>> = [];
|
|
506
|
+
let packagePaths = await glob(this.projectGlobs, {
|
|
507
|
+
cwd: this.path,
|
|
508
|
+
onlyDirectories: true,
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
for (let packagePath of packagePaths) {
|
|
512
|
+
let project = await Project.read(path.join(this.path, packagePath), this);
|
|
513
|
+
|
|
514
|
+
projects.push(project);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
this.projects = projects;
|
|
518
|
+
} else {
|
|
519
|
+
this.projects = [await Project.read(this.path, this)];
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
this.sortProjects();
|
|
523
|
+
this.updateDependencies();
|
|
524
|
+
|
|
525
|
+
return this.projects;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
private sortProjects(): void {
|
|
529
|
+
this.projects.sort((projectA, projectB) => {
|
|
530
|
+
if (projectA.path < projectB.path) {
|
|
531
|
+
return -1;
|
|
532
|
+
} else if (projectA.path > projectB.path) {
|
|
533
|
+
return 1;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return 0;
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private updateDependencies(): void {
|
|
541
|
+
this.dependencies = WorkspaceDependencies.fromWorkspace(this);
|
|
542
|
+
this.devDependencies = WorkspaceDependencies.fromWorkspace(this, {
|
|
543
|
+
includeOnlyDevDependencies: true,
|
|
544
|
+
});
|
|
545
|
+
this.allDependencies = WorkspaceDependencies.fromWorkspace(this, {
|
|
546
|
+
includeDevDependencies: true,
|
|
547
|
+
includeWorkspaceDependencies: true,
|
|
548
|
+
});
|
|
549
|
+
this.dependencyVersions = this.allDependencies.getVersions({
|
|
550
|
+
useExactVersions: true,
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|