Skip to content

Instantly share code, notes, and snippets.

@troZee
Created December 15, 2022 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troZee/d8346fe4558b6f2d2177283f73c24413 to your computer and use it in GitHub Desktop.
Save troZee/d8346fe4558b6f2d2177283f73c24413 to your computer and use it in GitHub Desktop.
Check npm last publish date for each dependency inside a package.json file
const fs = require('fs');
const { exec } = require('child_process');
function execute(command, callback) {
exec(command, function (error, stdout, stderr) {
if (stderr || error) {
// console.log(`πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ ${stderr} ${error}`);
} else {
callback(stdout);
}
});
}
// Read the contents of the package.json file
const packageJson = fs.readFileSync('package.json', 'utf8');
// Parse the JSON to get the dependencies object
const dependencies = JSON.parse(packageJson).dependencies;
// Print the dependencies
Object.entries(dependencies).forEach((element) => {
const depName = element[0];
execute(`npm view ${depName} version`, (version) => {
execute(`npm view ${depName} time --json`, (data) => {
const parse = JSON.parse(data);
const trimVersion = version.trim();
const date = parse[trimVersion];
if (new Date(date).getFullYear() < new Date().getFullYear()) {
console.log(`${depName} ${trimVersion} ${date}`);
}
});
});
});
@troZee
Copy link
Author

troZee commented Jan 12, 2024

const fs = require('fs');
const { exec } = require('child_process');

function execute(command, callback) {
  exec(command, function (error, stdout, stderr) {
    if (stderr || error) {
      //   console.log(`πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ ${stderr} ${error}`);
    } else {
      callback(stdout);
    }
  });
}

// Read the contents of the package.json file
const packageJson = fs.readFileSync('package.json', 'utf8');

// Parse the JSON to get the dependencies object
const dependencies = JSON.parse(packageJson).dependencies;
const dependenciesList = Object.entries(dependencies)
const array = [];
let counter = 0;
// Print the dependencies
dependenciesList.forEach((element,index) => {
  const depName = element[0];

  execute(`npm view ${depName} version`, (version) => {
    execute(`npm view ${depName} time --json`, (data) => {
      const parse = JSON.parse(data);
      const currentVersion = version.trim();
      const theLatestVersion = Object.keys(parse).pop()
      const theLatestDate = parse[theLatestVersion]
      const date = parse[currentVersion];

      if (new Date(date).getFullYear() < new Date().getFullYear()) {
        console.log(`${depName} ${currentVersion} ${date}`);
        array.push({depName, currentVersion, theLatestVersion, date, theLatestDate})
      }
      counter = counter + 1
      if (counter === dependenciesList.length) {
        console.table(array, ["depName", "currentVersion", "date", "theLatestVersion", "theLatestDate"]);
      }
    });
  });
});

@troZee
Copy link
Author

troZee commented Jan 12, 2024

const fs = require('fs');
const { exec } = require('child_process');

function execute(command, callback) {
  exec(command, function (error, stdout, stderr) {
    if (stderr || error) {
      //   console.log(`πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ ${stderr} ${error}`);
    } else {
      callback(stdout);
    }
  });
}

// Read the contents of the package.json file
const packageJson = fs.readFileSync('package.json', 'utf8');

// Parse the JSON to get the dependencies object
const dependencies = JSON.parse(packageJson).dependencies;
const dependenciesList = Object.entries(dependencies)
const array = [];
let counter = 0;
// Print the dependencies
dependenciesList.forEach((element,index) => {
  const depName = element[0];

  execute(`npm view ${depName} version`, (version) => {
    execute(`npm view ${depName} time --json`, (data) => {
      const parse = JSON.parse(data);
      const currentVersion = version.trim();
      const theLatestVersion = Object.keys(parse).pop()
      const theLatestDate = parse[theLatestVersion]
      const date = parse[currentVersion];

      const isMoreThanYear = (new Date().getTime() - new Date(date).getTime()) / (1000 * 3600 * 24 * 365) > 1

      if (isMoreThanYear) {
        console.log(`${depName} ${currentVersion} ${date}`);
        array.push({depName, currentVersion, theLatestVersion, date, theLatestDate})
      }
      counter = counter + 1
      if (counter === dependenciesList.length) {
        console.table(array, ["depName", "currentVersion", "date", "theLatestVersion", "theLatestDate"]);
      }
    });
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment