Skip to content

Commit

Permalink
feat(engine): new beta flow (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Nov 11, 2024
1 parent 1752e13 commit 0c2c499
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 41 deletions.
5 changes: 3 additions & 2 deletions packages/engine/src/cli/synsets-cmd/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ export type SynsetsArgvAny = PullArgv & PushArgv & RebuildArgv;

export type PullArgv = {
subcommand: 'pull';
beta: boolean;
partial: boolean;
only: boolean;
_: string[];
};

export type PushArgv = {
subcommand: 'push';
beta: boolean;
partial: boolean;
note: string;
only: boolean;
_: string[];
};
Expand Down
4 changes: 2 additions & 2 deletions packages/engine/src/cli/synsets-cmd/pull.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GSheets2Git } from '../../sync';

import type { PullArgv } from './common';
import type { PullArgv } from './argv';
import { getGoogleGitSyncPrerequisites, parseSelectedSynsets } from './common';

export async function pull(argv: PullArgv) {
Expand All @@ -16,11 +16,11 @@ export async function pull(argv: PullArgv) {
}

const sync = new GSheets2Git({
beta: argv.beta,
multisynsets,
wordsAddLang,
words,
selectedIds,
partialSync: argv.partial,
});

console.log('Fetching synsets...');
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/src/cli/synsets-cmd/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Git2Gsheets } from '../../sync';

import type { PushArgv } from './common';
import type { PushArgv } from './argv';
import { getGoogleGitSyncPrerequisites, parseSelectedSynsets } from './common';

