-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MOBILE-4616 gulp: Create task to freeze libraries
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters