70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import 'dotenv/config';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { readFile, unlink, writeFile } from 'node:fs/promises';
|
|
import { resolve } from 'node:path';
|
|
import { parse } from 'dotenv';
|
|
import {
|
|
assertLocalAdminTarget,
|
|
localAdminSettings,
|
|
} from '../identity/local-admin-seed';
|
|
|
|
const credentialFile = resolve('.env.local-admin');
|
|
|
|
async function main() {
|
|
assertLocalAdminTarget(process.env.NODE_ENV, process.env.DATABASE_URL);
|
|
const mode = process.argv[2];
|
|
if (mode === 'prepare') {
|
|
const settings = localAdminSettings();
|
|
await writeFile(
|
|
credentialFile,
|
|
Object.entries(settings)
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join('\n') + '\n',
|
|
{ flag: 'wx', mode: 0o600 },
|
|
);
|
|
if (process.platform === 'win32') {
|
|
const account = `${process.env.USERDOMAIN}\\${process.env.USERNAME}`;
|
|
const protectedFile = spawnSync(
|
|
'icacls.exe',
|
|
[
|
|
credentialFile,
|
|
'/inheritance:r',
|
|
'/grant:r',
|
|
`${account}:(F)`,
|
|
'SYSTEM:(F)',
|
|
'Administrators:(F)',
|
|
],
|
|
{ stdio: 'ignore' },
|
|
);
|
|
if (protectedFile.status !== 0) {
|
|
await unlink(credentialFile);
|
|
throw new Error('Could not restrict local credential file access');
|
|
}
|
|
}
|
|
console.log('Local admin credentials prepared in .env.local-admin');
|
|
return;
|
|
}
|
|
if (mode === 'seed') {
|
|
const settings = parse(await readFile(credentialFile));
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[resolve('dist/cli/bootstrap-owner.js')],
|
|
{
|
|
env: { ...process.env, ...settings },
|
|
stdio: 'inherit',
|
|
},
|
|
);
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) process.exitCode = result.status ?? 1;
|
|
return;
|
|
}
|
|
throw new Error('Use prepare or seed');
|
|
}
|
|
|
|
void main().catch((error: unknown) => {
|
|
console.error(
|
|
error instanceof Error ? error.message : 'Local admin setup failed',
|
|
);
|
|
process.exitCode = 1;
|
|
});
|