Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .aiignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# yaml-language-server: $schema=./node_modules/harperdb/config-app.schema.json

# This is the configuration file for the application.
# It specifies built-in Harper components that will load the specified feature and files.
# For more information, see https://docs.harperdb.io/docs/reference/components/built-in-extensions
Expand Down
3 changes: 3 additions & 0 deletions graphql.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: schema.graphql
include: node_modules/harperdb/schema.graphql
documents: '**/*.graphql'
49 changes: 31 additions & 18 deletions login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
// CLI_TARGET
// CLI_TARGET_USERNAME
// CLI_TARGET_PASSWORD
//
// This only needs to be run once to update your .env! Then you can delete it.

import fs from 'node:fs/promises';
import path from 'node:path';
import {readFile, writeFile} from 'node:fs/promises';
import path, {join} from 'node:path';
import readline from 'node:readline';
import {existsSync} from 'node:fs';

process.on('SIGINT', handleSigInt);

Expand Down Expand Up @@ -52,23 +55,11 @@ async function main() {
process.exit(1);
}

const env = [
`CLI_TARGET='${escapeSingleQuotes(url)}'`,
`CLI_TARGET_USERNAME='${escapeSingleQuotes(username)}'`,
`CLI_TARGET_PASSWORD='${escapeSingleQuotes(password)}'`,
'',
].join('\n');
await updateEnv('CLI_TARGET', escapeSingleQuotes(url));
await updateEnv('CLI_TARGET_USERNAME', escapeSingleQuotes(username));
await updateEnv('CLI_TARGET_PASSWORD', escapeSingleQuotes(password));

const envPath = path.join(process.cwd(), '.env');
try {
await fs.writeFile(envPath, env, { mode: 0o400 });
} catch {
await fs.writeFile(envPath, env);
await fs.chmod(envPath, 0o400).catch(() => {
});
}

process.stdout.write(`Saved credentials to ${path.relative(process.cwd(), envPath)}\n`);
console.log(`Saved credentials to ${path.relative(process.cwd(), '.env')}`);
}

function createInterface() {
Expand Down Expand Up @@ -164,6 +155,28 @@ function escapeSingleQuotes(v) {
return String(v).replace(/'/g, '\'\\\'\'');
}

async function updateEnv(key, value) {
process.env[key] = value;
const envPath = join(process.cwd(), '.env');

let envContent = '';
if (existsSync(envPath)) {
envContent = await readFile(envPath, 'utf-8');
}

const regex = new RegExp(`^${key}=.*`, 'm');
if (regex.test(envContent)) {
envContent = envContent.replace(regex, `${key}=${value}`);
} else {
if (envContent && !envContent.endsWith('\n')) {
envContent += '\n';
}
envContent += `${key}=${value}\n`;
}

await writeFile(envPath, envContent);
}

function handleSigInt() {
process.stdout.write('\n');
process.exit(130);
Expand Down
Loading