Skip to content

Instantly share code, notes, and snippets.

@ruyadorno
Last active January 27, 2021 17:33
Show Gist options
  • Save ruyadorno/3e44ed884d6d0fb791467cbdf6c0125e to your computer and use it in GitHub Desktop.
Save ruyadorno/3e44ed884d6d0fb791467cbdf6c0125e to your computer and use it in GitHub Desktop.
What arguments mean in npm diff

npm diff args

Special conversions:

  • IF NO arg:
    • READ package.json name
      • IF FOUND:
        • RETURN package-json-name@tag - file:.
      • ELSE:
        • THROW usage
  • IF arg is package-name only:
    • READ arborist load actual tree AND PARSE package-name
      • IF NOT SPEC TYPE REGISTRY:
        • THROW unsupported spec type
      • IF FOUND:
        • RETURN package-name@FOUND_VERSION
      • ELSE:
        • RETURN package-name@latest
  • IF arg is validl semver range|version:
    • READ package-name from either arg1 or arg2
      • IF FOUND:
        • RETURN other-arg-parsed-name@arg
      • ELSE:
        • READ package.json name
          • IF FOUND:
            • RETURN package-json-name@arg
          • IF NO package.json:
            • THROW error need to run from package dir to user versions

Arguments combinations and their results

No args (ie, npm diff)

  1. Equivalent to npm diff file:. $(package.json name)@latest

Note: Default tag name can be configured via --tag config.

Using 1 argument (ie, npm diff --diff=arg1)

  1. arg is a known dependency name (ie, npm diff depname)
    • equivalent to npm diff node_modules/${arg} ${arg}@${wanted version}
  2. arg is a known dep name with spec (ie npm diff depname@version)
    • equivalent to npm diff node_modules/${depname} ${arg}
  3. arg is a SemVer range (including single versions) (ie, npm diff 1.x)
    • equivalent to npm diff ${package.json name}@${arg} file:.
  4. arg is a package name not a dependency with spec (ie, npm diff [email protected])
  5. arg is a package name not a dependency with NO SPEC (ie, npm diff pkg)
    • equivalent to npm diff arg@latest file:.

With 2 arguments provided (ie, npm diff --diff=${arg1} --diff=${arg2})

  1. first arg is a known dependency name

    1. second arg is a known dependency name
      • equivalent to npm diff ${nm/arg1.name@nm/arg1.version} ${nm/arg2.name@nm/arg2.version}
    2. second arg is a known dependency name with spec
      • equivalent to npm diff ${nm/arg1.name@nm/arg1.version} arg2
    3. second arg is a SemVer range (including single versions)
      • equivalent to npm diff ${nm/arg1.name@nm/arg1.version} ${nm/arg1.name@arg2}
    4. second arg is a package name not a dependency with spec
      • equivalent to npm diff ${nm/arg1.name@nm/arg1.version} arg2
    5. second arg is a package name not a dependency NO spec
      • equivalent to npm diff ${nm/arg1.name@nm/arg1.version} ${arg2@latest}
  2. first arg is a known dep name with spec (ie, depname@tag)

    1. second arg is a known dependency name
      • equivalent to npm diff arg1 ${nm/arg2}
    2. second arg is a known dependency name with spec
      • equivalent to npm diff arg1 arg2
    3. second arg is a SemVer range (including single versions)
      • equivalent to npm diff arg1 ${nm/arg1.name}@arg2
    4. second arg is a package name not a dependency with spec
      • equivalent to npm diff arg1 arg2
    5. second arg is a package name not a dependency NO spec
      • equivalent to npm diff arg1 arg2@latest
  3. first arg is a SemVer range (including single versions)

    1. second arg is a known dependency name
      • equivalent to npm diff ${nm/arg2.name}@arg1 ${nm/arg2.name@nm/arg2.version}
    2. second arg is a known dependency name with spec
      • equivalent to npm diff ${nm/arg2.name}@arg1 arg2
    3. second arg is a SemVer range (including single versions)
      • equivalent to npm diff ${package-json-name}@arg1 ${package-json-name}@arg2
    4. second arg is a package name not a dependency with spec
      • equivalent to npm diff ${arg2.name}@arg1 arg2
    5. second arg is a package name not a dependency NO spec
      • equivalent to npm diff ${arg2}@arg1 arg2@latest
  4. first arg is a package name not a dependency with spec

    1. second arg is a known dependency name
      • equivalent to npm diff arg1 ${nm/arg2.name@nm/arg2.version}
    2. second arg is a known dependency name with spec
      • equivalent to npm diff arg1 arg2
    3. second arg is a SemVer range (including single versions)
      • equivalent to npm diff arg1 ${arg1.name}@arg2
    4. second arg is a package name not a dependency with spec
      • equivalent to npm diff arg1 arg2
    5. second arg is a package name not a dependency NO spec
      • equivalent to npm diff arg1 arg2@latest
  5. first arg is a package name not a dependency NO spec

    1. second arg is a known dependency name
      • equivalent to npm diff arg1@latest ${nm/arg2.name@nm/arg2.version}
    2. second arg is a known dependency name with spec
      • equivalent to npm diff arg1@latest arg2
    3. second arg is a SemVer range (including single versions)
      • equivalent to npm diff arg1@latest ${arg1.name}@arg2
    4. second arg is a package name not a dependency with spec
      • equivalent to npm diff arg1@latest arg2
    5. second arg is a package name not a dependency NO spec
      • equivalent to npm diff arg1@latest arg2@latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment