Skip to content

Commit

Permalink
json spec
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Jul 12, 2022
1 parent cef1830 commit 0b8790c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,28 @@ const userId = async ({ cml } = {}) => {
let id = uuidv4();
const oldPath = userConfigDir('dvc/user_id', 'iterative');
const newPath = userConfigDir('iterative/telemetry');
const readId = async ({ fpath }) => {
const json = (await fs.readFile(fpath)).toString('utf-8');
const { user_id: id } = JSON.parse(json);

return id;
};

const writeId = async ({ fpath, id }) => {
await fs.mkdir(path.dirname(fpath), { recursive: true });
await fs.writeFile(fpath, JSON.stringify({ user_id: id }));
};

if (await fileExists(newPath)) {
id = (await fs.readFile(newPath)).toString('utf-8');
id = await readId({ fpath: newPath });
} else {
if (await fileExists(oldPath)) {
const json = (await fs.readFile(oldPath)).toString('utf-8');
({ user_id: id } = JSON.parse(json));
}
if (await fileExists(oldPath)) id = await readId({ fpath: oldPath });

await fs.mkdir(path.dirname(newPath), { recursive: true });
await fs.writeFile(newPath, id);
await writeId({ fpath: newPath, id });
}

if (!(await fileExists(oldPath)) && id !== ID_DO_NOT_TRACK) {
await fs.mkdir(path.dirname(oldPath), { recursive: true });
await fs.writeFile(oldPath, JSON.stringify({ user_id: id }));
}
if (!(await fileExists(oldPath)) && id !== ID_DO_NOT_TRACK)
await writeId({ fpath: oldPath, id });

return id;
} catch (err) {
Expand Down

1 comment on commit 0b8790c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment

CML watermark

Please sign in to comment.