/
package-json.ts
191 lines (171 loc) · 4.87 KB
/
package-json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import {
InputDefinition,
TargetConfiguration,
} from '../config/workspace-json-project-json';
import { readJsonFile } from './fileutils';
import { workspaceRoot } from './workspace-root';
export type PackageJsonTargetConfiguration = Omit<
TargetConfiguration,
'executor'
>;
export interface NxProjectPackageJsonConfiguration {
implicitDependencies?: string[];
tags?: string[];
namedInputs?: { [inputName: string]: (string | InputDefinition)[] };
targets?: Record<string, PackageJsonTargetConfiguration>;
}
export type PackageGroup =
| (string | { package: string; version: string })[]
| Record<string, string>;
export interface NxMigrationsConfiguration {
migrations?: string;
packageGroup?: PackageGroup;
}
export interface PackageJson {
// Generic Package.Json Configuration
name: string;
version: string;
scripts?: Record<string, string>;
type?: 'module' | 'commonjs';
main?: string;
types?: string;
module?: string;
exports?:
| string
| Record<
string,
string | { types?: string; require?: string; import?: string }
>;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
bin?: Record<string, string>;
workspaces?:
| string[]
| {
packages: string[];
};
// Nx Project Configuration
nx?: NxProjectPackageJsonConfiguration;
// Nx Plugin Configuration
generators?: string;
schematics?: string;
builders?: string;
executors?: string;
'nx-migrations'?: string | NxMigrationsConfiguration;
'ng-update'?: string | NxMigrationsConfiguration;
}
export function readNxMigrateConfig(
json: Partial<PackageJson>
): NxMigrationsConfiguration {
const parseNxMigrationsConfig = (
fromJson?: string | NxMigrationsConfiguration
): NxMigrationsConfiguration => {
if (!fromJson) {
return {};
}
if (typeof fromJson === 'string') {
return { migrations: fromJson, packageGroup: [] };
}
return {
...(fromJson.migrations ? { migrations: fromJson.migrations } : {}),
...(fromJson.packageGroup ? { packageGroup: fromJson.packageGroup } : {}),
};
};
return {
...parseNxMigrationsConfig(json['ng-update']),
...parseNxMigrationsConfig(json['nx-migrations']),
// In case there's a `migrations` field in `package.json`
...parseNxMigrationsConfig(json as any),
};
}
export function buildTargetFromScript(
script: string,
nx: NxProjectPackageJsonConfiguration
) {
const nxTargetConfiguration = nx?.targets?.[script] || {};
return {
...nxTargetConfiguration,
executor: 'nx:run-script',
options: {
...(nxTargetConfiguration.options || {}),
script,
},
};
}
/**
* Uses `require.resolve` to read the package.json for a module.
*
* This will fail if the module doesn't export package.json
*
* @returns package json contents and path
*/
export function readModulePackageJsonWithoutFallbacks(
moduleSpecifier: string,
requirePaths = [workspaceRoot]
): {
packageJson: PackageJson;
path: string;
} {
const packageJsonPath: string = require.resolve(
`${moduleSpecifier}/package.json`,
{
paths: requirePaths,
}
);
const packageJson: PackageJson = readJsonFile(packageJsonPath);
return {
path: packageJsonPath,
packageJson,
};
}
/**
* Reads the package.json file for a specified module.
*
* Includes a fallback that accounts for modules that don't export package.json
*
* @param {string} moduleSpecifier The module to look up
* @param {string[]} requirePaths List of paths look in. Pass `module.paths` to ensure non-hoisted dependencies are found.
*
* @example
* // Use the caller's lookup paths for non-hoisted dependencies
* readModulePackageJson('http-server', module.paths);
*
* @returns package json contents and path
*/
export function readModulePackageJson(
moduleSpecifier: string,
requirePaths = [workspaceRoot]
): {
packageJson: PackageJson;
path: string;
} {
let packageJsonPath: string;
let packageJson: PackageJson;
try {
({ path: packageJsonPath, packageJson } =
readModulePackageJsonWithoutFallbacks(moduleSpecifier, requirePaths));
} catch {
const entryPoint = require.resolve(moduleSpecifier, {
paths: requirePaths,
});
let moduleRootPath = dirname(entryPoint);
packageJsonPath = join(moduleRootPath, 'package.json');
while (!existsSync(packageJsonPath)) {
moduleRootPath = dirname(moduleRootPath);
packageJsonPath = join(moduleRootPath, 'package.json');
}
packageJson = readJsonFile(packageJsonPath);
if (packageJson.name && packageJson.name !== moduleSpecifier) {
throw new Error(
`Found module ${packageJson.name} while trying to locate ${moduleSpecifier}/package.json`
);
}
}
return {
packageJson,
path: packageJsonPath,
};
}