Skip to content

Commit 9639c26

Browse files
authored
Merge pull request #4190 from dpalou/MOBILE-4616
Mobile 4616
2 parents ad12f89 + 4f970b7 commit 9639c26

File tree

6 files changed

+801
-1385
lines changed

6 files changed

+801
-1385
lines changed

cordova-plugin-moodleapp/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cordova-plugin-moodleapp/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"lint": "tsc --noEmit"
1212
},
1313
"devDependencies": {
14-
"chokidar-cli": "^3.0.0",
15-
"concurrently": "^8.2.0",
16-
"esbuild": "^0.18.20",
17-
"typescript": "^5.3.3"
14+
"chokidar-cli": "3.0.0",
15+
"concurrently": "8.2.2",
16+
"esbuild": "0.18.20",
17+
"typescript": "5.5.3"
1818
}
1919
}

gulp/task-freeze-dependencies.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const { readFileSync, writeFile } = require('fs');
16+
17+
/**
18+
* Freeze all dependencies versions in package.json using the version declared in package-lock.
19+
*/
20+
class FreezeDependenciesTask {
21+
22+
/**
23+
* Run the task.
24+
*
25+
* @param done Function to call when done.
26+
*/
27+
run(done) {
28+
const packageData = JSON.parse(readFileSync('package.json'));
29+
const packageLockData = JSON.parse(readFileSync('package-lock.json'));
30+
31+
this.freezeDependencies(packageLockData, packageData.dependencies);
32+
this.freezeDependencies(packageLockData, packageData.devDependencies);
33+
34+
writeFile('package.json', JSON.stringify(packageData, null, 4), done);
35+
}
36+
37+
/**
38+
* Get the version declared in package-lock for a certain dependency.
39+
*
40+
* @param packageLockData Package-lock data.
41+
* @param name Name of the dependency.
42+
*/
43+
getDependencyVersion(packageLockData, name) {
44+
const dependency = packageLockData.packages['node_modules/' + name];
45+
if (!dependency) {
46+
console.error('Dependency not found in package-lock: ' + name);
47+
return;
48+
}
49+
if (!dependency.version) {
50+
console.error('Dependency found but version is empty in package-lock: ' + name);
51+
return;
52+
}
53+
54+
return dependency.version;
55+
}
56+
57+
/**
58+
* Freeze versions of dependencies.
59+
*
60+
* @param packageLockData Package-lock data.
61+
* @param dependencies Object with the dependencies to freeze.
62+
*/
63+
freezeDependencies(packageLockData, dependencies) {
64+
for (const name in dependencies) {
65+
if (dependencies[name].match(/^[0-9A-Za-z]/)) {
66+
// Already fixed, don't change it;
67+
continue;
68+
}
69+
70+
const version = this.getDependencyVersion(packageLockData, name);
71+
if (version) {
72+
dependencies[name] = version;
73+
}
74+
}
75+
}
76+
}
77+
78+
module.exports = FreezeDependenciesTask;

gulpfile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const BuildBehatPluginTask = require('./gulp/task-build-behat-plugin');
1717
const BuildEnvTask = require('./gulp/task-build-env');
1818
const BuildIconsJsonTask = require('./gulp/task-build-icons-json');
1919
const OverrideLangTask = require('./gulp/task-override-lang');
20+
const FreezeDependenciesTask = require('./gulp/task-freeze-dependencies');
2021
const gulp = require('gulp');
2122

2223
const paths = {
@@ -48,6 +49,10 @@ gulp.task('icons', (done) => {
4849
new BuildIconsJsonTask().run(done);
4950
});
5051

52+
gulp.task('freeze-dependencies', (done) => {
53+
new FreezeDependenciesTask().run(done);
54+
});
55+
5156
// Build a Moodle plugin to run Behat tests.
5257
if (BuildBehatPluginTask.isBehatConfigured()) {
5358
gulp.task('behat', (done) => {

0 commit comments

Comments
 (0)