@decohere/workspace 0.0.1-dev.20261105.99 → 0.0.1-dev.20261106.139
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/package.json +5 -6
- package/commonjs/src/const.js +0 -69
- package/commonjs/src/errors.js +0 -12
- package/commonjs/src/factories/config.factory.js +0 -90
- package/commonjs/src/factories/project.factory.js +0 -104
- package/commonjs/src/factories/workspace.factory.js +0 -50
- package/commonjs/src/index.js +0 -70
- package/commonjs/src/models/has-package-json.mixin.js +0 -50
- package/commonjs/src/models/has-tsconfig.mixin.js +0 -75
- package/commonjs/src/models/jsonc.js +0 -57
- package/commonjs/src/models/package-aliases.js +0 -112
- package/commonjs/src/models/package-json.model.js +0 -202
- package/commonjs/src/models/project-file-event.js +0 -4
- package/commonjs/src/models/project.model.js +0 -316
- package/commonjs/src/models/source-extensions.js +0 -81
- package/commonjs/src/models/tsconfig.model.js +0 -87
- package/commonjs/src/models/workspace-config.type.js +0 -7
- package/commonjs/src/models/workspace.model.js +0 -160
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "sourceExtensions", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return sourceExtensions;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
|
|
12
|
-
function _interop_require_default(obj) {
|
|
13
|
-
return obj && obj.__esModule ? obj : {
|
|
14
|
-
default: obj
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
/** TypeScript source extensions a workspace compiles. */ const SOURCE = [
|
|
18
|
-
".ts",
|
|
19
|
-
".tsx",
|
|
20
|
-
".mts",
|
|
21
|
-
".cts"
|
|
22
|
-
];
|
|
23
|
-
/** The sources an import's extension can name: `./a.js` is `a.ts` or `a.tsx`, `./a.mjs` is `a.mts`. */ const SOURCES_FOR = {
|
|
24
|
-
".js": [
|
|
25
|
-
".ts",
|
|
26
|
-
".tsx"
|
|
27
|
-
],
|
|
28
|
-
".jsx": [
|
|
29
|
-
".tsx"
|
|
30
|
-
],
|
|
31
|
-
".mjs": [
|
|
32
|
-
".mts"
|
|
33
|
-
],
|
|
34
|
-
".cjs": [
|
|
35
|
-
".cts"
|
|
36
|
-
],
|
|
37
|
-
"": [
|
|
38
|
-
".ts",
|
|
39
|
-
".tsx"
|
|
40
|
-
]
|
|
41
|
-
};
|
|
42
|
-
/** Whether a path is a TypeScript source (not a declaration file). */ function isSource(file) {
|
|
43
|
-
return SOURCE.some((ext)=>file.endsWith(ext)) && !/\.d\.[mc]?ts$/.test(file);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* The source files a path written in an import (or a built path) could name, most likely first:
|
|
47
|
-
* itself when it is already a source, else the same stem with each matching source extension.
|
|
48
|
-
*/ function candidates(file) {
|
|
49
|
-
if (isSource(file)) return [
|
|
50
|
-
file
|
|
51
|
-
];
|
|
52
|
-
const ext = _nodepath.default.extname(file);
|
|
53
|
-
const known = SOURCES_FOR[ext];
|
|
54
|
-
if (known == null) return [
|
|
55
|
-
`${file}.ts`,
|
|
56
|
-
`${file}.tsx`
|
|
57
|
-
];
|
|
58
|
-
const stem = ext === "" ? file : file.slice(0, -ext.length);
|
|
59
|
-
return known.map((sourceExt)=>`${stem}${sourceExt}`);
|
|
60
|
-
}
|
|
61
|
-
/** The extension a source builds to: `.mts` to `.mjs`, `.cts` to `.cjs`, the rest to `.js`. */ function outputExtension(file) {
|
|
62
|
-
if (file.endsWith(".mts")) return ".mjs";
|
|
63
|
-
if (file.endsWith(".cts")) return ".cjs";
|
|
64
|
-
return ".js";
|
|
65
|
-
}
|
|
66
|
-
/** The path a source builds to: `a.mts` to `a.mjs`, `a.cts` to `a.cjs`, `a.ts` and `a.tsx` to `a.js`. */ function outputPath(file) {
|
|
67
|
-
const ext = _nodepath.default.extname(file);
|
|
68
|
-
const stem = ext === "" ? file : file.slice(0, -ext.length);
|
|
69
|
-
return `${stem}${outputExtension(file)}`;
|
|
70
|
-
}
|
|
71
|
-
/** Whether a source is CommonJS (`.cts`); everything else compiles as an ES module. */ function isCommonJs(file) {
|
|
72
|
-
return file.endsWith(".cts");
|
|
73
|
-
}
|
|
74
|
-
const sourceExtensions = {
|
|
75
|
-
SOURCE,
|
|
76
|
-
isSource,
|
|
77
|
-
candidates,
|
|
78
|
-
outputExtension,
|
|
79
|
-
outputPath,
|
|
80
|
-
isCommonJs
|
|
81
|
-
};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get DEFAULT_COMPILER_OPTIONS () {
|
|
13
|
-
return DEFAULT_COMPILER_OPTIONS;
|
|
14
|
-
},
|
|
15
|
-
get Tsconfig () {
|
|
16
|
-
return Tsconfig;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const _nodefs = /*#__PURE__*/ _interop_require_default(require("node:fs"));
|
|
20
|
-
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
|
|
21
|
-
const _io = require("@decohere/core/io");
|
|
22
|
-
const _gettsconfig = require("get-tsconfig");
|
|
23
|
-
const _errors = require("../errors.js");
|
|
24
|
-
const _jsonc = require("./jsonc.js");
|
|
25
|
-
function _interop_require_default(obj) {
|
|
26
|
-
return obj && obj.__esModule ? obj : {
|
|
27
|
-
default: obj
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
const DEFAULT_COMPILER_OPTIONS = {
|
|
31
|
-
emitDeclarationOnly: true,
|
|
32
|
-
declaration: true,
|
|
33
|
-
baseUrl: "./"
|
|
34
|
-
};
|
|
35
|
-
class Tsconfig {
|
|
36
|
-
static default() {
|
|
37
|
-
return new Tsconfig();
|
|
38
|
-
}
|
|
39
|
-
static async parse(filePath) {
|
|
40
|
-
if (!await _io.io.fileExists(filePath)) {
|
|
41
|
-
throw new _errors.WorkspaceConfigurationError(`TSConfig not found ${filePath}`);
|
|
42
|
-
}
|
|
43
|
-
return Tsconfig.read(filePath);
|
|
44
|
-
}
|
|
45
|
-
static parseSync(filePath) {
|
|
46
|
-
if (!Tsconfig.fileExistsSync(filePath)) {
|
|
47
|
-
throw new _errors.WorkspaceConfigurationError(`TSConfig not found ${filePath}`);
|
|
48
|
-
}
|
|
49
|
-
return Tsconfig.read(filePath);
|
|
50
|
-
}
|
|
51
|
-
static read(filePath) {
|
|
52
|
-
const fullPath = _nodepath.default.resolve(filePath);
|
|
53
|
-
let compilerOptions;
|
|
54
|
-
try {
|
|
55
|
-
// get-tsconfig reads broken JSON as `{}`; check the file itself first.
|
|
56
|
-
(0, _jsonc.parseJsonc)(_nodefs.default.readFileSync(fullPath, "utf8"));
|
|
57
|
-
compilerOptions = (0, _gettsconfig.parseTsconfig)(fullPath).compilerOptions ?? {};
|
|
58
|
-
} catch (error) {
|
|
59
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
60
|
-
throw new _errors.WorkspaceConfigurationError(`Error parsing ${filePath}. ${message}`, {
|
|
61
|
-
cause: error
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
const { baseUrl } = compilerOptions;
|
|
65
|
-
return new Tsconfig({
|
|
66
|
-
filePath: fullPath,
|
|
67
|
-
compilerOptions: baseUrl == null ? compilerOptions : {
|
|
68
|
-
...compilerOptions,
|
|
69
|
-
baseUrl: _nodepath.default.resolve(_nodepath.default.dirname(fullPath), baseUrl)
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
static fileExistsSync(filePath) {
|
|
74
|
-
try {
|
|
75
|
-
return _nodefs.default.statSync(filePath).isFile();
|
|
76
|
-
} catch {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
constructor(data = {
|
|
81
|
-
compilerOptions: DEFAULT_COMPILER_OPTIONS
|
|
82
|
-
}){
|
|
83
|
-
Object.assign(this, data);
|
|
84
|
-
}
|
|
85
|
-
/** Absolute path of the file read; undefined for {@link Tsconfig.default}. */ filePath;
|
|
86
|
-
compilerOptions;
|
|
87
|
-
}
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "Workspace", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return Workspace;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _decorators = require("@decohere/crucible/decorators");
|
|
12
|
-
const _collections = require("@decohere/core/collections");
|
|
13
|
-
const _extensibility = require("@decohere/core/extensibility");
|
|
14
|
-
const _system = require("@decohere/core/system");
|
|
15
|
-
const _errors = require("../errors.js");
|
|
16
|
-
const _haspackagejsonmixin = require("./has-package-json.mixin.js");
|
|
17
|
-
const _hastsconfigmixin = require("./has-tsconfig.mixin.js");
|
|
18
|
-
const _packagealiases = require("./package-aliases.js");
|
|
19
|
-
const _packagejsonmodel = require("./package-json.model.js");
|
|
20
|
-
const _projectmodel = require("./project.model.js");
|
|
21
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
22
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
-
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
25
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
-
}
|
|
27
|
-
function _ts_metadata(k, v) {
|
|
28
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
29
|
-
}
|
|
30
|
-
class Workspace extends (0, _extensibility.mixwith)(_haspackagejsonmixin.HasPackageJson, _hastsconfigmixin.HasTsConfig) {
|
|
31
|
-
packageJson;
|
|
32
|
-
#name;
|
|
33
|
-
#config;
|
|
34
|
-
#projectSeeds = [];
|
|
35
|
-
#projectCache = new Map();
|
|
36
|
-
/**
|
|
37
|
-
* Each project's source file paths as a set, so `findProject` is a lookup rather than a scan
|
|
38
|
-
* of every file: the Vitest plugin asks once per transformed file. Keyed by the list
|
|
39
|
-
* `getAllFiles` returns, so a new list (after its cache is flushed) gets a new set.
|
|
40
|
-
*/ #sourcePaths = new WeakMap();
|
|
41
|
-
#projectFileChangedSubject = new _system.Subject();
|
|
42
|
-
#projectWatchSubscriptions = new Map();
|
|
43
|
-
#watchedProjects = [];
|
|
44
|
-
constructor(options = {}){
|
|
45
|
-
super();
|
|
46
|
-
const { packageJson, config, projects } = options;
|
|
47
|
-
this.#name = packageJson?.name ?? "WORKSPACE";
|
|
48
|
-
this.packageJson = packageJson ?? new _packagejsonmodel.PackageJson();
|
|
49
|
-
this.#config = config ?? {};
|
|
50
|
-
this.#projectSeeds = projects ?? [];
|
|
51
|
-
this.validateWorkspace();
|
|
52
|
-
}
|
|
53
|
-
get name() {
|
|
54
|
-
return this.#name;
|
|
55
|
-
}
|
|
56
|
-
get projectSeeds() {
|
|
57
|
-
return this.#projectSeeds;
|
|
58
|
-
}
|
|
59
|
-
getProjects(configuration) {
|
|
60
|
-
const cached = this.#projectCache.get(configuration);
|
|
61
|
-
if (cached != null) {
|
|
62
|
-
return cached;
|
|
63
|
-
}
|
|
64
|
-
const projects = this.#projectSeeds.map((seed)=>new _projectmodel.Project(seed.name, seed.config ?? {}, configuration));
|
|
65
|
-
this.#projectCache.set(configuration, projects);
|
|
66
|
-
return projects;
|
|
67
|
-
}
|
|
68
|
-
get rootDir() {
|
|
69
|
-
return this.#config.rootDir ?? "./";
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* One project, whose `package.json` is the root's: a folder without `projects/`. Commands
|
|
73
|
-
* that write a project's manifest from the root's (dependency sync) or map projects to each
|
|
74
|
-
* other (paths) have nothing to do then.
|
|
75
|
-
*/ get isStandalone() {
|
|
76
|
-
return this.#projectSeeds.length === 1 && this.#projectSeeds[0]?.config?.projectDir === ".";
|
|
77
|
-
}
|
|
78
|
-
get packageJsonDirectory() {
|
|
79
|
-
return this.rootDir;
|
|
80
|
-
}
|
|
81
|
-
get tsConfigDirectory() {
|
|
82
|
-
return this.rootDir;
|
|
83
|
-
}
|
|
84
|
-
get fallbackTsConfigDirectory() {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Package-name aliases for this configuration: workspace convention, then
|
|
89
|
-
* workspace `tsconfig.json` `paths` overrides. Forge and Flux must use this
|
|
90
|
-
* instead of assembling the map themselves.
|
|
91
|
-
*/ getPackageAliases(configuration) {
|
|
92
|
-
const tsconfig = this.findTsConfigPath() != null ? this.getTsConfigSync() : undefined;
|
|
93
|
-
const baseUrl = typeof tsconfig?.compilerOptions?.baseUrl === "string" && tsconfig.compilerOptions.baseUrl.length > 0 ? tsconfig.compilerOptions.baseUrl : this.rootDir;
|
|
94
|
-
return _packagealiases.packageAliases.build(this.getProjects(configuration), {
|
|
95
|
-
baseUrl,
|
|
96
|
-
tsconfigPaths: tsconfig?.compilerOptions?.paths
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
async watch(configuration) {
|
|
100
|
-
this.stopWatching();
|
|
101
|
-
const projects = this.getProjects(configuration);
|
|
102
|
-
this.#watchedProjects = projects;
|
|
103
|
-
for (const project of projects){
|
|
104
|
-
await project.watch(configuration);
|
|
105
|
-
const subscription = project.projectFileChanged$.subscribe((evt)=>this.#projectFileChangedSubject.next(evt));
|
|
106
|
-
this.#projectWatchSubscriptions.set(project.projectDirectory, subscription);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
stopWatching() {
|
|
110
|
-
for (const subscription of this.#projectWatchSubscriptions.values()){
|
|
111
|
-
subscription.unsubscribe();
|
|
112
|
-
}
|
|
113
|
-
for (const project of this.#watchedProjects){
|
|
114
|
-
project.stopWatching();
|
|
115
|
-
}
|
|
116
|
-
this.#watchedProjects = [];
|
|
117
|
-
this.#projectWatchSubscriptions.clear();
|
|
118
|
-
}
|
|
119
|
-
[Symbol.dispose]() {
|
|
120
|
-
this.stopWatching();
|
|
121
|
-
}
|
|
122
|
-
get projectFileChanged$() {
|
|
123
|
-
return this.#projectFileChangedSubject.asObservable();
|
|
124
|
-
}
|
|
125
|
-
async findProject(filePath, buildConfig) {
|
|
126
|
-
for (const project of this.getProjects(buildConfig)){
|
|
127
|
-
if ((await this.sourcePathsOf(project)).has(filePath)) {
|
|
128
|
-
return project;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
async sourcePathsOf(project) {
|
|
134
|
-
const files = await project.getAllFiles();
|
|
135
|
-
const cached = this.#sourcePaths.get(files);
|
|
136
|
-
if (cached != null) return cached;
|
|
137
|
-
const paths = new Set(files.map((file)=>file.fullPath));
|
|
138
|
-
this.#sourcePaths.set(files, paths);
|
|
139
|
-
return paths;
|
|
140
|
-
}
|
|
141
|
-
validateWorkspace() {
|
|
142
|
-
const errors = this.findDuplicateProjectSeeds(this.#projectSeeds);
|
|
143
|
-
if (errors.length > 0) {
|
|
144
|
-
throw new _errors.WorkspaceConfigurationError(errors.join("\n"));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
findDuplicateProjectSeeds(seeds) {
|
|
148
|
-
const duplicates = _collections.collections.findDuplicatesByKey([
|
|
149
|
-
...seeds
|
|
150
|
-
], "name");
|
|
151
|
-
return duplicates.map(([name, group])=>`Duplicate project name "${name}" found at: ${group.map((seed)=>seed.config?.projectDir ?? name).join(", ")}`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
Workspace = _ts_decorate([
|
|
155
|
-
(0, _decorators.injectable)(),
|
|
156
|
-
_ts_metadata("design:type", Function),
|
|
157
|
-
_ts_metadata("design:paramtypes", [
|
|
158
|
-
typeof Partial === "undefined" ? Object : Partial
|
|
159
|
-
])
|
|
160
|
-
], Workspace);
|