export async function push(argv: PushArgv) {
Expand All @@ -16,11 +16,12 @@ export async function push(argv: PushArgv) {
}

const sync = new Git2Gsheets({
beta: argv.beta,
words,
wordsAddLang,
multisynsets,
selectedIds,
partialSync: argv.partial,
changeNote: argv.note,
});

console.log('Pushing synsets...');
Expand Down
11 changes: 8 additions & 3 deletions packages/engine/src/cli/synsets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ export const builder: CommandBuilder<subcommand.SynsetsArgvAny, any> = {
description: 'Subcommand to execute',
demandOption: true,
},
beta: {
note: {
type: 'string',
description: 'Note for the change',
default: process.env.ISV_NOTE,
},
partial: {
type: 'boolean',
description: 'Use beta features',
default: process.env.ISV_BETA === 'true',
description: 'Partial sync (disable deletion)',
default: false,
},
only: {
type: 'boolean',
Expand Down
11 changes: 5 additions & 6 deletions packages/engine/src/sync/words/GSheetsOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import type {
} from '../../google';
import { log } from '../../utils';

import { isBeta } from './utils';

export type GSheetsOpOptions = {
readonly beta: boolean;
readonly words: WordsSheet;
readonly wordsAddLang: WordsAddLangSheet;
readonly selectedIds?: number[];
readonly multisynsets: MultilingualSynsetRepository;
/** Disables deletion of entities */
readonly partialSync: boolean;
};

type TableDTO = WordsRecord | WordsAddLangRecord;
Expand All @@ -28,20 +27,21 @@ export abstract class GSheetsOp extends IdSyncOperation<number> {
private _wordsAdd?: Promise<Map<number, WordsAddLangDTO>>;
protected readonly wordsSheet: WordsSheet;
protected readonly wordsAddLangSheet: WordsAddLangSheet;
protected readonly beta: boolean;
protected readonly multisynsets: MultilingualSynsetRepository;
protected readonly selectedIds?: Set<number>;

protected constructor(options: Readonly<GSheetsOpOptions>) {
super();

this.beta = options.beta;
this.wordsSheet = options.words;
this.wordsAddLangSheet = options.wordsAddLang;
this.multisynsets = options.multisynsets;
if (options.selectedIds) {
this.selectedIds = new Set(options.selectedIds);
}
if (options.partialSync) {
this.delete = async () => {};
}
}

protected async wordIds(): Promise<number[]> {
Expand Down Expand Up @@ -74,7 +74,6 @@ export abstract class GSheetsOp extends IdSyncOperation<number> {
const dtos = (await sheet.getValues()) as unknown as DTO[];
const grecords = new Map(
dtos
.filter((dto) => this.beta || !isBeta(dto))
.map((dto) => [Math.abs(+dto.id), dto]),
);

Expand Down
51 changes: 31 additions & 20 deletions packages/engine/src/sync/words/Git2Gsheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import type {
} from '../../google';
import { log } from '../../utils';

import type { GSheetsOpOptions } from './GSheetsOp';
import { GSheetsOp } from './GSheetsOp';
import { isBeta } from './utils';
import { GSheetsOp, type GSheetsOpOptions } from './GSheetsOp';

export type Git2GsheetsOptions = GSheetsOpOptions;
export type Git2GsheetsOptions = GSheetsOpOptions & {
readonly changeNote: string;
};

export class Git2Gsheets extends GSheetsOp {
private readonly changeNote: string;

constructor(options: Git2GsheetsOptions) {
super(options);

if (!options.beta) {
throw new Error('Git2Gsheets is only for beta words');
if (!options.changeNote) {
throw new TypeError('Change note is required — you cannot just change the dictionary without explaining why');
}

this.changeNote = options.changeNote;
}

protected async getAfterIds(): Promise<number[]> {
Expand All @@ -46,8 +50,11 @@ export class Git2Gsheets extends GSheetsOp {
const dto = this._synset2dto(synset!);
const dtoAddLang = this._synset2dtoAddLang(synset!);

const notes = [[...this._annotate(null, dto)] as string[]];

this.wordsSheet.batch.appendRows({
values: [[...dto]],
notes,
});

this.wordsAddLangSheet.batch.appendRows({
Expand Down Expand Up @@ -92,7 +99,7 @@ export class Git2Gsheets extends GSheetsOp {
const steen = isv.lemmas[0]!.steen!;

const dto: WordsDTO = new Mapper({
id: isBeta(ms) ? -ms.id : ms.id,
id: ms.id,
isv: isv.toString(),
addition: steen.addition ?? '',
partOfSpeech: steen.partOfSpeech ?? '',
Expand All @@ -118,7 +125,7 @@ export class Git2Gsheets extends GSheetsOp {
frequency: steen.frequency ?? '',
intelligibility: '',
using_example: steen.using_example ?? '',
});
}, undefined, []);

for (const key of ms.steen?.debated ?? []) {
dto[key] = `#${dto[key]}`;
Expand All @@ -134,7 +141,7 @@ export class Git2Gsheets extends GSheetsOp {
// TODO: this is a violation of synset vs lemma separation
const steen = isv.lemmas[0]!.steen!;
const dto: WordsAddLangDTO = new Mapper({
id: isBeta(ms) ? -ms.id : ms.id,
id: ms.id,
isv: isv.toString(),
addition: steen.addition ?? '',
partOfSpeech: steen.partOfSpeech ?? '',
Expand Down Expand Up @@ -214,9 +221,7 @@ export class Git2Gsheets extends GSheetsOp {
return false;
}

const notes = isBeta(dtoOld)
? undefined
: [[...this._annotate(dtoOld, dtoNew)] as string[]];
const notes = [[...this._annotate(dtoOld, dtoNew)] as string[]];

this.wordsSheet.batch.updateRows({
startRowIndex,
Expand Down Expand Up @@ -248,26 +253,32 @@ export class Git2Gsheets extends GSheetsOp {
}
}

private _annotate(dtoOld: WordsDTO, dtoNew: WordsDTO) {
const keys = [
private _annotate(dtoOld: WordsDTO | null, dtoNew: WordsDTO) {
let addedNote = false;

const allKeys = [
'isv',
'addition',
'partOfSpeech',
'type',
'en',
'sameInLanguages',
] as const;
const keys = dtoOld ? allKeys : ['isv'] as const;

const notes = dtoOld.getNotes()!.getCopy();
const notes = (dtoOld ?? dtoNew).getNotes()!.getCopy();
for (const key of keys) {
const sOld = asString(dtoOld[key]);
const sOld = asString(dtoOld?.[key]);
const sNew = asString(dtoNew[key]);

if (sOld && sOld !== sNew) {
notes[key] ??= sOld;
if (notes[key] === sNew) {
notes[key] = undefined;
} else if (!sNew.startsWith('#')) {
if (!addedNote) {
notes[key] ??= sOld + ' → ' + sNew;
notes[key] += `\n\n${this.changeNote}`;
addedNote = true;
}

if (!sNew.startsWith('#') && !sNew.startsWith('!#')) {
dtoNew[key] = `#${sNew}`;
}
}
Expand Down
3 changes: 0 additions & 3 deletions packages/engine/src/sync/words/utils.ts

This file was deleted.

11 changes: 8 additions & 3 deletions packages/google/src/sheets/BatchExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type BatchExecutorConfig = {
export type BatchExecutor$AppendRowsRequest = {
sheetId?: number;
values: unknown[][];
notes?: string[][];
};

export type BatchExecutor$UpdateRowsRequest = {
Expand Down Expand Up @@ -133,11 +134,15 @@ export class BatchExecutor {
}

public appendRows(request: BatchExecutor$AppendRowsRequest): this {
const hasNotes = request.notes != null;

this.appendCells({
fields: 'userEnteredValue',
fields: 'userEnteredValue' + (hasNotes ? ',note' : ''),
sheetId: request.sheetId ?? this.sheetId,
rows: request.values.map((row) => ({
values: row.map((value) => this._toCellData(value)),
rows: request.values.map((row, rowIndex) => ({
values: row.map((value, colIndex) =>
this._toCellData(value, request.notes?.[rowIndex]?.[colIndex])
),
})),
});

Expand Down

0 comments on commit 0c2c499

Please sign in to comment.