feat(main): seed random generator with project name
This commit is contained in:
parent
54c6f4a6fc
commit
50cf48fdcf
|
@ -27,6 +27,7 @@
|
|||
"@types/mocha": "8.0.1",
|
||||
"@types/react": "^16.9.45",
|
||||
"@types/react-dom": "^16.9.8",
|
||||
"@types/seedrandom": "^2.4.28",
|
||||
"@types/sinon-chai": "3.2.4",
|
||||
"@types/source-map-support": "0.5.2",
|
||||
"@types/yargs": "^15.0.5",
|
||||
|
@ -73,6 +74,7 @@
|
|||
"rollup-plugin-uglify": "^6.0.4",
|
||||
"rollup-plugin-visualizer": "^4.0.4",
|
||||
"rollup-plugin-yaml": "2.0.0",
|
||||
"seedrandom": "^3.0.5",
|
||||
"sinon": "9.0.2",
|
||||
"sinon-chai": "3.5.0",
|
||||
"source-map-support": "0.5.19",
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { doesExist, InvalidArgumentError } from '@apextoaster/js-utils';
|
||||
import { doesExist } from '@apextoaster/js-utils';
|
||||
import { prng } from 'seedrandom';
|
||||
|
||||
import { randomItem } from './utils';
|
||||
|
||||
|
@ -123,26 +124,16 @@ export function prioritySort<TLabel extends BaseLabel>(labels: Array<TLabel>): A
|
|||
*
|
||||
* TODO: this is a terrible overload
|
||||
*/
|
||||
export function getLabelColor(flag: FlagLabel, colors: Array<string>): string;
|
||||
export function getLabelColor(state: StateLabel, value: StateValue, colors: Array<string>): string;
|
||||
export function getLabelColor(label: BaseLabel, colorsOrValue: Array<string> | StateValue, maybeColors?: Array<string>): string {
|
||||
if (Array.isArray(colorsOrValue)) {
|
||||
const { color } = label;
|
||||
if (doesExist(color)) {
|
||||
return color;
|
||||
} else {
|
||||
return randomItem(colorsOrValue);
|
||||
}
|
||||
} else {
|
||||
if (!Array.isArray(maybeColors)) {
|
||||
throw new InvalidArgumentError();
|
||||
}
|
||||
|
||||
const { color = colorsOrValue.color } = label;
|
||||
if (doesExist(color)) {
|
||||
return color;
|
||||
} else {
|
||||
return randomItem(maybeColors);
|
||||
}
|
||||
export function getLabelColor(colors: Array<string>, random: prng, flag: FlagLabel): string;
|
||||
export function getLabelColor(colors: Array<string>, random: prng, state: StateLabel, value: StateValue): string;
|
||||
export function getLabelColor(colors: Array<string>, random: prng, label: BaseLabel, value?: StateValue): string {
|
||||
if (doesExist(value) && doesExist(value.color)) {
|
||||
return value.color;
|
||||
}
|
||||
|
||||
if (doesExist(label.color)) {
|
||||
return label.color;
|
||||
}
|
||||
|
||||
return randomItem(colors, random);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import { createSchema } from '@apextoaster/js-yaml-schema';
|
|||
import { existsSync, readFileSync, realpathSync } from 'fs';
|
||||
import { DEFAULT_SAFE_SCHEMA, safeLoad } from 'js-yaml';
|
||||
import { join } from 'path';
|
||||
import { alea } from 'seedrandom';
|
||||
|
||||
import { ConfigData } from './config';
|
||||
import { Commands, createParser } from './config/args';
|
||||
|
@ -64,9 +65,10 @@ export async function main(argv: Array<string>): Promise<number> {
|
|||
continue;
|
||||
}
|
||||
|
||||
const random = alea(name);
|
||||
const remote = new GithubRemote({
|
||||
data: project.remote.data,
|
||||
dryrun: args.dryrun || project.remote.dryrun,
|
||||
dryrun: args.dryrun || project.remote.dryrun || false,
|
||||
logger,
|
||||
type: project.remote.type,
|
||||
});
|
||||
|
@ -78,6 +80,7 @@ export async function main(argv: Array<string>): Promise<number> {
|
|||
flags,
|
||||
logger,
|
||||
project: name,
|
||||
random,
|
||||
remote,
|
||||
states,
|
||||
};
|
||||
|
|
10
src/sync.ts
10
src/sync.ts
|
@ -1,5 +1,6 @@
|
|||
import { doesExist, mustExist } from '@apextoaster/js-utils';
|
||||
import { Logger } from 'noicejs';
|
||||
import { prng } from 'seedrandom';
|
||||
|
||||
import { FlagLabel, getLabelColor, getLabelNames, getValueName, StateLabel } from './labels';
|
||||
import { LabelUpdate, Remote } from './remote';
|
||||
|
@ -17,6 +18,7 @@ export interface SyncOptions {
|
|||
|
||||
logger: Logger;
|
||||
project: string;
|
||||
random: prng;
|
||||
remote: Remote;
|
||||
|
||||
/**
|
||||
|
@ -103,7 +105,7 @@ export async function createLabel(options: SyncOptions, name: string) {
|
|||
const flag = options.flags.find((it) => name === it.name);
|
||||
if (doesExist(flag)) {
|
||||
await options.remote.createLabel({
|
||||
color: getLabelColor(flag, options.colors),
|
||||
color: getLabelColor(options.colors, options.random, flag),
|
||||
desc: mustExist(flag.desc),
|
||||
name,
|
||||
project: options.project,
|
||||
|
@ -117,7 +119,7 @@ export async function createLabel(options: SyncOptions, name: string) {
|
|||
const value = state.values.find((it) => getValueName(state, it) === name);
|
||||
if (doesExist(value)) {
|
||||
await options.remote.createLabel({
|
||||
color: getLabelColor(state, value, options.colors),
|
||||
color: getLabelColor(options.colors, options.random, state, value),
|
||||
desc: defaultUntil(value.desc, state.desc, ''),
|
||||
name: getValueName(state, value),
|
||||
project: options.project,
|
||||
|
@ -153,7 +155,7 @@ export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate):
|
|||
const flag = options.flags.find((it) => label.name === it.name);
|
||||
if (doesExist(flag)) {
|
||||
await syncLabelDiff(options, label, {
|
||||
color: getLabelColor(flag, options.colors),
|
||||
color: getLabelColor(options.colors, options.random, flag),
|
||||
desc: defaultTo(flag.desc, label.desc),
|
||||
name: flag.name,
|
||||
project: options.project,
|
||||
|
@ -167,7 +169,7 @@ export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate):
|
|||
const value = state.values.find((it) => getValueName(state, it) === label.name);
|
||||
if (doesExist(value)) {
|
||||
await syncLabelDiff(options, label, {
|
||||
color: getLabelColor(state, value, options.colors),
|
||||
color: getLabelColor(options.colors, options.random, state, value),
|
||||
desc: defaultTo(value.desc, label.desc),
|
||||
name: getValueName(state, value),
|
||||
project: options.project,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { doesExist, Optional, mustExist } from '@apextoaster/js-utils';
|
||||
import { doesExist, mustExist, Optional } from '@apextoaster/js-utils';
|
||||
import { prng } from 'seedrandom';
|
||||
|
||||
export function defaultTo<T>(a: Optional<T>, b: T): T {
|
||||
if (doesExist(a)) {
|
||||
|
@ -13,7 +14,7 @@ export function defaultUntil<T>(...items: Array<Optional<T>>): T {
|
|||
return mustExist(result);
|
||||
}
|
||||
|
||||
export function randomItem<T>(items: Array<T>, source = Math.random): T {
|
||||
export function randomItem<T>(items: Array<T>, source: prng): T {
|
||||
const idx = Math.floor(source() * items.length);
|
||||
return items[idx];
|
||||
}
|
||||
|
|
10
yarn.lock
10
yarn.lock
|
@ -592,6 +592,11 @@
|
|||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/seedrandom@^2.4.28":
|
||||
version "2.4.28"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/seedrandom/-/seedrandom-2.4.28.tgz#9ce8fa048c1e8c85cb71d7fe4d704e000226036f"
|
||||
integrity sha512-SMA+fUwULwK7sd/ZJicUztiPs8F1yCPwF3O23Z9uQ32ME5Ha0NmDK9+QTsYE4O2tHXChzXomSWWeIhCnoN1LqA==
|
||||
|
||||
"@types/sinon-chai@3.2.4":
|
||||
version "3.2.4"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/sinon-chai/-/sinon-chai-3.2.4.tgz#c425625681f4f8d3a43a7551a77f590ce1c49b21"
|
||||
|
@ -4556,6 +4561,11 @@ scheduler@^0.19.1:
|
|||
loose-envify "^1.1.0"
|
||||
object-assign "^4.1.1"
|
||||
|
||||
seedrandom@^3.0.5:
|
||||
version "3.0.5"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/seedrandom/-/seedrandom-3.0.5.tgz#54edc85c95222525b0c7a6f6b3543d8e0b3aa0a7"
|
||||
integrity sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==
|
||||
|
||||
semver-compare@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://artifacts.apextoaster.com/repository/group-npm/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
|
||||
|
|
Loading…
Reference in New Issue