Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): don't create config.json files if none was present before #1054

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/cdktf-cli/lib/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,31 @@ export async function sendTelemetry(
function getId(
filePath: string,
key: string,
forceCreation = false,
explanatoryComment?: string
): string {
// If the file doesn't exist, we don't have an ID. So we create a file with the ID for next time
const _uuid = uuidv4();
const _idFile = {} as Record<string, string>;
if (explanatoryComment) {
_idFile["//"] = explanatoryComment.replace(/\n/g, " ");
}
_idFile[key] = _uuid;
const _uuid = uuidv4(); // create a new UUID in case we don't find one

let jsonFile;
try {
jsonFile = require(filePath);
jsonFile = require(filePath); // we found the file
} catch {
fs.writeFileSync(filePath, JSON.stringify(_idFile, null, 2));
// we found no file, create one if we're forcing a creation
if (forceCreation) {
const _idFile = {} as Record<string, string>; // compose JSON id file in case we don't find one
if (explanatoryComment) {
_idFile["//"] = explanatoryComment.replace(/\n/g, " ");
}
_idFile[key] = _uuid;
fs.writeFileSync(filePath, JSON.stringify(_idFile, null, 2));
}
return _uuid;
}

if (jsonFile[key]) {
return jsonFile[key];
return jsonFile[key]; // we found an id
} else {
// we found no id, we add it to the file for future use
fs.writeFileSync(
filePath,
JSON.stringify({ ...jsonFile, [key]: _uuid }, null, 2)
Expand All @@ -122,6 +126,7 @@ function getUserId(): string {
return getId(
path.resolve(os.homedir(), ".cdktf", "config.json"),
"userId",
true,
`This signature is a randomly generated UUID used to anonymously differentiate users in telemetry data order to inform product direction.
This signature is random, it is not based on any personally identifiable information.
To create a new signature, you can simply delete this file at any time.
Expand Down