forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.ts
More file actions
149 lines (136 loc) · 5.3 KB
/
Copy pathgenerator.ts
File metadata and controls
149 lines (136 loc) · 5.3 KB
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
import assert from 'node:assert';
import { basename, extname, resolve } from 'node:path';
import { globSync } from 'tinyglobby';
import BaseGenerator from '../../generators/base-core/index.ts';
import type { Config } from '../../generators/base-core/types.ts';
import { packageJson } from '../../lib/index.ts';
import { promptSamplesFolder } from '../support.ts';
const GENERATOR_JDL = 'jdl';
const GENERATOR_APP = 'app';
const GENERATOR_INFO = 'info';
import { entitiesByType, generateSample } from './support/index.ts';
export default class extends BaseGenerator<Config & { entities: string[] }> {
sampleName!: string;
global?: boolean;
projectFolder!: string;
projectVersion?: string;
entitiesSample!: string;
sampleYorcFolder?: boolean;
sampleOnly?: boolean;
sample?: Awaited<ReturnType<typeof generateSample>>;
get [BaseGenerator.INITIALIZING]() {
return this.asAnyTaskGroup({
projectVersion() {
this.projectVersion = `${packageJson.version}-git`;
},
});
}
get [BaseGenerator.PROMPTING]() {
return this.asAnyTaskGroup({
async promptOptions() {
if (this.global) {
await promptSamplesFolder.call(this);
} else if (!process.env.CI && !this.projectFolder && basename(process.cwd()) !== this.sampleName) {
const answers = await this.prompt([
{
type: 'confirm',
name: 'confirmProjectFolder',
message: `Do you wan't to generate the sample in a folder named ${this.sampleName}?`,
default: true,
},
]);
if (answers.confirmProjectFolder) {
this.projectFolder = resolve(this.sampleName);
}
}
},
});
}
get [BaseGenerator.WRITING]() {
return this.asAnyTaskGroup({
async copySample() {
if (extname(this.sampleName) === '.jdl') {
this.copyTemplate(`samples/${this.sampleName}`, this.sampleName);
} else if (this.sampleYorcFolder) {
this.copyTemplate(`test-integration/${this.sampleName}/.yo-rc.json`, '.yo-rc.json');
const entitiesFiles = entitiesByType[this.entitiesSample];
if (entitiesFiles) {
this.jhipsterConfig.entities = entitiesFiles;
this.log.info(`Copying entities ${this.entitiesSample} (${entitiesFiles})`);
this.copyTemplate(
entitiesFiles.map(entity => `.jhipster/${entity}.json`),
this.projectFolder,
{ noGlob: true, fromBasePath: this.templatePath('test-integration/samples/') },
);
}
} else {
this.sample = await generateSample(this.sampleName, {
destProjectFolder: this.projectFolder,
entity: this.entitiesSample,
memFs: this.fs,
});
}
},
});
}
get [BaseGenerator.END]() {
return this.asAnyTaskGroup({
async generateJdlSample() {
if (extname(this.sampleName) !== '.jdl' || this.sampleOnly) return;
await this.composeWithJHipster(GENERATOR_JDL, {
generatorArgs: [this.sampleName],
generatorOptions: { projectVersion: this.projectVersion, destinationRoot: this.projectFolder },
});
},
async generateSample() {
if (extname(this.sampleName) === '.jdl' || this.sampleYorcFolder || this.sampleOnly) return;
const sample = this.sample;
assert.ok(sample, `Sample ${this.sampleName} not found`);
let generatorOptions: any = {
projectVersion: this.projectVersion,
destinationRoot: this.projectFolder,
...sample.sample.generatorOptions,
};
if (sample.sample.workspaces && sample.sample.workspaces !== 'false') {
generatorOptions = { ...generatorOptions, workspaces: true, monorepository: true };
}
if (sample.generator === 'jdl') {
const files = globSync('*.jdl');
await this.composeWithJHipster(GENERATOR_JDL, {
generatorArgs: files,
generatorOptions,
});
} else {
if (sample.jdlFiles) {
const files = globSync('*.jdl');
await this.composeWithJHipster(GENERATOR_JDL, {
generatorArgs: files,
generatorOptions: { ...generatorOptions, jsonOnly: true, destinationRoot: this.projectFolder },
});
}
await this.composeWithJHipster(GENERATOR_APP, { generatorOptions });
}
},
async generateYoRcSample() {
if (!this.sampleYorcFolder || this.sampleOnly) return;
const yoRc = (this.fs.readJSON(`${this.projectFolder}/.yo-rc.json`) as any)?.['generator-jhipster'] as unknown as
| typeof this.jhipsterConfig
| undefined;
const defaultCommand = yoRc?.defaultCommand ?? GENERATOR_APP;
await this.composeWithJHipster(defaultCommand, { generatorOptions: { destinationRoot: this.projectFolder } });
},
async updateVscodeWorkspace() {
if (this.global) {
await this.composeWithJHipster('@jhipster/jhipster-dev:code-workspace', {
generatorOptions: {
samplePath: this.sampleName,
},
} as any);
}
},
async info() {
await this.composeWithJHipster(GENERATOR_INFO, { generatorOptions: { destinationRoot: this.projectFolder } });
},
});
}
}