Skip to content

Commit a248afe

Browse files
mbartoofftherailz
authored andcommitted
Some improvements to projects ecosystem: aliases, proxy configuration interactive createProject script (#3701)
1 parent 4e91b4b commit a248afe

18 files changed

Lines changed: 377 additions & 243 deletions

build/buildConfig.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,30 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
77
const path = require('path');
88
const ParallelUglifyPlugin = require("webpack-parallel-uglify-plugin");
99

10-
module.exports = (bundles, themeEntries, paths, extractThemesPlugin, prod, publicPath, cssPrefix, chunks) => ({
10+
/**
11+
* Webpack configuration builder.
12+
* Returns a webpack configuration object for the given parameters.
13+
*
14+
* @param {object} bundles object that defines the javascript (or jsx) entry points and related bundles
15+
* to be built (bundle name -> entry point path)
16+
* @param {object} themeEntries object that defines the css (or less) entry points and related bundles
17+
* to be built (bundle name -> entry point path)
18+
* @param {object} paths object with paths used by the configuration builder:
19+
* - dist: path to the output folder for the bundles
20+
* - base: root folder of the project
21+
* - framework: root folder of the MapStore2 framework
22+
* - code: root folder(s) for javascript / jsx code, can be an array with several folders (e.g. framework code and
23+
* project code)
24+
* @param {object} extractThemesPlugin plugin to be used for bundling css (usually defined in themes.js)
25+
* @param {boolean} prod flag for production / development mode (true = production)
26+
* @param {string} publicPath web public path for loading bundles (e.g. dist/)
27+
* @param {string} cssPrefix prefix to be appended on every generated css rule (e.g. ms2)
28+
* @param {array} prodPlugins plugins to be used only in production mode
29+
* @param {object} alias aliases to be used by webpack to resolve paths (alias -> real path)
30+
* @param {object} proxy webpack-devserver custom proxy configuration object
31+
* @returns a webpack configuration object
32+
*/
33+
module.exports = (bundles, themeEntries, paths, extractThemesPlugin, prod, publicPath, cssPrefix, prodPlugins, alias = {}, proxy) => ({
1134
entry: assign({
1235
'webpack-dev-server': 'webpack-dev-server/client?http://0.0.0.0:8081', // WebpackDevServer host and port
1336
'webpack': 'webpack/hot/only-dev-server' // "only" prevents reload on syntax errors
@@ -49,15 +72,16 @@ module.exports = (bundles, themeEntries, paths, extractThemesPlugin, prod, publi
4972
new NormalModuleReplacementPlugin(/proj4$/, path.join(paths.framework, "libs", "proj4")),
5073
new NoEmitOnErrorsPlugin(),
5174
extractThemesPlugin
52-
].concat(prod && chunks || []).concat(prod ? [new ParallelUglifyPlugin({
75+
].concat(prod && prodPlugins || []).concat(prod ? [new ParallelUglifyPlugin({
5376
uglifyJS: {
5477
sourceMap: false,
5578
compress: {warnings: false},
5679
mangle: true
5780
}
5881
})] : []),
5982
resolve: {
60-
extensions: [".js", ".jsx"]
83+
extensions: [".js", ".jsx"],
84+
alias: alias
6185
},
6286
module: {
6387
noParse: [/html2canvas/],
@@ -139,7 +163,7 @@ module.exports = (bundles, themeEntries, paths, extractThemesPlugin, prod, publi
139163
}] : [])
140164
},
141165
devServer: {
142-
proxy: {
166+
proxy: proxy || {
143167
'/rest/geostore': {
144168
target: "https://dev.mapstore.geo-solutions.it/mapstore",
145169
secure: false,

build/testConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = ({files, path, testFile, singleRun, basePath = "."}) => ({
1+
module.exports = ({files, path, testFile, singleRun, basePath = ".", alias = {}}) => ({
22
browsers: [ 'Chrome' ],
33

44
browserNoActivityTimeout: 30000,
@@ -100,7 +100,8 @@ module.exports = ({files, path, testFile, singleRun, basePath = "."}) => ({
100100
]
101101
},
102102
resolve: {
103-
extensions: ['.js', '.json', '.jsx']
103+
extensions: ['.js', '.json', '.jsx'],
104+
alias: alias
104105
}
105106
},
106107
webpackServer: {

createProject.js

Lines changed: 116 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,124 @@
1-
const projectType = process.argv[2];
2-
const projectName = process.argv[3];
3-
const projectVersion = process.argv[4];
4-
const projectDescription = process.argv[5];
5-
const repoURL = process.argv[6];
6-
const outFolder = process.argv[7];
1+
let projectType = process.argv[2];
2+
let projectName = process.argv[3];
3+
let projectVersion = process.argv[4];
4+
let projectDescription = process.argv[5];
5+
let repoURL = process.argv[6];
6+
let outFolder = process.argv[7];
77
const project = require('./utility/projects/projectLib');
88
const denodeify = require('denodeify');
9+
const readline = require('readline-promise').default;
910

10-
if (!projectType || !projectName || !projectVersion || !projectDescription || !repoURL || !outFolder) {
11-
process.stdout.write('Usage: node ./createProject.js <projectType> <projectName> <projectVersion> <projectDescription> <GitHUB repo URL> <outputFolder>\n');
12-
throw new Error("Wrong parameters!");
13-
}
11+
const paramsDesc = [{
12+
label: 'Project Type (standard): ',
13+
name: 'projectType',
14+
"default": 'standard',
15+
validate: () => true
16+
}, {
17+
label: 'Project Name: ',
18+
name: 'projectName',
19+
"default": '',
20+
validate: (val) => val !== ''
21+
}, {
22+
label: 'ProjectVersion (1.0.0): ',
23+
name: 'projectVersion',
24+
"default": '1.0.0',
25+
validate: () => true
26+
}, {
27+
label: 'ProjectDescription: ',
28+
name: 'projectDescription',
29+
"default": '',
30+
validate: (val) => val !== ''
31+
}, {
32+
label: 'Repository URL: ',
33+
name: 'repoURL',
34+
"default": '',
35+
validate: (val) => val !== ''
36+
}, {
37+
label: 'Output folder: ',
38+
name: 'outFolder',
39+
"default": '',
40+
validate: (val) => val !== ''
41+
}];
1442

15-
const fs = require('fs');
16-
const mkdirp = denodeify(require('mkdirp'));
17-
const readdir = denodeify(fs.readdir);
43+
function doWork(params) {
44+
const fs = require('fs');
45+
const mkdirp = denodeify(require('mkdirp'));
46+
const readdir = denodeify(fs.readdir);
1847

19-
const options = {
20-
name: projectName,
21-
version: projectVersion,
22-
description: projectDescription,
23-
repository: repoURL,
24-
scripts: require('./utility/projects/projectScripts.json')
25-
};
48+
const options = {
49+
name: params.projectName,
50+
version: params.projectVersion,
51+
description: params.projectDescription,
52+
repository: params.repoURL,
53+
scripts: require('./utility/projects/projectScripts.json')
54+
};
2655

27-
const projectFolder = './project/' + projectType;
56+
const projectFolder = './project/' + params.projectType;
2857

29-
readdir(projectFolder)
30-
.then(() => {
31-
return mkdirp(outFolder);
32-
})
33-
.then(() => {
34-
process.stdout.write('Out folder created (' + outFolder + ')\n');
35-
return project.createPackageJSON(options, outFolder);
36-
})
37-
.then(() => {
38-
process.stdout.write('package.json file created\n');
39-
return project.copyStaticFiles(projectFolder + '/static', outFolder, options, ['.editorconfig', '.eslintrc', '.eslintignore', 'LICENSE.txt', 'web/client/.babelrc']);
40-
})
41-
.then(() => {
42-
process.stdout.write('Static files copied\n');
43-
process.stdout.write('Copying template files\n');
44-
return project.copyTemplates(projectFolder + '/templates', outFolder, options);
45-
})
46-
.then(() => {
47-
process.stdout.write('Templates copied\n');
48-
return project.initGit(outFolder);
49-
})
50-
.then(() => {
51-
process.stdout.write('git repo OK!\n');
52-
})
53-
.catch((err) => {
54-
process.stderr.write(err + '\n');
58+
readdir(projectFolder)
59+
.then(() => {
60+
return mkdirp(params.outFolder);
61+
})
62+
.then(() => {
63+
process.stdout.write('Out folder created (' + params.outFolder + ')\n');
64+
return project.createPackageJSON(options, params.outFolder);
65+
})
66+
.then(() => {
67+
process.stdout.write('package.json file created\n');
68+
return project.copyStaticFiles(projectFolder + '/static', params.outFolder, options, ['.editorconfig', '.eslintrc', '.eslintignore', 'LICENSE.txt', 'web/client/.babelrc']);
69+
})
70+
.then(() => {
71+
process.stdout.write('Static files copied\n');
72+
process.stdout.write('Copying template files\n');
73+
return project.copyTemplates(projectFolder + '/templates', params.outFolder, options);
74+
})
75+
.then(() => {
76+
process.stdout.write('Templates copied\n');
77+
return project.initGit(params.outFolder);
78+
})
79+
.then(() => {
80+
process.stdout.write('git repo OK!\n');
81+
process.exit();
82+
})
83+
.catch((err) => {
84+
process.stderr.write(err + '\n');
85+
});
86+
}
87+
88+
function readParam(rl, params, result) {
89+
return new Promise((resolve, reject) => {
90+
if (params.length === 0) {
91+
resolve(result);
92+
} else {
93+
const [param, ...other] = params;
94+
rl.questionAsync(param.label).then((answer) => {
95+
result[param.name] = answer || param.default;
96+
if (param.validate(result[param.name])) {
97+
resolve(readParam(rl, other, result));
98+
} else {
99+
reject(result);
100+
}
101+
});
102+
}
55103
});
104+
}
105+
106+
function readParams() {
107+
const rl = readline.createInterface({
108+
input: process.stdin,
109+
output: process.stdout
110+
});
111+
return readParam(rl, paramsDesc, {});
112+
}
113+
if (process.argv.length === 2) {
114+
readParams().then((params) => {
115+
doWork(params);
116+
}).catch(() => {
117+
throw new Error("Wrong parameters!");
118+
});
119+
} else if (!projectType || !projectName || !projectVersion || !projectDescription || !repoURL || !outFolder) {
120+
process.stdout.write('Usage: node ./createProject.js <projectType> <projectName> <projectVersion> <projectDescription> <GitHUB repo URL> <outputFolder>\n');
121+
throw new Error("Wrong parameters!");
122+
} else {
123+
doWork({projectType, projectName, projectDescription, projectVersion, repoURL, outFolder});
124+
}

docs/developer-guide/project-creation-script.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Create your own MapStore project
22

3-
To create a new MapStore based project you can use the createProject script:
3+
To create a new MapStore based project you can use the createProject script.
4+
First of all, if you don't have done it before, clone the MapStore2 repository master branch into a local folder:
5+
6+
```sh
7+
git clone https://github.com/geosolutions-it/MapStore2
8+
```
9+
10+
Then, move into the folder that has just been created, containing MapStore2:
11+
12+
```sh
13+
cd MapStore2
14+
```
15+
16+
Finally, to create the project, use the following command:
417

518
```sh
619
node ./createProject.js <projectType> <projectName> <projectVersion> <projectDescription> <gitRepositoryUrl> <outputFolder>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"react-motion": "0.5.0",
6262
"react-transform-catch-errors": "1.0.2",
6363
"react-transition-group": "1.1.3",
64+
"readline-promise": "1.0.4",
6465
"redbox-react": "1.3.6",
6566
"redux-devtools": "3.4.0",
6667
"redux-devtools-dock-monitor": "1.1.2",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2015",
4+
"allowSyntheticDefaultImports": false,
5+
"baseUrl": "./",
6+
"paths": {
7+
"@mapstore/*": [
8+
"MapStore2/web/client/*"
9+
],
10+
"@js/*": [
11+
"js/*"
12+
]
13+
}
14+
},
15+
"exclude": [
16+
"node_modules",
17+
"dist"
18+
]
19+
}

project/custom/static/karma.conf.continuous-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ module.exports = function karmaConfig(config) {
104104
]
105105
},
106106
resolve: {
107-
extensions: ['.js', '.json', '.jsx']
107+
extensions: ['.js', '.json', '.jsx'],
108+
alias: {
109+
"@mapstore": path.resolve(__dirname, "MapStore2", "web", "client"),
110+
"@js": path.resolve(__dirname, "js")
111+
}
108112
}
109113
},
110114

project/custom/static/karma.conf.single-run.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ module.exports = function karmaConfig(config) {
115115
]
116116
},
117117
resolve: {
118-
extensions: ['.js', '.json', '.jsx']
118+
extensions: ['.js', '.json', '.jsx'],
119+
alias: {
120+
"@mapstore": path.resolve(__dirname, "MapStore2", "web", "client"),
121+
"@js": path.resolve(__dirname, "js")
122+
}
119123
}
120124
},
121125

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1 @@
1-
var webpackConfig = require('./webpack.config.js');
2-
var path = require("path");
3-
var LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");
4-
var ParallelUglifyPlugin = require("webpack-parallel-uglify-plugin");
5-
var DefinePlugin = require("webpack/lib/DefinePlugin");
6-
var NormalModuleReplacementPlugin = require("webpack/lib/NormalModuleReplacementPlugin");
7-
const extractThemesPlugin = require('./MapStore2/build/themes.js').extractThemesPlugin;
8-
var CopyWebpackPlugin = require('copy-webpack-plugin');
9-
10-
webpackConfig.plugins = [
11-
new CopyWebpackPlugin([
12-
{ from: path.join(__dirname, 'node_modules', 'bootstrap', 'less'), to: path.join(__dirname, "web", "client", "dist", "bootstrap", "less") }
13-
]),
14-
new LoaderOptionsPlugin({
15-
debug: false,
16-
options: {
17-
postcss: {
18-
plugins: [
19-
require('postcss-prefix-selector')({prefix: '.__PROJECTNAME__', exclude: ['.__PROJECTNAME__', '.ms2', '[data-ms2-container]']})
20-
]
21-
},
22-
context: __dirname
23-
}
24-
}),
25-
new DefinePlugin({
26-
"__DEVTOOLS__": false
27-
}),
28-
new DefinePlugin({
29-
'process.env': {
30-
'NODE_ENV': '"production"'
31-
}
32-
}),
33-
new NormalModuleReplacementPlugin(/leaflet$/, path.join(__dirname, "MapStore2", "web", "client", "libs", "leaflet")),
34-
new NormalModuleReplacementPlugin(/openlayers$/, path.join(__dirname, "MapStore2", "web", "client", "libs", "openlayers")),
35-
new NormalModuleReplacementPlugin(/cesium$/, path.join(__dirname, "MapStore2", "web", "client", "libs", "cesium")),
36-
new NormalModuleReplacementPlugin(/proj4$/, path.join(__dirname, "MapStore2", "web", "client", "libs", "proj4")),
37-
new ParallelUglifyPlugin({
38-
uglifyJS: {
39-
sourceMap: false,
40-
compress: {warnings: false},
41-
mangle: true
42-
}
43-
}),
44-
extractThemesPlugin
45-
];
46-
webpackConfig.devtool = undefined;
47-
48-
// this is a workaround for this issue https://github.com/webpack/file-loader/issues/3
49-
// use `__webpack_public_path__` in the index.html when fixed
50-
webpackConfig.output.publicPath = "/__PROJECTNAME__/dist/";
51-
52-
module.exports = webpackConfig;
1+
module.exports = require('./webpack.config.js');

0 commit comments

Comments
 (0)