MOBILE-3689 config: Migrate config build to gulp

main
Noel De Martin 2021-02-01 17:27:28 +01:00
parent f2f8ae664d
commit bc1d0ee338
16 changed files with 150 additions and 189 deletions

5
.gitignore vendored
View File

@ -29,5 +29,8 @@ npm-debug.log*
/plugins /plugins
/www /www
/config/config.*.json /moodle.*.config.json
!/moodle.example.config.json
/src/assets/lang/* /src/assets/lang/*
/src/assets/env.json

View File

@ -5,4 +5,3 @@ script:
- npm run lint - npm run lint
- npm run test:ci - npm run test:ci
- npm run build:prod - npm run build:prod

View File

@ -1,8 +1,8 @@
{ {
"files.associations": { "files.associations": {
"config.json": "jsonc", "moodle.config.json": "jsonc",
"config.*.json": "jsonc", "moodle.*.config.json": "jsonc",
}, },
} }

View File

@ -12,7 +12,7 @@
"schematics": {}, "schematics": {},
"architect": { "architect": {
"build": { "build": {
"builder": "@angular-builders/custom-webpack:browser", "builder": "@angular-devkit/build-angular:browser",
"options": { "options": {
"outputPath": "www", "outputPath": "www",
"index": "src/index.html", "index": "src/index.html",
@ -36,10 +36,7 @@
"input": "src/theme/global.scss" "input": "src/theme/global.scss"
} }
], ],
"scripts": [], "scripts": []
"customWebpackConfig": {
"path": "./config/webpack.config.js"
}
}, },
"configurations": { "configurations": {
"production": { "production": {
@ -66,7 +63,7 @@
} }
}, },
"serve": { "serve": {
"builder": "@angular-builders/custom-webpack:dev-server", "builder": "@angular-devkit/build-angular:dev-server",
"options": { "options": {
"browserTarget": "app:build" "browserTarget": "app:build"
}, },
@ -89,9 +86,9 @@
"builder": "@angular-eslint/builder:lint", "builder": "@angular-eslint/builder:lint",
"options": { "options": {
"lintFilePatterns": [ "lintFilePatterns": [
"src/**/*.ts", "src/**/*.ts",
"src/core/**/*.html", "src/core/**/*.html",
"src/addons/**/*.html" "src/addons/**/*.html"
] ]
} }
}, },

View File

@ -1,40 +0,0 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const webpack = require('webpack');
const { getConfig, getBuild } = require('./utils');
const { resolve } = require('path');
module.exports = config => {
config.resolve.alias['@'] = resolve('src');
config.resolve.alias['@classes'] = resolve('src/core/classes');
config.resolve.alias['@components'] = resolve('src/core/components');
config.resolve.alias['@directives'] = resolve('src/core/directives');
config.resolve.alias['@features'] = resolve('src/core/features');
config.resolve.alias['@guards'] = resolve('src/core/guards');
config.resolve.alias['@pipes'] = resolve('src/core/pipes');
config.resolve.alias['@services'] = resolve('src/core/services');
config.resolve.alias['@singletons'] = resolve('src/core/singletons');
config.plugins.push(
new webpack.DefinePlugin({
'window.MoodleApp': {
CONFIG: JSON.stringify(getConfig(process.env.NODE_ENV || 'development')),
BUILD: JSON.stringify(getBuild(process.env.NODE_ENV || 'development')),
},
}),
);
return config;
};

View File

@ -0,0 +1,41 @@
// (C) Copyright 2015 Moodle Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const { getConfig, getBuild } = require('../scripts/env-utils');
const { resolve } = require('path');
const { writeFile } = require('fs');
/**
* Task to build an env file depending on the current environment.
*/
class BuildEnvTask {
/**
* Run the task.
*
* @param done Function to call when done.
*/
run(done) {
const envFile = resolve(__dirname, '../src/assets/env.json');
const env = {
CONFIG: getConfig(process.env.NODE_ENV || 'development'),
BUILD: getBuild(process.env.NODE_ENV || 'development'),
};
writeFile(envFile, JSON.stringify(env), done);
}
}
module.exports = BuildEnvTask;

View File

@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
const BuildLangTask = require('./gulp/task-build-lang'); const BuildLangTask = require('./gulp/task-build-lang');
const BuildEnvTask = require('./gulp/task-build-env');
const PushTask = require('./gulp/task-push'); const PushTask = require('./gulp/task-push');
const Utils = require('./gulp/utils'); const Utils = require('./gulp/utils');
const gulp = require('gulp'); const gulp = require('gulp');
@ -34,12 +35,18 @@ gulp.task('lang', (done) => {
new BuildLangTask().run(paths.lang, done); new BuildLangTask().run(paths.lang, done);
}); });
// Build an env file depending on the current environment.
gulp.task('env', (done) => {
new BuildEnvTask().run(done);
});
gulp.task('push', (done) => { gulp.task('push', (done) => {
new PushTask().run(args, done); new PushTask().run(args, done);
}); });
gulp.task('default', gulp.parallel('lang')); gulp.task('default', gulp.parallel(['lang', 'env']));
gulp.task('watch', () => { gulp.task('watch', () => {
gulp.watch(paths.lang, { interval: 500 }, gulp.parallel('lang')); gulp.watch(paths.lang, { interval: 500 }, gulp.parallel('lang'));
gulp.watch(['./moodle.config.json', './moodle.*.config.json'], { interval: 500 }, gulp.parallel('env'));
}); });

View File

@ -1,9 +1,8 @@
/** /**
* Application config. * Application config.
* *
* You can create your own environment files such as "config.prod.json" and "config.dev.json" * You can create your own environment files such as "moodle.config.prod.json" and "moodle.config.dev.json"
* to override some values. The values will be merged, so you don't need to duplicate everything * to override some values. The values will be merged, so you don't need to duplicate everything in this file.
* in this file.
*/ */
{ {

79
package-lock.json generated
View File

@ -4,58 +4,29 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@angular-builders/custom-webpack": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/@angular-builders/custom-webpack/-/custom-webpack-10.0.1.tgz",
"integrity": "sha512-YDy5zEKVwXdoXLjmbsY6kGaEbmunQxaPipxrwLUc9hIjRLU2WcrX9vopf1R9Pgj4POad73IPBNGu+ibqNRFIEQ==",
"dev": true,
"requires": {
"@angular-devkit/architect": ">=0.1000.0 < 0.1100.0",
"@angular-devkit/build-angular": ">=0.1000.0 < 0.1100.0",
"@angular-devkit/core": "^10.0.0",
"lodash": "^4.17.15",
"ts-node": "^9.0.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"ts-node": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.0.0.tgz",
"integrity": "sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==",
"dev": true,
"requires": {
"arg": "^4.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"source-map-support": "^0.5.17",
"yn": "3.1.1"
}
}
}
},
"@angular-devkit/architect": { "@angular-devkit/architect": {
"version": "0.1001.4", "version": "0.1101.2",
"resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.1001.4.tgz", "resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.1101.2.tgz",
"integrity": "sha512-0U/w+61vWxnEe9Ln/hNOH6O27FVcU+s/sbJAuPREbP875R4bQzK2PX0eYRlISzkDtQyw16GzlsikLWOoJ3vjTA==", "integrity": "sha512-MLmBfHiiyPhbFSSAX4oMecPjEuBauOui5uBpI6BKNnk/7783fznbkbAKjXlOco7M81gkNeEoHMR8c+mOfcvv7g==",
"dev": true, "dev": true,
"requires": { "requires": {
"@angular-devkit/core": "10.1.4", "@angular-devkit/core": "11.1.2",
"rxjs": "6.6.2" "rxjs": "6.6.3"
}, },
"dependencies": { "dependencies": {
"rxjs": { "rxjs": {
"version": "6.6.2", "version": "6.6.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.2.tgz", "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz",
"integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==", "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"tslib": "^1.9.0" "tslib": "^1.9.0"
} }
}, },
"tslib": { "tslib": {
"version": "1.13.0", "version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"dev": true "dev": true
} }
} }
@ -249,22 +220,22 @@
} }
}, },
"@angular-devkit/core": { "@angular-devkit/core": {
"version": "10.1.4", "version": "11.1.2",
"resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-10.1.4.tgz", "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-11.1.2.tgz",
"integrity": "sha512-B1cwVcfChBvmEacydE2uqZ1UC2ez1G+KY0GyVnCQKpAb/DdfDgtaYjTx9JLvGQjE/BlVklEj8YCKDjVV0WPE5g==", "integrity": "sha512-V7zOMqL2l56JcwXVyswkG+7+t67r9XtkrVzRcG2Z5ZYwafU+iKWMwg5kBFZr1SX7fM1M9E4MpskxqtagQeUKng==",
"dev": true, "dev": true,
"requires": { "requires": {
"ajv": "6.12.4", "ajv": "6.12.6",
"fast-json-stable-stringify": "2.1.0", "fast-json-stable-stringify": "2.1.0",
"magic-string": "0.25.7", "magic-string": "0.25.7",
"rxjs": "6.6.2", "rxjs": "6.6.3",
"source-map": "0.7.3" "source-map": "0.7.3"
}, },
"dependencies": { "dependencies": {
"ajv": { "ajv": {
"version": "6.12.4", "version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.4.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
"integrity": "sha512-eienB2c9qVQs2KWexhkrdMLVDoIQCz5KSeLxwg9Lzk4DOfBtIK9PQwwufcsn1jjGuf9WZmqPMbGxOzfcuphJCQ==", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
"dev": true, "dev": true,
"requires": { "requires": {
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
@ -274,18 +245,18 @@
} }
}, },
"rxjs": { "rxjs": {
"version": "6.6.2", "version": "6.6.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.2.tgz", "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz",
"integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==", "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"tslib": "^1.9.0" "tslib": "^1.9.0"
} }
}, },
"tslib": { "tslib": {
"version": "1.13.0", "version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"dev": true "dev": true
} }
} }

