Skip to content

Commit

Permalink
feat: 當版本編號具有以下字元時 ><|=& 忽略更新
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Jul 12, 2020
1 parent 65a0b89 commit 40aa9f7
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/@yarn-tool/ncu/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface IVersionCacheMapValue extends IVersionCacheMapKey {
version_new: IVersionValue;
}
export declare type ISetupNcuToYargsReturnType = ReturnType<typeof setupNcuToYargs>;
export declare type IOptionsNpmCheckUpdates = IUnpackYargsArgv<ISetupNcuToYargsReturnType> & {
export declare type IOptionsNpmCheckUpdates = Partial<IUnpackYargsArgv<ISetupNcuToYargsReturnType>> & {
json_old: IPackageJson;
cwd?: string;
packageData?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/@yarn-tool/ncu/lib/types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@yarn-tool/ncu/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface IVersionCacheMapValue extends IVersionCacheMapKey

export type ISetupNcuToYargsReturnType = ReturnType<typeof setupNcuToYargs>;

export type IOptionsNpmCheckUpdates = IUnpackYargsArgv<ISetupNcuToYargsReturnType> & {
export type IOptionsNpmCheckUpdates = Partial<IUnpackYargsArgv<ISetupNcuToYargsReturnType>> & {
json_old: IPackageJson;
cwd?: string;
packageData?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/@yarn-tool/ncu/lib/update.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IPackageMap, IOptionsNpmCheckUpdates, IVersionCacheMapValue } from './types';
import { IYarnLockfileParseObject, IWrapDedupeCache } from '@yarn-tool/yarnlock';
import Bluebird from 'bluebird';
import { ITSRequireAtLeastOne } from 'ts-type';
export declare function checkResolutionsUpdate(resolutions: IPackageMap, yarnlock_old_obj: IYarnLockfileParseObject | string, options: Partial<IOptionsNpmCheckUpdates>): Bluebird<{
yarnlock_old_obj: Record<string, import("@yarn-tool/yarnlock").IYarnLockfileParseObjectRow<string[]>>;
yarnlock_new_obj: Record<string, import("@yarn-tool/yarnlock").IYarnLockfileParseObjectRow<string[]>>;
Expand All @@ -10,4 +11,4 @@ export declare function checkResolutionsUpdate(resolutions: IPackageMap, yarnloc
deps2: IPackageMap;
deps3: Record<string, IVersionCacheMapValue>;
}>;
export declare function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial<C>, ncuOptions: IOptionsNpmCheckUpdates): Promise<IOptionsNpmCheckUpdates>;
export declare function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial<C>, ncuOptions: ITSRequireAtLeastOne<IOptionsNpmCheckUpdates, 'json_old' | 'packageData'>): Promise<Pick<IOptionsNpmCheckUpdates, "dep" | "minimal" | "newest" | "packageManager" | "registry" | "silent" | "greatest" | "upgrade" | "semverLevel" | "removeRange" | "dedupe" | "versionTarget" | "cwd" | "json_new" | "json_changed" | "list_updated" | "loglevel" | "current" | "noSafe"> & import("ts-type").ITSRequiredPick<IOptionsNpmCheckUpdates, "json_old"> & import("ts-type").ITSPartialPick<IOptionsNpmCheckUpdates, "packageData">>;
27 changes: 16 additions & 11 deletions packages/@yarn-tool/ncu/lib/update.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions packages/@yarn-tool/ncu/lib/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
IWrapDedupeCache,
} from '@yarn-tool/yarnlock';
import Bluebird from 'bluebird';
import { keyObjectToPackageMap } from './util';
import { keyObjectToPackageMap, allowUpdateVersion } from './util';
import semver from 'semver';
import { queryRemoteVersions } from './remote';
import { npmCheckUpdatesOptions } from './options';
import { IPackageJsonDependenciesField } from '@ts-type/package-dts/package-json';
import { toDependencyTable } from '@yarn-tool/table';
import { ITSRequireAtLeastOne } from 'ts-type';

export function checkResolutionsUpdate(resolutions: IPackageMap,
yarnlock_old_obj: IYarnLockfileParseObject | string,
Expand Down Expand Up @@ -124,7 +125,7 @@ export function checkResolutionsUpdate(resolutions: IPackageMap,
;
}

export async function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial<C>, ncuOptions: IOptionsNpmCheckUpdates)
export async function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial<C>, ncuOptions: ITSRequireAtLeastOne<IOptionsNpmCheckUpdates, 'json_old' | 'packageData'>)
{
//ncuOptions.silent = false;

Expand All @@ -143,16 +144,18 @@ export async function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial

ncuOptions.list_updated = await _npmCheckUpdates(ncuOptions) as Record<string, string>;

let ks = Object.keys(ncuOptions.list_updated);
const ks = Object.keys(ncuOptions.list_updated);

ncuOptions.json_changed = !!ks.length;
let json_changed = false;

let current: IDependency = {};
const current: IDependency = {};
const list_updated: IDependency = {};

if (ks.length)
{
ks.forEach(name =>
{
const version_new = ncuOptions.list_updated[name];

([
'dependencies',
Expand All @@ -162,17 +165,20 @@ export async function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial
] as IPackageJsonDependenciesField[]).forEach(key =>
{

let data = ncuOptions.json_new[key];
const deps = ncuOptions.json_new[key];

if (data)
if (deps)
{
let value = data[name];
const version_old = deps[name];

if (value && value != EnumVersionValue2.any && value != EnumVersionValue.latest)
if (version_old !== version_new && allowUpdateVersion(version_old))
{
current[name] = value;
list_updated[name] = version_new;
current[name] = version_old;

data[name] = ncuOptions.list_updated[name];
deps[name] = version_new;

json_changed = true;
}
}

Expand All @@ -182,9 +188,11 @@ export async function npmCheckUpdates<C extends IWrapDedupeCache>(cache: Partial

}

ncuOptions.json_changed = json_changed;
ncuOptions.list_updated = list_updated;
ncuOptions.current = current;

let table = toDependencyTable({
const table = toDependencyTable({
from: ncuOptions.current,
to: ncuOptions.list_updated,
}).toString();
Expand Down
1 change: 1 addition & 0 deletions packages/@yarn-tool/ncu/lib/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import { IVersionValue } from '@ts-type/package-dts/lib/package-json/types';
import { IVersionCacheMapKey, IVersionCacheMapValue, IPackageMap } from './types';
export declare function isBadVersion(version: IVersionValue): boolean;
export declare function keyObjectToPackageMap(obj: IVersionCacheMapKey[] | IVersionCacheMapValue[], useVarsionNew?: boolean): IPackageMap;
export declare function allowUpdateVersion(version: string): boolean;
6 changes: 5 additions & 1 deletion packages/@yarn-tool/ncu/lib/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/@yarn-tool/ncu/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ export function keyObjectToPackageMap(obj: IVersionCacheMapKey[] | IVersionCache
// @ts-ignore
}, {})
}

export function allowUpdateVersion(version: string)
{
return version?.length && version !== EnumVersionValue2.any && version !== EnumVersionValue.latest && !/^\s*(?:[><])|&|=|\|/.test(version)
}
41 changes: 41 additions & 0 deletions packages/@yarn-tool/ncu/test/ncu.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import IPackageJson from '@ts-type/package-dts/package-json';
import { npmCheckUpdates } from '../lib/update';

describe(`should not update`, () =>
{

[
">= 1",
"> 1",
"= 1",
"< 1",
"<= 1",
"^1 | ^2",
"^1 & ^2",
].forEach(version => {

const name = "@types/node";
const field = "devDependencies";

test(version, async (done) =>
{

let json_old: IPackageJson = {
[field]: {
[name]: version,
},
};

let actual = await npmCheckUpdates({}, {
json_old,
})

expect(actual.json_new[field]).toHaveProperty(name, version);
expect(actual).toMatchSnapshot();

return done();
});

});

})

0 comments on commit 40aa9f7

Please sign in to comment.