forked from SAP/yeoman-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
192 lines (161 loc) · 7.11 KB
/
env.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
192
import * as _ from "lodash";
import { homedir } from "os";
import * as path from "path";
import { existsSync } from "fs";
import { isWin32, NpmCommand } from "./npm";
import * as customLocation from "./customLocation";
import * as Environment from "yeoman-environment";
import TerminalAdapter = require("yeoman-environment/lib/adapter");
import { IChildLogger } from "@vscode-logging/logger";
import { getClassLogger } from "../logger/logger-wrapper";
const GENERATOR = "generator-";
const NAMESPACE = "namespace";
export type EnvGen = {
env: Environment<Environment.Options>;
gen: any;
};
export type GeneratorData = {
generatorMeta: Environment.LookupGeneratorMeta;
generatorPackageJson: any;
};
export class GeneratorNotFoundError extends Error {
constructor(message: string) {
super(message);
}
}
class EnvUtil {
private logger: IChildLogger;
private existingNpmPathsPromise: Promise<string[]>;
private allInstalledGensMeta: Environment.LookupGeneratorMeta[];
constructor() {
try {
this.logger = getClassLogger(EnvUtil.name);
} catch (e) {
// nothing TODO : testing scope
}
this.loadNpmPath();
}
public loadNpmPath(force = false) {
if (_.isNil(this.existingNpmPathsPromise) || force === true) {
this.existingNpmPathsPromise = this.getExistingNpmPath().then((paths) => {
this.logger?.debug("existing npm paths", { paths: paths.join(";") });
return paths;
});
}
return this;
}
private getEnvNpmPath(): Promise<string[]> {
return new Promise((resolve) => {
setTimeout(() => {
// this operation takes up to 2 seconds
// it should be wrapped with setTimeout to provide promise like behaviour
resolve(this.createEnvInstance().getNpmPaths());
}, 1);
});
}
private async getExistingNpmPath(): Promise<string[]> {
const globalNpmPaths: string[] = await this.getEnvNpmPath();
const userNpmPaths = homedir()
.split(path.sep)
.map((part, index, parts) => {
const resPath = path.join(...parts.slice(0, index + 1), "node_modules");
return isWin32 ? resPath : path.join(path.sep, resPath);
});
// uniq and existing only paths (global npm path is always added)
const paths: string[] = _.union(globalNpmPaths, userNpmPaths).filter((npmPath: string) => existsSync(npmPath));
paths.push(await NpmCommand.getGlobalNodeModulesPath());
return _.uniq(paths);
}
private createEnvInstance(
args?: string | string[],
opts?: Environment.Options,
adapter?: TerminalAdapter
): Environment<Environment.Options> {
return Environment.createEnv(args, opts, adapter);
}
private unloadGeneratorModules(genNamespace: string): void {
let generatorName;
const genShortName = Environment.namespaceToName(genNamespace);
if (genShortName.startsWith("@")) {
const firstSlashIndex = genShortName.indexOf("/");
generatorName = `${GENERATOR}${genShortName.substring(firstSlashIndex + 1)}`;
} else {
generatorName = `${GENERATOR}${genShortName}`;
}
const keys = Object.keys(require.cache);
for (const key of keys) {
if (key.includes(generatorName)) {
delete require.cache[key];
}
}
}
private lookupGensMeta(options?: Environment.LookupOptions): Environment.LookupGeneratorMeta[] {
return this.createEnvInstance().lookup(options);
}
// returns installed generators meta from global and custom installation location
// custom installation generators have priority over global installed generators when names are identical
private async lookupAllGensMeta(): Promise<Environment.LookupGeneratorMeta[]> {
const globallyInstalledGensMeta = this.lookupGensMeta({ npmPaths: await this.existingNpmPathsPromise });
const customNpmPath = customLocation.getNodeModulesPath();
const customInstalledGensMeta = _.isEmpty(customNpmPath) ? [] : this.lookupGensMeta({ npmPaths: [customNpmPath] });
const gensMeta = _.unionBy(customInstalledGensMeta, globallyInstalledGensMeta, NAMESPACE);
return _.orderBy(gensMeta, [NAMESPACE], ["asc"]);
}
private async getGenMetadata(genNamespace: string): Promise<Environment.LookupGeneratorMeta> {
this.allInstalledGensMeta = await this.lookupAllGensMeta();
const genMetadata = this.allInstalledGensMeta.find((genMeta) => genMeta.namespace === genNamespace);
if (genMetadata) {
return genMetadata;
}
throw new GeneratorNotFoundError(`${genNamespace} generator metadata was not found.`);
}
private genMainGensMeta(gensMeta: Environment.LookupGeneratorMeta[]): Environment.LookupGeneratorMeta[] {
return gensMeta.filter((genMeta) => genMeta.namespace.endsWith(":app"));
}
private async getGensMetaByInstallationPath(): Promise<Environment.LookupGeneratorMeta[]> {
const npmInstallationPaths = [customLocation.getNodeModulesPath() ?? (await NpmCommand.getGlobalNodeModulesPath())];
return this.lookupGensMeta({ npmPaths: npmInstallationPaths });
}
private async getGeneratorsMeta(mainOnly = true): Promise<Environment.LookupGeneratorMeta[]> {
this.allInstalledGensMeta = await this.lookupAllGensMeta();
return mainOnly ? this.genMainGensMeta(this.allInstalledGensMeta) : this.allInstalledGensMeta;
}
public async getAllGeneratorNamespaces(): Promise<string[]> {
const gensMeta: Environment.LookupGeneratorMeta[] = await this.getGeneratorsMeta(false);
return _.map(gensMeta, (genMeta) => genMeta.namespace);
}
public async createEnvAndGen(genNamespace: string, options: any, adapter: any): Promise<EnvGen> {
const meta: Environment.LookupGeneratorMeta = await this.getGenMetadata(genNamespace);
this.unloadGeneratorModules(genNamespace);
const env: Environment<Environment.Options> = this.createEnvInstance(
undefined,
{ sharedOptions: { forwardErrorToEnvironment: true } },
adapter
);
// @types/yeoman-environment bug: generatorPath is still not exposed on LookupGeneratorMeta
env.register(_.get(meta, "generatorPath"), genNamespace, meta.packagePath);
const gen = env.create(genNamespace, { options });
return { env, gen };
}
public async getGeneratorsData(mainOnly = true): Promise<GeneratorData[]> {
const gensMeta: Environment.LookupGeneratorMeta[] = await this.getGeneratorsMeta(mainOnly);
const packageJsons = await NpmCommand.getPackageJsons(gensMeta);
const gensData = packageJsons.map((generatorPackageJson: any | undefined, index: number) => {
if (generatorPackageJson) {
const generatorMeta = gensMeta[index];
return { generatorMeta, generatorPackageJson };
}
});
return _.compact(gensData);
}
public async getGeneratorNamesWithOutdatedVersion(): Promise<string[]> {
const gensMeta: Environment.LookupGeneratorMeta[] = await this.getGensMetaByInstallationPath();
return NpmCommand.getPackageNamesWithOutdatedVersion(gensMeta);
}
public getGeneratorFullName(genNamespace: string): string {
const genName = Environment.namespaceToName(genNamespace);
const parts = _.split(genName, "/");
return _.size(parts) === 1 ? `${GENERATOR}${genName}` : `${parts[0]}/${GENERATOR}${parts[1]}`;
}
}
export const Env = new EnvUtil();