From 4dd82d0260865a781148803659447886b291186c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 27 Sep 2024 12:35:32 +0200 Subject: [PATCH] MOBILE-4616 gulp: Create task to freeze libraries --- gulp/task-freeze-dependencies.js | 78 ++++++++++++++++++++++++++++++++ gulpfile.js | 5 ++ 2 files changed, 83 insertions(+) create mode 100644 gulp/task-freeze-dependencies.js diff --git a/gulp/task-freeze-dependencies.js b/gulp/task-freeze-dependencies.js new file mode 100644 index 00000000000..1ace98191b7 --- /dev/null +++ b/gulp/task-freeze-dependencies.js @@ -0,0 +1,78 @@ +// (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 { readFileSync, writeFile } = require('fs'); + +/** + * Freeze all dependencies versions in package.json using the version declared in package-lock. + */ +class FreezeDependenciesTask { + + /** + * Run the task. + * + * @param done Function to call when done. + */ + run(done) { + const packageData = JSON.parse(readFileSync('package.json')); + const packageLockData = JSON.parse(readFileSync('package-lock.json')); + + this.freezeDependencies(packageLockData, packageData.dependencies); + this.freezeDependencies(packageLockData, packageData.devDependencies); + + writeFile('package.json', JSON.stringify(packageData, null, 4), done); + } + + /** + * Get the version declared in package-lock for a certain dependency. + * + * @param packageLockData Package-lock data. + * @param name Name of the dependency. + */ + getDependencyVersion(packageLockData, name) { + const dependency = packageLockData.packages['node_modules/' + name]; + if (!dependency) { + console.error('Dependency not found in package-lock: ' + name); + return; + } + if (!dependency.version) { + console.error('Dependency found but version is empty in package-lock: ' + name); + return; + } + + return dependency.version; + } + + /** + * Freeze versions of dependencies. + * + * @param packageLockData Package-lock data. + * @param dependencies Object with the dependencies to freeze. + */ + freezeDependencies(packageLockData, dependencies) { + for (const name in dependencies) { + if (dependencies[name].match(/^[0-9A-Za-z]/)) { + // Already fixed, don't change it; + continue; + } + + const version = this.getDependencyVersion(packageLockData, name); + if (version) { + dependencies[name] = version; + } + } + } +} + +module.exports = FreezeDependenciesTask; diff --git a/gulpfile.js b/gulpfile.js index ec4a3f83124..a94a6ac1220 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -17,6 +17,7 @@ const BuildBehatPluginTask = require('./gulp/task-build-behat-plugin'); const BuildEnvTask = require('./gulp/task-build-env'); const BuildIconsJsonTask = require('./gulp/task-build-icons-json'); const OverrideLangTask = require('./gulp/task-override-lang'); +const FreezeDependenciesTask = require('./gulp/task-freeze-dependencies'); const gulp = require('gulp'); const paths = { @@ -48,6 +49,10 @@ gulp.task('icons', (done) => { new BuildIconsJsonTask().run(done); }); +gulp.task('freeze-dependencies', (done) => { + new FreezeDependenciesTask().run(done); +}); + // Build a Moodle plugin to run Behat tests. if (BuildBehatPluginTask.isBehatConfigured()) { gulp.task('behat', (done) => {