How to use the semver.compare function in semver

To help you get started, we’ve selected a few semver examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github quicktype / quicktype / script / patch-npm-version.ts View on Github external
import * as semver from "semver";

function exec(command: string) {
  const result = shell.exec(command, { silent: true });
  return (result.stdout as string).trim();
}

const PUBLISHED = (() => {
  // Get the highest published version of any tag
  const all = JSON.parse(exec(`npm show quicktype versions --json`));
  return all[all.length - 1];
})();

const CURRENT = exec(`npm version`).match(/quicktype: '(.+)'/)[1];

switch (semver.compare(CURRENT, PUBLISHED)) {
  case -1:
    console.error(
      `* package.json version is ${CURRENT} but ${PUBLISHED} is published. Patching...`
    );
    exec(`npm version ${PUBLISHED} --force --no-git-tag-version`);
    shell.exec(`npm version patch --no-git-tag-version`);
    break;
  case 0:
    console.error(
      `* package.json version is ${CURRENT} but ${PUBLISHED} is published. Patching...`
    );
    shell.exec(`npm version patch --no-git-tag-version`);
    break;
  default:
    // Greater than published, nothing to do
    break;
github johandb / svg-drawing-tool / node_modules / @schematics / update / migrate / index.js View on Github external
schematicsToRun.sort((a, b) => {
            const cmp = semver.compare(a.version, b.version);
            // Revert to comparing the names of the collection if the versions are equal.
            return cmp == 0 ? a.name.localeCompare(b.name) : cmp;
        });
        if (schematicsToRun.length > 0) {
github adaptlearning / adapt_authoring / plugins / output / adapt / helpers.js View on Github external
return cb(null, {category: 'green-install', authoringToolVersion: app.polyglot.t('app.none')});
        }
        return cb(null, {category: 'red', authoringToolVersion: app.polyglot.t('app.none')});
      }

      // A version is already installed so could be:
      // White - same version installed as in course
      // Red - invalid version
      // Amber - newer version installed and will be used in the course instead
      // Green - older version installed so this newer version will be upgraded in the AT
      var serverPlugin = records[0];
      if (!importPluginVersionValid || !semver.valid(serverPlugin.version)) {
        return cb(null, {category: 'red', authoringToolVersion: serverPlugin.version});
      }
      var serverPluginVersion = semver.clean(serverPlugin.version);
      var isServerVersionLatest = semver.compare(serverPluginVersion, pluginMetaData.version);
      if (isServerVersionLatest === 0) {
        return cb(null, {category: 'white', authoringToolVersion: app.polyglot.t('app.none')});
      }
      if (semver.satisfies(serverFrameworkVersion, pluginFrameworkVersion, {
          includePrerelease: true
        }) && isServerVersionLatest < 0) {
        return cb(null, {category: 'green-update', authoringToolVersion: serverPluginVersion});
      }
      cb(null, {category: 'amber', authoringToolVersion: serverPluginVersion});
    }
  ], function(error, category) {
github seedjs / seed / tests / semver.js View on Github external
Ct.test('semver.compare()', function(t) {
  t.equal(semver.compare('1.9.0', '1.10.0'), -1);
  t.equal(semver.compare('1.11.0', '1.10.0'), 1);
  t.equal(semver.compare('2.0.1', '1.10.0'),  1);
  t.equal(semver.compare('2.0.1', '2.0.1'), 0);

  t.equal(semver.compare('1.0.0beta1', '1.0.0beta2'), -1);
  t.equal(semver.compare('1.0.0', '1.0.0beta2'), 1);

  t.equal(semver.compare('1.0.0beta10', '1.0.0beta2'), 1);
  t.equal(semver.compare('1.0.0beta20.1', '1.0.0beta20.2'), -1);
  t.equal(semver.compare('1.0.0beta20.1', '1.0.0'), -1);

  t.equal(semver.compare('1.0.0beta1.9', '1.0.0beta100'), -1);
});
github atlassian / nucleus / src / migrations / latest-installer / LatestInstallerMigration.ts View on Github external
        for (const version of rolledOutVersions.sort((a, b) => semver.compare(a.name, b.name))) {
          for (const file of version.files) {
github appcelerator / titanium_mobile / build / scons-deprecations.js View on Github external
function compareVersions(a, b) {
	return semver.compare(a, b);
}
github semantic-release / semantic-release / lib / get-release-to-add.js View on Github external
  ).sort((a, b) => semver.compare(b.version, a.version))[0];
github jsynowiec / airqmon / src / data / update-checker.ts View on Github external
            .sort((r1, r2) => semver.compare(r1.tag_name, r2.tag_name));
github ctx-core / ctx-core / packages / monorepo / lib.ts View on Github external
async function update__dependencies(dependencies) {
		const update_a1 = []
		for (let package_name in dependencies) {
			const dependency_workspace = workspaces[package_name]
			const version = dependencies[package_name]
			const has_carrot = version.slice(0, 1) === '^'
			if (dependency_workspace) {
				const { location } = dependency_workspace
				const pkg = JSON.parse(
					(await readFile(`${location}/package.json`)).toString()
				)
				const latest_version =
					`${version.slice(0, 1) === '^' ? '^' : ''}${pkg.version}`
				package_name__x__latest_version[package_name] = pkg.version
				if (compare(coerce(latest_version), coerce(version)) > 0) {
					push__update_a1(update_a1, package_name, version, latest_version)
					dependencies[package_name] = latest_version
				}
			} else {
				if (!valid(coerce(dependencies[package_name]))) continue
				if (!package_name__x__latest_version[package_name]) {
					const promise = queue.add(async ()=>
						(
							await exec(`npm show ${package_name}@latest | grep latest | grep \\: | cut -f2 -d: | xargs echo`)
						).stdout.trim()
					)
					package_name__x__latest_version[package_name] = promise
				}
				if (package_name__x__latest_version[package_name].then) {
					package_name__x__latest_version[package_name] =
						await package_name__x__latest_version[package_name]