forked from liferay/liferay-js-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla-portlet.ts
104 lines (90 loc) · 2.55 KB
/
vanilla-portlet.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
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
import path from 'path';
import Generator from 'yeoman-generator';
import {Copier, promptWithConfig} from '../utils';
import ProjectAnalyzer from '../utils/ProjectAnalyzer';
import NpmbuildrcModifier from '../utils/modifier/npmbuildrc';
import PkgJsonModifier from '../utils/modifier/package.json';
import * as standardTarget from '../utils/target/standard';
/**
* Implementation of generation of plain JavaScript portlets.
*/
export default class extends Generator {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
answers: any;
namespace: string;
/**
* Standard Yeoman initialization function
*/
initializing() {
this.sourceRoot(path.join(__dirname, 'templates'));
this.namespace = 'target-vanilla-portlet';
}
/**
* Standard Yeoman prompt function
*/
async prompting() {
this.answers = await promptWithConfig(this, [
{
type: 'confirm',
name: 'useBabel',
message:
'Do you want to use Babel to transpile JavaScript sources?',
default: true,
},
]);
await standardTarget.prompting(this);
}
/**
* Standard Yeoman generation function
*/
writing() {
const cp = new Copier(this);
const npmbuildrc = new NpmbuildrcModifier(this);
const pkgJson = new PkgJsonModifier(this);
const projectAnalyzer = new ProjectAnalyzer(this);
const {useBabel} = this.answers;
// Configure build
if (useBabel) {
pkgJson.addDevDependency('babel-cli', '6.26.0');
pkgJson.addDevDependency('babel-preset-env', '1.7.0');
pkgJson.addBuildStep('babel --source-maps -d build src');
cp.copyFile('.babelrc');
} else {
pkgJson.addBuildStep('npm run copy-sources');
pkgJson.addScript('copy-sources', 'lnbs-copy-sources');
}
// Configure webpack
if (useBabel) {
pkgJson.addDevDependency('babel-loader', '7.1.5');
npmbuildrc.addWebpackRule(/src\/.*\.js$/, 'babel-loader');
}
// Prepare text labels
const labels = standardTarget.generateLabels(this);
// Prepare context
const context = standardTarget.generateContext(this, {
labels:
labels[
projectAnalyzer.hasLocalization
? useBabel
? 'template'
: 'js'
: useBabel
? 'raw'
: 'quoted'
],
});
// Copy JavaScript files
pkgJson.setMain('index.js');
cp.copyFile(`src/index.${useBabel ? 'babel' : 'nobabel'}.js`, {
context,
dest: 'src/index.js',
});
// Generate sample contents
standardTarget.generateSamples(this, labels);
}
}
module.exports = exports['default'];