Skip to content

Instantly share code, notes, and snippets.

@reneolivo
Created June 22, 2020 23:24
Show Gist options
  • Save reneolivo/8a2e98cf0a6f2c151f7ff3bb8f26d83b to your computer and use it in GitHub Desktop.
Save reneolivo/8a2e98cf0a6f2c151f7ff3bb8f26d83b to your computer and use it in GitHub Desktop.
List and Update Drupal Modules
const glob = require('glob');
const { readdirSync, createReadStream } = require('fs');
const { createInterface } = require('readline');
const { execSync } = require('child_process');
// 1) - For listing all modules and their versions:
// (async () => console.table(await getModulesList()))();
// 2) - For updating modules one by one:
// runUpdateModule(process.argv[2], process.argv[3]);
// downloads the update for the given module and replaces the existing one.
function runUpdateModule(moduleName, newVersion) {
const zipFileName = `${moduleName}-7.x-${newVersion}.zip`;
const moduleFileUrl = `https://ftp.drupal.org/files/projects/${zipFileName}`;
execSync(`rm -r -f ./${moduleName}`);
execSync(`wget ${moduleFileUrl}`);
execSync(`unzip ${zipFileName}`);
execSync(`rm ${zipFileName}`);
}
// returns a list of modules and their version
async function getModulesList () {
const moduleInfoPaths = glob.sync('./*/*.info');
return await Promise.all(moduleInfoPaths.map(async (moduleInfoPath) => {
const moduleName = moduleInfoPath.split('/')[1];
const versionLine = await searchForLineInFile(moduleInfoPath, 'version = ');
const version = /7\.?x-([^"]+)/.exec(versionLine)[1];
return {
moduleName,
version,
};
}));
}
// searches a whole file and returns the line matching the required string
function searchForLineInFile(fileName, searchFor) {
const lineReader = createInterface({
input: createReadStream(fileName)
});
return new Promise((resolve) => {
lineReader.on('line', (line) => {
if (line.includes(searchFor)) {
resolve(line);
lineReader.close();
}
});
lineReader.on('end', resolve);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment