generator-jhipster-sentry-module 1.0.4 → 1.0.6

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.
@@ -1,82 +1,33 @@
1
1
  const path = require('path');
2
+ const fs = require('fs');
2
3
  const chalk = require('chalk');
3
4
  const semver = require('semver');
4
-
5
- // Resolve generator-jhipster so the module works when installed globally (peer dependency
6
- // is not under this package's node_modules). Prefer project cwd, then global npm.
7
- function resolveGeneratorJhipster() {
8
- // When installed globally, __dirname is .../node_modules/generator-jhipster-sentry-module/generators/app.
9
- // The folder that contains node_modules (sibling of our package) is one level up from module root = 3 levels from __dirname.
10
- const globalNodeModulesParent = path.resolve(__dirname, '..', '..', '..', '..');
11
- const searchPaths = [
12
- process.cwd(),
13
- path.join(process.cwd(), 'node_modules'),
14
- globalNodeModulesParent
15
- ];
16
- try {
17
- const pkgPath = require.resolve('generator-jhipster/package.json', { paths: searchPaths });
18
- return path.dirname(pkgPath);
19
- } catch (e) {
20
- throw new Error(
21
- 'Could not find generator-jhipster. Install it in your project (npm install generator-jhipster) or globally (npm install -g generator-jhipster).'
22
- );
23
- }
24
- }
25
-
26
- const jhipsterRoot = resolveGeneratorJhipster();
27
- // v8+ publishes built files under dist/generators/<name>/index.js; older versions use generators/<name>.js
28
- function requireJhipsterSubpath(subpath) {
29
- const distPath = path.join(jhipsterRoot, 'dist', 'generators', subpath, 'index.js');
30
- const legacyPath = path.join(jhipsterRoot, 'generators', subpath);
31
- const fs = require('fs');
32
- if (fs.existsSync(distPath)) {
33
- return require(distPath);
34
- }
35
- if (fs.existsSync(legacyPath) || fs.existsSync(`${legacyPath}.js`)) {
36
- return require(legacyPath);
37
- }
38
- throw new Error(`Could not find generator-jhipster subpath: ${subpath}`);
39
- }
40
- const GeneratorBase = requireJhipsterSubpath('generator-base');
41
- const jhipsterConstants = requireJhipsterSubpath('generator-constants');
5
+ const Generator = require('yeoman-generator');
42
6
  const packageJson = require('../../package.json');
43
7
 
