Keep Studio business branding in verified client storage
This commit is contained in:
+11
-8
@@ -17,25 +17,26 @@ function createStore(rootDirectory) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
async function read(workspace) {
|
||||
async function read(workspace, kind = 'stores') {
|
||||
if (!['stores', 'brand'].includes(kind)) throw new Error('Invalid storage kind');
|
||||
const dir = await directory(workspace);
|
||||
let text;
|
||||
try { text = await fs.readFile(path.join(dir, 'stores.json'), 'utf8'); }
|
||||
catch (error) { if (error.code === 'ENOENT') return []; throw error; }
|
||||
try { text = await fs.readFile(path.join(dir, kind + '.json'), 'utf8'); }
|
||||
catch (error) { if (error.code === 'ENOENT') return kind === 'stores' ? [] : null; throw error; }
|
||||
const records = JSON.parse(text);
|
||||
if (!Array.isArray(records)) throw new Error('Invalid saved store data');
|
||||
if (kind === 'stores' ? !Array.isArray(records) : (!records || typeof records !== 'object' || Array.isArray(records))) throw new Error('Invalid saved workspace data');
|
||||
return records;
|
||||
}
|
||||
|
||||
async function update(workspace, change) {
|
||||
async function update(workspace, change, kind = 'stores') {
|
||||
const key = workspace.namespace;
|
||||
const previous = queues.get(key) || Promise.resolve();
|
||||
const task = previous.catch(() => {}).then(async () => {
|
||||
const records = await read(workspace);
|
||||
const records = await read(workspace, kind);
|
||||
const next = change(records);
|
||||
const dir = await directory(workspace);
|
||||
const target = path.join(dir, 'stores.json');
|
||||
const temporary = path.join(dir, '.stores-' + randomUUID() + '.tmp');
|
||||
const target = path.join(dir, kind + '.json');
|
||||
const temporary = path.join(dir, '.' + kind + '-' + randomUUID() + '.tmp');
|
||||
let handle;
|
||||
try {
|
||||
handle = await fs.open(temporary, 'wx', 0o600);
|
||||
@@ -61,6 +62,8 @@ function createStore(rootDirectory) {
|
||||
|
||||
return {
|
||||
read,
|
||||
readBrand(workspace) { return read(workspace, 'brand'); },
|
||||
saveBrand(workspace, brand) { return update(workspace, () => brand, 'brand'); },
|
||||
upsert(workspace, store) {
|
||||
if (!store || typeof store.id !== 'string' || !store.id || store.id.length > 160 || !Array.isArray(store.products)) {
|
||||
return Promise.reject(new Error('Invalid imported store'));
|
||||
|
||||
Reference in New Issue
Block a user