View File

@ -22,10 +22,10 @@
"start": "ionic serve", "start": "ionic serve",
"build": "ionic build", "build": "ionic build",
"build:prod": "ionic build --prod", "build:prod": "ionic build --prod",
"test": "jest --verbose", "test": "NODE_ENV=testing gulp && jest --verbose",
"test:ci": "jest -ci --runInBand --verbose", "test:ci": "NODE_ENV=testing gulp && jest -ci --runInBand --verbose",
"test:watch": "jest --watch", "test:watch": "NODE_ENV=testing gulp watch & jest --watch",
"test:coverage": "jest --coverage", "test:coverage": "NODE_ENV=testing gulp && jest --coverage",
"lint": "ng lint", "lint": "ng lint",
"ionic:serve:before": "gulp", "ionic:serve:before": "gulp",
"ionic:serve": "gulp watch & ng serve", "ionic:serve": "gulp watch & ng serve",
@ -119,7 +119,7 @@
"zone.js": "~0.10.3" "zone.js": "~0.10.3"
}, },
"devDependencies": { "devDependencies": {
"@angular-builders/custom-webpack": "^10.0.1", "@angular-devkit/architect": "^0.1101.2",
"@angular-devkit/build-angular": "~0.1000.0", "@angular-devkit/build-angular": "~0.1000.0",
"@angular-eslint/builder": "0.5.0-beta.2", "@angular-eslint/builder": "0.5.0-beta.2",
"@angular-eslint/eslint-plugin": "0.5.0-beta.2", "@angular-eslint/eslint-plugin": "0.5.0-beta.2",

View File

@ -23,9 +23,9 @@ function getConfig(environment) {
development: ['dev', 'development'], development: ['dev', 'development'],
production: ['prod', 'production'], production: ['prod', 'production'],
}; };
const config = parseJsonc(readFileSync(resolve('config/config.json')).toString()); const config = parseJsonc(readFileSync(resolve(__dirname, '../moodle.config.json')).toString());
const envSuffixes = (envSuffixesMap[environment] || []); const envSuffixes = (envSuffixesMap[environment] || []);
const envConfigPath = envSuffixes.map(suffix => resolve(`config/config.${suffix}.json`)).find(existsSync); const envConfigPath = envSuffixes.map(suffix => resolve(__dirname, `../moodle.${suffix}.config.json`)).find(existsSync);
if (envConfigPath) { if (envConfigPath) {
const envConfig = parseJsonc(readFileSync(envConfigPath).toString()); const envConfig = parseJsonc(readFileSync(envConfigPath).toString());

View File

@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
/* eslint-disable @typescript-eslint/naming-convention */
import { CoreColorScheme } from '@features/settings/services/settings-helper';
import { CoreSitesDemoSiteData } from '@services/sites';
import envJson from '@/assets/env.json';
/** /**
* Context levels enumeration. * Context levels enumeration.
*/ */
@ -114,7 +120,51 @@ export class CoreConstants {
static readonly MOD_ARCHETYPE_SYSTEM = 3; // System (not user-addable) module archetype. static readonly MOD_ARCHETYPE_SYSTEM = 3; // System (not user-addable) module archetype.
// Config & environment constants. // Config & environment constants.
static readonly CONFIG = (window as unknown as MoodleAppWindow).MoodleApp.CONFIG; // Data parsed from config.json files. static readonly CONFIG = envJson.CONFIG as unknown as EnvironmentConfig; // Data parsed from config.json files.
static readonly BUILD = (window as unknown as MoodleAppWindow).MoodleApp.BUILD; // Environment info. static readonly BUILD = envJson.BUILD as unknown as EnvironmentBuild; // Build info.
} }
type EnvironmentConfig = {
app_id: string;
appname: string;
versioncode: number;
versionname: string;
cache_update_frequency_usually: number;
cache_update_frequency_often: number;
cache_update_frequency_sometimes: number;
cache_update_frequency_rarely: number;
default_lang: string;
languages: Record<string, string>;
wsservice: string;
wsextservice: string;
demo_sites: Record<string, CoreSitesDemoSiteData>;
font_sizes: number[];
customurlscheme: string;
siteurl: string;
sitename: string;
multisitesdisplay: string;
sitefindersettings: Record<string, unknown>;
onlyallowlistedsites: boolean;
skipssoconfirmation: boolean;
forcedefaultlanguage: boolean;
privacypolicy: string;
notificoncolor: string;
enableanalytics: boolean;
enableonboarding: boolean;
forceColorScheme: CoreColorScheme;
forceLoginLogo: boolean;
ioswebviewscheme: string;
appstores: Record<string, string>;
displayqroncredentialscreen?: boolean;
displayqronsitescreen?: boolean;
forceOpenLinksIn: 'app' | 'browser';
};
type EnvironmentBuild = {
isProduction: boolean;
isTesting: boolean;
isDevelopment: boolean;
lastCommitHash: string;
compilationTime: number;
};

View File

@ -19,7 +19,6 @@ import { CoreSite, CoreSiteWSPreSets } from '@classes/site';
import { CoreError } from '@classes/errors/error'; import { CoreError } from '@classes/errors/error';
import { makeSingleton, Translate } from '@singletons'; import { makeSingleton, Translate } from '@singletons';
import { CoreWSExternalWarning } from '@services/ws'; import { CoreWSExternalWarning } from '@services/ws';
import { CoreCourseBase } from '@/types/global';
const ROOT_CACHE_KEY = 'mmGroups:'; const ROOT_CACHE_KEY = 'mmGroups:';

View File

@ -12,13 +12,4 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
/* eslint-disable @typescript-eslint/naming-convention */
import 'jest-preset-angular'; import 'jest-preset-angular';
import { getConfig, getBuild } from '../../config/utils';
(window as unknown as MoodleAppWindow).MoodleApp = {
CONFIG: getConfig('testing'),
BUILD: getBuild('testing'),
};

62
src/types/global.d.ts vendored
View File

@ -13,69 +13,13 @@
// limitations under the License. // limitations under the License.
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
interface Window {
import { CoreColorScheme } from '@features/settings/services/settings-helper'; __Zone_disable_customElements: boolean;
import { CoreSitesDemoSiteData } from '@services/sites';
declare global {
interface Window {
__Zone_disable_customElements: boolean;
}
type MoodleAppWindow = {
MoodleApp: {
CONFIG: {
app_id: string;
appname: string;
versioncode: number;
versionname: string;
cache_update_frequency_usually: number;
cache_update_frequency_often: number;
cache_update_frequency_sometimes: number;
cache_update_frequency_rarely: number;
default_lang: string;
languages: Record<string, string>;
wsservice: string;
wsextservice: string;
demo_sites: Record<string, CoreSitesDemoSiteData>;
font_sizes: number[];
customurlscheme: string;
siteurl: string;
sitename: string;
multisitesdisplay: string;
sitefindersettings: Record<string, unknown>;
onlyallowlistedsites: boolean;
skipssoconfirmation: boolean;
forcedefaultlanguage: boolean;
privacypolicy: string;
notificoncolor: string;
enableanalytics: boolean;
enableonboarding: boolean;
forceColorScheme: CoreColorScheme;
forceLoginLogo: boolean;
ioswebviewscheme: string;
appstores: Record<string, string>;
displayqroncredentialscreen?: boolean;
displayqronsitescreen?: boolean;
forceOpenLinksIn: 'app' | 'browser';
};
BUILD: {
isProduction: boolean;
isTesting: boolean;
isDevelopment: boolean;
lastCommitHash: string;
compilationTime: number;
};
};
};
} }
/** /**
* Course base definition. * Course base definition.
*/ */
export type CoreCourseBase = { type CoreCourseBase = {
id: number; // Course Id. id: number; // Course Id.
}; };