Skip to content

Commit

Permalink
fix(daemon): pet-store removes petname when overwriting with write
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Dec 1, 2023
1 parent 980dd57 commit a198476
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions packages/daemon/src/pet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,6 @@ export const makePetStoreMaker = (filePowers, locator) => {
return formulaIdentifier;
};

/**
* @param {string} petName
* @param {string} formulaIdentifier
*/
const write = async (petName, formulaIdentifier) => {
assertValidName(petName);
if (!validFormulaPattern.test(formulaIdentifier)) {
throw new Error(`Invalid formula identifier ${q(formulaIdentifier)}`);
}

petNames.set(petName, formulaIdentifier);

const formulaPetNames = formulaIdentifiers.get(formulaIdentifier);
if (formulaPetNames === undefined) {
formulaIdentifiers.set(formulaIdentifier, new Set([petName]));
} else {
formulaPetNames.add(petName);
}

const petNamePath = filePowers.joinPath(petNameDirectoryPath, petName);
const petNameText = `${formulaIdentifier}\n`;
await filePowers.writeFileText(petNamePath, petNameText);
changesTopic.publisher.next({ add: petName });
};

const list = () => harden([...petNames.keys()].sort());

const follow = async () =>
Expand Down Expand Up @@ -132,6 +107,35 @@ export const makePetStoreMaker = (filePowers, locator) => {
// TODO consider tracking historical pet names for formulas
};

/**
* @param {string} petName
* @param {string} formulaIdentifier
*/
const write = async (petName, formulaIdentifier) => {
assertValidName(petName);
if (!validFormulaPattern.test(formulaIdentifier)) {
throw new Error(`Invalid formula identifier ${q(formulaIdentifier)}`);
}

if (petNames.has(petName)) {
await remove(petName);
}

petNames.set(petName, formulaIdentifier);

const formulaPetNames = formulaIdentifiers.get(formulaIdentifier);
if (formulaPetNames === undefined) {
formulaIdentifiers.set(formulaIdentifier, new Set([petName]));
} else {
formulaPetNames.add(petName);
}

const petNamePath = filePowers.joinPath(petNameDirectoryPath, petName);
const petNameText = `${formulaIdentifier}\n`;
await filePowers.writeFileText(petNamePath, petNameText);
changesTopic.publisher.next({ add: petName });
};

/**
* @param {string} fromName
* @param {string} toName
Expand Down

0 comments on commit a198476

Please sign in to comment.