44
- module.exports = class extends GeneratorBase {
45
- get initializing() {
46
- return {
47
- init(args) {
48
- if (args === 'default') {
49
- this.message = 'default message';
50
- }
51
- },
52
-
53
- readConfig() {
54
- this.jhipsterAppConfig = this.getAllJhipsterConfig();
55
- if (!this.jhipsterAppConfig) {
56
- this.error('Cannot read .yo-rc.json');
57
- }
58
- },
8
+ // JHipster constants
9
+ const jhipsterConstants = {
10
+ SERVER_MAIN_SRC_DIR: 'src/main/java/',
11
+ SERVER_MAIN_RES_DIR: 'src/main/resources/',
12
+ CLIENT_MAIN_SRC_DIR: 'src/main/webapp/'
13
+ };
59
14
 
60
- displayLogo() {
61
- this.printJHipsterLogo();
62
- this.log(
63
- `\nWelcome to the ${chalk.bold.yellow('JHipster Sentry Module')} generator! ${chalk.yellow(
64
- `v${packageJson.version}\n`
65
- )}`
66
- );
67
- },
15
+ module.exports = class extends Generator {
16
+ async initializing() {
17
+ // Read JHipster config
18
+ this.jhipsterAppConfig = this.fs.readJSON(this.destinationPath('.yo-rc.json'), {})['generator-jhipster'];
19
+ if (!this.jhipsterAppConfig) {
20
+ this.log(chalk.red('Cannot read .yo-rc.json or JHipster config missing'));
21
+ process.exit(1);
22
+ }
68
23
 
69
- checkJhipster() {
70
- const currentJhipsterVersion = this.jhipsterAppConfig.jhipsterVersion;
71
- const minimumJhipsterVersion = '>=8.0.0';
24
+ this.log(`\nWelcome to the ${chalk.bold.yellow('JHipster Sentry Module')} generator! ${chalk.yellow(`v${packageJson.version}\n`)}`);
72
25
 
73
- if (!semver.satisfies(currentJhipsterVersion, minimumJhipsterVersion)) {
74
- this.warning(
75
- `\nYour generated project uses JHipster ${currentJhipsterVersion}. Minimum required is ${minimumJhipsterVersion}\n`
76
- );
77
- }
78
- }
79
- };
26
+ // Check JHipster version
27
+ const currentJhipsterVersion = this.jhipsterAppConfig.jhipsterVersion;
28
+ if (!semver.satisfies(currentJhipsterVersion, '>=8.0.0')) {
29
+ this.log(chalk.yellow(`Warning: Your JHipster version is ${currentJhipsterVersion}, minimum required is 8.0.0\n`));
30
+ }
80
31
  }
81
32
 
82
33
  async prompting() {
@@ -94,7 +45,6 @@ module.exports = class extends GeneratorBase {
94
45
  message: 'Enter your Sentry DSN'
95
46
  }
96
47
  ];
97
-
98
48
  this.promptAnswers = await this.prompt(prompts);
99
49
  }
100
50
 
@@ -107,8 +57,6 @@ module.exports = class extends GeneratorBase {
107
57
  this.clientPackageManager = this.jhipsterAppConfig.clientPackageManager;
108
58
  this.buildTool = this.jhipsterAppConfig.buildTool;
109
59
 
110
- this.angularAppName = this.getAngularAppName();
111
-
112
60
  const javaDir = `${jhipsterConstants.SERVER_MAIN_SRC_DIR}${this.packageFolder}/`;
113
61
  const resourceDir = jhipsterConstants.SERVER_MAIN_RES_DIR;
114
62
  const webappDir = jhipsterConstants.CLIENT_MAIN_SRC_DIR;
@@ -118,16 +66,11 @@ module.exports = class extends GeneratorBase {
118
66
  enabled: this.promptAnswers.enableSentry,
119
67
  dsn: this.promptAnswers.sentryDsn
120
68
  };
121
-
122
- // Write to .yo-rc.json safely
123
- const fs = require('fs');
124
69
  const configPath = this.destinationPath('.yo-rc.json');
125
- if (fs.existsSync(configPath)) {
126
- const config = this.fs.readJSON(configPath, {});
127
- config['generator-jhipster'] = config['generator-jhipster'] || {};
128
- config['generator-jhipster'].sentry = this.jhipsterAppConfig.sentry;
129
- this.fs.writeJSON(configPath, config, null, 2);
130
- }
70
+ const config = this.fs.readJSON(configPath, {});
71
+ config['generator-jhipster'] = config['generator-jhipster'] || {};
72
+ config['generator-jhipster'].sentry = this.jhipsterAppConfig.sentry;
73
+ this.fs.writeJSON(configPath, config, null, 2);
131
74
 
132
75
  // Logging
133
76
  this.log('\n--- JHipster Config ---');
@@ -135,12 +78,6 @@ module.exports = class extends GeneratorBase {
135
78
  this.log(`packageName=${this.packageName}`);
136
79
  this.log(`clientFramework=${this.clientFramework}`);
137
80
  this.log(`buildTool=${this.buildTool}`);
138
-
139
- this.log('\n--- Directories ---');
140
- this.log(`javaDir=${javaDir}`);
141
- this.log(`resourceDir=${resourceDir}`);
142
- this.log(`webappDir=${webappDir}`);
143
-
144
81
  this.log('\n--- Sentry ---');
145
82
  this.log(`enabled=${this.promptAnswers.enableSentry}`);
146
83
  this.log('----------------------\n');
@@ -150,54 +87,59 @@ module.exports = class extends GeneratorBase {
150
87
  return;
151
88
  }
152
89
 
153
- // Backend
90
+ // Backend dependencies
154
91
  if (this.buildTool === 'maven') {
155
- this.addMavenDependency('io.sentry', 'sentry-spring-boot-starter', '7.3.0');
92
+ const pomPath = this.destinationPath('pom.xml');
93
+ if (fs.existsSync(pomPath)) {
94
+ let pom = fs.readFileSync(pomPath, 'utf8');
95
+ if (!pom.includes('sentry-spring-boot-starter')) {
96
+ pom = pom.replace(
97
+ /<\/dependencies>/,
98
+ ` <dependency>
99
+ <groupId>io.sentry</groupId>
100
+ <artifactId>sentry-spring-boot-starter</artifactId>
101
+ <version>7.3.0</version>
102
+ </dependency>
103
+ </dependencies>`
104
+ );
105
+ fs.writeFileSync(pomPath, pom);
106
+ }
107
+ }
156
108
  }
109
+
157
110
  if (this.buildTool === 'gradle') {
158
- this.addGradleDependency('implementation', 'io.sentry:sentry-spring-boot-starter:7.3.0');
111
+ const buildGradle = this.destinationPath('build.gradle');
112
+ if (fs.existsSync(buildGradle)) {
113
+ let gradle = fs.readFileSync(buildGradle, 'utf8');
114
+ if (!gradle.includes('sentry-spring-boot-starter')) {
115
+ gradle += '\ndependencies {\n implementation "io.sentry:sentry-spring-boot-starter:7.3.0"\n}\n';
116
+ fs.writeFileSync(buildGradle, gradle);
117
+ }
118
+ }
159
119
  }
160
- this.template('server/sentry-application.yml.ejs', `${resourceDir}config/sentry.yml`);
161
120
 
162
- // Frontend
121
+ // Frontend templates
163
122
  if (this.clientFramework === 'react') {
164
- this.addNpmDependency('@sentry/react', '^7.3.0');
165
- this.template('client/react/sentry.ts.ejs', `${webappDir}app/sentry.ts`);
166
- }
167
- if (this.clientFramework === 'angularX') {
168
- this.addNpmDependency('@sentry/angular', '^7.3.0');
169
- this.template('client/angular/sentry.ts.ejs', `${webappDir}app/sentry.ts`);
170
- }
171
-
172
- // Register module hook
173
- try {
174
- this.registerModule(
175
- 'generator-jhipster-sentry-module',
176
- 'app',
177
- 'post',
178
- 'app',
179
- 'Adds Sentry integration'
180
- );
181
- } catch (err) {
182
- this.log(`${chalk.red.bold('WARN!')} Could not register JHipster hook`);
123
+ this.fs.copyTpl(this.templatePath('client/react/sentry.ts.ejs'), `${webappDir}app/sentry.ts`);
124
+ } else if (this.clientFramework === 'angularX') {
125
+ this.fs.copyTpl(this.templatePath('client/angular/sentry.ts.ejs'), `${webappDir}app/sentry.ts`);
183
126
  }
184
127
  }
185
128
 
186
129
  install() {
187
- const logMsg = `To install dependencies manually, run: ${chalk.yellow.bold(
188
- `${this.clientPackageManager} install`
189
- )}`;
130
+ if (!this.promptAnswers.enableSentry) return;
190
131
 
191
- if (this.options['skip-install']) {
192
- this.log(logMsg);
193
- return;
132
+ const packages = [];
133
+ if (this.clientFramework === 'react') {
134
+ packages.push('@sentry/react@^7.3.0');
135
+ } else if (this.clientFramework === 'angularX') {
136
+ packages.push('@sentry/angular@^7.3.0');
194
137
  }
195
138
 
196
- this.installDependencies({
197
- npm: this.clientPackageManager === 'npm',
198
- yarn: this.clientPackageManager === 'yarn',
199
- bower: false
200
- });
139
+ if (packages.length > 0) {
140
+ const cmd = this.clientPackageManager === 'yarn' ? 'add' : 'install';
141
+ this.spawnCommandSync(this.clientPackageManager, [...(this.clientPackageManager === 'yarn' ? ['add'] : ['install']), ...packages]);
142
+ }
201
143
  }
202
144
 
203
145
  end() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generator-jhipster-sentry-module",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Adding Sentry integration to JHipster projects",
5
5
  "keywords": [
6
6
  "yeoman-generator",